[Python-checkins] r54244 - in python/trunk: Lib/ctypes/__init__.py Lib/ctypes/test/test_memfunctions.py Misc/NEWS Modules/_ctypes/_ctypes.c

thomas.heller python-checkins at python.org
Fri Mar 9 20:21:34 CET 2007


Author: thomas.heller
Date: Fri Mar  9 20:21:28 2007
New Revision: 54244

Modified:
   python/trunk/Lib/ctypes/__init__.py
   python/trunk/Lib/ctypes/test/test_memfunctions.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_ctypes/_ctypes.c
Log:
Fix bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0)
returned string up to the first NUL character.


Modified: python/trunk/Lib/ctypes/__init__.py
==============================================================================
--- python/trunk/Lib/ctypes/__init__.py	(original)
+++ python/trunk/Lib/ctypes/__init__.py	Fri Mar  9 20:21:28 2007
@@ -480,7 +480,7 @@
     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 @@
     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."""

Modified: python/trunk/Lib/ctypes/test/test_memfunctions.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_memfunctions.py	(original)
+++ python/trunk/Lib/ctypes/test/test_memfunctions.py	Fri Mar  9 20:21:28 2007
@@ -14,6 +14,7 @@
         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 @@
             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()

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Mar  9 20:21:28 2007
@@ -152,6 +152,9 @@
 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.

Modified: python/trunk/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/trunk/Modules/_ctypes/_ctypes.c	(original)
+++ python/trunk/Modules/_ctypes/_ctypes.c	Fri Mar  9 20:21:28 2007
@@ -4537,9 +4537,9 @@
 #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 @@
 static PyObject *
 wstring_at(const wchar_t *ptr, int size)
 {
-	if (size == 0)
+	if (size == -1)
 		size = wcslen(ptr);
 	return PyUnicode_FromWideChar(ptr, size);
 }


More information about the Python-checkins mailing list