[Python-Dev] how to find the file path to an extension module at init time?
mark florisson
markflorisson88 at gmail.com
Mon Nov 14 12:55:10 CET 2011
On 14 November 2011 08:18, Stefan Behnel <stefan_ml at behnel.de> wrote:
> "Martin v. Löwis", 13.11.2011 21:46:
>>>
>>> I'm asking specifically because I'd like to properly implement __file__
>>> in Cython modules at module init time.
>>
>> Why do you need to implement __file__? Python will set it eventually to
>> its correct value, no?
>
> Well, yes, eventually. However, almost all real world usages are at module
> init time, not afterwards. So having CPython set it after running through
> the module global code doesn't help much.
>
Perhaps Cython could detect use of __file__ at module scope (if this
package context function is not available), and if it's used it tries
to use something akin to imp.find_module(__name__) to find the path to
the file and set __file__ manually. It should handle dots out of the
box and perhaps not rely on any __path__ attributes of packages (I
think not many people change __path__ or use pkgutil anyway).
Would this be feasible for python < 3.3?
>>> Another problem is that package local imports from __init__.py no longer
>>> work when it's compiled
>>
>> Does it actually work to have __init__ be an extension module?
>
> I'm just starting to try it, and the problems I found so far were __file__
> (in general), __path__ and relative imports (specifically).
>
>
>>> Any ideas how this could currently be achieved?
>>
>> Currently, for Cython? I don't think that can work.
>
> Hmm, it might work to put an empty module next to the 'real' extension and
> to import it to figure out the common directory of both. As long as it's
> still there after installation and the right one gets imported, that is. A
> relative import should help on versions that support it. Although that won't
> help in the __init__ case because a relative import will likely depend on
> __path__ being available first. Chicken and egg...
>
> Support in CPython would definitely help.
>
>
>>> Or could this become a new feature in the future?
>>
>> Certainly. An approach similar to _Py_PackageContext should be possible.
>
> Yes, and a "_Py_ModuleImportContext" would be rather trivial to do. Could
> that go into 3.3? What about 2.7? Could an exception be made there regarding
> new "features"? It's not likely to break anything, but it would help Cython.
>
> Stefan
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/markflorisson88%40gmail.com
>
More information about the Python-Dev
mailing list