Stefan Behnel wrote:
> 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?

It's a package. Add

   import distutils.command as _commie

and you will see the failure.

>>>> 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.

Ah, I see. I think the problem is that _kp_29 becomes a unicode string u'*'. 
The 
function that raises the exception explicitly checks for exactly str.

                 if (!PyString_Check(item)) { 

                         PyErr_SetString(PyExc_TypeError,
                                         "Item in ``from list'' not a string");

If I manually go into the importas.c source (after adding my failing line) and 
set is_unicode to 0 in the string_tab, then it can import.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

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

Reply via email to