]> granicus.if.org Git - python/commitdiff
Python strings ending with '\0' should not be equivalent to their C counterparts...
authorBenjamin Peterson <benjamin@python.org>
Sat, 9 Jan 2010 21:45:28 +0000 (21:45 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 9 Jan 2010 21:45:28 +0000 (21:45 +0000)
Misc/NEWS
Modules/_testcapimodule.c
Objects/unicodeobject.c

index adf9fb3c21c760528031c40a47dfe189820f89e5..fc9fa6371afb54a0eb95c8365fc65700450bed1e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -155,6 +155,9 @@ Core and Builtins
 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
index 19afbf283afba6d25cb0f4723e9fd1cf98510235..56717837a300b9a511ae15ea33901b144df6cc60 100644 (file)
@@ -1287,6 +1287,23 @@ test_string_from_format(PyObject *self, PyObject *args)
 #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 @@ static PyMethodDef TestMethods[] = {
        {"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, 
index 9c0be9b23ca7569fd6ec3e965b9d2504a7c3e424..40377ea48d80075cce1bc62f7cdff524c69c2eaf 100644 (file)
@@ -7001,6 +7001,11 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
     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])