[Python-checkins] r77174 - in python/branches/py3k: Doc/c-api/object.rst Misc/NEWS Objects/bytesobject.c

alexandre.vassalotti python-checkins at python.org
Thu Dec 31 04:56:10 CET 2009


Author: alexandre.vassalotti
Date: Thu Dec 31 04:56:09 2009
New Revision: 77174

Log:
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.


Modified:
   python/branches/py3k/Doc/c-api/object.rst
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/bytesobject.c

Modified: python/branches/py3k/Doc/c-api/object.rst
==============================================================================
--- python/branches/py3k/Doc/c-api/object.rst	(original)
+++ python/branches/py3k/Doc/c-api/object.rst	Thu Dec 31 04:56:09 2009
@@ -142,10 +142,11 @@
 
    .. index:: builtin: bytes
 
-   Compute a bytes representation of object *o*.  *NULL* is returned on failure
-   and a bytes object on success.  This is equivalent to the Python expression
-   ``bytes(o)``.
-
+   Compute a bytes representation of object *o*.  *NULL* is returned on
+   failure and a bytes object on success.  This is equivalent to the Python
+   expression ``bytes(o)``, when *o* is not an integer.  Unlike ``bytes(o)``,
+   a TypeError is raised when *o* is an integer instead of a zero-initialized
+   bytes object.
 
 .. cfunction:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Dec 31 04:56:09 2009
@@ -140,6 +140,9 @@
 
 - Issue #4856: Remove checks for win NT.
 
+- Issue #6687: PyBytes_FromObject() no longer accepts an integer as its
+  argument to construct a null-initialized bytes object.
+
 C-API
 -----
 

Modified: python/branches/py3k/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k/Objects/bytesobject.c	(original)
+++ python/branches/py3k/Objects/bytesobject.c	Thu Dec 31 04:56:09 2009
@@ -2884,6 +2884,7 @@
 	const char *encoding = NULL;
 	const char *errors = NULL;
 	PyObject *new = NULL;
+	Py_ssize_t size;
 	static char *kwlist[] = {"source", "encoding", "errors", 0};
 
 	if (type != &PyBytes_Type)
@@ -2914,6 +2915,25 @@
 		assert(PyBytes_Check(new));
 		return new;
 	}
+	/* Is it an integer? */
+	size = PyNumber_AsSsize_t(x, PyExc_ValueError);
+	if (size == -1 && PyErr_Occurred()) {
+		PyErr_Clear();		
+	}
+	else {
+		if (size < 0) {
+			PyErr_SetString(PyExc_ValueError, "negative count");
+			return NULL;
+		}
+		new = PyBytes_FromStringAndSize(NULL, size);
+		if (new == NULL) {
+			return NULL;
+		}
+		if (size > 0) {
+			memset(((PyBytesObject*)new)->ob_sval, 0, size);
+		}
+		return new;
+	}
 
 	/* If it's not unicode, there can't be encoding or errors */
 	if (encoding != NULL || errors != NULL) {
@@ -2934,27 +2954,6 @@
 		PyErr_BadInternalCall();
 		return NULL;
 	}
-
-	/* Is it an int? */
-	size = PyNumber_AsSsize_t(x, PyExc_ValueError);
-	if (size == -1 && PyErr_Occurred()) {
-		PyErr_Clear();
-	}
-	else {
-		if (size < 0) {
-			PyErr_SetString(PyExc_ValueError, "negative count");
-			return NULL;
-		}
-		new = PyBytes_FromStringAndSize(NULL, size);
-		if (new == NULL) {
-			return NULL;
-		}
-		if (size > 0) {
-			memset(((PyBytesObject*)new)->ob_sval, 0, size);
-		}
-		return new;
-	}
-
 	/* Use the modern buffer interface */
 	if (PyObject_CheckBuffer(x)) {
 		Py_buffer view;
@@ -2974,6 +2973,11 @@
 		PyBuffer_Release(&view);
 		return NULL;
 	}
+	if (PyUnicode_Check(x)) {
+		PyErr_SetString(PyExc_TypeError,
+				"cannot convert unicode object to bytes");
+		return NULL;
+	}
 
 	/* For iterator version, create a string object and resize as needed */
 	/* XXX(gb): is 64 a good value? also, optimize if length is known */


More information about the Python-checkins mailing list