Skip to content

Add Python 3 support #1

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions command_seq_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
def items_of_interest(dct, types_of_interest):
return [(k, v) for (k, v) in dct.items() if isinstance(v, types_of_interest)]

def recordingexec(arg, types_of_interest = (int, basestring)):
def recordingexec(arg, types_of_interest = (int, str)):
cmdbuf = []
lastcmd = ''
commandInProgress = []
Expand All @@ -41,27 +41,27 @@ def recordingexec(arg, types_of_interest = (int, basestring)):
commandInProgress = []
except IndentationError:
pass
except SyntaxError, e:
except SyntaxError as e:
if not commandUnfinished.search(str(e)):
raise
except Exception, e:
raise Exception, '%s:\n%s' % (e, line)
except Exception as e:
raise Exception(f'{e}:\n{line}')
return (lastcmd, dict(lastlocals))

def last_assignment_or_evaluatable(s, types_of_interest=(basestring, int, float)):
def last_assignment_or_evaluatable(s, types_of_interest=(str, int, float)):
s = s.strip()
if not s:
return None
exec(s)
(lastcmd, lastlocals) = recordingexec(s, types_of_interest)
try:
return eval(lastcmd)
except SyntaxError, e:
except SyntaxError as e:
if 'invalid syntax' not in str(e):
raise
if len(lastlocals) > 1:
raise ValueError, "Multiple assignments - couldn't determine which one"
return eval(lastlocals.keys()[0])
raise ValueError("Multiple assignments - couldn't determine which one")
return eval(next(lastlocals.keys()))

class RecordingExecTestSuite(unittest.TestCase):
def testSimpleAssignments(self):
Expand All @@ -78,7 +78,7 @@ def testIgnoreObj(self):
uts = unittest.TestSuite()
''', (unittest.TestSuite))
self.assertEqual(lastcmd, 'uts = unittest.TestSuite()')
self.assertEqual(lastlocals.keys(), ['uts'])
self.assertEqual(list(lastlocals.keys()), ['uts'])
def testExecutableLastLine(self):
(lastcmd, lastlocals) = recordingexec('''
a = 1
Expand Down Expand Up @@ -116,4 +116,4 @@ def testEmpty(self):

if __name__ == '__main__':
#unittest.main()
doctest.testmod()
doctest.testmod()
18 changes: 9 additions & 9 deletions pyparsing_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

By Catherine Devlin (http://catherinedevlin.blogspot.com)
'''
from Tkinter import *
from tkinter import *
from command_seq_reader import last_assignment_or_evaluatable
import pyparsing
import time, threading, functools, string, optparse, sys
Expand All @@ -25,14 +25,14 @@
def _eq_monkeypatch(self, other):
if isinstance(other, pyparsing.ParserElement):
return self.__dict__ == other.__dict__
elif isinstance(other, basestring):
elif isinstance(other, str):
try:
(self + StringEnd()).parseString(_ustr(other))
return True
except ParseBaseException:
except pyparsing.ParseBaseException:
return False
else:
return super(ParserElement,self)==other
return super(pyparsing.ParserElement, self) == other

pyparsing.ParserElement.__eq__ = _eq_monkeypatch

Expand Down Expand Up @@ -79,8 +79,8 @@ def apply_grammar(self, i):
result = self.grammar.transformString(target)
else:
result = self.grammar.parseString(target).dump()
except Exception, e:
result = '%s\n%s' % (str(e.__class__), str(e))
except Exception as e:
result = f'{e.__class__}\n{e}'
self.set_result(i, result)

def set_result(self, i, txt):
Expand All @@ -97,14 +97,14 @@ def set_all_results(self, txt):
def reparse(self, event=None):
self.grammar = self.grammar_text.get(1.0, END).strip()
if self.grammar:
self.grammar = '%s\n%s' % (self.import_type.get(), self.grammar)
self.grammar = f'{self.import_type.get()}\n{self.grammar}'
try:
self.grammar = last_assignment_or_evaluatable(self.grammar, types_of_interest=(pyparsing.ParserElement))
for i in range(self.num_targets):
self.apply_grammar(i)
except Exception, e:
except Exception as e:
if hasattr(e, 'text'):
errtxt = '%s\n\n%s' % (str(e), e.text)
errtxt = f'{e}\n\n{e.text}'
else:
errtxt = str(e)
self.set_all_results(errtxt)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools>=46.1.0", "wheel"]
build-backend = "setuptools.build_meta"