From 7fe997e9f7a1049c63957ca3c9dd52f8f656c3be Mon Sep 17 00:00:00 2001 From: schuster-rainer Date: Sat, 1 Mar 2014 00:23:49 +0100 Subject: [PATCH 01/17] Added name and keyword functions to core --- hy/core/language.hy | 33 ++++++++++++++++++++++++++++++--- hy/models/keyword.py | 5 +++-- tests/native_tests/language.hy | 26 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 19e8486..21c7808 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -25,7 +25,8 @@ (import [hy._compat [long-type]]) ; long for python2, int for python3 -(import [hy.models.cons [HyCons]]) +(import [hy.models.cons [HyCons]] + [hy.models.keyword [HyKeyword KEYWORD_PREFIX]]) (defn _numeric-check [x] @@ -351,10 +352,36 @@ (import functools) (map (functools.partial (fn [f args] (apply f args)) func) (apply zip lists)))) +(defn hyify [text] + "Convert text to match hy identifier" + (.replace (string text) "_" "-")) + +(defn keyword [value] + "Create a keyword from the given value. Strings numbers and even objects + with the __name__ magic will work" + (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (hyify value) + (if (string? value) + (HyKeyword (+ ":" (hyify value))) + (try + (hyify (.__name__ value)) + (catch [] (HyKeyword (+ ":" (string value)))))))) + +(defn name [value] + "Convert the given value to a string. Keyword special character will be stripped. + String will be used as is. Even objects with the __name__ magic will work" + (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (hyify (slice value 2)) + (if (string? value) + (hyify value) + (try + (hyify (. value __name__)) + (catch [] (string value)))))) + (def *exports* '[calling-module-name coll? cons cons? cycle dec distinct disassemble drop drop-while empty? even? every? first filter flatten float? gensym identity inc instance? integer - integer? integer-char? iterable? iterate iterator? - list* macroexpand macroexpand-1 neg? nil? none? nth + integer? integer-char? iterable? iterate iterator? keyword + list* macroexpand macroexpand-1 name neg? nil? none? nth numeric? odd? pos? remove repeat repeatedly rest second some string string? take take-nth take-while zero? zipwith]) diff --git a/hy/models/keyword.py b/hy/models/keyword.py index 6d25633..a3d778b 100644 --- a/hy/models/keyword.py +++ b/hy/models/keyword.py @@ -22,6 +22,7 @@ from __future__ import unicode_literals from hy.models import HyObject from hy._compat import str_type +KEYWORD_PREFIX = "\uFDD0" class HyKeyword(HyObject, str_type): """Generic Hy Keyword object. It's either a ``str`` or a ``unicode``, @@ -29,8 +30,8 @@ class HyKeyword(HyObject, str_type): """ def __new__(cls, value): - if not value.startswith("\uFDD0"): - value = "\uFDD0" + value + if not value.startswith(KEYWORD_PREFIX): + value = KEYWORD_PREFIX + value obj = str_type.__new__(cls, value) return obj diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 5e6f6c7..dc91da0 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -988,3 +988,29 @@ "NATIVE: test keyword quoting magic" (assert (= :foo "\ufdd0:foo")) (assert (= `:foo "\ufdd0:foo"))) + +(defn test-keyword-creation [] + "NATIVE: Test keyword creation" + (assert (= (keyword "foo") :foo)) + (assert (= (keyword "foo_bar") :foo-bar)) + (assert (= (keyword `foo) :foo)) + (assert (= (keyword `foo-bar) :foo-bar)) + (assert (= (keyword 'foo) :foo)) + (assert (= (keyword 'foo-bar) :foo-bar)) + (assert (= (keyword 1) :1)) + (assert (= (keyword 1.0) :1.0)) + (assert (= (keyword :foo_bar) :foo-bar))) + +(defn test-name-conversion [] + "NATIVE: Test name conversion" + (assert (= (name "foo") "foo")) + (assert (= (name "foo_bar") "foo-bar")) + (assert (= (name `foo) "foo")) + (assert (= (name `foo_bar) "foo-bar")) + (assert (= (name 'foo) "foo")) + (assert (= (name 'foo_bar) "foo-bar")) + (assert (= (name 1) "1")) + (assert (= (name 1.0) "1.0")) + (assert (= (name :foo) "foo")) + (assert (= (name :foo_bar) "foo-bar")) + (assert (= (name test-name-conversion) "test-name-conversion"))) From a49047b7a3b77e267ec9d148c2ee6ac39cbabf7f Mon Sep 17 00:00:00 2001 From: schuster-rainer Date: Sat, 1 Mar 2014 00:23:49 +0100 Subject: [PATCH 02/17] Added name and keyword functions to core --- hy/core/language.hy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 21c7808..c65bcbe 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -26,7 +26,7 @@ (import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy.models.cons [HyCons]] - [hy.models.keyword [HyKeyword KEYWORD_PREFIX]]) + [hy.models.keyword [HyKeyword *keyword-prefix*]]) (defn _numeric-check [x] @@ -359,7 +359,7 @@ (defn keyword [value] "Create a keyword from the given value. Strings numbers and even objects with the __name__ magic will work" - (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (if (and (string? value) (value.startswith *keyword-prefix*)) (hyify value) (if (string? value) (HyKeyword (+ ":" (hyify value))) @@ -370,7 +370,7 @@ (defn name [value] "Convert the given value to a string. Keyword special character will be stripped. String will be used as is. Even objects with the __name__ magic will work" - (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (if (and (string? value) (value.startswith *keyword-prefix*)) (hyify (slice value 2)) (if (string? value) (hyify value) From c1b83c0265cacfbf1a3f2865c960b485fba28cbd Mon Sep 17 00:00:00 2001 From: schuster-rainer Date: Sat, 1 Mar 2014 01:00:46 +0100 Subject: [PATCH 03/17] Fixed flake8 blank line issue --- hy/models/keyword.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hy/models/keyword.py b/hy/models/keyword.py index a3d778b..32b2306 100644 --- a/hy/models/keyword.py +++ b/hy/models/keyword.py @@ -22,8 +22,10 @@ from __future__ import unicode_literals from hy.models import HyObject from hy._compat import str_type + KEYWORD_PREFIX = "\uFDD0" + class HyKeyword(HyObject, str_type): """Generic Hy Keyword object. It's either a ``str`` or a ``unicode``, depending on the Python version. From 76d7e3479a23d37bcf34940763d107bba1a4ab4b Mon Sep 17 00:00:00 2001 From: bismigalis Date: Tue, 14 Jan 2014 09:30:36 +0200 Subject: [PATCH 04/17] Added merge-with --- docs/language/core.rst | 18 ++++++++++++++++++ hy/core/language.hy | 18 +++++++++++++++++- tests/native_tests/language.hy | 21 ++++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/language/core.rst b/docs/language/core.rst index 59af6bc..703bdf4 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -449,6 +449,24 @@ Returns the single step macro expansion of form. => (macroexpand-1 '(-> (a b) (-> (c d) (e f)))) (u'_>' (u'a' u'b') (u'c' u'd') (u'e' u'f')) + +.. _merge-with-fn: + +merge-with +---------- + +.. versionadded:: 0.10.0 + +Usage: ``(merge-with f &rest maps) + +Returns a map that consist of the rest of the maps joined onto first. If a key occurs in more than one map, the mapping(s) from the latter (left-to-right) will be combined with the mapping in the result by calling (f val-in-result val-in-latter). + +.. code-block:: clojure + + => (merge-with (fn [x y] (+ x y)) {"a" 10 "b" 20} {"a" 1 "c" 30}) + {u'a': 11L, u'c': 30L, u'b': 20L} + + .. _neg?-fn: neg? diff --git a/hy/core/language.hy b/hy/core/language.hy index 91cd990..749fa9e 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -253,6 +253,22 @@ (setv name (calling-module-name)) (hy.macros.macroexpand-1 form name)) +(defn merge-with [f &rest maps] + "Returns a map that consists of the rest of the maps joined onto + the first. If a key occurs in more than one map, the mapping(s) + from the latter (left-to-right) will be combined with the mapping in + the result by calling (f val-in-result val-in-latter)." + (if (any maps) + (let [[merge-entry (fn [m e] + (let [[k (get e 0)] [v (get e 1)]] + (if (in k m) + (assoc m k (f (get m k) v)) + (assoc m k v))) + m)] + [merge2 (fn [m1 m2] + (reduce merge-entry (.items m2) (or m1 {})))]] + (reduce merge2 maps)))) + (defn neg? [n] "Return true if n is < 0" (_numeric-check n) @@ -360,7 +376,7 @@ every? first filter filterfalse flatten float? gensym identity inc input instance? integer integer? integer-char? interleave interpose iterable? iterate iterator? keyword? list* - macroexpand macroexpand-1 map neg? nil? none? nth + macroexpand macroexpand-1 map merge-with neg? nil? none? nth numeric? odd? pos? range read remove repeat repeatedly rest reduce second some string string? take take-nth take-while zero? zip zip_longest zipwith]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 7f3ba54..895249f 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -1,6 +1,7 @@ (import [tests.resources [kwtest function-with-a-dash]] [os.path [exists isdir isfile]] - [sys :as systest]) + [sys :as systest] + [operator [or_]]) (import sys) (import [hy._compat [PY33 PY34]]) @@ -985,6 +986,24 @@ (assert (= (macroexpand-1 '(-> (a b) (-> (c d) (e f)))) '(-> (a b) (c d) (e f))))) +(defn test-merge-with [] + "NATIVE: test merge-with" + (assert (= (merge-with + {} {}) nil)) + (assert (= (merge-with + {"a" 10 "b" 20} {}) {"a" 10 "b" 20})) + (assert (= (merge-with + {} {"a" 10 "b" 20}) {"a" 10 "b" 20})) + (assert (= (merge-with + {"a" 10 "b" 20} {"a" 1 "c" 30}) + {"a" 11 "b" 20 "c" 30})) + (assert (= (merge-with + + {:a 1 :b 2} + {:a 9 :b 98 :c 0} + {:a 10 :b 100 :c 10} + {:a 5} + {:c 5 :d 42}) + {:d 42 :c 15 :a 25 :b 200})) + (assert (= (merge-with or_ + {"a" (set [1 2 3]) "b" (set [4 5 6])} + {"a" (set [2 3 7 8]) "c" (set [1 2 3])}) + {"a" (set [1 2 3 7 8]) "c" (set [1 2 3]) "b" (set [4 5 6])}))) (defn test-calling-module-name [] "NATIVE: Test the calling-module-name function" From 0041d508e8cc863fbbc07f19727aa71cd3cad3b0 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 6 Oct 2014 13:37:51 -0600 Subject: [PATCH 05/17] Add initial .dockerignore file This avoids adding ".git" to the Docker image, which speeds up the initial "uploading context" phase and helps slim the image a small amount (since ".git" of Hy itself isn't necessary for using Hy). --- .dockerignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6b8710a --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git From 377c3ec5ee7ae688ce0dd7a1e32db731d183ab31 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 9 Oct 2014 02:58:42 +0300 Subject: [PATCH 06/17] Update versionadded directive for merge-with. --- docs/language/core.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/language/core.rst b/docs/language/core.rst index 703bdf4..2a594ba 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -455,11 +455,14 @@ Returns the single step macro expansion of form. merge-with ---------- -.. versionadded:: 0.10.0 +.. versionadded:: 0.10.1 Usage: ``(merge-with f &rest maps) -Returns a map that consist of the rest of the maps joined onto first. If a key occurs in more than one map, the mapping(s) from the latter (left-to-right) will be combined with the mapping in the result by calling (f val-in-result val-in-latter). +Returns a map that consist of the rest of the maps joined onto first. +If a key occurs in more than one map, the mapping(s) from the latter +(left-to-right) will be combined with the mapping in the result by +calling ``(f val-in-result val-in-latter)``. .. code-block:: clojure From 78131a9d122f68ee996608c26167c4ff224cd52c Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Thu, 9 Oct 2014 03:00:08 +0300 Subject: [PATCH 07/17] Add @bismigalis to AUTHORS. --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 41514fe..9518075 100644 --- a/AUTHORS +++ b/AUTHORS @@ -48,3 +48,4 @@ * Allison Kaptur * Matthew Wampler-Doty * Tianon Gravi +* Ruslan Prokopiev From 0a8fc0ebb71906ffd3980a201a44558263699566 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 10 Oct 2014 09:45:07 -0400 Subject: [PATCH 08/17] Add in NEWS for 0.10.1 --- NEWS | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/NEWS b/NEWS index ed5c534..2cc4709 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,49 @@ +Changes from 0.10.0 + + This release took some time (sorry, all my fault) but it's got a bunch of + really nice feature. We hope you enjoy hacking with Hy as much as we enjoy + hacking on Hy. + + In other news, we're Dockerized as an official library image! + https://registry.hub.docker.com/_/hylang/> + + $ docker run -it --rm hylang + hy 0.10.0 using CPython(default) 3.4.1 on Linux + => ((lambda [] (print "Hello, World!"))) + Hello, World! + + - Hy Society + + [ Language Changes ] + * Implement raise :from, Python 3 only. + * defmain macro + * name & keyword functions added to core + * (read) added to core + * shadow added to core + * New functions interleave interpose zip_longest added to core + * nth returns default value when out of bounds + * merge-with added + * doto macro added + * keyword? to findout keywords + * setv no longer allows "." in names + + [Internals ] + * Builtins reimplemented in terms of python stdlib + * gensyms (defmacro/g!) handles non-string types better + + [Tools] + * Added hy2py to installed scripts + + [ Misc. Fixes ] + * Symbols like true, false, none can't be assigned + * Set sys.argv default to [''] like Python does + * REPL displays the the python version and platform at startup + * Dockerfile added for https://registry.hub.docker.com/_/hylang/ + + [ Contrib changes ] + * Fix ap-first and ap-last for failure conditions + + Changes from 0.9.12 0.10.0 - the "oh man I'm late for PyCon" release From ecc003b43433e5b46511157598e4857a761007bf Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 10 Oct 2014 09:45:23 -0400 Subject: [PATCH 09/17] 0.10.1 --- hy/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hy/version.py b/hy/version.py index 23a31bc..d1f5edb 100644 --- a/hy/version.py +++ b/hy/version.py @@ -20,4 +20,4 @@ __appname__ = "hy" -__version__ = "0.10.0" +__version__ = "0.10.1" From 75adfb93d6716306c9ad75165e1db13055f9158c Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 10 Oct 2014 09:52:44 -0400 Subject: [PATCH 10/17] Damnit @tianon --- NEWS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 2cc4709..423cc7d 100644 --- a/NEWS +++ b/NEWS @@ -1,7 +1,7 @@ Changes from 0.10.0 This release took some time (sorry, all my fault) but it's got a bunch of - really nice feature. We hope you enjoy hacking with Hy as much as we enjoy + really nice features. We hope you enjoy hacking with Hy as much as we enjoy hacking on Hy. In other news, we're Dockerized as an official library image! From f5f4384722520fe519e0c3a76599c18ec639ea12 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 10 Oct 2014 09:53:33 -0400 Subject: [PATCH 11/17] Another typo. Don't be a release manager in the morning --- NEWS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 423cc7d..08e6b98 100644 --- a/NEWS +++ b/NEWS @@ -5,7 +5,7 @@ Changes from 0.10.0 hacking on Hy. In other news, we're Dockerized as an official library image! - https://registry.hub.docker.com/_/hylang/> + $ docker run -it --rm hylang hy 0.10.0 using CPython(default) 3.4.1 on Linux From d01b6bbacc931eef0bcc303563c3058b233a728c Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Sat, 1 Nov 2014 15:00:41 -0500 Subject: [PATCH 12/17] Fix Python 3 re-raising --- hy/_compat.py | 5 +++++ hy/compiler.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/hy/_compat.py b/hy/_compat.py index 096282a..80d8f80 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -52,3 +52,8 @@ if PY3: long_type = int else: long_type = long # NOQA + +if PY3: + exec('def raise_empty(t, *args): raise t(*args) from None') +else: + def raise_empty(t, *args): raise t(*args) diff --git a/hy/compiler.py b/hy/compiler.py index 610d643..4c7af9c 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -38,7 +38,7 @@ from hy.models.cons import HyCons from hy.errors import HyCompileError, HyTypeError import hy.macros -from hy._compat import str_type, long_type, PY27, PY33, PY3, PY34 +from hy._compat import str_type, long_type, PY27, PY33, PY3, PY34, raise_empty from hy.macros import require, macroexpand, reader_macroexpand import hy.importer @@ -429,7 +429,7 @@ class HyASTCompiler(object): except HyTypeError as e: raise except Exception as e: - raise HyCompileError(e, sys.exc_info()[2]) + raise_empty(HyCompileError, e, sys.exc_info()[2]) raise HyCompileError(Exception("Unknown type: `%s'" % _type)) From a6d9a963b5ecf01efd0d444d3661758d30f117e5 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Sat, 1 Nov 2014 16:07:39 -0500 Subject: [PATCH 13/17] Fix flake8 errors --- hy/_compat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hy/_compat.py b/hy/_compat.py index 80d8f80..af710f2 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -56,4 +56,5 @@ else: if PY3: exec('def raise_empty(t, *args): raise t(*args) from None') else: - def raise_empty(t, *args): raise t(*args) + def raise_empty(t, *args): + raise t(*args) From c88e75251cd60be7e7adfff2263dba12196dfc3d Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Wed, 5 Nov 2014 21:01:10 -0600 Subject: [PATCH 14/17] Fix Travis failures --- .travis.yml | 8 +------- Makefile | 50 +++++++++++++++++++++++++++++++++++++++++++++-- tests/test_bin.py | 5 ++++- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0009569..9e51591 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,15 +8,9 @@ python: - "3.4" cache: - $HOME/.pip-cache -# command to install dependencies -install: - - pip install -r requirements-travis.txt --download-cache $HOME/.pip-cache - - pip install coveralls --download-cache $HOME/.pip-cache - - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi # needs for running tests - - pip install --allow-all-external -e . # command to run tests script: make travis -after_success: coveralls +after_success: make coveralls notifications: email: - paultag@gmail.com diff --git a/Makefile b/Makefile index e1f3ea9..374a48f 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,21 @@ +pypy_url=http://buildbot.pypy.org/nightly/trunk/pypy-c-jit-latest-linux64.tar.bz2 +pip_url=https://bootstrap.pypa.io/get-pip.py +python=python +pip=pip +coveralls=coveralls +nose=nosetests +pcache=$(HOME)/.pip-cache + +ifeq (PyPy 2.4,$(findstring PyPy 2.4,$(shell python -V 2>&1 | tail -1))) + bad_pypy=1 + python=./pypy + pip=./pip + coveralls=./coveralls + nose=./nosetests +else + bad_pypy= +endif + all: @echo "No default step. Use setup.py" @echo "" @@ -53,14 +71,42 @@ diff: r: d tox diff -travis: - nosetests -s --with-coverage --cover-package hy +python: +ifeq ($(bad_pypy),1) + # Due to stupid PyPy 2.4 bugs, a custom version needs to be downloaded + curl $(pypy_url) -o pypy.tbz2 + tar xf pypy.tbz2 + ln -sf `pwd`/pypy-*/bin/pypy $(python) + curl $(pip_url) | $(python) + ln -sf `pwd`/pypy-*/bin/pip $(pip) + sudo $(pip) install nose + ln -sf `pwd`/pypy-*/bin/nosetests $(nose) +endif +ifeq (Python 2.6,$(findstring Python 2.6,$(shell python -V 2>&1))) + $(pip) install unittest2 +endif + $(pip) install -r requirements-travis.txt --download-cache $(pcache) + $(pip) install coveralls --download-cache $(pcache) + $(pip) install --allow-all-external -e . +ifeq ($(bad_pypy),1) + ln -sf `pwd`/pypy-*/bin/coveralls $(coveralls) +endif + +travis: python +ifeq ($(bad_pypy),1) + HY_DIR=`pwd`/pypy-*/bin $(nose) -s --with-coverage --cover-package hy +else + $(nose) -s --with-coverage --cover-package hy +endif ifeq (PyPy,$(findstring PyPy,$(shell python -V 2>&1 | tail -1))) @echo "skipping flake8 on pypy" else flake8 hy bin tests endif +coveralls: + $(coveralls) + clean: @find . -name "*.pyc" -exec rm {} \; @find -name __pycache__ -delete diff --git a/tests/test_bin.py b/tests/test_bin.py index 16383e4..e8a0fc8 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -26,8 +26,11 @@ import subprocess from hy._compat import PY3 +hy_dir = os.environ.get('HY_DIR', '') + + def run_cmd(cmd, stdin_data=None): - p = subprocess.Popen(cmd, + p = subprocess.Popen(os.path.join(hy_dir, cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, From c9362d39c1be54f8290006db00fe71d824ff091c Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Thu, 13 Nov 2014 17:49:17 -0600 Subject: [PATCH 15/17] Fix #684 --- hy/compiler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hy/compiler.py b/hy/compiler.py index 4c7af9c..7c7925e 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1268,7 +1268,8 @@ class HyASTCompiler(object): def compile_decorate_expression(self, expr): expr.pop(0) # with-decorator fn = self.compile(expr.pop(-1)) - if not fn.stmts or not isinstance(fn.stmts[-1], ast.FunctionDef): + if not fn.stmts or not (isinstance(fn.stmts[-1], ast.FunctionDef) or + isinstance(fn.stmts[-1], ast.ClassDef)): raise HyTypeError(expr, "Decorated a non-function") decorators, ret = self._compile_collect(expr) fn.stmts[-1].decorator_list = decorators From c7e4d4cd6e0b68daa030ff474ac500a7a8bd6c92 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Fri, 14 Nov 2014 14:21:16 -0600 Subject: [PATCH 16/17] Add tests --- tests/native_tests/with_decorator.hy | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/native_tests/with_decorator.hy b/tests/native_tests/with_decorator.hy index dc6ad6e..867c63a 100644 --- a/tests/native_tests/with_decorator.hy +++ b/tests/native_tests/with_decorator.hy @@ -7,6 +7,15 @@ (* 2 2))) +(defn bardec [cls] + (setv cls.my_attr 123)) + +(with-decorator bardec + (defclass cls [] + [[my_attr 456]])) + + (defn test-decorators [] "NATIVE: test decorators." - (assert (= (tfunction) 2))) + (assert (= (tfunction) 2)) + (assert (= cls.my_attr 123))) From dcf29d3d2a147fe66a199712149f7b58fd7fef04 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sat, 15 Nov 2014 07:47:55 -0500 Subject: [PATCH 17/17] Fix the test decorator to return the class. --- tests/native_tests/with_decorator.hy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/native_tests/with_decorator.hy b/tests/native_tests/with_decorator.hy index 867c63a..d82214d 100644 --- a/tests/native_tests/with_decorator.hy +++ b/tests/native_tests/with_decorator.hy @@ -8,7 +8,8 @@ (defn bardec [cls] - (setv cls.my_attr 123)) + (setv cls.my_attr 123) + cls) (with-decorator bardec (defclass cls []