[rtems-tools commit] tester/gdb: mi_parser, fix on Python3 and remove globals

Chris Johns chrisj at rtems.org
Tue Nov 27 02:55:44 UTC 2018


Module:    rtems-tools
Branch:    master
Commit:    71cede05ae2cdd2f5162ea7aea9ee0d50452f04f
Changeset: http://git.rtems.org/rtems-tools/commit/?id=71cede05ae2cdd2f5162ea7aea9ee0d50452f04f

Author:    Chris Johns <chrisj at rtems.org>
Date:      Mon Nov 26 09:56:50 2018 +1100

tester/gdb: mi_parser, fix on Python3 and remove globals

- Fix mi_parser on Python3. Python3 does not support the __cmp__
  operator and rich comparision operators are required, See PEP 207.
- Remove the global variables and make a class containing them. Update
  the gdb class to use the mi_parser session class. Removing the globals
  means the global lock in the gdb module can be removed.

---

 tester/rt/gdb.py             | 39 +++++++------------------
 tester/rt/pygdb/__init__.py  |  3 +-
 tester/rt/pygdb/mi_parser.py | 69 +++++++++++++++++++++++++++++++-------------
 3 files changed, 61 insertions(+), 50 deletions(-)

diff --git a/tester/rt/gdb.py b/tester/rt/gdb.py
index 2416a50..15a3862 100644
--- a/tester/rt/gdb.py
+++ b/tester/rt/gdb.py
@@ -51,15 +51,11 @@ from rtemstoolkit import path
 import console
 import pygdb
 
-#
-# The MI parser needs a global lock. It has global objects.
-#
-mi_lock = threading.Lock()
-
 class gdb(object):
     '''RTEMS Testing GDB base.'''
 
     def __init__(self, bsp_arch, bsp, trace = False, mi_trace = False):
+        self.session = pygdb.mi_parser.session()
         self.trace = trace
         self.mi_trace = mi_trace
         self.lock_trace = False
@@ -90,12 +86,6 @@ class gdb(object):
             print('|] UNLOCK:%s [|' % (msg))
         self.lock.release()
 
-    def _mi_lock(self):
-        mi_lock.acquire()
-
-    def _mi_unlock(self):
-        mi_lock.release()
-
     def _put(self, text):
         if self.trace:
             print(')))', text)
@@ -208,15 +198,12 @@ class gdb(object):
                                            cleanup = self._cleanup)
         finally:
             self._unlock('_open')
-        try:
-            self.gdb_console('gdb: %s' % (' '.join(cmds)))
-            ec, proc = self.process.open(cmds, timeout = (timeout, self._timeout))
-            if self.trace:
-                print('gdb done', ec)
-            if ec > 0:
-                raise error.general('gdb exec: %s: %s' % (cmds[0], os.strerror(ec)))
-        except:
-            raise
+        self.gdb_console('gdb: %s' % (' '.join(cmds)))
+        ec, proc = self.process.open(cmds, timeout = (timeout, self._timeout))
+        if self.trace:
+            print('gdb done', ec)
+        if ec > 0:
+            raise error.general('gdb exec: %s: %s' % (cmds[0], os.strerror(ec)))
         self._lock('_open')
         try:
             self.process = None
@@ -248,13 +235,9 @@ class gdb(object):
 
     def gdb_parse(self, lines):
         try:
-            self._mi_lock()
-            try:
-                if self.mi_trace:
-                    print('mi-data:', lines)
-                rec = pygdb.mi_parser.process(lines)
-            finally:
-                self._mi_unlock()
+            if self.mi_trace:
+                print('mi-data:', lines)
+            rec = self.session.process(lines)
             if self.mi_trace:
                 print('mi-rec:', rec)
             if rec.record_type == 'result':
@@ -302,7 +285,7 @@ class gdb(object):
                         self.output_buffer = self.output_buffer[last_lf + 1:]
         except:
             if self.trace:
-                print('/// console output')
+                print('/// exception: console output')
             for line in lines.splitlines():
                 self.output(line)
 
diff --git a/tester/rt/pygdb/__init__.py b/tester/rt/pygdb/__init__.py
index 00b3364..e500077 100644
--- a/tester/rt/pygdb/__init__.py
+++ b/tester/rt/pygdb/__init__.py
@@ -18,5 +18,4 @@
 
 all = ['mi_parser']
 from . import mi_parser
-scan = mi_parser.scan
-process = mi_parser.process
+session = mi_parser.session
diff --git a/tester/rt/pygdb/mi_parser.py b/tester/rt/pygdb/mi_parser.py
index dd1d08f..339587e 100755
--- a/tester/rt/pygdb/mi_parser.py
+++ b/tester/rt/pygdb/mi_parser.py
@@ -35,28 +35,48 @@ import pprint
 
 from . import spark
 
-def __private():
-	class Token:
+def _private():
+	class Token(object):
 		def __init__(self, type, value=None):
 			self.type = type
 			self.value = value
-		def __cmp__(self, o):
-			return cmp(self.type, o)
+		def __lt__(self, o):
+			return self.type < o
+		def __gt__(self, o):
+			return self.type > o
+		def __le__(self, o):
+			return self.type <= o
+		def __ge__(self, o):
+			return self.type >= o
+		def __eq__(self, o):
+			return self.type == o
+		def __ne__(self, o):
+			return self.type != o
 		def __repr__(self):
 			return self.value or self.type
 
-	class AST:
+	class AST(object):
 		def __init__(self, type):
 			self.type = type
 			self._kids = []
 		def __getitem__(self, i):
 			return self._kids[i]
+		def __setitem__(self, i, k):
+			self._kids[i] = k
 		def __len__(self):
 			return len(self._kids)
-		def __setslice__(self, low, high, seq):
-			self._kids[low:high] = seq
-		def __cmp__(self, o):
-			return cmp(self.type, o)
+		def __lt__(self, o):
+			return self.type < o
+		def __gt__(self, o):
+			return self.type > o
+		def __le__(self, o):
+			return self.type <= o
+		def __ge__(self, o):
+			return self.type >= o
+		def __eq__(self, o):
+			return self.type == o
+		def __ne__(self, o):
+			return self.type != o
 
 	class GdbMiScannerBase(spark.GenericScanner):
 		def tokenize(self, input):
@@ -300,7 +320,7 @@ def __private():
 		#def default(self, node):
 			#print 'default: ' + node.type
 
-	class GdbDynamicObject:
+	class GdbDynamicObject(object):
 		def __init__(self, dict_):
 			self.graft(dict_)
 
@@ -355,20 +375,29 @@ def __private():
 
 	return (GdbMiScanner(), GdbMiParser(), GdbMiInterpreter, GdbMiRecord)
 
-(__the_scanner, __the_parser, __the_interpreter, __the_output) = __private()
 
-def scan(input):
-	return __the_scanner.tokenize(input)
+class session(object):
+        def __init__(self):
+                (self.the_scanner,
+                 self.the_parser,
+                 self.the_interpreter,
+                 self.the_output) = _private()
 
-def parse(tokens):
-	return __the_parser.parse(tokens)
+        def scan(self, input):
+	        return self.the_scanner.tokenize(input)
 
-def process(input):
-	tokens = scan(input)
-	ast = parse(tokens)
-	__the_interpreter(ast)
-	return __the_output(ast.value)
+        def parse(self, tokens):
+	        return self.the_parser.parse(tokens)
 
+        def process(self, input):
+	        tokens = self.scan(input)
+	        ast = self.parse(tokens)
+	        self.the_interpreter(ast)
+	        return self.the_output(ast.value)
+
+#
+# Not updated with the session class
+#
 if __name__ == '__main__':
 	def main():
 		def print_tokens(tokens):




More information about the vc mailing list