[Python-3000-checkins] r56346 - in python/branches/py3k-struni: Lib/ctypes/test/test_buffers.py Lib/ctypes/test/test_bytes.py Lib/ctypes/test/test_objects.py Lib/ctypes/test/test_random_things.py Lib/ctypes/test/test_repr.py Lib/ctypes/test/test_slicing.py Modules/_ctypes/cfield.c

thomas.heller python-3000-checkins at python.org
Fri Jul 13 14:52:51 CEST 2007


Author: thomas.heller
Date: Fri Jul 13 14:52:51 2007
New Revision: 56346

Modified:
   python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py
   python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py
   python/branches/py3k-struni/Lib/ctypes/test/test_objects.py
   python/branches/py3k-struni/Lib/ctypes/test/test_random_things.py
   python/branches/py3k-struni/Lib/ctypes/test/test_repr.py
   python/branches/py3k-struni/Lib/ctypes/test/test_slicing.py
   python/branches/py3k-struni/Modules/_ctypes/cfield.c
Log:
c_char, c_char_p objects and c_char array structure fields return
their value now as str, no longer str8.


Modified: python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_buffers.py	Fri Jul 13 14:52:51 2007
@@ -7,12 +7,12 @@
         b = create_string_buffer(32)
         self.failUnlessEqual(len(b), 32)
         self.failUnlessEqual(sizeof(b), 32 * sizeof(c_char))
-        self.failUnless(type(b[0]) is str8)
+        self.failUnless(type(b[0]) is str)
 
         b = create_string_buffer("abc")
         self.failUnlessEqual(len(b), 4) # trailing nul char
         self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
-        self.failUnless(type(b[0]) is str8)
+        self.failUnless(type(b[0]) is str)
         self.failUnlessEqual(b[0], "a")
         self.failUnlessEqual(b[:], "abc\0")
 
@@ -20,7 +20,7 @@
         b = create_string_buffer("abc")
         self.failUnlessEqual(len(b), 4) # trailing nul char
         self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char))
-        self.failUnless(type(b[0]) is str8)
+        self.failUnless(type(b[0]) is str)
         self.failUnlessEqual(b[0], "a")
         self.failUnlessEqual(b[:], "abc\0")
 

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py	Fri Jul 13 14:52:51 2007
@@ -29,14 +29,18 @@
             _fields_ = [("a", c_char * 3)]
 
         X("abc")
-        X(b"abc")
+        x = X(b"abc")
+        self.assertEqual(x.a, "abc")
+        self.assertEqual(type(x.a), str)
 
     def test_struct_W(self):
         class X(Structure):
             _fields_ = [("a", c_wchar * 3)]
 
         X("abc")
-        X(b"abc")
+        x = X(b"abc")
+        self.assertEqual(x.a, "abc")
+        self.assertEqual(type(x.a), str)
 
     if sys.platform == "win32":
         def test_BSTR(self):

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_objects.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_objects.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_objects.py	Fri Jul 13 14:52:51 2007
@@ -24,7 +24,7 @@
 >>> array._objects
 {'4': b'foo bar'}
 >>> array[4]
-s'foo bar'
+'foo bar'
 >>>
 
 It gets more complicated when the ctypes instance itself is contained

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_random_things.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_random_things.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_random_things.py	Fri Jul 13 14:52:51 2007
@@ -69,7 +69,7 @@
         out = self.capture_stderr(cb, "spam")
         self.failUnlessEqual(out.splitlines()[-1],
                              "TypeError: "
-                             "unsupported operand type(s) for /: 'int' and 'str8'")
+                             "unsupported operand type(s) for /: 'int' and 'str'")
 
 if __name__ == '__main__':
     unittest.main()

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_repr.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_repr.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_repr.py	Fri Jul 13 14:52:51 2007
@@ -22,7 +22,7 @@
             self.failUnlessEqual("<X object at", repr(typ(42))[:12])
 
     def test_char(self):
-        self.failUnlessEqual("c_char(s'x')", repr(c_char('x')))
+        self.failUnlessEqual("c_char('x')", repr(c_char('x')))
         self.failUnlessEqual("<X object at", repr(X('x'))[:12])
 
 if __name__ == "__main__":

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_slicing.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_slicing.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_slicing.py	Fri Jul 13 14:52:51 2007
@@ -70,7 +70,7 @@
         dll.my_strdup.errcheck = errcheck
         try:
             res = dll.my_strdup(s)
-            self.failUnlessEqual(res, s)
+            self.failUnlessEqual(res, str(s))
         finally:
             del dll.my_strdup.errcheck
 

Modified: python/branches/py3k-struni/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/py3k-struni/Modules/_ctypes/cfield.c	(original)
+++ python/branches/py3k-struni/Modules/_ctypes/cfield.c	Fri Jul 13 14:52:51 2007
@@ -1137,9 +1137,7 @@
 			return NULL;
 		if (PyBytes_GET_SIZE(value) != 1) {
 			Py_DECREF(value);
-			PyErr_Format(PyExc_TypeError,
-				     "one character string expected");
-			return NULL;
+			goto error;
 		}
 		*(char *)ptr = PyBytes_AsString(value)[0];
 		Py_DECREF(value);
@@ -1149,22 +1147,17 @@
 		*(char *)ptr = PyBytes_AsString(value)[0];
 		_RET(value);
 	}
-	/* XXX struni remove later */
-	if (!PyString_Check(value) || (1 != PyString_Size(value))) {
-		PyErr_Format(PyExc_TypeError,
-			     "one character string expected");
-		return NULL;
-	}
-	*(char *)ptr = PyString_AS_STRING(value)[0];
-	_RET(value);
+  error:
+	PyErr_Format(PyExc_TypeError,
+		     "one character string expected");
+	return NULL;
 }
 
 
 static PyObject *
 c_get(void *ptr, Py_ssize_t size)
 {
-	/* XXX struni return PyBytes (or PyUnicode?) later */
-	return PyString_FromStringAndSize((char *)ptr, 1);
+	return PyUnicode_FromStringAndSize((char *)ptr, 1);
 }
 
 #ifdef CTYPES_UNICODE
@@ -1280,24 +1273,16 @@
 static PyObject *
 s_get(void *ptr, Py_ssize_t size)
 {
-	PyObject *result;
-	size_t slen;
+	Py_ssize_t i;
+	char *p;
 
-	result = PyString_FromString((char *)ptr);
-	if (!result)
-		return NULL;
-	/* chop off at the first NUL character, if any.
-	 * On error, result will be deallocated and set to NULL.
-	 */
-	slen = strlen(PyString_AS_STRING(result));
-	size = min(size, (Py_ssize_t)slen);
-	if (result->ob_refcnt == 1) {
-		/* shorten the result */
-		_PyString_Resize(&result, size);
-		return result;
-	} else
-		/* cannot shorten the result */
-		return PyString_FromStringAndSize(ptr, size);
+	p = (char *)ptr;
+	for (i = 0; i < size; ++i) {
+		if (*p++ == '\0')
+			break;
+	}
+
+	return PyUnicode_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
 }
 
 static PyObject *
@@ -1393,7 +1378,7 @@
 			return NULL;
 		}
 #endif
-		return PyString_FromString(*(char **)ptr);
+		return PyUnicode_FromString(*(char **)ptr);
 	} else {
 		Py_INCREF(Py_None);
 		return Py_None;


More information about the Python-3000-checkins mailing list