-
Notifications
You must be signed in to change notification settings - Fork 370
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
New importer #1671
New importer #1671
Conversation
Add the necessary bits to provide a Python 3.4+ compliant import machinery. This means we now have our own PathFinder, which we register as in sys.meta_path, our own FileFinder, and our own Loader. Add a compatability later that builds on the Python 3 machinery to make it best-effort backwards compatible with Python 2.
We can't reply on hy_eval here as it doesn't properly associate the appropriate file information. Fixes hylang#1395
There are conflicts between certain combinations of `pytest` and `py` that cause recursion issues (see pytest-dev/pytest#2501 and pytest-dev/pytest#2485). The same errors were showing up in Travis builds when pip pulled the most recent versions of these two packages.
I don't think we're ready to drop Python 2 support, sorry. The Python folks are still supporting it till 1 Jan 2020. |
I should add that even if you want to drop Python 2, it's a big enough change that it should get its own PR. |
Agh, yeah; well, I'm in the process of reducing the import machinery to the bare essentials in 3.x, so, once I've figured out exactly what that should look like, I'll re-add 2.7 support. |
In the search for minimal integration with Python >= 3.4 import importlib
from hy.compiler import hy_compile
from hy.lex import tokenize
from hy.models import HyObject, HyExpression, HySymbol
importlib.machinery.SOURCE_SUFFIXES += ['.hy']
# Keep a copy of the original method
_source_to_code = importlib.machinery.SourceFileLoader.source_to_code
def hy_parse(source):
source = re.sub(r'\A#!.*', '', source)
return HyExpression([HySymbol("do")] + tokenize(source + "\n"))
# New, patched method that considers .hy files
def _hy_source_to_code(self, data, path, *, _optimize=-1):
if os.path.isfile(path) and path.rpartition('.')[-1] == 'hy':
data = hy_compile(hy_parse(data.decode("utf-8")), self.name)
return _source_to_code(self, data, path, _optimize=_optimize)
# Patch the standard file loader
importlib.machinery.SourceFileLoader.source_to_code = _hy_source_to_code If this approach works across the board, we could remove the custom copy of The current approach is a non-trivial work-around for |
Okay, that sounds like a decent thing to try. |
Hey, really happy to see this work continued!
Sorry I haven't had the time or energy to keep up my open source work. I'll try and give you a good code review tomorrow.
…On August 20, 2018 12:59:47 AM EDT, "Brandon T. Willard" ***@***.***> wrote:
Closed #1671.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#1671 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
This is an update (rebase to current master) of #1518, along with implemented
hyc
byte-compilation (copy ofpy_compile
), removed Python 2.x compatibility (Hy no longer supports it), andimporter
tests converted to the newimportlib
.(I couldn't find a good way to contribute changes to @vodik's existing PR; that's why this exists)
Also, a lot of utility code was removed in 5960c90, because it came directly from
importlib._bootstrap
and/orimportlib._bootstrap_external
and, since we were already importing fromimportlib._bootstrap
, it seemed just as reasonable to use the necessary functions from their original source; however, these modules are not public-facing. At the very least, we can have everything in working order by simply accounting for discrepancies (e.g.importlib._bootstrap_external
appearing after 3.5) across versions.