summaryrefslogtreecommitdiff
path: root/dev-python/genshi/files/genshi-0.7-issue582.patch
blob: fbcab626d6c4cc9f0a338a0f4f2cb5421b2d0e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
From 554fa3428bea3039decfd9064b860c753b2637a1 Mon Sep 17 00:00:00 2001
From: SVN-Git Migration <python-modules-team@lists.alioth.debian.org>
Date: Thu, 8 Oct 2015 09:13:48 -0700
Subject: Make genshi 0.7 compatible with Python 3.4.

Origin: http://genshi.edgewall.org/changeset/1252?format=diff&new=1252
Bug: http://genshi.edgewall.org/ticket/582
Forwarded: not-needed

Patch-Name: issue582.patch
---
 doc/upgrade.txt                   |  8 ++---
 genshi/compat.py                  | 10 +++++-
 genshi/filters/tests/test_html.py | 14 ++++++---
 genshi/template/astutil.py        | 66 ++++++++++++++++++++++++++++-----------
 genshi/template/eval.py           | 37 +++++++++++++---------
 genshi/template/tests/eval.py     | 23 ++++++++++++++
 run_benchmarks.sh                 | 31 ++++++++++++++++++
 setup.py                          |  6 +++-
 8 files changed, 151 insertions(+), 44 deletions(-)
 create mode 100644 run_benchmarks.sh

diff --git a/doc/upgrade.txt b/doc/upgrade.txt
index b240eda..ad4c080 100644
--- a/doc/upgrade.txt
+++ b/doc/upgrade.txt
@@ -7,11 +7,11 @@ Upgrading Genshi
    :depth: 2
 .. sectnum::
 
-------------------------------------------------------
-Upgrading from Genshi 0.6.x to the development version
-------------------------------------------------------
+-------------------------------------------
+Upgrading from Genshi 0.6.x to Genshi 0.7.x
+-------------------------------------------
 
-The Genshi development version now supports both Python 2 and Python 3.
+Genshi 0.7.x now supports both Python 2 and Python 3.
 
 The most noticable API change in the Genshi development version is that the
 default encoding in numerous places is now None (i.e. unicode) instead
diff --git a/genshi/compat.py b/genshi/compat.py
index 9787325..6574e39 100644
--- a/genshi/compat.py
+++ b/genshi/compat.py
@@ -35,6 +35,15 @@ else:
                 'Python 2 compatibility function. Not usable in Python 3.')
 
 
+# We need to test if an object is an instance of a string type in places
+
+if IS_PYTHON2:
+    def isstring(obj):
+        return isinstance(obj, basestring)
+else:
+    def isstring(obj):
+        return isinstance(obj, str)
+
 # We need to differentiate between StringIO and BytesIO in places
 
 if IS_PYTHON2:
@@ -112,4 +121,3 @@ except NameError:
             if not x:
                 return False
         return True
-
diff --git a/genshi/filters/tests/test_html.py b/genshi/filters/tests/test_html.py
index a8cfa04..7120988 100644
--- a/genshi/filters/tests/test_html.py
+++ b/genshi/filters/tests/test_html.py
@@ -368,12 +368,16 @@ def StyleSanitizer():
 
 class HTMLSanitizerTestCase(unittest.TestCase):
 
-    def assert_parse_error_or_equal(self, expected, exploit):
+    def assert_parse_error_or_equal(self, expected, exploit,
+                                    allow_strip=False):
         try:
             html = HTML(exploit)
         except ParseError:
             return
-        self.assertEquals(expected, (html | HTMLSanitizer()).render())
+        sanitized_html = (html | HTMLSanitizer()).render()
+        if not sanitized_html and allow_strip:
+            return
+        self.assertEquals(expected, sanitized_html)
 
     def test_sanitize_unchanged(self):
         html = HTML(u'<a href="#">fo<br />o</a>')
@@ -417,10 +421,12 @@ class HTMLSanitizerTestCase(unittest.TestCase):
         html = HTML(u'<SCRIPT SRC="http://example.com/"></SCRIPT>')
         self.assertEquals('', (html | HTMLSanitizer()).render())
         src = u'<SCR\0IPT>alert("foo")</SCR\0IPT>'
-        self.assert_parse_error_or_equal('&lt;SCR\x00IPT&gt;alert("foo")', src)
+        self.assert_parse_error_or_equal('&lt;SCR\x00IPT&gt;alert("foo")', src,
+                                         allow_strip=True)
         src = u'<SCRIPT&XYZ SRC="http://example.com/"></SCRIPT>'
         self.assert_parse_error_or_equal('&lt;SCRIPT&amp;XYZ; '
-                                         'SRC="http://example.com/"&gt;', src)
+                                         'SRC="http://example.com/"&gt;', src,
+                                         allow_strip=True)
 
     def test_sanitize_remove_onclick_attr(self):
         html = HTML(u'<div onclick=\'alert("foo")\' />')
diff --git a/genshi/template/astutil.py b/genshi/template/astutil.py
index b24f728..e561846 100644
--- a/genshi/template/astutil.py
+++ b/genshi/template/astutil.py
@@ -21,7 +21,7 @@ else:
     def parse(source, mode):
         return compile(source, '', mode, _ast.PyCF_ONLY_AST)
 
-from genshi.compat import IS_PYTHON2
+from genshi.compat import IS_PYTHON2, isstring
 
 __docformat__ = 'restructuredtext en'
 
@@ -103,32 +103,48 @@ class ASTCodeGenerator(object):
         self._new_line()
         return self.visit(node.body)
 
+    # Python < 3.4
     # arguments = (expr* args, identifier? vararg,
     #              identifier? kwarg, expr* defaults)
+    #
+    # Python >= 3.4
+    # arguments = (arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults,
+    #              arg? kwarg, expr* defaults)
     def visit_arguments(self, node):
-        first = True
-        no_default_count = len(node.args) - len(node.defaults)
-        for i, arg in enumerate(node.args):
-            if not first:
-                self._write(', ')
+        def write_possible_comma():
+            if _first[0]:
+                _first[0] = False
             else:
-                first = False
-            self.visit(arg)
-            if i >= no_default_count:
-                self._write('=')
-                self.visit(node.defaults[i - no_default_count])
-        if getattr(node, 'vararg', None):
-            if not first:
                 self._write(', ')
+        _first = [True]
+
+        def write_args(args, defaults):
+            no_default_count = len(args) - len(defaults)
+            for i, arg in enumerate(args):
+                write_possible_comma()
+                self.visit(arg)
+                default_idx = i - no_default_count
+                if default_idx >= 0 and defaults[default_idx] is not None:
+                    self._write('=')
+                    self.visit(defaults[i - no_default_count])
+
+        write_args(node.args, node.defaults)
+        if getattr(node, 'vararg', None):
+            write_possible_comma()
+            self._write('*')
+            if isstring(node.vararg):
+                self._write(node.vararg)
             else:
-                first = False
-            self._write('*' + node.vararg)
+                self.visit(node.vararg)
+        if getattr(node, 'kwonlyargs', None):
+            write_args(node.kwonlyargs, node.kw_defaults)
         if getattr(node, 'kwarg', None):
-            if not first:
-                self._write(', ')
+            write_possible_comma()
+            self._write('**')
+            if isstring(node.kwarg):
+                self._write(node.kwarg)
             else:
-                first = False
-            self._write('**' + node.kwarg)
+                self.visit(node.kwarg)
 
     if not IS_PYTHON2:
         # In Python 3 arguments get a special node
@@ -732,6 +748,17 @@ class ASTCodeGenerator(object):
     def visit_Name(self, node):
         self._write(node.id)
 
+    # NameConstant(singleton value)
+    def visit_NameConstant(self, node):
+        if node.value is None:
+            self._write('None')
+        elif node.value is True:
+            self._write('True')
+        elif node.value is False:
+            self._write('False')
+        else:
+            raise Exception("Unknown NameConstant %r" % (node.value,))
+
     # List(expr* elts, expr_context ctx)
     def visit_List(self, node):
         self._write('[')
@@ -837,6 +864,7 @@ class ASTTransformer(object):
     visit_Attribute = _clone
     visit_Subscript = _clone
     visit_Name = _clone
+    visit_NameConstant = _clone
     visit_List = _clone
     visit_Tuple = _clone
 
diff --git a/genshi/template/eval.py b/genshi/template/eval.py
index c00cfcb..81644a7 100644
--- a/genshi/template/eval.py
+++ b/genshi/template/eval.py
@@ -24,7 +24,8 @@ from genshi.template.astutil import ASTTransformer, ASTCodeGenerator, \
 from genshi.template.base import TemplateRuntimeError
 from genshi.util import flatten
 
-from genshi.compat import get_code_params, build_code_chunk, IS_PYTHON2
+from genshi.compat import get_code_params, build_code_chunk, isstring, \
+                          IS_PYTHON2
 
 __all__ = ['Code', 'Expression', 'Suite', 'LenientLookup', 'StrictLookup',
            'Undefined', 'UndefinedError']
@@ -495,28 +496,34 @@ class TemplateASTTransformer(ASTTransformer):
     def __init__(self):
         self.locals = [CONSTANTS]
 
+    def _process(self, names, node):
+        if not IS_PYTHON2 and isinstance(node, _ast.arg):
+            names.add(node.arg)
+        elif isstring(node):
+            names.add(node)
+        elif isinstance(node, _ast.Name):
+            names.add(node.id)
+        elif isinstance(node, _ast.alias):
+            names.add(node.asname or node.name)
+        elif isinstance(node, _ast.Tuple):
+            for elt in node.elts:
+                self._process(names, elt)
+
     def _extract_names(self, node):
         names = set()
-        def _process(node):
-            if not IS_PYTHON2 and isinstance(node, _ast.arg):
-                names.add(node.arg)
-            if isinstance(node, _ast.Name):
-                names.add(node.id)
-            elif isinstance(node, _ast.alias):
-                names.add(node.asname or node.name)
-            elif isinstance(node, _ast.Tuple):
-                for elt in node.elts:
-                    _process(elt)
         if hasattr(node, 'args'):
             for arg in node.args:
-                _process(arg)
+                self._process(names, arg)
+            if hasattr(node, 'kwonlyargs'):
+                for arg in node.kwonlyargs:
+                    self._process(names, arg)
             if hasattr(node, 'vararg'):
-                names.add(node.vararg)
+                self._process(names, node.vararg)
             if hasattr(node, 'kwarg'):
-                names.add(node.kwarg)
+                self._process(names, node.kwarg)
         elif hasattr(node, 'names'):
             for elt in node.names:
-                _process(elt)
+                self._process(names, elt)
         return names
 
     def visit_Str(self, node):
diff --git a/genshi/template/tests/eval.py b/genshi/template/tests/eval.py
index 7722571..c44a0e3 100644
--- a/genshi/template/tests/eval.py
+++ b/genshi/template/tests/eval.py
@@ -590,6 +590,29 @@ x = smash(foo='abc', bar='def')
         suite.execute(data)
         self.assertEqual(['bardef', 'fooabc'], sorted(data['x']))
 
+    if not IS_PYTHON2:
+        def test_def_kwonlyarg(self):
+            suite = Suite("""
+def kwonly(*args, k):
+    return k
+x = kwonly(k="foo")
+""")
+            data = {}
+            suite.execute(data)
+            self.assertEqual("foo", data['x'])
+
+        def test_def_kwonlyarg_with_default(self):
+            suite = Suite("""
+def kwonly(*args, k="bar"):
+    return k
+x = kwonly(k="foo")
+y = kwonly()
+""")
+            data = {}
+            suite.execute(data)
+            self.assertEqual("foo", data['x'])
+            self.assertEqual("bar", data['y'])
+
     def test_def_nested(self):
         suite = Suite("""
 def doit():
diff --git a/run_benchmarks.sh b/run_benchmarks.sh
new file mode 100644
index 0000000..0c64cc8
--- /dev/null
+++ b/run_benchmarks.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+#
+# 1. Run the tests with `tox` (this will set up all the tox envs).
+# 2. ./run_benchmarks.sh <env-name> | tee results-<env-name>.out
+
+NAME="$1"
+PYTHON="./.tox/$NAME/bin/python"
+BENCH_DIR="bench_build/$1"
+BENCH_BIN_DIR="$BENCH_DIR/bin"
+mkdir -p "bench_build"
+
+rm -rf "$BENCH_DIR"
+cp -R "examples/bench" "$BENCH_DIR"
+
+case "$NAME" in
+  py32|py33)
+    2to3 -w --no-diffs "$BENCH_DIR"
+    ;;
+esac
+
+echo "-- basic --"
+"$PYTHON" "$BENCH_DIR/basic.py" 
+echo
+
+echo "-- bigtable --"
+"$PYTHON" "$BENCH_DIR/bigtable.py"
+echo
+
+echo "-- xpath --"
+"$PYTHON" "$BENCH_DIR/xpath.py"
+echo
diff --git a/setup.py b/setup.py
index 294ba9b..45099b5 100755
--- a/setup.py
+++ b/setup.py
@@ -65,9 +65,13 @@ available.""")
 
 
 if Feature:
+    # Optional C extension module for speeding up Genshi:
+    # Not activated by default on:
+    # - PyPy (where it harms performance)
+    # - CPython >= 3.3 (the new Unicode C API is not supported yet)
     speedups = Feature(
         "optional C speed-enhancements",
-        standard = not is_pypy,
+        standard = not is_pypy and sys.version_info < (3, 3),
         ext_modules = [
             Extension('genshi._speedups', ['genshi/_speedups.c']),
         ],