get rid of the few usages of strcmp(), now replaced with utility inline function...
authorLisandro Dalcin <dalcinl@gmail.com>
Sun, 8 Mar 2009 00:33:39 +0000 (21:33 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Sun, 8 Mar 2009 00:33:39 +0000 (21:33 -0300)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py
Cython/Compiler/Symtab.py

index 02da52022e7e8e08d0cee4a750106f8aa83046ed..58199cb00d954a35b0f94408e0b811dfed22573f 100644 (file)
@@ -558,6 +558,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
             code.putln('static char %s[] = "%s";' % (
                     env.doc_cname, escape_byte_string(docstr)))
 
+        env.use_utility_code(streq_utility_code)
+
     def generate_extern_c_macro_definition(self, code):
         name = Naming.extern_c_macro
         code.putln("#ifdef __cplusplus")
@@ -1517,7 +1519,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set)
         code.putln("char** type_name = %s_type_names;" % Naming.import_star)
         code.putln("while (*type_name) {")
-        code.putln("if (!strcmp(name, *type_name)) {")
+        code.putln("if (__Pyx_StrEq(name, *type_name)) {")
         code.putln('PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);')
         code.putln('goto bad;')
         code.putln("}")
@@ -1527,7 +1529,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         code.putln("if (0);") # so the first one can be "else if"
         for name, entry in env.entries.items():
             if entry.is_cglobal and entry.used:
-                code.putln('else if (!strcmp(name, "%s")) {' % name)
+                code.putln('else if (__Pyx_StrEq(name, "%s")) {' % name)
                 if entry.type.is_pyobject:
                     if entry.type.is_extension_type or entry.type.is_builtin_type:
                         code.putln("if (!(%s)) %s;" % (
@@ -2030,6 +2032,21 @@ proto = """\
 #endif
 """)
 
+#------------------------------------------------------------------------------------
+
+streq_utility_code = UtilityCode(
+proto = """
+static INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
+""",
+impl = """
+static INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
+     while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
+     return *s1 == *s2;
+}
+""")
+
+#------------------------------------------------------------------------------------
+
 import_module_utility_code = UtilityCode(
 proto = """
 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
@@ -2171,7 +2188,8 @@ static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void **f
 #endif
     PyObject *d = 0;
     PyObject *cobj = 0;
-    char *desc;
+    const char *desc;
+    const char *s1, *s2;
 
     d = PyObject_GetAttrString(module, api);
     if (!d)
@@ -2183,13 +2201,15 @@ static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void **f
                 PyModule_GetName(module), funcname);
         goto bad;
     }
-    desc = (char *)PyCObject_GetDesc(cobj);
+    desc = (const char *)PyCObject_GetDesc(cobj);
     if (!desc)
         goto bad;
-    if (strcmp(desc, sig) != 0) {
+    s1 = desc; s2 = sig;
+    while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
+    if (*s1 != *s2) {
         PyErr_Format(PyExc_TypeError,
             "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
-                PyModule_GetName(module), funcname, sig, desc);
+             PyModule_GetName(module), funcname, sig, desc);
         goto bad;
     }
     *f = PyCObject_AsVoidPtr(cobj);
index c174cb836db9fd4084437e5a992b04f6e11c72fa..171151a3688215f56f29f2add6e36de2cff9901a 100644 (file)
@@ -5248,8 +5248,7 @@ static int __Pyx_ParseOptionalKeywords(
                         PyUnicode_Compare(**name, key) == 0) break;
                     #else
                     if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
-                        strcmp(PyString_AS_STRING(**name),
-                               PyString_AS_STRING(key)) == 0) break;
+                        _PyString_Eq(**name, key)) break;
                     #endif
                 }
                 if (*name) {
@@ -5263,8 +5262,7 @@ static int __Pyx_ParseOptionalKeywords(
                             PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
                         #else
                         if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
-                            strcmp(PyString_AS_STRING(**name),
-                                   PyString_AS_STRING(key)) == 0) goto arg_passed_twice;
+                            _PyString_Eq(**name, key)) goto arg_passed_twice;
                         #endif
                     }
                     if (kwds2) {
index b1fdfac30f828dcd35304f7cf6169d3d9baed96e..14a205b948173baa551616c821b83a16df4b5149 100644 (file)
@@ -1562,7 +1562,7 @@ impl = """
 static PyObject* __Pyx_Method_ClassMethod(PyObject *method) {
     /* It appears that PyMethodDescr_Type is not anywhere exposed in the Python/C API */
     /* if (!PyObject_TypeCheck(method, &PyMethodDescr_Type)) { */ 
-    if (strcmp(Py_TYPE(method)->tp_name, "method_descriptor") == 0) { /* cdef classes */
+    if (__Pyx_StrEq(Py_TYPE(method)->tp_name, "method_descriptor")) { /* cdef classes */
         PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
         return PyDescr_NewClassMethod(descr->d_type, descr->d_method);
     }