- Expose `read`, `read_str`, and `eval` in Python
- Add string evaluation example to interop section of docs
- Add test for `eval`
- Explain `eof` keyword argument in `read` docstring
The implementation of `hy.core.language.exec` draws code from the `exec_` function in commit f574c7be6ebc80041ef58ca29588f310248ebed4 of the library Six, which is copyright 2010–2017 Benjamin Peterson and licensed under the Expat license.
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.
I've added shadow versions of many operators that didn't have one. And, I've changed the behavior of various binary operators with more or fewer than 2 arguments to make the shadow and real versions more consistent and to make the behavior more logical in either case. For details, see the additions to NEWS and the new file tests/native_tests/operators.hy, which simultaneously tests shadow and real operators.
Although there are a lot of changes, I've put them all in one commit because they're interdependent.
This is no longer necessary now that `defn` always produces a `FunctionDef`.
To compensate, I've made small edits to two contrib modules and reverted a small test change.
The bug was a regression that I introduced in #1228.
I've created a new special form named `fn*` that works like the old `fn` (that is, it always creates a `FunctionDef`). Since this is intended only for internal use, like `with*`, I haven't documented it.
* Remove uses of `car` and `cdr` in /hy
* Remove uses of `car` and `cdr` in quote tests
* Remove `car` and `cdr` in favor of `first` and `rest`
I beefed up the documentation and tests for `first` and `rest` while I was at it.
I defined `car` and `cdr` in native_tests.cons so the tests read a bit more naturally.
* with-decorator: Allow a `setv` form as the form to be decorated
This feature is of dubious value by itself, but it's necessary to allow `defn` to create a lambda instead of a `def`.
* Make `fn` work the same as `lambda`
That is, allow it to generate a `lambda` instead of a `def` statement if the function body is just an expression.
I've removed two uses of with_decorator in hy.compiler because they'd require adding another case to HyASTCompiler.compile_decorate_expression and they have no ultimate effect, anyway.
In a few tests, I've added a meaningless statement in `fn` bodies to force generation of a `def`.
I've removed `test_fn_compiler_empty_function` rather than rewrite it because it seems like a pain to maintain and not very useful.
* Remove `lambda`, now that `fn` does the same thing
You can use them as thousands separators.
This change differs from PEP 515 in that not only does it allow commas in addition to underscores, but it's much more liberal about placement. Any number of underscores or commas can be placed anywhere, even at the start.
Unlike Python, Hy allows the programmer to intermingle positional and keyword arguments. This change removes an exception to that rule for method calls, in which the method callee always had to be the first thing after the method. Thus, `(.split :sep "o" "foo")` now compiles to `"foo".split(sep="o")` instead of `HyKeyword("sep").split("o", "foo")`.
I don't see why you'd put this in the standard library. I guess it could be useful for when you're maintaining a library and you want to change the name of a function or macro but keep the old name around for a while so people's code doesn't break immediately. But that's a pretty limited purpose.
* Add comp, constantly and complement
relates #1176
* Fix composition order in comp
* comp without parameters returns identity
* Doc edits for comp, complement, constantly
* Test that `(comp)` returns `identity` exactly
* Simplify the `reduce` call in `comp`
* updated version of comp
I have some macros for using pandas and NumPy that expect : to be a keyword instead of an ordinary symbol. These tests will ensure that we don't break this unless we want to.
* added defmacro!
* revert #924#924 had an error and should never have been merged in the first place. (see #903)
* put back import getargspec
Without the `formatargspec` this time.
* Give better error message on failed macro expansion
Better error messages work most of the time. In cases where there are
parameters that aren't valid in Python, error message shown is rather
ugly. But this is better than no error messages at all and such
macros with strange parameter names are rather rare.
* fix flake8 errors
* Minor English improvements
Per the straw poll in #908, as an alternative to #1147.
Now you must use `True`, `False`, and `None`, as in Python. Or just assign `true` to `True`, etc.; the old synonyms aren't reserved words anymore.
In Python 2.x (range 10) is mapped to xrange(10) in Python
terms. However, xrange doesn't support slicing, which caused tests to
fail. By forxing xrange into list, we have slicing available.
Give `require` the same features as `import`
You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.
Implements #1118 and perhaps partly addresses #277.
N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.
There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.
* Make `require` syntax stricter and add tests
* Update documentation for `require`
* Documentation wording improvements
* Allow :as in `require` name lists
This allows them to be used with numeric types that aren't built in, such as NumPy arrays. Because Python uses duck typing, there's generally no way to know in advance whether a given value will accept a given operator. Of course, things like `(inc "hello")` will still raise a `TypeError`, because so does `(+ "hello" 1)`.
This allows macros to take a keyword dict containing useful things by
defining a keyword argument. This allows us to pass in new objects
which might be handy to have in macros.
This changeset refactors module_name to become `compiler`, so that we
can pass the compiler itself into the macros as `opts['compiler']`.
This allows the macro to both get the macro name
(`compiler.module_name`), as well as use the compiler to build AST.
In the future, this will enable us to create "super-macros" which return
AST, not HyAST, in order to manually create insane things from userland.
For userland macros (not `defmacro`) the core.language `macroexpand`
will go ahead and make a new compiler for you.
This makes it possible to use strings as the macro name argument to
defreader, which in turn makes it possible to define reader macros with
names that would otherwise result in parse errors.
Such as `#.`.
This fixes#918.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
The `if` form now supports elif clauses.
It works like `cond` but without the implicit `do`.
The old `if` is now `if*`
variadic lif now supports "ellif" clauses.
Update if-no-waste compiler to use `if*` properly.
(Sometimes one character is all it takes.)
document if
reword truthiness
Comparison operators such as =, !=, <, >, <=, >= should support a
one-arity version too, and return true in those cases (except for !=,
which returns false).
This closes#949.
Reported-by: Matthew Egan Odendahl
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This changes with syntax from (with [[x (expr)] (expr)] ...) to (with
[x (expr) (expr)] ...). Should have no ill side effects apart from the
syntax change.
Closes#852.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This changes let to use a flat list of symbol-value pairs instead of a
vector of vectors. One side effect is that (let [[a 1] z]) is not
expressible now, and one will explicitly need to set a nil value for z,
such as: (let [a 1 z nil]).
Closes#713.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
apply now mangles strings and keywords according to the Hy mangling
rules (by using the same function, now imported from
hy.lex.parser). With this change, if the dict passed to apply has
keywords, strings or quoted symbols, they'll get mangled, to turn them
into proper keys.
This only works for the cases where the keys are directly in the apply
params. A previously deffed dict, or key through a variable will not be
mangled.
This closes#219.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
To make it easier to write --init-- functions, defclass will now check
any (setv) expressions (and its property list), to find any --init--
declarations, and append a nil to the end.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
For easier macro writing purposes, allow an empty (cond), that simply
returns nil. Closes#904.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
To mirror the behaviour of (setv), allow an empty (del) too: one that
shall return nil. Closes#905.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
To allow classes to have methods that match built-in names, yet, still
disallow them outside of defclass, keep an internal state whether
builtins are allowed in the current context.
By default, this is false. But defclass will set it to True when it
compiles its body, and set it back to the previous value when it's done
with that. We need to set back to the previous value to allow nested
defclasses to work properly.
This closes#783.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
Strip the \ufdd0 prefix from the keyword argument before turning it into
a string: the same representation the user entered looks better, and is
printable too, thus Python2 doesn't choke on it.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
Some valid-looking list comprehensions, such as (genexpr x []) can crash
Python 2.7. The AST we generate from these cannot be expressed in
Python, but were valid in Hy.
Added two guards to guard against this, so we raise an error instead of
crashing Python.
Closes#572, #591 and #634.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
Postfixing functions with a bang, like set!, get!, etc are relatively
common. However, those names are not valid in python, so in the name of
python interoperability, lets mangle them to set_bang and get_bang,
respectively.
Closes#536.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
When trying to setv a callable, raise an error instead of showing the
user an incredibly ugly backtrace. Closes#532.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
In case for doesn't get a body, raise the appropriate, descriptive error
instead of an IndexOutOfBounds one. Also updated the failing test case.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
defclass now has a new syntax:
(defclass Name [BaseList]
[property value
property value] ;; optional
(defn method [self]
self.property))
Anything after the optional property list (which will be translated to a
setv within the class context) will be added to the class body. This
allows one to have side effects and complex expressions within the class
definition.
As a side effect, defining methods is much more friendly now!
Closes#850.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
xor with more than two input parameters is not well defined and people
have different expectations on how it should behave. Avoid confusion by
sticking with two parameters only.
Added xor to complement and, or, not operators. Standard python
falsey/truthy semantics are followed. This implementation works for
two or more parameters.
The `with-decorator` special form is not the most ergonomic—this commit
introduces a new builtin `#@` reader macro that expands to an invocation
of `with-decorator`. To support this, `reader_macroexpand` is made to
also look in the default `None` namespace, in imitation of how
regular (non-reader) macros defined in hy.core are looked up. The
docstring of `hy.macros.reader` is also edited slightly for accuracy.
This in the matter of issue #856.
Much like how `can_compile` returns the compilation result, which some
tests make use of, it may be useful for for `cant_compile` to return the
exception object that it caught, for more specific assertions.
Python 3 supports keyword-only arguments as described in the immortal
PEP 3102. This commit implements keyword-only argument support for Hy
using a `&kwonly` lambda-list-keyword with semantics analogous how
`&optional` arguments are handled: `&kwonly` arguments are either a
symbol, in which case the keyword argument so named is mandatory, or a
two-element list, the first of which is the symbolic name of the keyword
argument and the second of which is its default value if not
supplied. If Hy is running under Python 2, attempting to use `&kwonly`
args will raise a HyTypeError.
This effort is with the aim of resolving #453.
Expressions can sometimes contain itertools.islice objects, which we can
only walk if we force them into a list. To do this, the walk function
has to be taught that collections that are not instances of list should
be forced into a list.
Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>