[Pyrex] Pyrex generates broken code for "Z = X or Y"

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Wed Mar 29 21:35:53 CEST 2006


Hi,

I used the following statement in Pyrex 0.9.3.1 (patched, but shouldn't make a
difference).

cdef class XSLTContext:
    cdef object _extension_elements
    def __init__(self, namespaces, extensions, elements):
        self._extension_elements = elements or {}

Here, "elements" is either None or a dictionary, None in the case of the
tested call. The generated C code is

------------------------

__pyx_1 = __pyx_v_elements;

Py_INCREF(__pyx_1);

__pyx_2 = PyObject_IsTrue(__pyx_1); if (__pyx_2 < 0) {__pyx_filename =
__pyx_f[3]; __pyx_lineno = 208; goto __pyx_L1;}

if (!__pyx_2) {
  Py_DECREF(__pyx_1); __pyx_1 = 0;

  __pyx_3 = PyDict_New(); if (!__pyx_3) {__pyx_filename = __pyx_f[3];
__pyx_lineno = 208; goto __pyx_L1;}
}

Py_DECREF(((struct __pyx_obj_5etree_XSLTContext
*)__pyx_v_self)->_extension_elements);

((struct __pyx_obj_5etree_XSLTContext *)__pyx_v_self)->_extension_elements =
__pyx_1;

__pyx_1 = 0;

------------------------

The before last line assigns the wrong value to the object field. It should be
__pyx_3, but is __pyx_1. I actually believe that the assignment to __pyx_3
inside the if{} was meant to end up in __pyx_1, which would make sense here.
__pyx_3 should not have been used at all.

The obvious work-around is not to use the "or" statement for assignments
(which is cleaner anyway), but it's still a bug.

Stefan



More information about the Pyrex mailing list