[Checkins] SVN: zope.tal/trunk/src/zope/tal/ Towards Py3K: print() function

Marius Gedminas cvs-admin at zope.org
Thu Feb 7 20:35:42 UTC 2013


Log message for revision 129177:
  Towards Py3K: print() function

Changed:
  U   zope.tal/trunk/src/zope/tal/driver.py
  U   zope.tal/trunk/src/zope/tal/ndiff.py
  U   zope.tal/trunk/src/zope/tal/runtest.py
  U   zope.tal/trunk/src/zope/tal/talgettext.py
  U   zope.tal/trunk/src/zope/tal/tests/markbench.py
  U   zope.tal/trunk/src/zope/tal/tests/utils.py
  U   zope.tal/trunk/src/zope/tal/timer.py

-=-
Modified: zope.tal/trunk/src/zope/tal/driver.py
===================================================================
--- zope.tal/trunk/src/zope/tal/driver.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/driver.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -37,6 +37,8 @@
         Enable source annotations
 """
 
+from __future__ import print_function
+
 import os
 import sys
 
@@ -104,9 +106,9 @@
            }
 
 def usage(code, msg=''):
-    print >> sys.stderr, __doc__
+    print(__doc__, file=sys.stderr)
     if msg:
-        print >> sys.stderr, msg
+        print(msg, file=sys.stderr)
     sys.exit(code)
 
 def main():

Modified: zope.tal/trunk/src/zope/tal/ndiff.py
===================================================================
--- zope.tal/trunk/src/zope/tal/ndiff.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/ndiff.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -56,6 +56,8 @@
 See module comments for details and programmatic interface.
 """
 
+from __future__ import print_function
+
 __version__ = 1, 5, 0
 
 # SequenceMatcher tries to compute a "human-friendly diff" between
@@ -314,8 +316,8 @@
             bestsize = bestsize + 1
 
         if TRACE:
-            print "get_matching_blocks", alo, ahi, blo, bhi
-            print "    returns", besti, bestj, bestsize
+            print("get_matching_blocks", alo, ahi, blo, bhi)
+            print("    returns", besti, bestj, bestsize)
         return besti, bestj, bestsize
 
     def get_matching_blocks(self):
@@ -326,7 +328,7 @@
         self.__helper(0, la, 0, lb, self.matching_blocks)
         self.matching_blocks.append((la, lb, 0))
         if TRACE:
-            print '*** matching blocks', self.matching_blocks
+            print('*** matching blocks', self.matching_blocks)
         return self.matching_blocks
 
     # builds list of matching blocks covering a[alo:ahi] and
@@ -418,7 +420,7 @@
 # meant for dumping lines
 def dump(tag, x, lo, hi):
     for i in xrange(lo, hi):
-        print tag, x[i],
+        print(tag, x[i], end=' ')
 
 def plain_replace(a, alo, ahi, b, blo, bhi):
     assert alo < ahi and blo < bhi
@@ -438,7 +440,7 @@
 
 def fancy_replace(a, alo, ahi, b, blo, bhi):
     if TRACE:
-        print '*** fancy_replace', alo, ahi, blo, bhi
+        print('*** fancy_replace', alo, ahi, blo, bhi)
         dump('>', a, alo, ahi)
         dump('<', b, blo, bhi)
 
@@ -486,7 +488,7 @@
     # a[best_i] very similar to b[best_j]; eqi is None iff they're not
     # identical
     if TRACE:
-        print '*** best_ratio', best_ratio, best_i, best_j
+        print('*** best_ratio', best_ratio, best_i, best_j)
         dump('>', a, best_i, best_i+1)
         dump('<', b, best_j, best_j+1)
 
@@ -516,7 +518,7 @@
         printq(aelt, belt, atags, btags)
     else:
         # the synch pair is identical
-        print ' ', aelt,
+        print(' ', aelt, end=' ')
 
     # pump out diffs from after the synch point
     fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi)
@@ -537,12 +539,12 @@
     common = min(count_leading(aline, "\t"),
                  count_leading(bline, "\t"))
     common = min(common, count_leading(atags[:common], " "))
-    print "-", aline,
+    print("-", aline, end=' ')
     if count_leading(atags, " ") < len(atags):
-        print "?", "\t" * common + atags[common:]
-    print "+", bline,
+        print("?", "\t" * common + atags[common:])
+    print("+", bline, end=' ')
     if count_leading(btags, " ") < len(btags):
-        print "?", "\t" * common + btags[common:]
+        print("?", "\t" * common + btags[common:])
 
 def count_leading(line, ch):
     i, n = 0, len(line)
@@ -621,8 +623,8 @@
         return fail("need 2 filename args")
     f1name, f2name = args
     if noisy:
-        print '-:', f1name
-        print '+:', f2name
+        print('-:', f1name)
+        print('+:', f2name)
     return fcompare(f1name, f2name)
 
 def restore(which):
@@ -631,7 +633,7 @@
     prefixes = ("  ", tag)
     for line in sys.stdin.readlines():
         if line[:2] in prefixes:
-            print line[2:],
+            print(line[2:], end=' ')
 
 if __name__ == '__main__':
     import sys

Modified: zope.tal/trunk/src/zope/tal/runtest.py
===================================================================
--- zope.tal/trunk/src/zope/tal/runtest.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/runtest.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -14,6 +14,9 @@
 ##############################################################################
 """Driver program to run METAL and TAL regression tests.
 """
+
+from __future__ import print_function
+
 import glob
 import os
 import sys
@@ -33,10 +36,10 @@
     for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
         if tag == "equal":
             continue
-        print nicerange(alo, ahi) + tag[0] + nicerange(blo, bhi)
+        print(nicerange(alo, ahi) + tag[0] + nicerange(blo, bhi))
         ndiff.dump('<', a, alo, ahi)
         if a and b:
-            print '---'
+            print('---')
         ndiff.dump('>', b, blo, bhi)
 
 def nicerange(lo, hi):
@@ -80,10 +83,10 @@
         if arg.find("_sa") >= 0 and "-a" not in opts:
             locopts.append("-a")
         if not unittesting:
-            print arg,
+            print(arg, end=' ')
             sys.stdout.flush()
         if zope.tal.tests.utils.skipxml and arg.endswith(".xml"):
-            print "SKIPPED (XML parser not available)"
+            print("SKIPPED (XML parser not available)")
             continue
         save = sys.stdout, sys.argv
         try:
@@ -98,13 +101,13 @@
         except:
             errors = 1
             if quiet:
-                print sys.exc_type
+                print(sys.exc_info()[0])
                 sys.stdout.flush()
             else:
                 if unittesting:
-                    print
+                    print()
                 else:
-                    print "Failed:"
+                    print("Failed:")
                     sys.stdout.flush()
                 traceback.print_exc()
             continue
@@ -116,7 +119,7 @@
             f = open(outfile)
         except IOError:
             expected = None
-            print "(missing file %s)" % outfile,
+            print("(missing file %s)" % outfile, end=' ')
         else:
             expected = f.readlines()
             f.close()
@@ -127,12 +130,12 @@
             actual = readlines(stdout)
         if actual == expected:
             if not unittesting:
-                print "OK"
+                print("OK")
         else:
             if unittesting:
-                print
+                print()
             else:
-                print "not OK"
+                print("not OK")
             errors = 1
             if not quiet and expected is not None:
                 showdiff(expected, actual)

Modified: zope.tal/trunk/src/zope/tal/talgettext.py
===================================================================
--- zope.tal/trunk/src/zope/tal/talgettext.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/talgettext.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -27,6 +27,9 @@
         Update the existing translation <file> with any new translation strings
         found.
 """
+
+from __future__ import print_function
+
 import sys
 import time
 import getopt
@@ -62,9 +65,9 @@
 
 def usage(code, msg=''):
     # Python 2.1 required
-    print >> sys.stderr, __doc__
+    print(__doc__, file=sys.stderr)
     if msg:
-        print >> sys.stderr, msg
+        print(msg, file=sys.stderr)
     sys.exit(code)
 
 
@@ -131,10 +134,10 @@
             existing_msgid = msgids[idx]
             if msgid.default != existing_msgid.default:
                 references = '\n'.join([location[0]+':'+str(location[1]) for location in domain[msgid]])
-                print >> sys.stderr, (u"Warning: msgid '%s' in %s already exists " \
-                         "with a different default (bad: %s, should be: %s)\n" \
-                         "The references for the existent value are:\n%s\n" % \
-                         (msgid, self.file+':'+str(position), msgid.default.encode('utf-8'), existing_msgid.default.encode('utf-8'), references)).encode('utf-8')
+                print(("Warning: msgid '%s' in %s already exists "
+                       "with a different default (bad: %s, should be: %s)\n"
+                       "The references for the existent value are:\n%s\n" %
+                         (msgid, self.file+':'+str(position), msgid.default.encode('utf-8'), existing_msgid.default.encode('utf-8'), references)).encode('utf-8'), file=sys.stderr)
         domain[msgid].append((self.file, position))
         return 'x'
 
@@ -171,7 +174,7 @@
         try:
             lines = open(self._filename).readlines()
         except IOError as msg:
-            print >> sys.stderr, msg
+            print(msg, file=sys.stderr)
             sys.exit(1)
 
         section = None
@@ -213,9 +216,9 @@
             elif section == STR:
                 msgstr += '%s\n' % l
             else:
-                print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), \
-                      'before:'
-                print >> sys.stderr, l
+                print('Syntax error on %s:%d' % (infile, lno),
+                      'before:', file=sys.stderr)
+                print(l, file=sys.stderr)
                 sys.exit(1)
         # Add last entry
         if section == STR:
@@ -261,7 +264,7 @@
             engine = UpdatePOEngine(filename=arg)
 
     if not args:
-        print 'nothing to do'
+        print('nothing to do')
         return
 
     # We don't care about the rendered output of the .pt file
@@ -284,7 +287,7 @@
             POTALInterpreter(program, macros, engine, stream=Devnull(),
                              metal=False)()
         except: # Hee hee, I love bare excepts!
-            print 'There was an error processing', filename
+            print('There was an error processing', filename)
             traceback.print_exc()
 
     # Now output the keys in the engine.  Write them to a file if --output or
@@ -304,8 +307,8 @@
     except AttributeError:
         pass
     if '' not in messages:
-        print >> outfile, pot_header % {'time': time.ctime(),
-                                        'version': __version__}
+        print(pot_header % {'time': time.ctime(), 'version': __version__},
+              file=outfile)
 
     msgids = catalog.keys()
     # TODO: You should not sort by msgid, but by filename and position. (SR)

Modified: zope.tal/trunk/src/zope/tal/tests/markbench.py
===================================================================
--- zope.tal/trunk/src/zope/tal/tests/markbench.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/tests/markbench.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -15,6 +15,8 @@
 """Run benchmarks of TAL vs. DTML
 """
 
+from __future__ import print_function
+
 import warnings
 warnings.filterwarnings("ignore", category=DeprecationWarning)
 
@@ -108,14 +110,14 @@
         t1 = int(time_zpt(tal_fn % n, count) * 1000 + 0.5)
         t2 = int(time_tal(tal_fn % n, count) * 1000 + 0.5)
         t3 = 'n/a' # int(time_dtml(dtml_fn % n, count) * 1000 + 0.5)
-        print '%.2d: %10s %10s %10s' % (n, t1, t2, t3)
+        print('%.2d: %10s %10s %10s' % (n, t1, t2, t3))
     if profiler:
         profile_tal(tal_fn % n, count, profiler)
 
 def main(count, profiler=None, verbose=1):
     n = 1
     if verbose:
-        print '##: %10s %10s %10s' % ('ZPT', 'TAL', 'DTML')
+        print('##: %10s %10s %10s' % ('ZPT', 'TAL', 'DTML'))
     while os.path.isfile(tal_fn % n) and os.path.isfile(dtml_fn % n):
         compare(n, count, profiler, verbose)
         n = n + 1
@@ -160,9 +162,9 @@
             sys.exit(rc)
         elif rc < 0:
             sig = -rc
-            print >>sys.stderr, (
+            print((
                 "Process exited, signal %d (%s)."
-                % (sig, get_signal_name(sig) or "<unknown signal>"))
+                % (sig, get_signal_name(sig) or "<unknown signal>")), file=sys.stderr)
             sys.exit(1)
         os.chdir(pwd)
 

Modified: zope.tal/trunk/src/zope/tal/tests/utils.py
===================================================================
--- zope.tal/trunk/src/zope/tal/tests/utils.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/tests/utils.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -50,8 +50,8 @@
 ##         print
     newerrs = len(result.errors) + len(result.failures)
     if newerrs:
-        print "'Errors' indicate exceptions other than AssertionError."
-        print "'Failures' indicate AssertionError"
+        print("'Errors' indicate exceptions other than AssertionError.")
+        print("'Failures' indicate AssertionError")
         if errf is None:
             errf = sys.stderr
         errf.write("%d errors, %d failures\n"

Modified: zope.tal/trunk/src/zope/tal/timer.py
===================================================================
--- zope.tal/trunk/src/zope/tal/timer.py	2013-02-07 16:02:59 UTC (rev 129176)
+++ zope.tal/trunk/src/zope/tal/timer.py	2013-02-07 20:35:41 UTC (rev 129177)
@@ -28,7 +28,7 @@
     try:
         opts, args = getopt.getopt(sys.argv[1:], "n:")
     except getopt.error as msg:
-        print msg
+        print(msg)
         sys.exit(2)
     for o, a in opts:
         if o == "-n":
@@ -36,7 +36,7 @@
     if not args:
         args = [FILE]
     for file in args:
-        print file
+        print(file)
         dummyfile = StringIO()
         it = timefunc(count, compilefile, file)
         timefunc(count, interpretit, it, None, dummyfile)



More information about the checkins mailing list