hylang/hy — pull request #680 by kirbyfan64 (2014-11-01 20:06:04+00:00)
Title: Fix Python 3 re-raising

If I have this module in `test.hy`:

```
(import [hy.importer [import-file-to-module]])

(import-file-to-module "abc" "abc.hy")
```

and this in `abc.hy`:

```
(require nonexistent_module)
```

the error message is (mostly) normal under Python 2.7:

```
Traceback (most recent call last):
File "/usr/local/bin/hy", line 9, in
load_entry_point('hy==0.10.1', 'console_scripts', 'hy')()
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/cmdline.py", line 305, in hy_main
sys.exit(cmdline_handler("hy", sys.argv))
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/cmdline.py", line 293, in cmdline_handler
return run_file(options.args[0])
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/cmdline.py", line 194, in run_file
import_file_to_module("__main__", filename)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/importer.py", line 74, in import_file_to_module
eval(ast_compile(_ast, fpath, "exec"), mod.__dict__)
File "test.hy", line 3, in
(import-file-to-module "abc" "abc.hy")
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/importer.py", line 71, in import_file_to_module
_ast = import_file_to_ast(fpath, module_name)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/importer.py", line 63, in import_file_to_ast
return hy_compile(import_file_to_hst(fpath), module_name)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 2208, in hy_compile
result = compiler.compile(tree)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 420, in compile
ret = self.compile_atom(_type, tree)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 564, in compile_raw_list
ret = self._compile_branch(entries)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 451, in _compile_branch
return _branch(self.compile(expr) for expr in exprs)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 305, in _branch
results = list(results)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 451, in
return _branch(self.compile(expr) for expr in exprs)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 432, in compile
raise HyCompileError(e, sys.exc_info()[2])
hy.errors.HyCompileError: Internal Compiler Bug 😱
⤷ ImportError: No module named nonexistent_module
Compilation traceback:
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 420, in compile
ret = self.compile_atom(_type, tree)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 1737, in compile_expression
ret = self.compile_atom(fn, expression)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/usr/local/lib/python2.7/dist-packages/hy-0.10.1-py2.7.egg/hy/compiler.py", line 1553, in compile_require
__import__(entry) # Import it fo' them macros.
```

Python 3's is a tad disheartening, though:

```
Traceback (most recent call last):
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 420, in compile
ret = self.compile_atom(_type, tree)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 1737, in compile_expression
ret = self.compile_atom(fn, expression)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 1553, in compile_require
__import__(entry) # Import it fo' them macros.
ImportError: No module named 'nonexistent_module'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ryan/stuff/anaconda/bin/hy", line 9, in
load_entry_point('hy==0.10.1', 'console_scripts', 'hy')()
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/cmdline.py", line 305, in hy_main
sys.exit(cmdline_handler("hy", sys.argv))
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/cmdline.py", line 293, in cmdline_handler
return run_file(options.args[0])
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/cmdline.py", line 194, in run_file
import_file_to_module("__main__", filename)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/importer.py", line 74, in import_file_to_module
eval(ast_compile(_ast, fpath, "exec"), mod.__dict__)
File "test.hy", line 3, in
(import-file-to-module "abc" "abc.hy")
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/importer.py", line 71, in import_file_to_module
_ast = import_file_to_ast(fpath, module_name)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/importer.py", line 63, in import_file_to_ast
return hy_compile(import_file_to_hst(fpath), module_name)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 2208, in hy_compile
result = compiler.compile(tree)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 420, in compile
ret = self.compile_atom(_type, tree)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 564, in compile_raw_list
ret = self._compile_branch(entries)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 451, in _compile_branch
return _branch(self.compile(expr) for expr in exprs)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 305, in _branch
results = list(results)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 451, in
return _branch(self.compile(expr) for expr in exprs)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 432, in compile
raise HyCompileError(e, sys.exc_info()[2])
hy.errors.HyCompileError: Internal Compiler Bug 😱
⤷ ImportError: No module named 'nonexistent_module'
Compilation traceback:
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 420, in compile
ret = self.compile_atom(_type, tree)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 1737, in compile_expression
ret = self.compile_atom(fn, expression)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 412, in compile_atom
ret = _compile_table[atom_type](self, atom)
File "/home/ryan/stuff/anaconda/lib/python3.3/site-packages/hy-0.10.1-py3.3.egg/hy/compiler.py", line 1553, in compile_require
__import__(entry) # Import it fo' them macros.
```

Notice the "During the above traceback..." part. That probably isn't supposed to be there. This fixes that by adding a raise_empty function to hy/_compat.py for raising an exception and using that in hy/compiler.py. I could have just put the code directly in compiler.py, but I have a distinct feeling that this isn't the only place this is going to be used...

All the tests seem to pass under Python 2.7 and Python 3.3.