Barry, that kind of circular import is actually fine in many (if not most)
cases. Modules are immediately created and importable, thenincrementally
populated. The problem arises when you try to do something with contents of
the module that have not been populated, usually manifesting in the
AttributeError above.

If you'd like to test this yourself, I've made a tiny demo with a little
bit of documentation:
https://gist.github.com/mahmoud/32fd056a3d4d1cd03a4e8aeff6b5ee70

Long story short, circular imports can be a code smell, but they're by no
means universally an error condition. :)

On Sun, Jun 18, 2017 at 11:21 AM, Barry Scott <ba...@barrys-emacs.org>
wrote:

>
> On 14 Jun 2017, at 07:33, Nick Coghlan <ncogh...@gmail.com> wrote:
>
> On 14 June 2017 at 13:02, Mahmoud Hashemi <mahm...@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
>
> 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
>
>
> 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?
>
> Barry
>
>
>
>
>
>
> Cheers,
> Nick.
>
> P.S. I also double checked that ImportError & AttributeError have
> compatible binary layouts, so dual inheritance from them works :)
>
> --
> Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to