[Python-ideas] ImportError raised for a circular import

Barry Scott barry at barrys-emacs.org
Sun Jun 18 14:42:25 EDT 2017


> On 18 Jun 2017, at 19:21, Barry Scott <barry at barrys-emacs.org> wrote:
> 
>> 
>> On 14 Jun 2017, at 07:33, Nick Coghlan <ncoghlan at gmail.com <mailto:ncoghlan at gmail.com>> wrote:
>> 
>> On 14 June 2017 at 13:02, Mahmoud Hashemi <mahmoud at hatnote.com <mailto:mahmoud at hatnote.com>> wrote:
>>> That would be amazing! If there's anything I can do to help make that
>>> happen, please let me know. It'll almost certainly save that much time for
>>> me alone down the line, anyway :)
>> 
>> The `IMPORT_FROM` opcode's error handling would probably be the best
>> place to start poking around:
>> https://github.com/python/cpython/blob/master/Python/ceval.c#L5055 <https://github.com/python/cpython/blob/master/Python/ceval.c#L5055>
>> 
>> If you can prove the concept there, that would:
>> 
>> 1. Directly handle the "from x import y" and "import x.y as name" cases
>> 2. Provide a starting point for factoring out a "report missing module
>> attribute" helper that could be shared with ModuleType
>> 
>> As an example of querying _frozen_importlib state from C code, I'd
>> point to https://github.com/python/cpython/blob/master/Python/import.c#L478 <https://github.com/python/cpython/blob/master/Python/import.c#L478>
> 
> I had thought that the solution would be in the .py implementation of the import
> machinery not in the core C code.
> 
> I was going to simply keep track of the names of the modules that are being imported
> and raise an exception if an import attempted to import a module that had not completed
> being imported. It seems from a quick loom at the code that this would be practical.
> 
> Are you saying that there is a subtle point about import and detection of cycles that
> means the work must be done in C?

It seemed that PyImport_ImportModuleLevelObject() always calls out the
interp->importlib. For example:

                value = _PyObject_CallMethodIdObjArgs(interp->importlib,
                                                &PyId__lock_unlock_module, abs_name,
                                                NULL);

Where interp->importlib is the frozen importlib.py code I thought.

I'd assumed that I would need to change the importlib.py code and
build that as the frozen version to implement this.

Barry


> 
> Barry
> 
> 
> 
> 
> 
>> 
>> Cheers,
>> Nick.
>> 
>> P.S. I also double checked that ImportError & AttributeError have
>> compatible binary layouts, so dual inheritance from them works :)
>> 
>> -- 
>> Nick Coghlan   |   ncoghlan at gmail.com <mailto:ncoghlan at gmail.com>   |   Brisbane, Australia
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org <mailto:Python-ideas at python.org>
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>> 
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org <mailto:Python-ideas at python.org>
> https://mail.python.org/mailman/listinfo/python-ideas <https://mail.python.org/mailman/listinfo/python-ideas>
> Code of Conduct: http://python.org/psf/codeofconduct/ <http://python.org/psf/codeofconduct/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170618/4f79e186/attachment.html>


More information about the Python-ideas mailing list