diff options
author | David Malcolm <dmalcolm@redhat.com> | 2009-11-19 15:28:47 -0500 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2009-11-19 15:28:47 -0500 |
commit | 4fa4447a66af6d6dc165e4a90b90d9fcce6e4689 (patch) | |
tree | 02bcab87027f193ce0e6bd9f3df15fdf6c848a84 | |
parent | 3a78d17118fc648bfcb04f3f9e2c951621934b8f (diff) | |
download | 2to3c-4fa4447a66af6d6dc165e4a90b90d9fcce6e4689.tar.gz 2to3c-4fa4447a66af6d6dc165e4a90b90d9fcce6e4689.tar.xz 2to3c-4fa4447a66af6d6dc165e4a90b90d9fcce6e4689.zip |
Hack up typeobject initializers
-rw-r--r-- | typeobject.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/typeobject.py b/typeobject.py new file mode 100644 index 0000000..17dfafd --- /dev/null +++ b/typeobject.py @@ -0,0 +1,56 @@ +import sys +import re +from difflib import unified_diff + +# Whitespace patterns: +req_ws = r'\s+' +opt_ws = r'\s*' +c_identifier = r'([_A-Za-z][_0-9A-Za-z]*)' + +pat = (r'PyTypeObject' + req_ws + + c_identifier + opt_ws + + r'=' + opt_ws + r'\{' + opt_ws + + r'(PyObject_HEAD_INIT\((.*)\)' + opt_ws + + r'([0-9]+)' + opt_ws + r',)' + ) + +def fixup_typeobject_initializers(content): + while True: + m = re.match(pat, content) + if m: + if True: + print m.groups() + print m.group(2) + print m.group(3) + print m.group(4) + print m.span(2) + content = (content[:m.start(2)] + + 'PyVarObject_HEAD_INIT(%s, %s)' % (m.group(3),m.group(4)) + + content[m.end(2):]) + else: + return content + +import unittest +class TestFixups(unittest.TestCase): + def test_fixups(self): + self.assertEquals(fixup_typeobject_initializers('''PyTypeObject DBusPyIntBase_Type = { + PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type)) + 0, + "_dbus_bindings._IntBase", +''' + ), + '''PyTypeObject DBusPyIntBase_Type = { + PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) + "_dbus_bindings._IntBase", +''' + ) + +if __name__ == '__main__': + if len(sys.argv) == 1: + unittest.main() + else: + for filename in sys.argv[1:]: + content = open(filename, 'r').read() + fixed_content = fixup_typeobject_initializers(content) + for line in unified_diff(content.splitlines(), fixed_content.splitlines(), lineterm=''): + print line |