From 42468051f526d34b72933bd90d8e39f1f4e87104 Mon Sep 17 00:00:00 2001 From: Morten Linderud Date: Tue, 25 Jun 2013 17:02:02 +0200 Subject: [PATCH 1/8] Builtins fix for 2 and 3, tests aswell --- bin/hy | 21 --------------------- hy/cmdline.py | 25 +++++++++++++++++++++++++ tests/test_bin.py | 7 +++++++ 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/bin/hy b/bin/hy index 3668c54..14cf5e0 100755 --- a/bin/hy +++ b/bin/hy @@ -4,26 +4,5 @@ import sys from hy.cmdline import cmdline_handler -class HyQuitter(object): - def __init__(self, name): - self.name = name - - def __repr__(self): - return "Use (%s) or Ctrl-D (i.e. EOF) to exit" % (self.name) - - __str__ = __repr__ - - def __call__(self, code=None): - try: - sys.stdin.close() - except: - pass - raise SystemExit(code) - - -__builtins__.quit = HyQuitter('quit') -__builtins__.exit = HyQuitter('exit') - - if __name__ == '__main__': sys.exit(cmdline_handler("hy", sys.argv)) diff --git a/hy/cmdline.py b/hy/cmdline.py index 3d61669..0393e6a 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -49,6 +49,31 @@ from hy.models.symbol import HySymbol _machine = Machine(Idle, 1, 0) +try: + import __builtin__ as builtins +except ImportError: + import builtins + + +class HyQuitter(object): + def __init__(self, name): + self.name = name + + def __repr__(self): + return "Use (%s) or Ctrl-D (i.e. EOF) to exit" % (self.name) + + __str__ = __repr__ + + def __call__(self, code=None): + try: + sys.stdin.close() + except: + pass + raise SystemExit(code) + +builtins.quit = HyQuitter('quit') +builtins.exit = HyQuitter('exit') + class HyREPL(code.InteractiveConsole): def runsource(self, source, filename='', symbol='single'): diff --git a/tests/test_bin.py b/tests/test_bin.py index 79d20fa..e366c0c 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -91,3 +91,10 @@ def test_hy2py(): assert len(ret[1]) > 1, f assert len(ret[2]) == 0, f assert i + + +def test_bin_hy_builtins(): + import hy.cmdline + + assert str(exit) == "Use (exit) or Ctrl-D (i.e. EOF) to exit" + assert str(quit) == "Use (quit) or Ctrl-D (i.e. EOF) to exit" From de8b62d0d5ee00f8e0927706bd7f1223bad86210 Mon Sep 17 00:00:00 2001 From: Duncan McGreggor Date: Tue, 25 Jun 2013 10:54:40 -0700 Subject: [PATCH 2/8] Added Twisted example. --- eg/twisted/get-page.hy | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 eg/twisted/get-page.hy diff --git a/eg/twisted/get-page.hy b/eg/twisted/get-page.hy new file mode 100644 index 0000000..3d8e4ce --- /dev/null +++ b/eg/twisted/get-page.hy @@ -0,0 +1,45 @@ +;; To run this example, do the following: +;; $ hy get-page.hy http://docs.hylang.org/en/latest/ +;; +;; At which point, you should see output like this: +;; 2013-06-24 23:03:57-0700 [-] Log opened. +;; 2013-06-24 23:03:57-0700 [-] Starting factory +;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Byte count for the content of the HTTP page passed: 11835 +;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Preparing to stop reactor ... +;; 2013-06-24 23:03:57-0700 [HTTPPageGetter,client] Stopping factory +;; 2013-06-24 23:03:57-0700 [-] Main loop terminated. +(import sys) + +(import [twisted.web.client [getPage]] + [twisted.internet [reactor]] + [twisted.python [log]]) + +(defun get-page-size (result) + (print + (+ "Byte count for the content of the HTTP page passed: " + (str (len result))))) + +(defun log-error (err) + (log.msg err)) + +(defun finish (ignore) + (log.msg "Preparing to stop reactor ...") + (reactor.stop)) + +(defun get-page (url) + (let ((d (getPage url))) + (d.addCallback get-page-size) + (d.addErrback log-error) + (d.addCallback finish))) + +(defun caddr (list) + ; This is how you'd do this in CL: + ;(car (cdr (cdr list)))) + ; However, I think this is more efficient in hy: + (get list 2)) + +(if (= __name__ "__main__") + (do + (log.startLogging sys.stdout) + (get-page (caddr sys.argv)) + (reactor.run))) From 59550c9abd7ab6b83f27201db8a71b615804e2a0 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Tue, 25 Jun 2013 19:23:44 -0400 Subject: [PATCH 3/8] tell flake8 to shut up --- tests/test_bin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bin.py b/tests/test_bin.py index e366c0c..4cdfb58 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -94,7 +94,7 @@ def test_hy2py(): def test_bin_hy_builtins(): - import hy.cmdline + import hy.cmdline # NOQA assert str(exit) == "Use (exit) or Ctrl-D (i.e. EOF) to exit" assert str(quit) == "Use (quit) or Ctrl-D (i.e. EOF) to exit" From 9b45dd4ffe9cd700856255c85779ac4c8cfea5bd Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Tue, 25 Jun 2013 21:47:54 -0400 Subject: [PATCH 4/8] add tumblr printer --- eg/lxml/parse-tumblr.hy | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 eg/lxml/parse-tumblr.hy diff --git a/eg/lxml/parse-tumblr.hy b/eg/lxml/parse-tumblr.hy new file mode 100644 index 0000000..3f61719 --- /dev/null +++ b/eg/lxml/parse-tumblr.hy @@ -0,0 +1,22 @@ +;;; Hy tumblr printer. +;;; Copyright (c) Paul R. Tagliamonte, 2013, MIT/Expat license. + + +(import [urllib2 [urlopen]] + [lxml [etree]] + [sys [argv]]) + + +(defn get-rss-feed-name [tumblr] + (kwapply (.format "http://{tumblr}.tumblr.com/rss") {"tumblr" tumblr})) + +(defn get-rss-feed [tumblr] + (.parse etree (urlopen (get-rss-feed-name tumblr)))) + +(defn print-posts [tumblr] + (for [post (.xpath (get-rss-feed tumblr) "//item/title")] + (print post.text))) + +(if (slice argv 2) + (print-posts (get argv 2)) + (print-posts "this-plt-life")) From 7b7b95341038c8ca7d0df56a4d4f56f12ee84c99 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Wed, 26 Jun 2013 01:40:10 +0200 Subject: [PATCH 5/8] set the value of empty hy expression to [] --- hy/macros.py | 7 ++++++- tests/native_tests/language.hy | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hy/macros.py b/hy/macros.py index d3530fd..32ac483 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -70,7 +70,12 @@ _wrappers = { def process(tree, module_name): if isinstance(tree, HyExpression): - fn = tree[0] + try: + fn = tree[0] + except IndexError: + ntree = HyList() + ntree.replace(tree) + return ntree if fn in ("quote", "quasiquote"): return tree ntree = HyExpression( diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 534ae40..004f0b8 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -755,3 +755,7 @@ (continue)) (.append y x)) (assert (= y [5]))) + +(defn test-empty-list [] + "Evaluate an empty list to a []" + (assert (= () []))) \ No newline at end of file From 4c7b4f70e3cbfff1752f1ab06bfc9ae13c375483 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Wed, 26 Jun 2013 01:58:28 +0200 Subject: [PATCH 6/8] changed try to testing for an empty list for legibility (as suggested by paultag), also added to authors --- AUTHORS | 1 + hy/macros.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 92c6c0b..5fb15b6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -13,3 +13,4 @@ * rogererens * Thomas Ballinger * Morten Linderud +* Guillermo Vayá \ No newline at end of file diff --git a/hy/macros.py b/hy/macros.py index 32ac483..dab960a 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -70,12 +70,13 @@ _wrappers = { def process(tree, module_name): if isinstance(tree, HyExpression): - try: - fn = tree[0] - except IndexError: + if tree == []: + # set value to [] ntree = HyList() ntree.replace(tree) return ntree + + fn = tree[0] if fn in ("quote", "quasiquote"): return tree ntree = HyExpression( From 482282178c952660a8441ec6264062fc569cd11d Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Wed, 26 Jun 2013 02:20:15 +0200 Subject: [PATCH 7/8] changed macro to ignore empty trees and made compiler do the substitution work --- hy/compiler.py | 2 ++ hy/macros.py | 5 +---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 4aa8365..d92ede3 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1421,6 +1421,8 @@ class HyASTCompiler(object): @builds(HyExpression) def compile_expression(self, expression): + if expression == []: + return self.compile_list(expression) fn = expression[0] func = None if isinstance(fn, HyKeyword): diff --git a/hy/macros.py b/hy/macros.py index dab960a..b824cea 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -71,10 +71,7 @@ _wrappers = { def process(tree, module_name): if isinstance(tree, HyExpression): if tree == []: - # set value to [] - ntree = HyList() - ntree.replace(tree) - return ntree + return tree fn = tree[0] if fn in ("quote", "quasiquote"): From b65c2a459656ef6da79583c6f87a57804e7584cc Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Wed, 26 Jun 2013 08:44:09 +0200 Subject: [PATCH 8/8] whitespace fix --- AUTHORS | 3 ++- hy/macros.py | 2 +- tests/native_tests/language.hy | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 5fb15b6..9136050 100644 --- a/AUTHORS +++ b/AUTHORS @@ -13,4 +13,5 @@ * rogererens * Thomas Ballinger * Morten Linderud -* Guillermo Vayá \ No newline at end of file +* Guillermo Vayá + diff --git a/hy/macros.py b/hy/macros.py index b824cea..2c5bc02 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -72,7 +72,7 @@ def process(tree, module_name): if isinstance(tree, HyExpression): if tree == []: return tree - + fn = tree[0] if fn in ("quote", "quasiquote"): return tree diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 004f0b8..03c62b0 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -758,4 +758,5 @@ (defn test-empty-list [] "Evaluate an empty list to a []" - (assert (= () []))) \ No newline at end of file + (assert (= () []))) +