Skip to content
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

Support for python -m package.module with package/module.hy. #1018

Closed
rduplain opened this issue Jan 26, 2016 · 2 comments
Closed

Support for python -m package.module with package/module.hy. #1018

rduplain opened this issue Jan 26, 2016 · 2 comments
Labels

Comments

@rduplain
Copy link

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.

@algernon
Copy link
Member

+1 for fixing it for Python3. Going forward, py3 is the way to go, lets make things easier there!

(This'd also allow me to sneak Hy into more places without anyone noticing...)

@Kodiologist
Copy link
Member

This actually can't be made to work in general. The strategy described above, where you have an __init__.py or some other Python code (even an empty Python file) as an entry point, now works, and is probably the smartest thing to do. But in the general case of pure Hy code, none of which has been byte-compiled yet, python3 -m foo.bar gives us no chance to run our own code to convince the importer to import Hy files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants