-
Notifications
You must be signed in to change notification settings - Fork 373
Closed
Labels
Description
For Python packages with hy modules, users have to know whether the module is .py or .hy when using Python's module-as-script feature.
If package/module.py
:
python -m package.module
If package/module.hy
:
hy -m package.module
Can hy support python -m package.module
regardless of .py or .hy? In Python 3, it could.
$ mkdir foo
$ echo 'import hy' > foo/__init__.py
$ touch foo/bar.hy
$ cat > foo/bar.hy
(defmain [prog &rest args]
(print "Hello, world!"))
$ hy -m foo.bar
Hello, world!
$ python -m foo.bar
Traceback (most recent call last):
File ".../lib/python3.5/runpy.py", line 151, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name)
File ".../lib/python3.5/runpy.py", line 126, in _get_module_details
code = loader.get_code(mod_name)
AttributeError: 'MetaLoader' object has no attribute 'get_code'
Python 2.7 does not even try to load it:
$ python -m foo.bar
.../bin/python: No module named foo.bar
This patch against hy/importer.py
fixes it on Python 3 (whereas there's no change on Python 2):
--- a/hy/importer.py
+++ b/hy/importer.py
@@ -202,6 +202,10 @@ class MetaLoader(object):
sys.modules[fullname] = mod
return mod
+ def get_code(self, fullname):
+ _ast = import_file_to_ast(self.path, fullname)
+ return ast_compile(_ast, self.path, "exec")
+
$ python -m foo.bar
Hello, world!
I'm creating this issue in case others run into it. We can either close it if there's no interest in supporting this or fix it for Python 3.x, in which case I can create a pull request.