DebugClients/Python/coverage/backward.py

changeset 31
744cd0b4b8cd
parent 0
de9c2efb9d02
child 790
2c0ea0163ef4
diff -r 9513afbd57f1 -r 744cd0b4b8cd DebugClients/Python/coverage/backward.py
--- a/DebugClients/Python/coverage/backward.py	Thu Jan 07 13:42:05 2010 +0000
+++ b/DebugClients/Python/coverage/backward.py	Thu Jan 07 13:42:51 2010 +0000
@@ -1,15 +1,17 @@
 """Add things to old Pythons so I can pretend they are newer."""
 
-# pylint: disable-msg=W0622
-# (Redefining built-in blah)
-# The whole point of this file is to redefine built-ins, so shut up about it.
+# This file does lots of tricky stuff, so disable a bunch of lintisms.
+# pylint: disable-msg=F0401,W0611,W0622
+# F0401: Unable to import blah
+# W0611: Unused import blah
+# W0622: Redefining built-in blah
 
+import os, sys
 
 # Python 2.3 doesn't have `set`
 try:
     set = set       # new in 2.4
 except NameError:
-    # (Redefining built-in 'set')
     from sets import Set as set
 
 
@@ -22,3 +24,49 @@
         lst = list(iterable)
         lst.sort()
         return lst
+
+# Pythons 2 and 3 differ on where to get StringIO
+
+try:
+    from cStringIO import StringIO
+    BytesIO = StringIO
+except ImportError:
+    from io import StringIO, BytesIO
+
+# What's a string called?
+
+try:
+    string_class = basestring
+except NameError:
+    string_class = str
+
+# Where do pickles come from?
+
+try:
+    import cPickle as pickle
+except ImportError:
+    import pickle
+
+# range or xrange?
+
+try:
+    range = xrange
+except NameError:
+    range = range
+
+# Exec is a statement in Py2, a function in Py3
+
+if sys.hexversion > 0x03000000:
+    def exec_function(source, filename, global_map):
+        """A wrapper around exec()."""
+        exec(compile(source, filename, "exec"), global_map)
+else:
+    # OK, this is pretty gross.  In Py2, exec was a statement, but that will
+    # be a syntax error if we try to put it in a Py3 file, even if it is never
+    # executed.  So hide it inside an evaluated string literal instead.
+    eval(compile("""\
+def exec_function(source, filename, global_map):
+    exec compile(source, filename, "exec") in global_map
+""",
+    "<exec_function>", "exec"
+    ))

eric ide

mercurial