Hi,

Robert Kern wrote:
> Hoyt Koepke wrote:
>>> Are you sure? I changed the line to this:
>>>
>>>   from numpy import random as rn

Hmm, I added this as tests/run/importas.pyx, and it works perfectly:

===========================
__doc__ = u"""
>>> import sys as sous
>>> import distutils.core as corey
>>> from copy import copy as copey

>>> sous is _sous
True
>>> corey is _corey
True
>>> copey is _copey
True

>>> _sous is not None
True
>>> _corey is not None
True
>>> _copey is not None
True

>>> print(_sous.__name__)
sys
>>> print(sous.__name__)
sys
>>> print(_corey.__name__)
distutils.core
>>> print(corey.__name__)
distutils.core
>>> print(_copey.__name__)
deepcopy
>>> print(copey.__name__)
deepcopy
"""

import sys as _sous
import distutils.core as _corey
from copy import copy as _copey
===========================

Is numpy.random a module or a module attribute?


>>> And it worked with Cython 0.10. Possibly the code that handles "import
>>> <foo>.<bar> as <baz>" is incorrect. It's a tricky bit of syntax.
>> You're right; this does work.  I think in trying a bunch of
>> combinations to figure it out, I must have missed recompiling
>> something and so missed this.  Sorry for the noise!
> 
> It's not noise. This does appear to be a bug.
> 
>    /* "/Users/rkern/today/geometry/geometry.pyx":4
>   *
>   *
>   * import numpy.random as rn             # <<<<<<<<<<<<<<
>   * from copy import copy
>   *
>   */
>    __pyx_1 = PyList_New(1); if (unlikely(!__pyx_1)) {__pyx_filename = 
> __pyx_f[0]; __pyx_lineno = 4; __pyx_c
> lineno = __LINE__; goto __pyx_L1_error;}
>    Py_INCREF(__pyx_kp_29);
>    PyList_SET_ITEM(__pyx_1, 0, __pyx_kp_29);
>    __pyx_2 = __Pyx_Import(__pyx_kp_28, ((PyObject *)__pyx_1)); if 
> (unlikely(!__pyx_2)) {__pyx_filename = __
> pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
>    Py_DECREF(((PyObject *)__pyx_1)); __pyx_1 = 0;
>    if (PyObject_SetAttr(__pyx_m, __pyx_kp_rn, __pyx_2) < 0) {__pyx_filename = 
> __pyx_f[0]; __pyx_lineno = 4;
>   __pyx_clineno = __LINE__; goto __pyx_L1_error;}
>    Py_DECREF(__pyx_2); __pyx_2 = 0;
> 
> 
> _pyx_kp_29 is never given a value in the generated code so the from_list is 
> invalid. I am guessing that the "as" is fooling the code generator into 
> thinking 
> that there is a from_list.

It's not a general bug in any case. The constant _kp_29 is assigned its value
from _k_29 in __Pyx_InitStrings() by walking __pyx_string_tab, so if _k_29 has
a value in the code and appears in the string table, _kp_29 will initialised
as well.


> If that gets fixed, there's more stuff that's broken. The module that gets 
> returned from __Pyx_Import() as __pyx_2 will be numpy, not numpy.random, so 
> there must be one or more PyObject_GetAttr()s to get the actual module that 
> should be assigned to the name "rn".

Hmmm, could you try to extend the test case above so that it shows the problem?

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to