From e8feadeae9243e61a8d7fce8652490b05f53cba4 Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Sun, 25 Mar 2018 02:33:32 -0400 Subject: [PATCH] Use PEP 448 for better unquoted expressions With Python 3.5, we can have an arbitrary number of unpackings. We can use this to emit cleaner expressions. Falls back to legacy behaviour for older Python versions. --- hy/compiler.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 0eb067e9a..cc053fb46 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -699,28 +699,31 @@ def _render_quoted_form(self, form, level): imports = set([name]) if isinstance(form, (HyList, HyDict, HySet)): - if not form: - contents = HyList() - else: - # If there are arguments, they can be spliced - # so we build a sum... - contents = HyExpression([HySymbol("+"), HyList()]) - + contents = HyList() for x in form: f_imports, f_contents, splice = self._render_quoted_form(x, level) imports.update(f_imports) if splice: - to_add = HyExpression([ - HySymbol("list"), - HyExpression([HySymbol("or"), f_contents, HyList()])]) + # On Python 3.5 and newer, leverage PEP 448 to generate + # expression using tuple unpacking instead of addition. + # + # Should be marginally faster too. + contents.append(HyExpression([ + HySymbol("unpack-iterable" if PY35 else "list"), + HyExpression([HySymbol("or"), f_contents, HyList()]) + ])) else: - to_add = HyList([f_contents]) + contents.append(f_contents if PY35 + else HyList([f_contents])) - contents.append(to_add) + if not PY35: + # Without Python 3.5's unpacking improvements, we need + # to concatenate the expression manually + contents = HyExpression([HySymbol("+"), HyList()]) + contents - return imports, HyExpression([HySymbol(name), - contents]).replace(form), False + expr = HyExpression([HySymbol(name), contents]) + return imports, expr.replace(form), False elif isinstance(form, HyCons): ret = HyExpression([HySymbol(name)])