From ffd85bcc3e13cca54e646618963d38095b1fc089 Mon Sep 17 00:00:00 2001 From: Bob Tolbert Date: Sun, 7 Dec 2014 11:02:48 -0700 Subject: [PATCH] Fixes a long-standing bug in import under Python 3.3 and later. Our MetaImporter was being inserted at the end of sys.meta_path. For Python prior to 3.3, this was fine since sys.meta_path was empty by default. As of the completion of PEP 302 in Py3.3 and later, there are several importers registered by default. One of these was trying (and failing) to import simple Hy modules, resulting in a failure to import anything inside __init__.hy. This change simply inserts the Hy-specific importer at the front of the list. This was noted in issue #620 (great catch @algernon) --- hy/importer.py | 4 ++-- tests/native_tests/core.hy | 7 ++++++- tests/resources/bin/__init__.hy | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hy/importer.py b/hy/importer.py index e588f1d..a81d27b 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -1,5 +1,5 @@ # Copyright (c) 2013 Paul Tagliamonte -# Copyright (c) 2013 Bob Tolbert +# Copyright (c) 2013, 2014 Bob Tolbert # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -209,5 +209,5 @@ class MetaImporter(object): return MetaLoader(path) -sys.meta_path.append(MetaImporter()) +sys.meta_path.insert(0, MetaImporter()) sys.path.insert(0, "") diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index dcdb59b..2e92545 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -1,5 +1,5 @@ ;; Copyright (c) 2013 Paul Tagliamonte -;; Copyright (c) 2013 Bob Tolbert +;; Copyright (c) 2013, 2014 Bob Tolbert ;; Permission is hereby granted, free of charge, to any person obtaining a ;; copy of this software and associated documentation files (the "Software"), @@ -597,3 +597,8 @@ (assert (not (keyword? ":foo"))) (assert (not (keyword? 1))) (assert (not (keyword? nil)))) + +(defn test-import-init-hy [] + "NATIVE: testing import of __init__.hy" + (import tests.resources.bin) + (assert (in "_null_fn_for_import_test" (dir tests.resources.bin)))) diff --git a/tests/resources/bin/__init__.hy b/tests/resources/bin/__init__.hy index e69de29..ddb8095 100644 --- a/tests/resources/bin/__init__.hy +++ b/tests/resources/bin/__init__.hy @@ -0,0 +1,2 @@ +(defn -null-fn-for-import-test [] + pass)