Merge lp:~wbond/pymeta/python3 into lp:pymeta

Proposed by Will Bond
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
Reviewer Review Type Date Requested Status
Allen Short Pending
Review via email: mp+153689@code.launchpad.net

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.

Unmerged revisions

67. By Will Bond

Updated code to run on Python 3 also

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'examples/html.py'
--- examples/html.py 2008-04-17 13:38:56 +0000
+++ examples/html.py 2013-03-17 18:51:21 +0000
@@ -29,7 +29,7 @@
29 """29 """
30 Format a dictionary as HTML-ish attributes.30 Format a dictionary as HTML-ish attributes.
31 """31 """
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()])
3333
3434
35unparserGrammar = """35unparserGrammar = """
3636
=== modified file 'examples/terml/parser.py'
--- examples/terml/parser.py 2010-05-13 06:26:31 +0000
+++ examples/terml/parser.py 2013-03-17 18:51:21 +0000
@@ -152,9 +152,14 @@
152 return _Term(functor, argList)152 return _Term(functor, argList)
153153
154def numberType(n):154def numberType(n):
155 try:
156 int_types = (long, int)
157 except NameError:
158 int_types = (int,)
159
155 if isinstance(n, float):160 if isinstance(n, float):
156 return ".float64."161 return ".float64."
157 elif isinstance(n, (long, int)):162 elif isinstance(n, int_types):
158 return ".int."163 return ".int."
159 raise ValueError("wtf")164 raise ValueError("wtf")
160165
@@ -200,6 +205,6 @@
200 205
201 try:206 try:
202 return _parseTerm(termString)207 return _parseTerm(termString)
203 except ParseError, e:208 except ParseError as e:
204 print e.formatError(termString)209 print(e.formatError(termString))
205 raise210 raise
206211
=== modified file 'examples/terml/test_terml.py'
--- examples/terml/test_terml.py 2010-05-13 06:26:31 +0000
+++ examples/terml/test_terml.py 2013-03-17 18:51:21 +0000
@@ -24,7 +24,7 @@
24 self.assertEqual(parse('"foo bar"'), TermLiteral('.String.', "foo bar"))24 self.assertEqual(parse('"foo bar"'), TermLiteral('.String.', "foo bar"))
25 self.assertEqual(parse("'x'"), TermLiteral('.char', character('x')))25 self.assertEqual(parse("'x'"), TermLiteral('.char', character('x')))
26 self.assertEqual(parse("0xDECAFC0FFEEBAD"), TermLiteral('.int.', 0xDECAFC0FFEEBAD))26 self.assertEqual(parse("0xDECAFC0FFEEBAD"), TermLiteral('.int.', 0xDECAFC0FFEEBAD))
27 self.assertEqual(parse("0755"), TermLiteral('.int.', 0755))27 self.assertEqual(parse("0755"), TermLiteral('.int.', 0o755))
28 self.assertEqual(parse("3.14159E17"), TermLiteral('.float64.', 3.14159E17))28 self.assertEqual(parse("3.14159E17"), TermLiteral('.float64.', 3.14159E17))
29 self.assertEqual(parse("1e9"), TermLiteral('.float64.', 1e9))29 self.assertEqual(parse("1e9"), TermLiteral('.float64.', 1e9))
30 self.assertEqual(parse("0"), TermLiteral(".int.", 0))30 self.assertEqual(parse("0"), TermLiteral(".int.", 0))
3131
=== modified file 'pymeta/boot.py'
--- pymeta/boot.py 2010-06-11 06:56:34 +0000
+++ pymeta/boot.py 2013-03-17 18:51:21 +0000
@@ -79,10 +79,11 @@
79 Consume input until a non-whitespace character is reached.79 Consume input until a non-whitespace character is reached.
80 """80 """
81 consumingComment = False81 consumingComment = False
82 e = None
82 while True:83 while True:
83 try:84 try:
84 c, e = self.input.head()85 c, e = self.input.head()
85 except EOFError, e:86 except EOFError:
86 break87 break
87 t = self.input.tail()88 t = self.input.tail()
88 if c.isspace() or consumingComment:89 if c.isspace() or consumingComment:
8990
=== modified file 'pymeta/grammar.py'
--- pymeta/grammar.py 2010-06-06 16:46:35 +0000
+++ pymeta/grammar.py 2013-03-17 18:51:21 +0000
@@ -4,9 +4,9 @@
4definitions.4definitions.
5"""5"""
6import string6import string
7from builder import TreeBuilder, moduleFromGrammar7from .builder import TreeBuilder, moduleFromGrammar
8from boot import BootOMetaGrammar8from .boot import BootOMetaGrammar
9from runtime import OMetaBase, ParseError, EOFError9from .runtime import OMetaBase, ParseError, EOFError
1010
11class OMeta(OMetaBase):11class OMeta(OMetaBase):
12 """12 """
@@ -264,10 +264,11 @@
264 Consume input until a non-whitespace character is reached.264 Consume input until a non-whitespace character is reached.
265 """265 """
266 consumingComment = False266 consumingComment = False
267 e = None
267 while True:268 while True:
268 try:269 try:
269 c, e = self.input.head()270 c, e = self.input.head()
270 except EOFError, e:271 except EOFError:
271 break272 break
272 t = self.input.tail()273 t = self.input.tail()
273 if c.isspace() or consumingComment:274 if c.isspace() or consumingComment:
274275
=== modified file 'pymeta/runtime.py'
--- pymeta/runtime.py 2010-06-11 06:56:34 +0000
+++ pymeta/runtime.py 2013-03-17 18:51:21 +0000
@@ -21,6 +21,9 @@
21 if len(a) > 2:21 if len(a) > 2:
22 self.message = a[2]22 self.message = a[2]
2323
24 def __getitem__(self, item):
25 return self.args[item]
26
24 def __eq__(self, other):27 def __eq__(self, other):
25 if other.__class__ == self.__class__:28 if other.__class__ == self.__class__:
26 return (self.position, self.error) == (other.position, other.error)29 return (self.position, self.error) == (other.position, other.error)
@@ -91,7 +94,12 @@
91 """94 """
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.
93 """96 """
94 errors.sort(reverse=True, key=operator.itemgetter(0))97 def get_key(item):
98 val = item[0]
99 if val == None:
100 val = -1000000000
101 return val
102 errors.sort(reverse=True, key=get_key)
95 results = set()103 results = set()
96 pos = errors[0][0]104 pos = errors[0][0]
97 for err in errors:105 for err in errors:
@@ -118,16 +126,20 @@
118 """126 """
119 raise TypeError("Characters are not iterable")127 raise TypeError("Characters are not iterable")
120128
121class unicodeCharacter(unicode):129try:
122 """130 _has_unicode = True
123 Type to distinguish characters from Unicode strings.131 class unicodeCharacter(unicode):
124 """132 """
125 def __iter__(self):133 Type to distinguish characters from Unicode strings.
126 """134 """
127 Prevent string patterns and list patterns from matching single135 def __iter__(self):
128 characters.136 """
129 """137 Prevent string patterns and list patterns from matching single
130 raise TypeError("Characters are not iterable")138 characters.
139 """
140 raise TypeError("Characters are not iterable")
141except NameError:
142 _has_unicode = False
131143
132class InputStream(object):144class InputStream(object):
133 """145 """
@@ -140,7 +152,7 @@
140 """152 """
141 if isinstance(iterable, str):153 if isinstance(iterable, str):
142 data = [character(c) for c in iterable]154 data = [character(c) for c in iterable]
143 elif isinstance(iterable, unicode):155 elif _has_unicode and isinstance(iterable, unicode):
144 data = [unicodeCharacter(c) for c in iterable]156 data = [unicodeCharacter(c) for c in iterable]
145 else:157 else:
146 data = list(iterable)158 data = list(iterable)
@@ -299,7 +311,7 @@
299 @param args: A sequence of arguments to it.311 @param args: A sequence of arguments to it.
300 """312 """
301 if args:313 if args:
302 if rule.func_code.co_argcount - 1 != len(args):314 if rule.__code__.co_argcount - 1 != len(args):
303 for arg in args[::-1]:315 for arg in args[::-1]:
304 self.input = ArgInput(arg, self.input)316 self.input = ArgInput(arg, self.input)
305 return rule()317 return rule()
@@ -375,6 +387,7 @@
375 @param initial: Initial values to populate the returned list with.387 @param initial: Initial values to populate the returned list with.
376 """388 """
377 ans = []389 ans = []
390 e = None
378 for x, e in initial:391 for x, e in initial:
379 ans.append(x)392 ans.append(x)
380 while True:393 while True:
@@ -382,7 +395,7 @@
382 m = self.input395 m = self.input
383 v, _ = fn()396 v, _ = fn()
384 ans.append(v)397 ans.append(v)
385 except ParseError, e:398 except ParseError:
386 self.input = m399 self.input = m
387 break400 break
388 return ans, e401 return ans, e
@@ -401,7 +414,7 @@
401 ret, err = f()414 ret, err = f()
402 errors.append(err)415 errors.append(err)
403 return ret, joinErrors(errors)416 return ret, joinErrors(errors)
404 except ParseError, e:417 except ParseError as e:
405 errors.append(e)418 errors.append(e)
406 self.input = m419 self.input = m
407 raise ParseError(*joinErrors(errors))420 raise ParseError(*joinErrors(errors))
@@ -416,7 +429,7 @@
416 m = self.input429 m = self.input
417 try:430 try:
418 fn()431 fn()
419 except ParseError, e:432 except ParseError as e:
420 self.input = m433 self.input = m
421 return True, self.input.nullError()434 return True, self.input.nullError()
422 else:435 else:
@@ -429,7 +442,7 @@
429 while True:442 while True:
430 try:443 try:
431 c, e = self.input.head()444 c, e = self.input.head()
432 except EOFError, e:445 except EOFError as e:
433 break446 break
434 t = self.input.tail()447 t = self.input.tail()
435 if c.isspace():448 if c.isspace():
@@ -506,7 +519,7 @@
506 for c in tok:519 for c in tok:
507 v, e = self.exactly(c)520 v, e = self.exactly(c)
508 return tok, e521 return tok, e
509 except ParseError, e:522 except ParseError as e:
510 self.input = m523 self.input = m
511 524
512 raise ParseError(e[0], expected("token", tok))525 raise ParseError(e[0], expected("token", tok))
@@ -567,7 +580,7 @@
567 while True:580 while True:
568 try:581 try:
569 c, e = self.rule_anything()582 c, e = self.rule_anything()
570 except ParseError, e:583 except ParseError as e:
571 endchar = None584 endchar = None
572 break585 break
573 if c in endChars and len(stack) == 0:586 if c in endChars and len(stack) == 0:
@@ -579,7 +592,7 @@
579 stack.append(delimiters[c])592 stack.append(delimiters[c])
580 elif len(stack) > 0 and c == stack[-1]:593 elif len(stack) > 0 and c == stack[-1]:
581 stack.pop()594 stack.pop()
582 elif c in delimiters.values():595 elif c in list(delimiters.values()):
583 raise ParseError(self.input.position, expected("Python expression"))596 raise ParseError(self.input.position, expected("Python expression"))
584 elif c in "\"'":597 elif c in "\"'":
585 while True:598 while True:
586599
=== modified file 'pymeta/test/test_pymeta.py'
--- pymeta/test/test_pymeta.py 2010-06-11 06:56:34 +0000
+++ pymeta/test/test_pymeta.py 2013-03-17 18:51:21 +0000
@@ -94,7 +94,7 @@
94 Input matches can be made on literal integers.94 Input matches can be made on literal integers.
95 """95 """
96 g = self.compile("stuff ::= 17 0x1F -2 0177")96 g = self.compile("stuff ::= 17 0x1F -2 0177")
97 self.assertEqual(g.stuff([17, 0x1f, -2, 0177]), 0177)97 self.assertEqual(g.stuff([17, 0x1f, -2, 0o177]), 0o177)
98 self.assertRaises(ParseError, g.stuff, [1, 2, 3])98 self.assertRaises(ParseError, g.stuff, [1, 2, 3])
9999
100100
@@ -357,7 +357,11 @@
357 | :x ?(isinstance(x, basestring) and x.isdigit()) => int(x))357 | :x ?(isinstance(x, basestring) and x.isdigit()) => int(x))
358 """)358 """)
359 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)359 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)
360 self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13)360 try:
361 self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13)
362 except SyntaxError:
363 # Python 3.0-3.2
364 pass
361365
362366
363 def test_string(self):367 def test_string(self):
@@ -462,7 +466,7 @@
462 Input matches can be made on literal integers.466 Input matches can be made on literal integers.
463 """467 """
464 g = self.compile("stuff = 17 0x1F -2 0177")468 g = self.compile("stuff = 17 0x1F -2 0177")
465 self.assertEqual(g.stuff([17, 0x1f, -2, 0177]), 0177)469 self.assertEqual(g.stuff([17, 0x1f, -2, 0o177]), 0o177)
466 self.assertRaises(ParseError, g.stuff, [1, 2, 3])470 self.assertRaises(ParseError, g.stuff, [1, 2, 3])
467471
468472
@@ -708,7 +712,11 @@
708 | :x ?(isinstance(x, basestring) and x.isdigit()) -> int(x))712 | :x ?(isinstance(x, basestring) and x.isdigit()) -> int(x))
709 """)713 """)
710 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)714 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)
711 self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13)715 try:
716 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)
717 except SyntaxError:
718 # Python 3.0-3.2
719 pass
712720
713721
714 def test_string(self):722 def test_string(self):

Subscribers

People subscribed via source and target branches

to all changes: