]> granicus.if.org Git - python/commitdiff
Merged revisions 54244 via svnmerge from
authorThomas Heller <theller@ctypes.org>
Fri, 9 Mar 2007 20:21:16 +0000 (20:21 +0000)
committerThomas Heller <theller@ctypes.org>
Fri, 9 Mar 2007 20:21:16 +0000 (20:21 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk/Lib/ctypes

........
  r54244 | thomas.heller | 2007-03-09 20:21:28 +0100 (Fr, 09 Mär 2007) | 3 lines

  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
Lib/ctypes/test/test_memfunctions.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index 4ebdb797d7d5fe8c76286cfb1a8f4290011db970..5937304495636dd6695c73caf38536a3b9ee438f 100644 (file)
@@ -478,7 +478,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."""
@@ -490,7 +490,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 525c9bd9f08fe5f3aa2778b477db0552f9e23d63..a5b3fc7998e122fce70a75c54bcc3a93b5c34d2b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -197,6 +197,9 @@ Extension Modules
 Library
 -------
 
+- Fix bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0)
+  returned string up to the first NUL character.
+
 - Bug #1637850:  make_table in difflib did not work with unicode
 
 - Bugs #1676321:  the empty() function in sched.py returned the wrong result
index 89c5aaea55bd2ac7cfcbfe6d0c056fe51753ec9f..69d3157bcfacc54829ce69fdee755d0e949e6dfa 100644 (file)
@@ -4531,9 +4531,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);
 }
@@ -4618,7 +4618,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);
 }