tags 521337 + patch
thanks

The attached patch is stolen from:
http://svn.python.org/view?view=rev&revision=65681
--
Jakub Wilk
diff --git a/ctypes/__init__.py b/ctypes/__init__.py
--- a/ctypes/__init__.py
+++ b/ctypes/__init__.py
@@ -477,7 +477,7 @@
 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
 
@@ -489,7 +489,7 @@
 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
 
diff --git a/ctypes/test/test_memfunctions.py b/ctypes/test/test_memfunctions.py
--- a/ctypes/test/test_memfunctions.py
+++ b/ctypes/test/test_memfunctions.py
@@ -3,6 +3,17 @@
 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.

Reply via email to