[Python-3000-checkins] r58843 - in python/branches/py3k-pep3137: Lib/ctypes/test/test_array_in_pointer.py Lib/ctypes/test/test_byteswap.py Lib/ctypes/test/test_slicing.py Modules/_ctypes/_ctypes.c Modules/_ctypes/callproc.c Modules/_ctypes/cfield.c Modules/binascii.c

christian.heimes python-3000-checkins at python.org
Sun Nov 4 17:59:11 CET 2007


Author: christian.heimes
Date: Sun Nov  4 17:59:11 2007
New Revision: 58843

Modified:
   python/branches/py3k-pep3137/Lib/ctypes/test/test_array_in_pointer.py
   python/branches/py3k-pep3137/Lib/ctypes/test/test_byteswap.py
   python/branches/py3k-pep3137/Lib/ctypes/test/test_slicing.py
   python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c
   python/branches/py3k-pep3137/Modules/_ctypes/callproc.c
   python/branches/py3k-pep3137/Modules/_ctypes/cfield.c
   python/branches/py3k-pep3137/Modules/binascii.c
Log:
Applied patch from Amaury Forgeot d'Arc, #1382
This patch corrects test_ctypes in the py3k-pep3137 branch.
Replacing PyBytes_* by PyString_* was 99% of the task.

Also had to modify binascii, which used to return buffers instead of
bytes strings.

Tested on winXP.
And tested on Ubuntu Linux i386 by me.

Modified: python/branches/py3k-pep3137/Lib/ctypes/test/test_array_in_pointer.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/ctypes/test/test_array_in_pointer.py	(original)
+++ python/branches/py3k-pep3137/Lib/ctypes/test/test_array_in_pointer.py	Sun Nov  4 17:59:11 2007
@@ -6,7 +6,7 @@
 def dump(obj):
     # helper function to dump memory contents in hex, with a hyphen
     # between the bytes.
-    h = str(hexlify(memoryview(obj)))
+    h = hexlify(memoryview(obj)).decode()
     return re.sub(r"(..)", r"\1-", h)[:-1]
 
 

Modified: python/branches/py3k-pep3137/Lib/ctypes/test/test_byteswap.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/ctypes/test/test_byteswap.py	(original)
+++ python/branches/py3k-pep3137/Lib/ctypes/test/test_byteswap.py	Sun Nov  4 17:59:11 2007
@@ -4,7 +4,7 @@
 from ctypes import *
 
 def bin(s):
-    return str(hexlify(memoryview(s))).upper()
+    return hexlify(memoryview(s)).decode().upper()
 
 # Each *simple* type that supports different byte orders has an
 # __ctype_be__ attribute that specifies the same type in BIG ENDIAN

Modified: python/branches/py3k-pep3137/Lib/ctypes/test/test_slicing.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/ctypes/test/test_slicing.py	(original)
+++ python/branches/py3k-pep3137/Lib/ctypes/test/test_slicing.py	Sun Nov  4 17:59:11 2007
@@ -115,7 +115,7 @@
         dll.my_strdup.errcheck = errcheck
         try:
             res = dll.my_strdup(s)
-            self.failUnlessEqual(res, str(s))
+            self.failUnlessEqual(res, s.decode())
         finally:
             del dll.my_strdup.errcheck
 

Modified: python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c	(original)
+++ python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c	Sun Nov  4 17:59:11 2007
@@ -763,7 +763,7 @@
 static PyObject *
 CharArray_get_raw(CDataObject *self)
 {
-	return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
+	return PyString_FromStringAndSize(self->b_ptr, self->b_size);
 }
 
 static PyObject *
@@ -774,7 +774,7 @@
 	for (i = 0; i < self->b_size; ++i)
 		if (*ptr++ == '\0')
 			break;
-	return PyBytes_FromStringAndSize(self->b_ptr, i);
+	return PyString_FromStringAndSize(self->b_ptr, i);
 }
 
 static int
@@ -789,7 +789,7 @@
 						  conversion_mode_errors);
 		if (!value)
 			return -1;
-	} else if (!PyBytes_Check(value)) {
+	} else if (!PyString_Check(value)) {
 		PyErr_Format(PyExc_TypeError,
 			     "str/bytes expected instead of %s instance",
 			     Py_Type(value)->tp_name);
@@ -838,7 +838,7 @@
 {
 	Py_ssize_t result = 0;
 
-	if (PyBytes_Check(value)) {
+	if (PyString_Check(value)) {
 		value = PyUnicode_FromEncodedObject(value,
 						    conversion_mode_encoding,
 						    conversion_mode_errors);
@@ -1106,7 +1106,7 @@
 		Py_INCREF(Py_None);
 		return Py_None;
 	}
-	if (PyUnicode_Check(value) || PyBytes_Check(value)) {
+	if (PyUnicode_Check(value) || PyString_Check(value)) {
 		PyCArgObject *parg;
 		struct fielddesc *fd = getentry("Z");
 
@@ -1167,7 +1167,7 @@
 		Py_INCREF(Py_None);
 		return Py_None;
 	}
-	if (PyBytes_Check(value) || PyUnicode_Check(value)) {
+	if (PyString_Check(value) || PyUnicode_Check(value)) {
 		PyCArgObject *parg;
 		struct fielddesc *fd = getentry("z");
 
@@ -1251,7 +1251,7 @@
 	}
 	/* XXX struni: remove later */
 /* string */
-	if (PyBytes_Check(value)) {
+	if (PyString_Check(value)) {
 		PyCArgObject *parg;
 		struct fielddesc *fd = getentry("z");
 
@@ -2705,8 +2705,8 @@
 		return 1;
 	}
 #endif
-	if (PyBytes_Check(obj)) {
-		*pname = PyBytes_AS_STRING(obj);
+	if (PyString_Check(obj)) {
+		*pname = PyString_AS_STRING(obj);
 		return *pname ? 1 : 0;
 	}
 	if (PyUnicode_Check(obj)) {
@@ -3734,9 +3734,9 @@
 			char *dest;
 
 			if (slicelen <= 0)
-				return PyBytes_FromStringAndSize("", 0);
+				return PyString_FromStringAndSize("", 0);
 			if (step == 1) {
-				return PyBytes_FromStringAndSize(ptr + start,
+				return PyString_FromStringAndSize(ptr + start,
 								 slicelen);
 			}
 			dest = (char *)PyMem_Malloc(slicelen);
@@ -3749,7 +3749,7 @@
 				dest[i] = ptr[cur];
 			}
 
-			np = PyBytes_FromStringAndSize(dest, slicelen);
+			np = PyString_FromStringAndSize(dest, slicelen);
 			PyMem_Free(dest);
 			return np;
 		}
@@ -4411,9 +4411,9 @@
 			char *dest;
 			
 			if (len <= 0)
-                        	return PyBytes_FromStringAndSize("", 0);
+                        	return PyString_FromStringAndSize("", 0);
 			if (step == 1) {
-				return PyBytes_FromStringAndSize(ptr + start,
+				return PyString_FromStringAndSize(ptr + start,
 								 len);
 			}
 			dest = (char *)PyMem_Malloc(len);
@@ -4422,7 +4422,7 @@
 			for (cur = start, i = 0; i < len; cur += step, i++) {
 				dest[i] = ptr[cur];
 			}
-			np = PyBytes_FromStringAndSize(dest, len);
+			np = PyString_FromStringAndSize(dest, len);
 			PyMem_Free(dest);
 			return np;
 		}
@@ -4658,8 +4658,8 @@
 string_at(const char *ptr, int size)
 {
 	if (size == -1)
-		return PyBytes_FromStringAndSize(ptr, strlen(ptr));
-	return PyBytes_FromStringAndSize(ptr, size);
+		return PyString_FromStringAndSize(ptr, strlen(ptr));
+	return PyString_FromStringAndSize(ptr, size);
 }
 
 static int

Modified: python/branches/py3k-pep3137/Modules/_ctypes/callproc.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_ctypes/callproc.c	(original)
+++ python/branches/py3k-pep3137/Modules/_ctypes/callproc.c	Sun Nov  4 17:59:11 2007
@@ -507,9 +507,9 @@
 		return 0;
 	}
 
-	if (PyBytes_Check(obj)) {
+	if (PyString_Check(obj)) {
 		pa->ffi_type = &ffi_type_pointer;
-		pa->value.p = PyBytes_AsString(obj);
+		pa->value.p = PyString_AsString(obj);
 		Py_INCREF(obj);
 		pa->keep = obj;
 		return 0;

Modified: python/branches/py3k-pep3137/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_ctypes/cfield.c	(original)
+++ python/branches/py3k-pep3137/Modules/_ctypes/cfield.c	Sun Nov  4 17:59:11 2007
@@ -1191,7 +1191,7 @@
 static PyObject *
 c_get(void *ptr, Py_ssize_t size)
 {
-	return PyBytes_FromStringAndSize((char *)ptr, 1);
+	return PyString_FromStringAndSize((char *)ptr, 1);
 }
 
 #ifdef CTYPES_UNICODE
@@ -1200,7 +1200,7 @@
 u_set(void *ptr, PyObject *value, Py_ssize_t size)
 {
 	Py_ssize_t len;
-	if (PyBytes_Check(value)) {
+	if (PyString_Check(value)) {
 		value = PyUnicode_FromEncodedObject(value,
 						    conversion_mode_encoding,
 						    conversion_mode_errors);
@@ -1275,7 +1275,7 @@
 	/* It's easier to calculate in characters than in bytes */
 	length /= sizeof(wchar_t);
 
-	if (PyBytes_Check(value)) {
+	if (PyString_Check(value)) {
 		value = PyUnicode_FromEncodedObject(value,
 						    conversion_mode_encoding,
 						    conversion_mode_errors);
@@ -1372,8 +1372,8 @@
 		Py_INCREF(value);
 		return value;
 	}
-	if (PyBytes_Check(value)) {
-		*(char **)ptr = PyBytes_AsString(value);
+	if (PyString_Check(value)) {
+		*(char **)ptr = PyString_AsString(value);
 		Py_INCREF(value);
 		return value;
 	} else if (PyUnicode_Check(value)) {
@@ -1436,7 +1436,7 @@
 		Py_INCREF(Py_None);
 		return Py_None;
 	}
-	if (PyBytes_Check(value)) {
+	if (PyString_Check(value)) {
 		value = PyUnicode_FromEncodedObject(value,
 						    conversion_mode_encoding,
 						    conversion_mode_errors);
@@ -1519,7 +1519,7 @@
 	/* convert value into a PyUnicodeObject or NULL */
 	if (Py_None == value) {
 		value = NULL;
-	} else if (PyBytes_Check(value)) {
+	} else if (PyString_Check(value)) {
 		value = PyUnicode_FromEncodedObject(value,
 						    conversion_mode_encoding,
 						    conversion_mode_errors);

Modified: python/branches/py3k-pep3137/Modules/binascii.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/binascii.c	(original)
+++ python/branches/py3k-pep3137/Modules/binascii.c	Sun Nov  4 17:59:11 2007
@@ -200,9 +200,9 @@
 	ascii_len--;
 
 	/* Allocate the buffer */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
 		return NULL;
-	bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	bin_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
 		/* XXX is it really best to add NULs if there's no more data */
@@ -277,9 +277,9 @@
 	}
 
 	/* We're lazy and allocate to much (fixed up later) */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
 		return NULL;
-	ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	ascii_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	/* Store the length */
 	*ascii_data++ = ' ' + (bin_len & 077);
@@ -301,9 +301,9 @@
 	}
 	*ascii_data++ = '\n';	/* Append a courtesy newline */
 
-	if (PyBytes_Resize(rv,
+	if (_PyString_Resize(&rv,
                            (ascii_data -
-                            (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+                            (unsigned char *)PyString_AS_STRING(rv))) < 0) {
 		Py_DECREF(rv);
 		rv = NULL;
 	}
@@ -355,9 +355,9 @@
 	bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
 
 	/* Allocate the buffer */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
 		return NULL;
-	bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	bin_data = (unsigned char *)PyString_AS_STRING(rv);
 	bin_len = 0;
 
 	for( ; ascii_len > 0; ascii_len--, ascii_data++) {
@@ -416,17 +416,17 @@
 
 	/* And set string size correctly. If the result string is empty
 	** (because the input was all invalid) return the shared empty
-	** string instead; PyBytes_Resize() won't do this for us.
+	** string instead; _PyString_Resize() won't do this for us.
 	*/
 	if (bin_len > 0) {
-		if (PyBytes_Resize(rv, bin_len) < 0) {
+		if (_PyString_Resize(&rv, bin_len) < 0) {
 			Py_DECREF(rv);
 			rv = NULL;
 		}
 	}
 	else {
 		Py_DECREF(rv);
-		rv = PyBytes_FromStringAndSize("", 0);
+		rv = PyString_FromStringAndSize("", 0);
 	}
 	return rv;
 }
@@ -453,9 +453,9 @@
 	/* We're lazy and allocate too much (fixed up later).
 	   "+3" leaves room for up to two pad characters and a trailing
 	   newline.  Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
 		return NULL;
-	ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	ascii_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
 		/* Shift the data into our buffer */
@@ -479,9 +479,9 @@
 	}
 	*ascii_data++ = '\n';	/* Append a courtesy newline */
 
-	if (PyBytes_Resize(rv,
+	if (_PyString_Resize(&rv,
 			   (ascii_data -
-			    (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+			    (unsigned char *)PyString_AS_STRING(rv))) < 0) {
 		Py_DECREF(rv);
 		rv = NULL;
 	}
@@ -507,9 +507,9 @@
 	/* Allocate a string that is too big (fixed later) 
 	   Add two to the initial length to prevent interning which
 	   would preclude subsequent resizing.  */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL )
 		return NULL;
-	bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	bin_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	for( ; len > 0 ; len--, ascii_data++ ) {
 		/* Get the byte and look it up */
@@ -543,9 +543,9 @@
 		Py_DECREF(rv);
 		return NULL;
 	}
-	if (PyBytes_Resize(rv,
+	if (_PyString_Resize(&rv,
 			   (bin_data -
-			    (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+			    (unsigned char *)PyString_AS_STRING(rv))) < 0) {
 		Py_DECREF(rv);
 		rv = NULL;
 	}
@@ -572,9 +572,9 @@
 		return NULL;
 
 	/* Worst case: output is twice as big as input (fixed later) */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
 		return NULL;
-	out_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	out_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	for( in=0; in<len; in++) {
 		ch = in_data[in];
@@ -600,9 +600,9 @@
 			}
 		}
 	}
-	if (PyBytes_Resize(rv,
+	if (_PyString_Resize(&rv,
 			   (out_data -
-			    (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+			    (unsigned char *)PyString_AS_STRING(rv))) < 0) {
 		Py_DECREF(rv);
 		rv = NULL;
 	}
@@ -625,9 +625,9 @@
 		return NULL;
 
 	/* Allocate a buffer that is at least large enough */
-	if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
 		return NULL;
-	ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	ascii_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	for( ; len > 0 ; len--, bin_data++ ) {
 		/* Shift into our buffer, and output any 6bits ready */
@@ -644,9 +644,9 @@
 		leftchar <<= (6-leftbits);
 		*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
 	}
-	if (PyBytes_Resize(rv,
+	if (_PyString_Resize(&rv,
 			   (ascii_data -
-			    (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+			    (unsigned char *)PyString_AS_STRING(rv))) < 0) {
 		Py_DECREF(rv);
 		rv = NULL;
 	}
@@ -668,14 +668,14 @@
 
 	/* Empty string is a special case */
 	if ( in_len == 0 )
-		return PyBytes_FromStringAndSize("", 0);
+		return PyString_FromStringAndSize("", 0);
 
 	/* Allocate a buffer of reasonable size. Resized when needed */
 	out_len = in_len*2;
-	if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, out_len)) == NULL )
 		return NULL;
 	out_len_left = out_len;
-	out_data = (unsigned char *)PyBytes_AS_STRING(rv);
+	out_data = (unsigned char *)PyString_AS_STRING(rv);
 
 	/*
 	** We need two macros here to get/put bytes and handle
@@ -694,9 +694,9 @@
 #define OUTBYTE(b) \
 	do { \
 		 if ( --out_len_left < 0 ) { \
-			  if (PyBytes_Resize(rv, 2*out_len) < 0) \
+			  if (_PyString_Resize(&rv, 2*out_len) < 0) \
 			    { Py_DECREF(rv); return NULL; } \
-			  out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
+			  out_data = (unsigned char *)PyString_AS_STRING(rv) \
 								 + out_len; \
 			  out_len_left = out_len-1; \
 			  out_len = out_len * 2; \
@@ -744,9 +744,9 @@
 			OUTBYTE(in_byte);
 		}
 	}
-	if (PyBytes_Resize(rv,
+	if (_PyString_Resize(&rv,
 			   (out_data -
-			    (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+			    (unsigned char *)PyString_AS_STRING(rv))) < 0) {
 		Py_DECREF(rv);
 		rv = NULL;
 	}
@@ -940,10 +940,10 @@
 	if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen))
 		return NULL;
 
-	retval = PyBytes_FromStringAndSize(NULL, arglen*2);
+	retval = PyString_FromStringAndSize(NULL, arglen*2);
 	if (!retval)
 		return NULL;
-	retbuf = PyBytes_AS_STRING(retval);
+	retbuf = PyString_AS_STRING(retval);
 
 	/* make hex version of string, taken from shamodule.c */
 	for (i=j=0; i < arglen; i++) {
@@ -1000,10 +1000,10 @@
 		return NULL;
 	}
 
-	retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
+	retval = PyString_FromStringAndSize(NULL, (arglen/2));
 	if (!retval)
 		return NULL;
-	retbuf = PyBytes_AS_STRING(retval);
+	retbuf = PyString_AS_STRING(retval);
 
 	for (i=j=0; i < arglen; i += 2) {
 		int top = to_int(Py_CHARMASK(argbuf[i]));
@@ -1115,7 +1115,7 @@
 			out++;
 		}
 	}
-	if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
+	if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
 		PyMem_Free(odata);
 		return NULL;
 	}
@@ -1315,7 +1315,7 @@
 			}
 		}
 	}
-	if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
+	if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
 		PyMem_Free(odata);
 		return NULL;
 	}


More information about the Python-3000-checkins mailing list