[Pyrex] Pyrex extended assert code is sub-optimal

Stefan Behnel stefan_ml at behnel.de
Wed Oct 31 13:08:48 CET 2007


Hi,

I noticed that when I compile this code:

        assert c_node.type == tree.XML_ELEMENT_NODE, \
            "invalid node type %d, expected %d" % (
            c_node.type, tree.XML_ELEMENT_NODE)

Pyrex and Cython generate this:

--------------------------------------
#ifndef PYREX_WITHOUT_ASSERTIONS

__pyx_1 = PyInt_FromLong(__pyx_v_c_node->type); if (unlikely(!__pyx_1))
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; goto __pyx_L1;}
__pyx_2 = PyInt_FromLong(XML_ELEMENT_NODE); if (unlikely(!__pyx_2))
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 328; goto __pyx_L1;}
__pyx_3 = PyTuple_New(2); if (unlikely(!__pyx_3)) {__pyx_filename =
__pyx_f[0]; __pyx_lineno = 328; goto __pyx_L1;}
PyTuple_SET_ITEM(__pyx_3, 0, __pyx_1);
PyTuple_SET_ITEM(__pyx_3, 1, __pyx_2);
__pyx_1 = 0;
__pyx_2 = 0;
__pyx_1 = PyNumber_Remainder(__pyx_k315p, __pyx_3); if (unlikely(!__pyx_1))
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; goto __pyx_L1;}
Py_DECREF(__pyx_3); __pyx_3 = 0;

if (unlikely(!(__pyx_v_c_node->type == XML_ELEMENT_NODE))) {
  PyErr_SetObject(PyExc_AssertionError, __pyx_1);
  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; goto __pyx_L1;}
}

Py_DECREF(__pyx_1); __pyx_1 = 0;

#endif
--------------------------------------

(except that Pyrex doesn't have the "unlikely()" bits)

I know I can compile out the assertion completely, but still, the generated
code shouldn't build the second argument of the assert statement outside the
if clause, as it's the very nature of an assert statement that it will most
likely not be needed.

Stefan



More information about the Pyrex mailing list