]> granicus.if.org Git - python/commitdiff
Fix bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0)
authorThomas Heller <theller@ctypes.org>
Fri, 9 Mar 2007 19:21:28 +0000 (19:21 +0000)
committerThomas Heller <theller@ctypes.org>
Fri, 9 Mar 2007 19:21:28 +0000 (19:21 +0000)
returned string up to the first NUL character.

Lib/ctypes/__init__.py
Lib/ctypes/test/test_memfunctions.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index f37c3b49a2d34debbde2049a9bc942bf3fdbbbe1..ef8e892d82805b5b87ce3ed6ba3e1b62a6d2ad87 100644 (file)
@@ -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."""
index fbae2ce17568fe7423b171f7b8dc482e3028be55..aef7a739fcf9a0fb54355c081ea1c5612540b393 100644 (file)
@@ -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()
index d3bb47d1d1a6d1f6bf1147c940243c95a71b5e5d..89304b07697150e94ee7bffef3cf46b24149793d 100644 (file)
--- 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.
index 51658ced6e0f665e346c5b2e0b0fc408f1eaa3c9..0a90b0a9ad7de06896f88954c4a8541610741c81 100644 (file)
@@ -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);
 }