]> granicus.if.org Git - python/commitdiff
issue #3554: ctypes.string_at and ctypes.wstring_at must use the
authorThomas Heller <theller@ctypes.org>
Thu, 14 Aug 2008 19:10:48 +0000 (19:10 +0000)
committerThomas Heller <theller@ctypes.org>
Thu, 14 Aug 2008 19:10:48 +0000 (19:10 +0000)
pythonapi calling convention so that the GIL is held and error return
values are checked.

Lib/ctypes/__init__.py
Lib/ctypes/test/test_memfunctions.py
Misc/NEWS

index d3e11dc663ac52d51e019a35fe8cf3ba91ca21fa..1b0383564e7c861daa2dd4829c8cfbf2cbe1f5bb 100644 (file)
@@ -488,7 +488,7 @@ _cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
 def cast(obj, typ):
     return _cast(obj, obj, typ)
 
-_string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
+_string_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
 def string_at(ptr, size=-1):
     """string_at(addr[, size]) -> string
 
@@ -500,7 +500,7 @@ try:
 except ImportError:
     pass
 else:
-    _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
+    _wstring_at = PYFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
     def wstring_at(ptr, size=-1):
         """wstring_at(addr[, size]) -> string
 
index 390c8ef71e25ee864447f3dec9f466e867257eb7..2660cde2b9681be14da477d1c36d551461271755 100644 (file)
@@ -3,6 +3,17 @@ import unittest
 from ctypes import *
 
 class MemFunctionsTest(unittest.TestCase):
+    def test_overflow(self):
+        # string_at and wstring_at must use the Python calling
+        # convention (which acquires the GIL and checks the Python
+        # error flag).  Provoke an error and catch it; see also issue
+        # #3554: <http://bugs.python.org/issue3554>
+        if hasattr(sys, "maxsize"):
+            self.assertRaises((OverflowError, MemoryError),
+                              lambda: wstring_at(u"foo", sys.maxsize))
+            self.assertRaises((OverflowError, MemoryError),
+                              lambda: string_at("foo", sys.maxsize))
+
     def test_memmove(self):
         # large buffers apparently increase the chance that the memory
         # is allocated in high address space.
index 586aa7c18e4ad7bde024ddcda0f05b5696759cb3..5349c554649098b28d87ad2e6d440ee8c6c6b177 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -199,6 +199,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #3554: ctypes.string_at and ctypes.wstring_at did call Python
+  api functions without holding the GIL, which could lead to a fatal
+  error when they failed.
+
 - Issue #799428: Fix Tkinter.Misc._nametowidget to unwrap Tcl command objects.
 
 - Issue #3395: fix reference in test_multiprocessing to old debugInfo method