[issue13591] import_module potentially imports a module twice
Brett Cannon br...@python.org added the comment: Patch looks good to me. -- assignee: - meador.inge stage: patch review - commit review ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Roundup Robot devn...@psf.upfronthosting.co.za added the comment: New changeset d2504d30f259 by Meador Inge in branch '3.2': Issue #13591: import_module potentially imports a module twice. http://hg.python.org/cpython/rev/d2504d30f259 New changeset e8fb61a0a2d7 by Meador Inge in branch 'default': Issue #13591: import_module potentially imports a module twice. http://hg.python.org/cpython/rev/e8fb61a0a2d7 -- nosy: +python-dev ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Meador Inge mead...@gmail.com added the comment: Thanks for the review Brett. Fix committed. -- resolution: - fixed stage: commit review - committed/rejected status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Roundup Robot devn...@psf.upfronthosting.co.za added the comment: New changeset 541f215a31f7 by Meador Inge in branch '3.2': Issue #13591: Moving the NEWS line to the right release. http://hg.python.org/cpython/rev/541f215a31f7 New changeset 92e94fd303d4 by Meador Inge in branch 'default': Issue #13591: Moving the NEWS line to the right release. http://hg.python.org/cpython/rev/92e94fd303d4 -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Meador Inge mead...@gmail.com added the comment: I can reproduce this on tip. What happens is that 'importlib.import_module(my_lib.bar)' is effectively computed as: import my_lib import bar by '_bootstrap._gcd_import'. When '_gcd_import' goes to do the import of 'bar' it does *not* check to see if 'bar' has already been imported by the parent import. Here is a patch *without* tests that fixes this. I will add the tests next. -- keywords: +patch stage: - patch review Added file: http://bugs.python.org/file23943/issue13591.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Changes by Antoine Pitrou pit...@free.fr: -- versions: +Python 3.3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Ryan Twitchell metatheo...@gmail.com added the comment: Confirmed that this patch fixes the behavior shown in my original example, with 3.2. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Meador Inge mead...@gmail.com added the comment: Updated patch with tests. -- Added file: http://bugs.python.org/file23951/issue13591-2.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
New submission from Ryan Twitchell metatheo...@gmail.com: Use of importlib's import_module function with modules belonging to a library can cause some modules to be imported twice, if such a module is referenced from sibling modules, and from __init__ in the package. I suspect this is a bug, or at best a nuance of packages that is quite subtle. Easier to show with an example. Below, the module my_lib.bar will be imported twice. Given the following file structure: ./scratch.py ./my_lib/__init__.py ./my_lib/foo.py ./my_lib/bar.py And the following file contents: ./scratch.py: import importlib importlib.import_module('my_lib.bar') importlib.import_module('my_lib.foo') ./my_lib/__init__.py: import my_lib.bar # Or alternately #from . import bar ./my_lib/foo.py: from . import bar print('In foo, id(bar.baz): %s' % id(bar.baz)) ./my_lib/bar.py: def baz(): pass print('In bar, id(bar.baz): %s' % id(baz)) Running scratch.py results in my_lib.bar being imported twice: $ echo $PYTHONPATH . $ python --version Python 3.2.2 $ python ./scratch.py In bar, id(bar.baz): 21328632 In bar, id(bar.baz): 21352240 In foo, id(bar.baz): 21352240 Replacing the calls to import_module with use of the import statement, or __import__, or simply rearranging the order of the two calls all result in the module my_lib.bar to be imported only once. As does eliminating the import statement in my_lib.__init__. This may be a misunderstanding on my part regarding the intended use of packages, but this behavior was quite unexpected, and rather difficult to track down in real code. -- components: Library (Lib) messages: 149358 nosy: Ryan.Twitchell priority: normal severity: normal status: open title: import_module potentially imports a module twice type: behavior versions: Python 3.2 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Changes by Ezio Melotti ezio.melo...@gmail.com: -- nosy: +brett.cannon, ncoghlan ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Changes by Meador Inge mead...@gmail.com: -- nosy: +meador.inge ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13591] import_module potentially imports a module twice
Nick Coghlan ncogh...@gmail.com added the comment: At first glance, I thought this might be just the circular import problem (#992389) appearing in a different guise. However, if that was the case, switching to an import statement or __import__ shouldn't have made any difference. What do you see if you put an import my_lib.bar *between* the two importlib calls? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue13591 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com