[Pyrex] Pyrex extended assert code is sub-optimal

Robert Bradshaw robertwb at math.washington.edu
Thu Nov 1 02:52:04 CET 2007


This will be easy to change, but I'm wondering if there is some  
reason you did it this way rather than following the Python spec.

- Robert


On Oct 31, 2007, at 5:08 AM, Stefan Behnel wrote:

> 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
>
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex




More information about the Pyrex mailing list