Commit Graph

76 Commits

Author SHA1 Message Date
Kodi Arfer 80eb27906a Update copyright years 2020-01-09 14:05:12 -05:00
Ryan Gonzalez 6f3b6ca735 Pre-Python 3.4 cleanup (including Python 2) 2019-07-22 14:11:17 -04:00
Kodi Arfer 67def3359f Remove Python 2 support from hy.importer 2019-06-04 14:01:59 -04:00
Andrew R. M da823d2cad Fix a temporary-file crash 2019-04-06 15:43:10 -04:00
Brandon T. Willard 51c7efe6e8 Retain compiled source and file information for exceptions
This commit refactors the exception/error classes and their handling.
It also retains Hy source strings and their originating file information, when
available, all throughout the core parser and compiler functions.

As well, with these changes, calling code is no longer responsible for providing
source and file details to exceptions,

Closes hylang/hy#657.
2019-02-07 13:43:58 -05:00
Kodi Arfer 62638b44a3 Update copyright years 2019-02-07 08:57:35 -05:00
Brandon T. Willard 86fda31ab1 Move compilation and parsing functions out of `importer.py`
Functions and variables relating to compilation and parsing have been moved to
`compiler.py` and `lex/__init__.py`, respectively.  Those functions are
  - `hy_parse` from `hy.importer` to `hy.lex`
  - `hy_eval`, `ast_compile`, and `calling_module` from `hy.importer` to
  `hy.compiler`

Closes hylang/hy#1695.
2018-11-28 14:12:33 -05:00
Brandon T. Willard 144a7fa240 Produce Python AST for `require` statements and skip self `require`s
Closes hylang/hy#1211.
2018-11-08 22:57:17 -06:00
Brandon T. Willard a9763b34cf Fix `sys.modules` for failed imports in Python 2.7
Newly imported modules with compile and/or run-time errors were not being
removed from `sys.modules`.  This commit modifies the Python 2.7 loader so that
it follows Python's failed-initial-import logic and removes the module from
`sys.modules`.
2018-09-29 20:57:28 -05:00
Brandon T. Willard c0c5c9c699 Make cmdline Hy process unknown filetypes as Hy source
This change a Hy-preferring `runhy` that is used by cmdline Hy.  Standard
`runpy` is still patched so that it can run `.hy` files, but the default
behaviour for unknown filetypes is preserved (i.e. assume they are Python
source).

Closes hylang/hy#1677.
2018-09-24 16:27:50 -04:00
Brandon T. Willard 5d325a5156 Add a test for module docstrings 2018-08-27 01:02:29 -05:00
Brandon T. Willard 2ea1e8e017 Make Hy a Python-source module type 2018-08-26 13:20:49 -05:00
Brandon T. Willard 87a5b117a1 Implement new importer using PEP-302 semantics
Python 3.x is patched in a way that integrates `.hy` source files into
Pythons default `importlib` machinery.  In Python 2.7, a PEP-302 "importer"
and "loader" is implemented according to the standard `import` logic (via
`pkgutil` and later pure-Python `imp` package code).

In both cases, the entry-point for the loaders is through `sys.path_hooks` only.
As well, the import semantics have been updated all throughout to utilize
`importlib` and follow aspects of PEP-420.  This, along with some light
patches, should allow for basic use of `runpy`, `py_compile` and `reload`.

In all cases, if a `.hy` file is shadowed by a `.py`, Hy will silently use
`.hy`.
2018-08-25 22:50:38 -05:00
Kodi Arfer 9c6714c176 Remove unused imports 2018-06-30 11:04:11 -07:00
Kodi Arfer 3204a9e8a3 Streamline auto-promotion and position spoofing
Auto-promotion now occurs in only two cases: when we start the compiler and when we expand a macro. It's fully recursive so even a non-model nested in a model will be promoted.

This change fixes some regressions induced by the stricter type checks of the pattern-matching compiler.
2018-06-05 09:23:21 -07:00
Simon Gomizelj 97cb19a8a2 Fix bytecode loading on Python 3.7 2018-03-24 12:34:43 -07:00
Simon Gomizelj ef2f435647 Add a hy.introspect module to deal with getargspec deprecation 2018-02-14 16:05:31 -05:00
Simon Gomizelj a2c8f50b30 Implement changes to support PEP 552 2018-02-09 16:43:25 -05:00
Kodi Arfer 6de7ddfee5 Update copyright years 2018-01-01 10:38:33 -05:00
gilch 20516cce79 make native tests run on Windows too 2017-09-05 13:35:28 -06:00
Kodi Arfer 0fc96306bc Drop support for Python 3.3 2017-08-26 13:36:51 -07:00
Kodi Arfer 28ce83524b Don't try to lex tag-macro calls as shebangs 2017-07-23 11:43:19 -07:00
Kodi Arfer 4be37b358b Make lists no longer magical for the compiler 2017-07-03 17:01:12 -07:00
Kodi Arfer f55fcf43bd Add a docstring for hy-eval
It's just a copy of the documentation for hy.core.langauge.eval.
2017-07-03 17:01:12 -07:00
Kodi Arfer ca95194240 Change `eval` from a special form to a function 2017-06-27 08:14:29 -06:00
Kodi Arfer f2278cf2f0 Support PYTHONDONTWRITEBYTECODE 2017-06-26 19:00:08 -06:00
Kodi Arfer 2eb81864df Make all files comply with license-header policy 2017-04-27 14:16:57 -07:00
Kodi Arfer d3fa375052 Migrate from Nose to pytest 2017-04-26 14:00:11 -07:00
Kodi Arfer ec0902b649 Set __file__ of bytecode imports 2017-04-25 09:30:13 -07:00
Kodi Arfer a27d737e1c Drop support for Pythons 3 older than 3.3 2017-04-24 14:22:13 -07:00
Kodi Arfer a87b23b4e8 Check the magic number of bytecode files 2017-04-14 13:38:38 -07:00
Kodi Arfer 36324e9499 Work around a Nose import bug on PyPy 2017-04-14 13:38:38 -07:00
Kodi Arfer 2b11b9be20 Automatically read and write bytecode
Importing or executing a Hy file now loads the byte-compiled version if it exists and is up to date, and if not, the source is byte-compiled after it's parsed.

This change can speed up Hy a lot. Here are some examples comparing run times of the current master (491b474e) to this commit, on my laptop with Python 3.6:

- `nosetests --exclude='test_bin'` goes from 3.8 s to 0.7 s (a 5-fold speedup)
- `hy -c '(print "hello world")` goes from 0.47 s to 0.20 s (a 2-fold speedup)
- Rogue TV's startup goes from 3.6 s to 0.4 s (a 9-fold speedup)

Accompanying changes include:

- `setup.py` now creates and installs bytecode for `hy.core`, `hy.contrib`, and `hy.extra`.
- The `hyc` command under Python 3 now creates bytecode in `__pycache__`, as usual for Python 3, instead of putting the `.pyc` right next to the source file like Python 2 does.

I've removed a test of `hy.extra.anaphoric.a-if` that triggers #1268 when the test file is byte-compiled and then hits some weird `macroexpand` bug or something when I try to work around that—Nose crashes when trying to produce an error message, and I can't seem to replicate the bug without Nose.
2017-04-14 13:38:33 -07:00
Kodi Arfer b71962bbaa Remove unused internal fn import_file_to_globals 2017-04-14 11:15:58 -07:00
Kodi Arfer 33a696d487 Add a command-line option --repl-output-fn (especially for hy.contrib.hy-repr) 2017-03-24 09:03:12 -07:00
Kodi Arfer 397fa80380 Revert "Modernized." (#1085) (#1188)
This reverts commit 13b484ce46 and fixes #1183.
2016-12-27 09:09:58 -06:00
Elf M. Sternberg 13b484ce46 Modernized. 2016-12-20 10:54:11 +02:00
Vinh Tu 6dc12dd703 Add import_file_to_globals for use in emacs inferior lisp mode 2016-07-01 23:27:36 +01:00
Jakub Wilk ff4ba5b103 Fix typos 2016-07-01 16:44:12 +02:00
Gregor Best 856212c9d7 Tidier string type check for eval 2015-08-13 09:59:58 +02:00
Gregor Best 44416fc276 Typecheck `eval` arguments during execution instead of compilation
When checking types during compilation, things like

    (eval '(print 1) (. foo mod))

fail, even if `(. foo mod)` is a dictionary.
2015-08-12 22:01:19 +02:00
Morten Linderud 4ee308c5f2 Merge pull request #761 from larme/fix-replace-hyobject
Add 'replace_hy_obj' to safely replace Hy objects
2015-07-23 14:09:56 +02:00
Zack M. Davis 4bea6dc59e distinguish IOError when loading file from that raised by program itself
Previously, Hy scripts that raised a subclass of IOError would be caught
by code intended to deal with IOErrors raised when Hy couldn't import
the script itself, resulting in either a misleading "Can't open file"
error message, or a misleading TypeError traceback from not being able
to format the "Can't open file" message (for IOErrors for which the
errno attribute was None). This commit (a straightforward implementation
of the idea proposed by @slimetree in #714) introduces a new HyIOError
class, raises that when `import_file_to_hst` can't open the file path,
and catches it in the `cmdline_handler`.

This is believed to fix #513, #714, and #727.
2015-02-16 14:27:18 -08:00
Zhao Shenyang 2375392962 Add 'replace_hy_obj' to safely replace Hy objects
Currently '.replace' method is used to replace hy objects. This is not
safe when we are not sure if the 'obj' in 'obj.replace(other)' is an
instance of HyObject.

In these cases, we can use function 'replace_hy_obj(obj, other)'
instead. This function will try to wrap 'obj' if it's not an instance of
HyObject.

This also means that we need a wrapping function in hy.models'. Hence I
moved the '_wrap_value' function from hy.macros into hy.models. To avoid
circular importing, the wrapper functions are provided individually by
each model type's own file.
2015-01-24 08:16:38 +08:00
Bob Tolbert ffd85bcc3e 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)
2014-12-07 11:02:48 -07:00
Paul Tagliamonte 575388fc13 Merge branch 'master' into pr/397
Conflicts:
	hy/importer.py
2014-01-01 16:40:16 -05:00
Bob Tolbert 5040c29946 Cleaning up some comments from berker 2013-12-31 09:56:03 -07:00
Bob Tolbert f6160c755a Much better version of new error messages.
This version is much simpler.

At the point that the exception is raised, we don't have access to
the actual source, just the current expression. but as the
exception percolates up, we can intercept it, add the source and
the re-raise it.

Then at the final point, in the cmdline handler, we can choose to
let the entire traceback print, or just the simpler, direct error
message.

And even with the full traceback, the last bit is nicely formatted
just like the shorter, simpler message.

The error message is colored if clint is installed, but to avoid
yet another dependency, you get monochrome without clint.

I'm sure there is a better way to do the markup, the current method
is kludgy but works.

I wish there was more shared code between HyTypeError and LexException
but they are kind of different in some fundamental ways.

This doesn't work (yet) with runtime errors generated from Python,
like NameError, but I have a method that can catch NameError and turn it
into a more pleasing output.

Finally, there is no obvious way to raise HyTypeError from pure Hy code,
so methods in core/language.hy throw ugly TypeError/ValueError.
2013-12-31 09:56:03 -07:00
Berker Peksag 8120a25c08 Add py_compile.{MAGIC, wr_long} to hy._compat.
Closes #344.
2013-12-29 13:10:36 +02:00
Berker Peksag f21ddeeded Add hy._compat module.
There was a couple of duplicate imports and type checkings in the
codebase. So I added a new module to unify all Python 2 and 3
compatibility codes.

Also, this is a somewhat common pattern in Python. See Jinja2 for
example:

https://github.com/mitsuhiko/jinja2/blob/master/jinja2/_compat.py
2013-09-29 12:10:08 +03:00