Status: | Needs review |
---|---|
Proposed branch: | lp:~wbond/pymeta/python3 |
Merge into: | lp:pymeta |
Diff against target: |
297 lines (+62/-34) 7 files modified
examples/html.py (+1/-1) examples/terml/parser.py (+8/-3) examples/terml/test_terml.py (+1/-1) pymeta/boot.py (+2/-1) pymeta/grammar.py (+5/-4) pymeta/runtime.py (+33/-20) pymeta/test/test_pymeta.py (+12/-4) |
To merge this branch: | bzr merge lp:~wbond/pymeta/python3 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Allen Short | Pending | ||
Review via email:
|
Commit message
Description of the change
I am working on getting pybars working on Python 3.3, and pymeta is a dependency of that.
This patch keeps all of the unit tests functioning the same with Python 2. Unfortunately from what I can tell, the twisted.trial package is not compatible with Python 3 yet.
That said, with these changes, all of the unit tests of pybars run successfully in Python 3.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'examples/html.py' | |||
2 | --- examples/html.py 2008-04-17 13:38:56 +0000 | |||
3 | +++ examples/html.py 2013-03-17 18:51:21 +0000 | |||
4 | @@ -29,7 +29,7 @@ | |||
5 | 29 | """ | 29 | """ |
6 | 30 | Format a dictionary as HTML-ish attributes. | 30 | Format a dictionary as HTML-ish attributes. |
7 | 31 | """ | 31 | """ |
9 | 32 | return ''.join([" %s='%s'" % (k, v) for (k, v) in attrs.iteritems()]) | 32 | return ''.join([" %s='%s'" % (k, v) for (k, v) in attrs.items()]) |
10 | 33 | 33 | ||
11 | 34 | 34 | ||
12 | 35 | unparserGrammar = """ | 35 | unparserGrammar = """ |
13 | 36 | 36 | ||
14 | === modified file 'examples/terml/parser.py' | |||
15 | --- examples/terml/parser.py 2010-05-13 06:26:31 +0000 | |||
16 | +++ examples/terml/parser.py 2013-03-17 18:51:21 +0000 | |||
17 | @@ -152,9 +152,14 @@ | |||
18 | 152 | return _Term(functor, argList) | 152 | return _Term(functor, argList) |
19 | 153 | 153 | ||
20 | 154 | def numberType(n): | 154 | def numberType(n): |
21 | 155 | try: | ||
22 | 156 | int_types = (long, int) | ||
23 | 157 | except NameError: | ||
24 | 158 | int_types = (int,) | ||
25 | 159 | |||
26 | 155 | if isinstance(n, float): | 160 | if isinstance(n, float): |
27 | 156 | return ".float64." | 161 | return ".float64." |
29 | 157 | elif isinstance(n, (long, int)): | 162 | elif isinstance(n, int_types): |
30 | 158 | return ".int." | 163 | return ".int." |
31 | 159 | raise ValueError("wtf") | 164 | raise ValueError("wtf") |
32 | 160 | 165 | ||
33 | @@ -200,6 +205,6 @@ | |||
34 | 200 | 205 | ||
35 | 201 | try: | 206 | try: |
36 | 202 | return _parseTerm(termString) | 207 | return _parseTerm(termString) |
39 | 203 | except ParseError, e: | 208 | except ParseError as e: |
40 | 204 | print e.formatError(termString) | 209 | print(e.formatError(termString)) |
41 | 205 | raise | 210 | raise |
42 | 206 | 211 | ||
43 | === modified file 'examples/terml/test_terml.py' | |||
44 | --- examples/terml/test_terml.py 2010-05-13 06:26:31 +0000 | |||
45 | +++ examples/terml/test_terml.py 2013-03-17 18:51:21 +0000 | |||
46 | @@ -24,7 +24,7 @@ | |||
47 | 24 | self.assertEqual(parse('"foo bar"'), TermLiteral('.String.', "foo bar")) | 24 | self.assertEqual(parse('"foo bar"'), TermLiteral('.String.', "foo bar")) |
48 | 25 | self.assertEqual(parse("'x'"), TermLiteral('.char', character('x'))) | 25 | self.assertEqual(parse("'x'"), TermLiteral('.char', character('x'))) |
49 | 26 | self.assertEqual(parse("0xDECAFC0FFEEBAD"), TermLiteral('.int.', 0xDECAFC0FFEEBAD)) | 26 | self.assertEqual(parse("0xDECAFC0FFEEBAD"), TermLiteral('.int.', 0xDECAFC0FFEEBAD)) |
51 | 27 | self.assertEqual(parse("0755"), TermLiteral('.int.', 0755)) | 27 | self.assertEqual(parse("0755"), TermLiteral('.int.', 0o755)) |
52 | 28 | self.assertEqual(parse("3.14159E17"), TermLiteral('.float64.', 3.14159E17)) | 28 | self.assertEqual(parse("3.14159E17"), TermLiteral('.float64.', 3.14159E17)) |
53 | 29 | self.assertEqual(parse("1e9"), TermLiteral('.float64.', 1e9)) | 29 | self.assertEqual(parse("1e9"), TermLiteral('.float64.', 1e9)) |
54 | 30 | self.assertEqual(parse("0"), TermLiteral(".int.", 0)) | 30 | self.assertEqual(parse("0"), TermLiteral(".int.", 0)) |
55 | 31 | 31 | ||
56 | === modified file 'pymeta/boot.py' | |||
57 | --- pymeta/boot.py 2010-06-11 06:56:34 +0000 | |||
58 | +++ pymeta/boot.py 2013-03-17 18:51:21 +0000 | |||
59 | @@ -79,10 +79,11 @@ | |||
60 | 79 | Consume input until a non-whitespace character is reached. | 79 | Consume input until a non-whitespace character is reached. |
61 | 80 | """ | 80 | """ |
62 | 81 | consumingComment = False | 81 | consumingComment = False |
63 | 82 | e = None | ||
64 | 82 | while True: | 83 | while True: |
65 | 83 | try: | 84 | try: |
66 | 84 | c, e = self.input.head() | 85 | c, e = self.input.head() |
68 | 85 | except EOFError, e: | 86 | except EOFError: |
69 | 86 | break | 87 | break |
70 | 87 | t = self.input.tail() | 88 | t = self.input.tail() |
71 | 88 | if c.isspace() or consumingComment: | 89 | if c.isspace() or consumingComment: |
72 | 89 | 90 | ||
73 | === modified file 'pymeta/grammar.py' | |||
74 | --- pymeta/grammar.py 2010-06-06 16:46:35 +0000 | |||
75 | +++ pymeta/grammar.py 2013-03-17 18:51:21 +0000 | |||
76 | @@ -4,9 +4,9 @@ | |||
77 | 4 | definitions. | 4 | definitions. |
78 | 5 | """ | 5 | """ |
79 | 6 | import string | 6 | import string |
83 | 7 | from builder import TreeBuilder, moduleFromGrammar | 7 | from .builder import TreeBuilder, moduleFromGrammar |
84 | 8 | from boot import BootOMetaGrammar | 8 | from .boot import BootOMetaGrammar |
85 | 9 | from runtime import OMetaBase, ParseError, EOFError | 9 | from .runtime import OMetaBase, ParseError, EOFError |
86 | 10 | 10 | ||
87 | 11 | class OMeta(OMetaBase): | 11 | class OMeta(OMetaBase): |
88 | 12 | """ | 12 | """ |
89 | @@ -264,10 +264,11 @@ | |||
90 | 264 | Consume input until a non-whitespace character is reached. | 264 | Consume input until a non-whitespace character is reached. |
91 | 265 | """ | 265 | """ |
92 | 266 | consumingComment = False | 266 | consumingComment = False |
93 | 267 | e = None | ||
94 | 267 | while True: | 268 | while True: |
95 | 268 | try: | 269 | try: |
96 | 269 | c, e = self.input.head() | 270 | c, e = self.input.head() |
98 | 270 | except EOFError, e: | 271 | except EOFError: |
99 | 271 | break | 272 | break |
100 | 272 | t = self.input.tail() | 273 | t = self.input.tail() |
101 | 273 | if c.isspace() or consumingComment: | 274 | if c.isspace() or consumingComment: |
102 | 274 | 275 | ||
103 | === modified file 'pymeta/runtime.py' | |||
104 | --- pymeta/runtime.py 2010-06-11 06:56:34 +0000 | |||
105 | +++ pymeta/runtime.py 2013-03-17 18:51:21 +0000 | |||
106 | @@ -21,6 +21,9 @@ | |||
107 | 21 | if len(a) > 2: | 21 | if len(a) > 2: |
108 | 22 | self.message = a[2] | 22 | self.message = a[2] |
109 | 23 | 23 | ||
110 | 24 | def __getitem__(self, item): | ||
111 | 25 | return self.args[item] | ||
112 | 26 | |||
113 | 24 | def __eq__(self, other): | 27 | def __eq__(self, other): |
114 | 25 | if other.__class__ == self.__class__: | 28 | if other.__class__ == self.__class__: |
115 | 26 | return (self.position, self.error) == (other.position, other.error) | 29 | return (self.position, self.error) == (other.position, other.error) |
116 | @@ -91,7 +94,12 @@ | |||
117 | 91 | """ | 94 | """ |
118 | 92 | Return the error from the branch that matched the most of the input. | 95 | Return the error from the branch that matched the most of the input. |
119 | 93 | """ | 96 | """ |
121 | 94 | errors.sort(reverse=True, key=operator.itemgetter(0)) | 97 | def get_key(item): |
122 | 98 | val = item[0] | ||
123 | 99 | if val == None: | ||
124 | 100 | val = -1000000000 | ||
125 | 101 | return val | ||
126 | 102 | errors.sort(reverse=True, key=get_key) | ||
127 | 95 | results = set() | 103 | results = set() |
128 | 96 | pos = errors[0][0] | 104 | pos = errors[0][0] |
129 | 97 | for err in errors: | 105 | for err in errors: |
130 | @@ -118,16 +126,20 @@ | |||
131 | 118 | """ | 126 | """ |
132 | 119 | raise TypeError("Characters are not iterable") | 127 | raise TypeError("Characters are not iterable") |
133 | 120 | 128 | ||
144 | 121 | class unicodeCharacter(unicode): | 129 | try: |
145 | 122 | """ | 130 | _has_unicode = True |
146 | 123 | Type to distinguish characters from Unicode strings. | 131 | class unicodeCharacter(unicode): |
147 | 124 | """ | 132 | """ |
148 | 125 | def __iter__(self): | 133 | Type to distinguish characters from Unicode strings. |
149 | 126 | """ | 134 | """ |
150 | 127 | Prevent string patterns and list patterns from matching single | 135 | def __iter__(self): |
151 | 128 | characters. | 136 | """ |
152 | 129 | """ | 137 | Prevent string patterns and list patterns from matching single |
153 | 130 | raise TypeError("Characters are not iterable") | 138 | characters. |
154 | 139 | """ | ||
155 | 140 | raise TypeError("Characters are not iterable") | ||
156 | 141 | except NameError: | ||
157 | 142 | _has_unicode = False | ||
158 | 131 | 143 | ||
159 | 132 | class InputStream(object): | 144 | class InputStream(object): |
160 | 133 | """ | 145 | """ |
161 | @@ -140,7 +152,7 @@ | |||
162 | 140 | """ | 152 | """ |
163 | 141 | if isinstance(iterable, str): | 153 | if isinstance(iterable, str): |
164 | 142 | data = [character(c) for c in iterable] | 154 | data = [character(c) for c in iterable] |
166 | 143 | elif isinstance(iterable, unicode): | 155 | elif _has_unicode and isinstance(iterable, unicode): |
167 | 144 | data = [unicodeCharacter(c) for c in iterable] | 156 | data = [unicodeCharacter(c) for c in iterable] |
168 | 145 | else: | 157 | else: |
169 | 146 | data = list(iterable) | 158 | data = list(iterable) |
170 | @@ -299,7 +311,7 @@ | |||
171 | 299 | @param args: A sequence of arguments to it. | 311 | @param args: A sequence of arguments to it. |
172 | 300 | """ | 312 | """ |
173 | 301 | if args: | 313 | if args: |
175 | 302 | if rule.func_code.co_argcount - 1 != len(args): | 314 | if rule.__code__.co_argcount - 1 != len(args): |
176 | 303 | for arg in args[::-1]: | 315 | for arg in args[::-1]: |
177 | 304 | self.input = ArgInput(arg, self.input) | 316 | self.input = ArgInput(arg, self.input) |
178 | 305 | return rule() | 317 | return rule() |
179 | @@ -375,6 +387,7 @@ | |||
180 | 375 | @param initial: Initial values to populate the returned list with. | 387 | @param initial: Initial values to populate the returned list with. |
181 | 376 | """ | 388 | """ |
182 | 377 | ans = [] | 389 | ans = [] |
183 | 390 | e = None | ||
184 | 378 | for x, e in initial: | 391 | for x, e in initial: |
185 | 379 | ans.append(x) | 392 | ans.append(x) |
186 | 380 | while True: | 393 | while True: |
187 | @@ -382,7 +395,7 @@ | |||
188 | 382 | m = self.input | 395 | m = self.input |
189 | 383 | v, _ = fn() | 396 | v, _ = fn() |
190 | 384 | ans.append(v) | 397 | ans.append(v) |
192 | 385 | except ParseError, e: | 398 | except ParseError: |
193 | 386 | self.input = m | 399 | self.input = m |
194 | 387 | break | 400 | break |
195 | 388 | return ans, e | 401 | return ans, e |
196 | @@ -401,7 +414,7 @@ | |||
197 | 401 | ret, err = f() | 414 | ret, err = f() |
198 | 402 | errors.append(err) | 415 | errors.append(err) |
199 | 403 | return ret, joinErrors(errors) | 416 | return ret, joinErrors(errors) |
201 | 404 | except ParseError, e: | 417 | except ParseError as e: |
202 | 405 | errors.append(e) | 418 | errors.append(e) |
203 | 406 | self.input = m | 419 | self.input = m |
204 | 407 | raise ParseError(*joinErrors(errors)) | 420 | raise ParseError(*joinErrors(errors)) |
205 | @@ -416,7 +429,7 @@ | |||
206 | 416 | m = self.input | 429 | m = self.input |
207 | 417 | try: | 430 | try: |
208 | 418 | fn() | 431 | fn() |
210 | 419 | except ParseError, e: | 432 | except ParseError as e: |
211 | 420 | self.input = m | 433 | self.input = m |
212 | 421 | return True, self.input.nullError() | 434 | return True, self.input.nullError() |
213 | 422 | else: | 435 | else: |
214 | @@ -429,7 +442,7 @@ | |||
215 | 429 | while True: | 442 | while True: |
216 | 430 | try: | 443 | try: |
217 | 431 | c, e = self.input.head() | 444 | c, e = self.input.head() |
219 | 432 | except EOFError, e: | 445 | except EOFError as e: |
220 | 433 | break | 446 | break |
221 | 434 | t = self.input.tail() | 447 | t = self.input.tail() |
222 | 435 | if c.isspace(): | 448 | if c.isspace(): |
223 | @@ -506,7 +519,7 @@ | |||
224 | 506 | for c in tok: | 519 | for c in tok: |
225 | 507 | v, e = self.exactly(c) | 520 | v, e = self.exactly(c) |
226 | 508 | return tok, e | 521 | return tok, e |
228 | 509 | except ParseError, e: | 522 | except ParseError as e: |
229 | 510 | self.input = m | 523 | self.input = m |
230 | 511 | 524 | ||
231 | 512 | raise ParseError(e[0], expected("token", tok)) | 525 | raise ParseError(e[0], expected("token", tok)) |
232 | @@ -567,7 +580,7 @@ | |||
233 | 567 | while True: | 580 | while True: |
234 | 568 | try: | 581 | try: |
235 | 569 | c, e = self.rule_anything() | 582 | c, e = self.rule_anything() |
237 | 570 | except ParseError, e: | 583 | except ParseError as e: |
238 | 571 | endchar = None | 584 | endchar = None |
239 | 572 | break | 585 | break |
240 | 573 | if c in endChars and len(stack) == 0: | 586 | if c in endChars and len(stack) == 0: |
241 | @@ -579,7 +592,7 @@ | |||
242 | 579 | stack.append(delimiters[c]) | 592 | stack.append(delimiters[c]) |
243 | 580 | elif len(stack) > 0 and c == stack[-1]: | 593 | elif len(stack) > 0 and c == stack[-1]: |
244 | 581 | stack.pop() | 594 | stack.pop() |
246 | 582 | elif c in delimiters.values(): | 595 | elif c in list(delimiters.values()): |
247 | 583 | raise ParseError(self.input.position, expected("Python expression")) | 596 | raise ParseError(self.input.position, expected("Python expression")) |
248 | 584 | elif c in "\"'": | 597 | elif c in "\"'": |
249 | 585 | while True: | 598 | while True: |
250 | 586 | 599 | ||
251 | === modified file 'pymeta/test/test_pymeta.py' | |||
252 | --- pymeta/test/test_pymeta.py 2010-06-11 06:56:34 +0000 | |||
253 | +++ pymeta/test/test_pymeta.py 2013-03-17 18:51:21 +0000 | |||
254 | @@ -94,7 +94,7 @@ | |||
255 | 94 | Input matches can be made on literal integers. | 94 | Input matches can be made on literal integers. |
256 | 95 | """ | 95 | """ |
257 | 96 | g = self.compile("stuff ::= 17 0x1F -2 0177") | 96 | g = self.compile("stuff ::= 17 0x1F -2 0177") |
259 | 97 | self.assertEqual(g.stuff([17, 0x1f, -2, 0177]), 0177) | 97 | self.assertEqual(g.stuff([17, 0x1f, -2, 0o177]), 0o177) |
260 | 98 | self.assertRaises(ParseError, g.stuff, [1, 2, 3]) | 98 | self.assertRaises(ParseError, g.stuff, [1, 2, 3]) |
261 | 99 | 99 | ||
262 | 100 | 100 | ||
263 | @@ -357,7 +357,11 @@ | |||
264 | 357 | | :x ?(isinstance(x, basestring) and x.isdigit()) => int(x)) | 357 | | :x ?(isinstance(x, basestring) and x.isdigit()) => int(x)) |
265 | 358 | """) | 358 | """) |
266 | 359 | self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13) | 359 | self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13) |
268 | 360 | self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13) | 360 | try: |
269 | 361 | self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13) | ||
270 | 362 | except SyntaxError: | ||
271 | 363 | # Python 3.0-3.2 | ||
272 | 364 | pass | ||
273 | 361 | 365 | ||
274 | 362 | 366 | ||
275 | 363 | def test_string(self): | 367 | def test_string(self): |
276 | @@ -462,7 +466,7 @@ | |||
277 | 462 | Input matches can be made on literal integers. | 466 | Input matches can be made on literal integers. |
278 | 463 | """ | 467 | """ |
279 | 464 | g = self.compile("stuff = 17 0x1F -2 0177") | 468 | g = self.compile("stuff = 17 0x1F -2 0177") |
281 | 465 | self.assertEqual(g.stuff([17, 0x1f, -2, 0177]), 0177) | 469 | self.assertEqual(g.stuff([17, 0x1f, -2, 0o177]), 0o177) |
282 | 466 | self.assertRaises(ParseError, g.stuff, [1, 2, 3]) | 470 | self.assertRaises(ParseError, g.stuff, [1, 2, 3]) |
283 | 467 | 471 | ||
284 | 468 | 472 | ||
285 | @@ -708,7 +712,11 @@ | |||
286 | 708 | | :x ?(isinstance(x, basestring) and x.isdigit()) -> int(x)) | 712 | | :x ?(isinstance(x, basestring) and x.isdigit()) -> int(x)) |
287 | 709 | """) | 713 | """) |
288 | 710 | self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13) | 714 | self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13) |
290 | 711 | self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13) | 715 | try: |
291 | 716 | self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13) | ||
292 | 717 | except SyntaxError: | ||
293 | 718 | # Python 3.0-3.2 | ||
294 | 719 | pass | ||
295 | 712 | 720 | ||
296 | 713 | 721 | ||
297 | 714 | def test_string(self): | 722 | def test_string(self): |