speed up object attribute setting a little
authorStefan Behnel <stefan_ml@behnel.de>
Fri, 8 Mar 2013 21:11:43 +0000 (22:11 +0100)
committerStefan Behnel <stefan_ml@behnel.de>
Fri, 8 Mar 2013 21:11:43 +0000 (22:11 +0100)
Cython/Compiler/ExprNodes.py
Cython/Utility/ObjectHandling.c

index 965e1cc..dfe09b6 100755 (executable)
@@ -1837,7 +1837,7 @@ class NameNode(AtomicExprNode):
             elif entry.is_pyclass_attr:
                 setter = 'PyObject_SetItem'
             else:
-                setter = 'PyObject_SetAttr'
+                assert False, repr(entry)
             code.put_error_if_neg(
                 self.pos,
                 '%s(%s, %s, %s)' % (
@@ -5171,8 +5171,10 @@ class AttributeNode(ExprNode):
     def generate_assignment_code(self, rhs, code):
         self.obj.generate_evaluation_code(code)
         if self.is_py_attr:
+            code.globalstate.use_utility_code(
+                UtilityCode.load_cached("PyObjectSetAttrStr", "ObjectHandling.c"))
             code.put_error_if_neg(self.pos,
-                'PyObject_SetAttr(%s, %s, %s)' % (
+                '__Pyx_PyObject_SetAttrStr(%s, %s, %s)' % (
                     self.obj.py_result(),
                     code.intern_identifier(self.attribute),
                     rhs.py_result()))
index 3f253a3..075b7fd 100644 (file)
@@ -657,6 +657,23 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject
 #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)
 #endif
 
+/////////////// PyObjectSetAttrStr.proto ///////////////
+
+#if CYTHON_COMPILING_IN_CPYTHON
+static CYTHON_INLINE int __Pyx_PyObject_SetAttrStr(PyObject* obj, PyObject* attr_name, PyObject* value) {
+    PyTypeObject* tp = Py_TYPE(obj);
+    if (likely(tp->tp_setattro))
+        return tp->tp_setattro(obj, attr_name, value);
+#if PY_MAJOR_VERSION < 3
+    if (likely(tp->tp_setattr))
+        return tp->tp_setattr(obj, PyString_AS_STRING(attr_name), value);
+#endif
+    return PyObject_SetAttr(obj, attr_name, value);
+}
+#else
+#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
+#endif
+
 /////////////// PyObjectCallMethod.proto ///////////////
 //@requires: PyObjectGetAttrStr
 //@substitute: naming