Changeset: 2ab7cfef1a19 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=2ab7cfef1a19
Modified Files:
        testing/Mfilter.py.in
        testing/cmptests.py
        testing/listexports.py.in
        testing/process.py
        testing/subprocess26.py
Branch: Oct2012
Log Message:

Ported a bunch of Python test scripts to Python 3.
Generally: avoid print, avoid arguments to except, use in operator
instead of has_key method.


diffs (truncated from 409 to 300 lines):

diff --git a/testing/Mfilter.py.in b/testing/Mfilter.py.in
--- a/testing/Mfilter.py.in
+++ b/testing/Mfilter.py.in
@@ -26,14 +26,14 @@ import re
 #       FUNCTIONS
 
 def Usage(THISFILE) :
-    print """
+    print("""
 
 Usage:  %s [-I<exp>] <files>
 
 -I<exp> : ignore lines matching <exp> during first count (optional, default: 
-I'^#`)
 <files> : list of files to be processed
 
-""" % THISFILE
+""" % THISFILE)
 ### Usage(THISFILE) #
 
 def warn(THISFILE,TEXT) :
@@ -233,7 +233,9 @@ def mFilter (FILE, IGNORE) :
                     ln = ln[:-1].replace('\n', '\n' + pref) + '\n'
             try:
                 fout.write(ln.expandtabs())
-            except IOError, (IOerrNo, IOerrStr):
+            except IOError:
+                IOerrNo = sys.exc_info()[1].errno
+                IOerrStr = sys.exc_info()[1].strerror
                 warn(THISFILE, "Writing to output file '%s' failed with #%d: 
'%s'." % (fout.name, IOerrNo, IOerrStr))
                 if IOerrNo == 28:
                     # No space left on device
@@ -247,7 +249,9 @@ def mFilter (FILE, IGNORE) :
                         pass
                     try:
                         fout.write(ln.expandtabs())
-                    except IOError, (IOerrNo, IOerrStr):
+                    except IOError:
+                        IOerrNo = sys.exc_info()[1].errno
+                        IOerrStr = sys.exc_info()[1].strerror
                         warn(THISFILE, "Writing to output file '%s' failed 
with #%d: '%s'." % (fout.name, IOerrNo, IOerrStr))
         fout.flush()
     fout.close()
diff --git a/testing/cmptests.py b/testing/cmptests.py
--- a/testing/cmptests.py
+++ b/testing/cmptests.py
@@ -23,8 +23,8 @@ def cmptests(dir1, dir2, timing = True, 
     new2 = []
     for line in open(lst1):
         line = line.strip().split('\t')
-        if res1.has_key(line[0]):
-            print >> sys.stderr, '%s: duplicate key %s' % (lst1, line[0])
+        if line[0] in res1:
+            sys.stderr.write('%s: duplicate key %s\n' % (lst1, line[0]))
             sys.exit(1)
         if len(line) != 4:
             continue
@@ -38,7 +38,7 @@ def cmptests(dir1, dir2, timing = True, 
             continue
         if line[0][-2:] == '/:':
             continue
-        if not res1.has_key(line[0]):
+        if line[0] not in res1:
             new2.append(line[0])
             continue
         tm1, out1, err1 = res1[line[0]]
@@ -47,9 +47,9 @@ def cmptests(dir1, dir2, timing = True, 
                (err1 != err2 and err2 != 'F_OK') or \
                not regressions:
             if out1 != out2:
-                print '%s output differs: %s %s' % (line[0], out1, out2)
+                sys.stdout.write('%s output differs: %s %s\n' % (line[0], 
out1, out2))
             elif err1 != err2:
-                print '%s errout differs: %s %s' % (line[0], err1, err2)
+                sys.stdout.write('%s errout differs: %s %s\n' % (line[0], 
err1, err2))
         if timing and out1 == 'F_OK' and out2 == 'F_OK' and err1 == 'F_OK' and 
err2 == 'F_OK':
             ftm1 = float(tm1)
             ftm2 = float(tm2)
@@ -58,17 +58,17 @@ def cmptests(dir1, dir2, timing = True, 
                     slowdown.append((line[0], tm1, tm2))
         del res1[line[0]]
     if res1:
-        print '\nRemoved tests in %s:' % lst1
+        sys.stdout.write('\nRemoved tests in %s:\n' % lst1)
         for tst in sorted(res1.keys()):
-            print tst.rstrip(':')
+            sys.stdout.write(tst.rstrip(':') + '\n')
     if new2:
-        print '\nNew tests in %s:' % lst2
+        sys.stdout.write('\nNew tests in %s:\n' % lst2)
         for tst in sorted(new2):
-            print tst.rstrip(':')
+            sys.stdout.write(tst.rstrip(':') + '\n')
     if slowdown:
-        print '\nSignificant slowdown in tests:'
+        sys.stdout.write('\nSignificant slowdown in tests:\n')
         for tst, tm1, tm2 in sorted(slowdown):
-            print '%s %s %s' % (tst, tm1, tm2)
+            sys.stdout.write('%s %s %s\n' % (tst, tm1, tm2))
 
 if __name__ == '__main__':
     import getopt, sys
@@ -77,10 +77,10 @@ if __name__ == '__main__':
     regressions = False
 
     def usage(ext):
-        print >> sys.stderr, 'Usage: %s [-t] [-r] dir1 dir2' % sys.argv[0]
-        print >> sys.stderr, 'Compare test outputs in dir1 and dir2.'
-        print >> sys.stderr, 'If -t option given, report significant slow 
down.'
-        print >> sys.stderr, 'If -r option given, report regressions only.'
+        sys.stderr.write('Usage: %s [-t] [-r] dir1 dir2\n' % sys.argv[0])
+        sys.stderr.write('Compare test outputs in dir1 and dir2.\n')
+        sys.stderr.write('If -t option given, report significant slow down.\n')
+        sys.stderr.write('If -r option given, report regressions only.\n')
         sys.exit(ext)
 
     try:
diff --git a/testing/listexports.py.in b/testing/listexports.py.in
--- a/testing/listexports.py.in
+++ b/testing/listexports.py.in
@@ -1,4 +1,5 @@
 import os
+import sys
 import re
 
 dirlist = {
@@ -62,7 +63,7 @@ def findfiles(dirlist, skipfiles = [], s
                 if d in dirs:
                     dirs.remove(d)
             for f in files:
-                if not done.has_key(f) and \
+                if f not in done and \
                         (f.endswith('.c') or f.endswith('.h')) and \
                         not f.startswith('.') and \
                         f not in skipfiles and \
@@ -70,7 +71,7 @@ def findfiles(dirlist, skipfiles = [], s
                     decls.extend(extract(os.path.join(root, f)))
                     done[f] = True
     decls.sort()
-    names, decls = zip(*decls)
+    names, decls = list(zip(*decls))
     return decls
 
 for lib in libs:
@@ -79,7 +80,8 @@ for lib in libs:
     if srcdir != blddir:
         dl.extend([os.path.join(blddir, d) for d in dirs])
     decls = findfiles(dl, skipfiles = skipfiles, skipdirs = skipdirs)
-    print '# %s' % lib
+    sys.stdout.write('# %s\n' % lib)
     for d in decls:
-        print d
-    print
+        sys.stdout.write(d)
+        sys.stdout.write('\n')
+    sys.stdout.write('\n')
diff --git a/testing/process.py b/testing/process.py
--- a/testing/process.py
+++ b/testing/process.py
@@ -7,7 +7,10 @@ import tempfile
 import copy
 import atexit
 import threading
-import Queue
+if sys.version[:1] == '2':
+    import Queue as queue
+else:
+    import queue
 
 from subprocess import PIPE
 
@@ -59,10 +62,10 @@ atexit.register(_delfiles)
 class _BufferedPipe:
     def __init__(self, fd, waitfor = None, skip = None):
         self._pipe = fd
-        self._queue = Queue.Queue()
+        self._queue = queue.Queue()
         self._eof = False
         if waitfor is not None:
-            self._wfq = Queue.Queue()
+            self._wfq = queue.Queue()
         else:
             self._wfq = None
         self._thread = threading.Thread(target = self._readerthread,
@@ -270,24 +273,24 @@ def client(lang, args = [], stdin = None
                 break
         cmd.append('--host=%s' % host)
     if verbose:
-        print 'Executing', ' '.join(cmd +  args)
+        sys.stdout.write('Executing' + ' '.join(cmd +  args) + '\n')
         sys.stdout.flush()
     if log:
         prompt = time.strftime('# %H:%M:%S >  ')
         cmdstr = ' '.join(cmd +  args)
         if hasattr(stdin, 'name'):
             cmdstr += ' < "%s"' % stdin.name
-        print
-        print prompt
-        print '%s%s' % (prompt, cmdstr)
-        print prompt
-        print
+        sys.stdout.write('\n')
+        sys.stdout.write(prompt + '\n')
+        sys.stdout.write('%s%s\n' % (prompt, cmdstr))
+        sys.stdout.write(prompt + '\n')
+        sys.stdout.write('\n')
         sys.stdout.flush()
-        print >> sys.stderr
-        print >> sys.stderr, prompt
-        print >> sys.stderr, '%s%s' % (prompt, cmdstr)
-        print >> sys.stderr, prompt
-        print >> sys.stderr
+        sys.stderr.write('\n')
+        sys.stderr.write(prompt + '\n')
+        sys.stderr.write('%s%s\n' % (prompt, cmdstr))
+        sys.stderr.write(prompt + '\n')
+        sys.stderr.write('\n')
         sys.stderr.flush()
     if stdin is None:
         # if no input provided, use /dev/null as input
@@ -346,24 +349,24 @@ def server(args = [], stdin = None, stdo
         cmd.append('--set')
         cmd.append('gdk_dbfarm=%s' % dbfarm)
     if verbose:
-        print 'Executing', ' '.join(cmd +  args)
+        sys.stdout.write('Executing' + ' '.join(cmd +  args) + '\n')
         sys.stdout.flush()
     if log:
         prompt = time.strftime('# %H:%M:%S >  ')
         cmdstr = ' '.join(cmd +  args)
         if hasattr(stdin, 'name'):
             cmdstr += ' < "%s"' % stdin.name
-        print
-        print prompt
-        print '%s%s' % (prompt, cmdstr)
-        print prompt
-        print
+        sys.stdout.write('\n')
+        sys.stdout.write(prompt + '\n')
+        sys.stdout.write('%s%s\n' % (prompt, cmdstr))
+        sys.stdout.write(prompt + '\n')
+        sys.stdout.write('\n')
         sys.stdout.flush()
-        print >> sys.stderr
-        print >> sys.stderr, prompt
-        print >> sys.stderr, '%s%s' % (prompt, cmdstr)
-        print >> sys.stderr, prompt
-        print >> sys.stderr
+        sys.stderr.write('\n')
+        sys.stderr.write(prompt + '\n')
+        sys.stderr.write('%s%s\n' % (prompt, cmdstr))
+        sys.stderr.write(prompt + '\n')
+        sys.stderr.write('\n')
         sys.stderr.flush()
     p = Popen(cmd + args,
               stdin = stdin,
diff --git a/testing/subprocess26.py b/testing/subprocess26.py
--- a/testing/subprocess26.py
+++ b/testing/subprocess26.py
@@ -409,6 +409,12 @@ try:
 except:
     MAXFD = 256
 
+try:
+    xrange
+except NameError:
+    # Python 2 vs Python 3 compatibility: in Py3, use range instead of xrange
+    xrange = range
+
 # True/False does not exist on 2.2.0
 try:
     False
@@ -420,7 +426,7 @@ _active = []
 
 def _cleanup():
     for inst in _active[:]:
-        if inst.poll(_deadstate=sys.maxint) >= 0:
+        if inst.poll(_deadstate=sys.maxsize) >= 0:
             try:
                 _active.remove(inst)
             except ValueError:
@@ -540,7 +546,7 @@ class Popen(object):
         _cleanup()
 
         self._child_created = False
-        if not isinstance(bufsize, (int, long)):
+        if not isinstance(bufsize, int):
             raise TypeError("bufsize must be an integer")
 
         if mswindows:
@@ -633,7 +639,7 @@ class Popen(object):
             # We didn't get to successfully create a child process.
             return
         # In case the child hasn't been waited on, check if it's done.
-        self.poll(_deadstate=sys.maxint)
+        self.poll(_deadstate=sys.maxsize)
         if self.returncode is None and _active is not None:
             # Child is still running, keep us alive until we can wait on it.
             _active.append(self)
@@ -768,7 +774,7 @@ class Popen(object):
                            errread, errwrite):
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to