From c2f7725c863e350626d5c27ef56191b7f4104487 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Fri, 9 Mar 2007 19:21:28 +0000 Subject: Fix bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0) returned string up to the first NUL character. --- Lib/ctypes/__init__.py | 4 ++-- Lib/ctypes/test/test_memfunctions.py | 2 ++ Misc/NEWS | 3 +++ Modules/_ctypes/_ctypes.c | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index f37c3b4..ef8e892 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -480,7 +480,7 @@ def cast(obj, typ): return _cast(obj, obj, typ) _string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr) -def string_at(ptr, size=0): +def string_at(ptr, size=-1): """string_at(addr[, size]) -> string Return the string at addr.""" @@ -492,7 +492,7 @@ except ImportError: pass else: _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr) - def wstring_at(ptr, size=0): + def wstring_at(ptr, size=-1): """wstring_at(addr[, size]) -> string Return the string at addr.""" diff --git a/Lib/ctypes/test/test_memfunctions.py b/Lib/ctypes/test/test_memfunctions.py index fbae2ce..aef7a73 100644 --- a/Lib/ctypes/test/test_memfunctions.py +++ b/Lib/ctypes/test/test_memfunctions.py @@ -14,6 +14,7 @@ class MemFunctionsTest(unittest.TestCase): self.failUnlessEqual(string_at(result), "Hello, World") self.failUnlessEqual(string_at(result, 5), "Hello") self.failUnlessEqual(string_at(result, 16), "Hello, World\0\0\0\0") + self.failUnlessEqual(string_at(result, 0), "") def test_memset(self): a = create_string_buffer(1000000) @@ -54,6 +55,7 @@ class MemFunctionsTest(unittest.TestCase): self.failUnlessEqual(wstring_at(a), "Hello, World") self.failUnlessEqual(wstring_at(a, 5), "Hello") self.failUnlessEqual(wstring_at(a, 16), "Hello, World\0\0\0\0") + self.failUnlessEqual(wstring_at(a, 0), "") if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS b/Misc/NEWS index d3bb47d..89304b0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -152,6 +152,9 @@ Core and builtins Library ------- +- Bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0) + returned string up to the first NUL character. + - Patch #957003: Implement smtplib.LMTP. - Patch #1481079: add support for HTTP_REFERER to CGIHTTPServer. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 51658ce..0a90b0a 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4537,9 +4537,9 @@ create_comerror(void) #endif static PyObject * -string_at(const char *ptr, Py_ssize_t size) +string_at(const char *ptr, int size) { - if (size == 0) + if (size == -1) return PyString_FromString(ptr); return PyString_FromStringAndSize(ptr, size); } @@ -4624,7 +4624,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype) static PyObject * wstring_at(const wchar_t *ptr, int size) { - if (size == 0) + if (size == -1) size = wcslen(ptr); return PyUnicode_FromWideChar(ptr, size); } -- cgit v0.12