[issue992389] attribute error after non-from import
Changes by Éric Araujo mer...@netwok.org: -- nosy: +eric.araujo ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Changes by Mark Lawrence breamore...@yahoo.co.uk: -- versions: +Python 3.2 -Python 2.7, Python 3.1 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Changes by Guido van Rossum gu...@python.org: -- nosy: -gvanrossum ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Adam Olsen rha...@gmail.com added the comment: The key distinction between this and a bad circular import is that this is lazy. You may list the import at the top of your module, but you never touch it until after you've finished importing yourself (and they feel the same about you.) An ugly fix could be done today for module imports by creating a proxy that triggers the import upon the first attribute access. A more general solution could be done with a lazyimport statement, triggered when the target module finishes importing; only problem there is the confusing error messages and other oddities if you reassign that name. -- nosy: +Rhamphoryncus ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Brett Cannon br...@python.org added the comment: I have done a lazy importer like you describe, Adam, and it does help solve this issue. And it does have the problem of import errors being triggered rather late and in an odd spot. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Adam Olsen rha...@gmail.com added the comment: It'd probably be sufficient if we raised NameError: lazy import 'foo' not yet complete. That should require a set of what names this module is lazy importing, which is checked in the failure paths of module attribute lookup and global/builtin lookup. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Nick Coghlan ncogh...@gmail.com added the comment: No argument from me that my suggestion is a mere glimmering of an idea, rather than a fully worked out definitely viable solution. It was just an angle of attack I hadn't seen suggested before, so I figured it was worth mentioning - the fact that a module is allowed to exist in sys.modules while only half constructed is the reason import a.b.c can work while from a.b import c or an explicit relative import will fail - the first approach gets a hit in sys.modules and succeeds, while the latter two approaches fail because the a.b package doesn't have a 'c' attribute yet. Figuring out a way to set the attribute in the parent package and then roll it back later if the import fails is still likely to be the more robust approach. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Changes by Brett Cannon br...@python.org: -- assignee: brett.cannon - ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Nick Coghlan ncogh...@gmail.com added the comment: This came up on python-dev again recently: http://mail.python.org/pipermail/python-dev/2009-March/087955.html -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Guido van Rossum gu...@python.org added the comment: Good sleuthing Nick! It's clearly the same bug that Fredrik found. I tried to test if using Brett' importlib has the same problem, but it can import neither p.a nor p.b, so that's not helpful as to sorting out the import semantics. I believe that at some point many of the details of importlib should be seen as the reference documentation for the darkest corners of import semantics. But it seems we aren't there yet. -- nosy: +gvanrossum ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Guido van Rossum gu...@python.org added the comment: Sorry, never mind about the importlib bug, that was my mistake. importlib actually behaves exactly the same way as the built-in import. I conclude that this is probably the best semantics of import that we can hope for in this corner case. I propose to close this as works as intended -- and perhaps document it somewhere. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Torsten Bronger bron...@physik.rwth-aachen.de added the comment: Maybe it's better to leave it open, waiting for someone to pick it up, even if this is some time in the future? In my opinion, this is suprising behaviour without an actual rationale, and a current implementation feature. I'd be a pitty for me to see it becoming an official part of the language. What bothers me most is that from . import moduleX doesn't work but import package.moduleX does work. So the circular import itself works without problems, however, not with a handy identifier. This is would be an odd asymmetry, I think. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Nick Coghlan ncogh...@gmail.com added the comment: I just had a thought: we may be able to eliminate this behaviour without mucking about in the package globals. What if the import semantics were adjusted so that, as a last gasp effort before bailing out with an ImportError, the import process checked sys.modules again with the full module name? Not a fully fleshed out idea at this point (and possibly symptomatic of not being fully awake yet), but I'll bring it up in the current python-dev thread anyway. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Guido van Rossum gu...@python.org added the comment: I'm sorely tempted to apply the Van Lindberg clause to the last two responses by Torsten and Nick. If there was an easy solution it wouldn't have been open for five years. If you don't believe me, post a fix. I'll even accept a fix for the importlib package, which should lower the bar quite a bit compared to a fix for import.c. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Changes by Daniel Diniz aja...@gmail.com: -- assignee: - brett.cannon nosy: +brett.cannon versions: +Python 2.7, Python 3.1 -Python 2.3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue992389 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Torsten Bronger [EMAIL PROTECTED] added the comment: I dare to make a follow-up although I have no idea at all about the internal processes in the Python interpreter. But I've experimented with circular imports a lot recently. Just two points: First, I think that circular imports don't necessarily exhibit a sub-opimal programming style. I had a large parser module which I just wanted to split in order to get handy file sizes. However, the parser parses human documents, and layout element A defined in module A may contain element B from module B and vice versa. In a language with declarations, you just include a big header file but in Python, you end up with circular imports. Or, you must stay with large files. So, while I think that this clean error message Nick suggests is a good provisional solution, it should not make the impression that the circular import is a flaw by itself. And secondly, the problem with modules that are not yet populated with objects is how circular imports have worked in Python anyway. You can easily cope with it by not referencing the imported module's objects in the top-level code of the importing module (but only in functions and methods). Tracker [EMAIL PROTECTED] http://bugs.python.org/issue992389 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Changes by Georg Brandl [EMAIL PROTECTED]: -- assignee: - ncoghlan priority: low - normal Tracker [EMAIL PROTECTED] http://bugs.python.org/issue992389 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Nick Coghlan [EMAIL PROTECTED] added the comment: This is actually a pretty tough problem - fixing it would involve some fairly subtle changes to the way imports from packages are handled. Given that I'm of the opinion that permitting circular imports in a code base is an extraordinarily bad coding practice (if I'm ever tempted to create a circular import, I consider it a sign that I need to separate out some of the common code into a utility module), I'm not personally going to be putting any effort into solving it (although I wouldn't necessarily oppose anyone else trying to fix it). However, I'll give a detailed description of the problem and a possible solution in case anyone else wants to tackle it (since I believe there's already a similar trick done for the sys.modules cache). At the moment, when resolving an import chain __import__ only sets the parent package's attribute for the submodule after the submodule import is complete. This is what Jim describes in his original post, and is the cause of the failure to resolve the name. Deferring the lookups solves the problem because it means the package attributes are checked only after the whole import chain is complete, instead of trying to get access to a half-executed module during the import itself. The most likely solution to the problem would be to change the attribute on the parent package to be handled in a fashion closer to the way the sys.modules cache is handled: set the attribute on the parent package *before* executing the module's code, and delete that attribute if a problem is encountered with the import. The consequence of this would be that circular imports would be permitted, although the module's imported in this fashion would be seen in a half constructed state. So instead of the import failing with an exception (which is what happens now), you would instead get a module which you can't actually use (since most its attributes won't actually be filled in yet, as the circular imports will normally occur near the top of the file). Attempts to use methods, attributes and functions from the module may or may not work depending on the order in which the original module does things. A clean failure indicating You have a circular import, get rid of it seems better to me than possible hard to diagnose bugs due to being able to retrieve things from a half-constructed module, but opinions obviously differ on that. However, I really don't see this changing without a PEP. -- assignee: ncoghlan - type: - feature request Tracker [EMAIL PROTECTED] http://bugs.python.org/issue992389 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue992389] attribute error after non-from import
Nick Coghlan [EMAIL PROTECTED] added the comment: I think the lowered priority got lost somewhere along the line. -- priority: normal - low Tracker [EMAIL PROTECTED] http://bugs.python.org/issue992389 ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com