deshaw / pyflyby

A set of productivity tools for Python
https://deshaw.github.io/pyflyby/
Other
348 stars 52 forks source link

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

Closed dshivashankar1994 closed 7 months ago

dshivashankar1994 commented 7 months ago

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 commented 7 months ago

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 https://github.com/deshaw/pyflyby/pull/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:

https://github.com/deshaw/pyflyby/blob/c7aa87d8e28cb1693657daea73a3f9ba0ddacccb/lib/python/pyflyby/_modules.py#L253

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 commented 7 months ago

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

dshivashankar1994 commented 7 months ago

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 https://github.com/deshaw/pyflyby/issues/280 could have helped us

dshivashankar1994 commented 7 months ago

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

Carreau commented 7 months ago

Ok, yes, I can reproduce on 3.11.

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

dshivashankar1994 commented 7 months ago

@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