[Python-checkins] r77395 - in python/branches/py3k: Misc/NEWS Modules/_testcapimodule.c Objects/unicodeobject.c

benjamin.peterson python-checkins at python.org
Sat Jan 9 22:45:29 CET 2010


Author: benjamin.peterson
Date: Sat Jan  9 22:45:28 2010
New Revision: 77395

Log:
Python strings ending with '\0' should not be equivalent to their C counterparts in PyUnicode_CompareWithASCIIString


Modified:
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/_testcapimodule.c
   python/branches/py3k/Objects/unicodeobject.c

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Jan  9 22:45:28 2010
@@ -155,6 +155,9 @@
 C-API
 -----
 
+- Make PyUnicode_CompareWithASCIIString return not equal if the Python string
+  has '\0' at the end.
+
 - Issue #5080: The argument parsing functions PyArg_ParseTuple,
   PyArg_ParseTupleAndKeywords, PyArg_VaParse,
   PyArg_VaParseTupleAndKeywords and PyArg_Parse now raise a

Modified: python/branches/py3k/Modules/_testcapimodule.c
==============================================================================
--- python/branches/py3k/Modules/_testcapimodule.c	(original)
+++ python/branches/py3k/Modules/_testcapimodule.c	Sat Jan  9 22:45:28 2010
@@ -1287,6 +1287,23 @@
 #undef CHECK_1_FORMAT
 }
 
+
+static PyObject *
+test_unicode_compare_with_ascii(PyObject *self) {
+	PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
+	int result;
+	if (py_s == NULL)
+		return NULL;
+	result = PyUnicode_CompareWithASCIIString(py_s, "str");
+	Py_DECREF(py_s);
+	if (!result) {
+		PyErr_SetString(TestError, "Python string ending in NULL "
+				"should not compare equal to c string.");
+		return NULL;
+	}
+	Py_RETURN_NONE;
+};
+
 /* This is here to provide a docstring for test_descr. */
 static PyObject *
 test_with_docstring(PyObject *self)
@@ -1756,6 +1773,7 @@
 	{"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
 	 PyDoc_STR("This is a pretty normal docstring.")},
 	{"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
+	{"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
 	{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
 	{"getargs_tuple",	getargs_tuple,			 METH_VARARGS},
 	{"getargs_keywords", (PyCFunction)getargs_keywords, 

Modified: python/branches/py3k/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k/Objects/unicodeobject.c	(original)
+++ python/branches/py3k/Objects/unicodeobject.c	Sat Jan  9 22:45:28 2010
@@ -7001,6 +7001,11 @@
     for (i = 0; id[i] && str[i]; i++)
         if (id[i] != str[i])
             return ((int)id[i] < (int)str[i]) ? -1 : 1;
+    /* This check keeps Python strings that end in '\0' from comparing equal
+     to C strings identical up to that point. */
+    if (PyUnicode_GET_SIZE(uni) != i)
+        /* We'll say the Python string is longer. */
+        return 1;
     if (id[i])
         return 1; /* uni is longer */
     if (str[i])


More information about the Python-checkins mailing list