[Python-Dev] how to find the file path to an extension module at init time? (original) (raw)
mark florisson markflorisson88 at gmail.com
Mon Nov 14 12:55:10 CET 2011
- Previous message: [Python-Dev] how to find the file path to an extension module at init time?
- Next message: [Python-Dev] how to find the file path to an extension module at init time?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 PyPackageContext should be possible. Yes, and a "PyModuleImportContext" 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
- Previous message: [Python-Dev] how to find the file path to an extension module at init time?
- Next message: [Python-Dev] how to find the file path to an extension module at init time?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]