Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace_star_imports broken in latest version (PyInf#11805) #281

Closed
dshivashankar1994 opened this issue Jan 22, 2024 · 6 comments
Closed
Assignees

Comments

@dshivashankar1994
Copy link
Collaborator

replace_star_imports is broken in latest version 1.8.8

from pyflyby._imports2s import fix_unused_and_missing_imports, replace_star_imports
code = """
... from os import *
...
... getcwd()
... """
replace_star_imports(code)
[PYFLYBY] None: couldn't import 'os' to enumerate exports, leaving unchanged: 'from os import *'. WrappedAttributeError: type object 'FrozenImporter' has no attribute 'get_filename'
PythonBlock('\nfrom os import *\n\ngetcwd()\n')

Can this be fixed on priority ? (cc: @quarl )

Carreau added a commit to Carreau/pyflyby that referenced this issue Jan 23, 2024
@Carreau
Copy link
Collaborator

Carreau commented Jan 23, 2024

I cannot reproduce, did you by any chance update something else, or use it in different environment where some things might have been cached ?

I added a test in #282, let see if it fails only on some platforms.

I also dug into CPython history, the really strange things is that 'get_filename' has been removed from FrozenImporter in Python 2.5, so quite a while ago,

Has not been touched in quite a while:

filename = loader.get_filename()

And when stepping through the code, seem to access SourceLoader instead of FrozenImporter, and it is likely you have a custom hook or environement.

Carreau added a commit to Carreau/pyflyby that referenced this issue Jan 23, 2024
I don't understand why the bug is happening, and why this get a
FrozenImporter as it was never getting one before, but this should fix
the bug.
@Carreau
Copy link
Collaborator

Carreau commented Jan 23, 2024

I'm guessing #283 will prevent the crash, but I'd still like to understand the cause to have a proper fix.

Carreau added a commit to Carreau/pyflyby that referenced this issue Jan 23, 2024
I don't understand why the bug is happening, and why this get a
FrozenImporter as it was never getting one before, but this should fix
the bug.
@dshivashankar1994
Copy link
Collaborator Author

I cannot reproduce, did you by any chance update something else, or use it in different environment where some things might have been cached ?

Right. Missed to mention that the issue happens in py3.11. Probably #280 could have helped us

@dshivashankar1994
Copy link
Collaborator Author

Also, I tried quickly verifying the commit.
The issue still persists

Carreau added a commit to Carreau/pyflyby that referenced this issue Jan 23, 2024
It seem that in 3.11 there is now another FrozenImporter from
_frozen_importlib, I did not manage to get isinstance to work with it so
we need to check with hasattr.
@Carreau
Copy link
Collaborator

Carreau commented Jan 23, 2024

Ok, yes, I can reproduce on 3.11.

5ec2d56 seem to be the first failing commit. I'll do a different fix.

Carreau added a commit to Carreau/pyflyby that referenced this issue Jan 23, 2024
It seem that in 3.11 there is now another FrozenImporter from
_frozen_importlib, I did not manage to get isinstance to work with it so
we need to check with hasattr.
Carreau added a commit that referenced this issue Jan 23, 2024
@dshivashankar1994
Copy link
Collaborator Author

@Carreau The issue still persists in the latest version as well

In [2]: code = """
   ...: 
   ...: from os import *
   ...: getcwd()
   ...: """

In [3]: replace_star_imports(code)
[PYFLYBY] None: couldn't import 'os' to enumerate exports, leaving unchanged: 'from os import *'.  WrappedAttributeError: type object 'FrozenImporter' has no attribute 'get_filename'
Out[3]: PythonBlock('\n\nfrom os import *\ngetcwd()\n')

Can you please check ? Its in python 3.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants