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
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 """
6 Format a dictionary as HTML-ish attributes.
7 """
8- return ''.join([" %s='%s'" % (k, v) for (k, v) in attrs.iteritems()])
9+ return ''.join([" %s='%s'" % (k, v) for (k, v) in attrs.items()])
10
11
12 unparserGrammar = """
13
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 return _Term(functor, argList)
19
20 def numberType(n):
21+ try:
22+ int_types = (long, int)
23+ except NameError:
24+ int_types = (int,)
25+
26 if isinstance(n, float):
27 return ".float64."
28- elif isinstance(n, (long, int)):
29+ elif isinstance(n, int_types):
30 return ".int."
31 raise ValueError("wtf")
32
33@@ -200,6 +205,6 @@
34
35 try:
36 return _parseTerm(termString)
37- except ParseError, e:
38- print e.formatError(termString)
39+ except ParseError as e:
40+ print(e.formatError(termString))
41 raise
42
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 self.assertEqual(parse('"foo bar"'), TermLiteral('.String.', "foo bar"))
48 self.assertEqual(parse("'x'"), TermLiteral('.char', character('x')))
49 self.assertEqual(parse("0xDECAFC0FFEEBAD"), TermLiteral('.int.', 0xDECAFC0FFEEBAD))
50- self.assertEqual(parse("0755"), TermLiteral('.int.', 0755))
51+ self.assertEqual(parse("0755"), TermLiteral('.int.', 0o755))
52 self.assertEqual(parse("3.14159E17"), TermLiteral('.float64.', 3.14159E17))
53 self.assertEqual(parse("1e9"), TermLiteral('.float64.', 1e9))
54 self.assertEqual(parse("0"), TermLiteral(".int.", 0))
55
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 Consume input until a non-whitespace character is reached.
61 """
62 consumingComment = False
63+ e = None
64 while True:
65 try:
66 c, e = self.input.head()
67- except EOFError, e:
68+ except EOFError:
69 break
70 t = self.input.tail()
71 if c.isspace() or consumingComment:
72
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 definitions.
78 """
79 import string
80-from builder import TreeBuilder, moduleFromGrammar
81-from boot import BootOMetaGrammar
82-from runtime import OMetaBase, ParseError, EOFError
83+from .builder import TreeBuilder, moduleFromGrammar
84+from .boot import BootOMetaGrammar
85+from .runtime import OMetaBase, ParseError, EOFError
86
87 class OMeta(OMetaBase):
88 """
89@@ -264,10 +264,11 @@
90 Consume input until a non-whitespace character is reached.
91 """
92 consumingComment = False
93+ e = None
94 while True:
95 try:
96 c, e = self.input.head()
97- except EOFError, e:
98+ except EOFError:
99 break
100 t = self.input.tail()
101 if c.isspace() or consumingComment:
102
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 if len(a) > 2:
108 self.message = a[2]
109
110+ def __getitem__(self, item):
111+ return self.args[item]
112+
113 def __eq__(self, other):
114 if other.__class__ == self.__class__:
115 return (self.position, self.error) == (other.position, other.error)
116@@ -91,7 +94,12 @@
117 """
118 Return the error from the branch that matched the most of the input.
119 """
120- errors.sort(reverse=True, key=operator.itemgetter(0))
121+ def get_key(item):
122+ val = item[0]
123+ if val == None:
124+ val = -1000000000
125+ return val
126+ errors.sort(reverse=True, key=get_key)
127 results = set()
128 pos = errors[0][0]
129 for err in errors:
130@@ -118,16 +126,20 @@
131 """
132 raise TypeError("Characters are not iterable")
133
134-class unicodeCharacter(unicode):
135- """
136- Type to distinguish characters from Unicode strings.
137- """
138- def __iter__(self):
139- """
140- Prevent string patterns and list patterns from matching single
141- characters.
142- """
143- raise TypeError("Characters are not iterable")
144+try:
145+ _has_unicode = True
146+ class unicodeCharacter(unicode):
147+ """
148+ Type to distinguish characters from Unicode strings.
149+ """
150+ def __iter__(self):
151+ """
152+ Prevent string patterns and list patterns from matching single
153+ characters.
154+ """
155+ raise TypeError("Characters are not iterable")
156+except NameError:
157+ _has_unicode = False
158
159 class InputStream(object):
160 """
161@@ -140,7 +152,7 @@
162 """
163 if isinstance(iterable, str):
164 data = [character(c) for c in iterable]
165- elif isinstance(iterable, unicode):
166+ elif _has_unicode and isinstance(iterable, unicode):
167 data = [unicodeCharacter(c) for c in iterable]
168 else:
169 data = list(iterable)
170@@ -299,7 +311,7 @@
171 @param args: A sequence of arguments to it.
172 """
173 if args:
174- if rule.func_code.co_argcount - 1 != len(args):
175+ if rule.__code__.co_argcount - 1 != len(args):
176 for arg in args[::-1]:
177 self.input = ArgInput(arg, self.input)
178 return rule()
179@@ -375,6 +387,7 @@
180 @param initial: Initial values to populate the returned list with.
181 """
182 ans = []
183+ e = None
184 for x, e in initial:
185 ans.append(x)
186 while True:
187@@ -382,7 +395,7 @@
188 m = self.input
189 v, _ = fn()
190 ans.append(v)
191- except ParseError, e:
192+ except ParseError:
193 self.input = m
194 break
195 return ans, e
196@@ -401,7 +414,7 @@
197 ret, err = f()
198 errors.append(err)
199 return ret, joinErrors(errors)
200- except ParseError, e:
201+ except ParseError as e:
202 errors.append(e)
203 self.input = m
204 raise ParseError(*joinErrors(errors))
205@@ -416,7 +429,7 @@
206 m = self.input
207 try:
208 fn()
209- except ParseError, e:
210+ except ParseError as e:
211 self.input = m
212 return True, self.input.nullError()
213 else:
214@@ -429,7 +442,7 @@
215 while True:
216 try:
217 c, e = self.input.head()
218- except EOFError, e:
219+ except EOFError as e:
220 break
221 t = self.input.tail()
222 if c.isspace():
223@@ -506,7 +519,7 @@
224 for c in tok:
225 v, e = self.exactly(c)
226 return tok, e
227- except ParseError, e:
228+ except ParseError as e:
229 self.input = m
230
231 raise ParseError(e[0], expected("token", tok))
232@@ -567,7 +580,7 @@
233 while True:
234 try:
235 c, e = self.rule_anything()
236- except ParseError, e:
237+ except ParseError as e:
238 endchar = None
239 break
240 if c in endChars and len(stack) == 0:
241@@ -579,7 +592,7 @@
242 stack.append(delimiters[c])
243 elif len(stack) > 0 and c == stack[-1]:
244 stack.pop()
245- elif c in delimiters.values():
246+ elif c in list(delimiters.values()):
247 raise ParseError(self.input.position, expected("Python expression"))
248 elif c in "\"'":
249 while True:
250
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 Input matches can be made on literal integers.
256 """
257 g = self.compile("stuff ::= 17 0x1F -2 0177")
258- self.assertEqual(g.stuff([17, 0x1f, -2, 0177]), 0177)
259+ self.assertEqual(g.stuff([17, 0x1f, -2, 0o177]), 0o177)
260 self.assertRaises(ParseError, g.stuff, [1, 2, 3])
261
262
263@@ -357,7 +357,11 @@
264 | :x ?(isinstance(x, basestring) and x.isdigit()) => int(x))
265 """)
266 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)
267- self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13)
268+ try:
269+ self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13)
270+ except SyntaxError:
271+ # Python 3.0-3.2
272+ pass
273
274
275 def test_string(self):
276@@ -462,7 +466,7 @@
277 Input matches can be made on literal integers.
278 """
279 g = self.compile("stuff = 17 0x1F -2 0177")
280- self.assertEqual(g.stuff([17, 0x1f, -2, 0177]), 0177)
281+ self.assertEqual(g.stuff([17, 0x1f, -2, 0o177]), 0o177)
282 self.assertRaises(ParseError, g.stuff, [1, 2, 3])
283
284
285@@ -708,7 +712,11 @@
286 | :x ?(isinstance(x, basestring) and x.isdigit()) -> int(x))
287 """)
288 self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)
289- self.assertEqual(g.interp([[u'3', u'+', [u'5', u'*', u'2']]]), 13)
290+ try:
291+ self.assertEqual(g.interp([['3', '+', ['5', '*', '2']]]), 13)
292+ except SyntaxError:
293+ # Python 3.0-3.2
294+ pass
295
296
297 def test_string(self):

Subscribers

People subscribed via source and target branches

to all changes: