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

Generate more concise syntax and compiler errors #1687

Merged
merged 8 commits into from
Feb 7, 2019
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ New Features

Bug Fixes
------------------------------
* Cleaned up syntax and compiler errors
* Fixed issue with empty arguments in `defmain`.
* `require` now compiles to Python AST.
* Fixed circular `require`s.
Expand Down
6 changes: 0 additions & 6 deletions docs/language/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ Command Line Options
`--spy` only works on REPL mode.
.. versionadded:: 0.9.11

.. cmdoption:: --show-tracebacks

Print extended tracebacks for Hy exceptions.

.. versionadded:: 0.9.12

.. cmdoption:: --repl-output-fn

Format REPL output using specific function (e.g., hy.contrib.hy-repr.hy-repr)
Expand Down
10 changes: 10 additions & 0 deletions hy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
__version__ = 'unknown'


def _initialize_env_var(env_var, default_val):
import os, distutils.util
try:
res = bool(distutils.util.strtobool(
os.environ.get(env_var, str(default_val))))
except ValueError as e:
res = default_val
return res


from hy.models import HyExpression, HyInteger, HyKeyword, HyComplex, HyString, HyBytes, HySymbol, HyFloat, HyDict, HyList, HySet # NOQA


Expand Down
57 changes: 53 additions & 4 deletions hy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import __builtin__ as builtins
except ImportError:
import builtins # NOQA
import sys, keyword
import sys, keyword, textwrap

PY3 = sys.version_info[0] >= 3
PY35 = sys.version_info >= (3, 5)
Expand All @@ -22,11 +22,60 @@
long_type = int if PY3 else long # NOQA
string_types = str if PY3 else basestring # NOQA

#
# Inspired by the same-named `six` functions.
#
if PY3:
exec('def raise_empty(t, *args): raise t(*args) from None')
raise_src = textwrap.dedent('''
def raise_from(value, from_value):
raise value from from_value
''')

def reraise(exc_type, value, traceback=None):
try:
raise value.with_traceback(traceback)
finally:
traceback = None

code_obj_args = ['argcount', 'kwonlyargcount', 'nlocals', 'stacksize',
'flags', 'code', 'consts', 'names', 'varnames',
'filename', 'name', 'firstlineno', 'lnotab', 'freevars',
'cellvars']
else:
def raise_empty(t, *args):
raise t(*args)
def raise_from(value, from_value=None):
raise value

raise_src = textwrap.dedent('''
def reraise(exc_type, value, traceback=None):
try:
raise exc_type, value, traceback
finally:
traceback = None
''')

code_obj_args = ['argcount', 'nlocals', 'stacksize', 'flags', 'code',
'consts', 'names', 'varnames', 'filename', 'name',
'firstlineno', 'lnotab', 'freevars', 'cellvars']

raise_code = compile(raise_src, __file__, 'exec')
exec(raise_code)


def rename_function(func, new_name):
"""Creates a copy of a function and [re]sets the name at the code-object
level.
"""
c = func.__code__
new_code = type(c)(*[getattr(c, 'co_{}'.format(a))
if a != 'name' else str(new_name)
for a in code_obj_args])

_fn = type(func)(new_code, func.__globals__, str(new_name),
func.__defaults__, func.__closure__)
_fn.__dict__.update(func.__dict__)

return _fn


def isidentifier(x):
if x in ('True', 'False', 'None', 'print'):
Expand Down
Loading