Rev 153: Make the parser a little bit stricter, correct an example case in http://bazaar.launchpad.net/~jameinel/meliae/non-json-loader

John Arbash Meinel john at arbash-meinel.com
Mon Jul 12 22:53:57 BST 2010


At http://bazaar.launchpad.net/~jameinel/meliae/non-json-loader

------------------------------------------------------------
revno: 153
revision-id: john at arbash-meinel.com-20100712215349-atbtjq133brkrlqe
parent: john at arbash-meinel.com-20100712202804-ob2al42yairkx80q
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: non-json-loader
timestamp: Mon 2010-07-12 16:53:49 -0500
message:
  Make the parser a little bit stricter, correct an example case
  (size comes before name), and add a test for the pure regex loader.
-------------- next part --------------
=== modified file 'meliae/loader.py'
--- a/meliae/loader.py	2010-05-20 15:58:12 +0000
+++ b/meliae/loader.py	2010-07-12 21:53:49 +0000
@@ -42,14 +42,15 @@
     timer = time.clock
 
 # This is the minimal regex that is guaranteed to match. In testing, it is
-# about 3x faster than using simplejson, it is just less generic.
+# faster than simplejson without extensions, though slower than simplejson w/
+# extensions.
 _object_re = re.compile(
     r'\{"address": (?P<address>\d+)'
-    r', "type": "(?P<type>.*)"'
+    r', "type": "(?P<type>[^"]*)"'
     r', "size": (?P<size>\d+)'
     r'(, "name": "(?P<name>.*)")?'
     r'(, "len": (?P<len>\d+))?'
-    r'(, "value": "?(?P<value>.*?)"?)?'
+    r'(, "value": "?(?P<value>[^"]*)"?)?'
     r', "refs": \[(?P<refs>[^]]*)\]'
     r'\}')
 
@@ -91,6 +92,11 @@
     if length is not None:
         length = int(length)
     refs = [int(val) for val in _refs_re.findall(refs)]
+    if value is not None:
+        try:
+            value = int(value)
+        except ValueError:
+            pass
     obj = cls(address=int(address),
               type_str=type_str,
               size=int(size),

=== modified file 'meliae/tests/test_loader.py'
--- a/meliae/tests/test_loader.py	2010-06-30 22:06:27 +0000
+++ b/meliae/tests/test_loader.py	2010-07-12 21:53:49 +0000
@@ -46,7 +46,7 @@
 '{"address": 7, "type": "tuple", "size": 20, "len": 2, "refs": [4, 5]}',
 '{"address": 6, "type": "str", "size": 29, "len": 5, "value": "a str"'
  ', "refs": []}',
-'{"address": 8, "type": "module", "name": "mymod", "size": 60, "refs": [2]}',
+'{"address": 8, "type": "module", "size": 60, "name": "mymod", "refs": [2]}',
 ]
 
 # Note that this doesn't have a complete copy of the references. Namely when
@@ -116,6 +116,25 @@
         # the objs dictionary.
         self.assertTrue(keys[0] is obj.address)
 
+    def test_load_without_simplejson(self):
+        objs = loader.load([
+            '{"address": 1234, "type": "int", "size": 12, "value": 10'
+                ', "refs": []}',
+            '{"address": 2345, "type": "module", "size": 60, "name": "mymod"'
+                ', "refs": [1234]}',
+            ], using_json=False, show_prog=False).objs
+        keys = sorted(objs.keys())
+        self.assertEqual([1234, 2345], keys)
+        obj = objs[1234]
+        self.assertTrue(isinstance(obj, _loader._MemObjectProxy))
+        # The address should be exactly the same python object as the key in
+        # the objs dictionary.
+        self.assertTrue(keys[0] is obj.address)
+        self.assertEqual(10, obj.value)
+        obj = objs[2345]
+        self.assertEqual("module", obj.type_str)
+        self.assertEqual("mymod", obj.value)
+
     def test_load_example(self):
         objs = loader.load(_example_dump, show_prog=False)
 



More information about the bazaar-commits mailing list