summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2008-08-14 19:10:48 (GMT)
committerThomas Heller <theller@ctypes.org>2008-08-14 19:10:48 (GMT)
commit57adf22f6e61783e15932aa5cdf1a555127a293a (patch)
tree4bb8978133952ba1a2d986ba6a4f3bd5686e21ef
parent67d1981c5172e840ac3e2fc3505ce9c3abe5cd63 (diff)
downloadcpython-57adf22f6e61783e15932aa5cdf1a555127a293a.zip
cpython-57adf22f6e61783e15932aa5cdf1a555127a293a.tar.gz
cpython-57adf22f6e61783e15932aa5cdf1a555127a293a.tar.bz2
issue #3554: ctypes.string_at and ctypes.wstring_at must use the
pythonapi calling convention so that the GIL is held and error return values are checked.
-rw-r--r--Lib/ctypes/__init__.py4
-rw-r--r--Lib/ctypes/test/test_memfunctions.py11
-rw-r--r--Misc/NEWS4
3 files changed, 17 insertions, 2 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index d3e11dc..1b03835 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -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
diff --git a/Lib/ctypes/test/test_memfunctions.py b/Lib/ctypes/test/test_memfunctions.py
index 390c8ef..2660cde 100644
--- a/Lib/ctypes/test/test_memfunctions.py
+++ b/Lib/ctypes/test/test_memfunctions.py
@@ -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.
diff --git a/Misc/NEWS b/Misc/NEWS
index 586aa7c..5349c55 100644
--- 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