summaryrefslogtreecommitdiffstats
path: root/lib2to3c/typeobject.py
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2010-03-05 19:06:29 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2010-03-05 19:06:29 -0500
commite4c495a8b6d9727de99b35bddf70f2c942e7f8b9 (patch)
tree0b41afa08d135b80f55195eb9cefb4a8bd643fa7 /lib2to3c/typeobject.py
parent234a10d5bc133f9963db58fd4c08715fe43c2b94 (diff)
download2to3c-master.tar.gz
2to3c-master.tar.xz
2to3c-master.zip
Rename "fixes" -> "lib2to3c"; start writing a setup.pyHEADmaster
Diffstat (limited to 'lib2to3c/typeobject.py')
-rw-r--r--lib2to3c/typeobject.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib2to3c/typeobject.py b/lib2to3c/typeobject.py
new file mode 100644
index 0000000..56a9744
--- /dev/null
+++ b/lib2to3c/typeobject.py
@@ -0,0 +1,77 @@
+import sys
+import re
+
+# I wasn't able to express this refactoring in SmPLs; the implied comma embedded
+# in the _HEAD_INIT macros seems to be too much for spatch to reasonably deal
+# with.
+
+# So I expressed this one as a regex.
+
+# 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, re.DOTALL)
+ if m:
+ if False:
+ 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
+
+from lib2to3c import Fix, FixTest
+class FixTypeobjectInitializers(Fix):
+ def transform(self, string):
+ return fixup_typeobject_initializers(string)
+
+import unittest
+class TestFixups(FixTest):
+ def setUp(self):
+ self.fixer = FixTypeobjectInitializers()
+
+ def test_fixups(self):
+ self.assertTransformsTo(self.fixer,
+ '''
+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",
+''')
+
+ def test_fixup_with_comment(self):
+ self.assertTransformsTo(self.fixer,
+ '''
+PyTypeObject PyFortran_Type = {
+ PyObject_HEAD_INIT(0)
+ 0, /*ob_size*/
+ "fortran", /*tp_name*/
+''',
+ '''
+PyTypeObject PyFortran_Type = {
+ PyVarObject_HEAD_INIT(0, 0) /*ob_size*/
+ "fortran", /*tp_name*/
+''')
+
+if __name__ == '__main__':
+ unittest.main()