grumpyhome / grumpy

Grumpy is a Python to Go source code transcompiler and runtime.
Apache License 2.0
420 stars 18 forks source link

Fix ImportError on conditional `from .. import *` statements #118

Closed alanjds closed 6 years ago

alanjds commented 6 years ago

This fixes some ImportError issues when importing * from a broken/absent module, as:

try:
    from _codecs import *
except ImportError:
    print 'Its ok. We do not need the _codecs native module'

I could not find a clean way to write the test, as imputil_test.py structure had not "clicked" on my mind yet. Help on this test would be appreciated.

alanjds commented 6 years ago

Can I merge this?

My main concern is about the a test that I could not understand how to change to the new behaviors. Then I got it disabled.

alanjds commented 6 years ago

I don't fully understand whats happening...

The transpiler originally raised util.ImportError on compile time, but this was wrong. Ideally, every import should allow_error=True and return an empty Import object, letting a __builtin__.ImportError to raise on run time and maybe be handled by a try..except.

However, internally the transpiler goes falling back several times in case of util.ImportError occurrences. From this came the need of only the last cases to allow_error=True.

And it (transpiler) originally had no support for import * or from ?? import *, and this two are different cases internally.

alanjds commented 6 years ago

I should start to fill the docstrings of internal APIs, or even writing another article to explain it. It is becoming a series :|