Support for all versions of Python from 2.3 to 3.1.
authorJohann C. Rocholl <johann@rocholl.net>
Fri, 2 Oct 2009 21:36:04 +0000 (14:36 -0700)
committerJohann C. Rocholl <johann@rocholl.net>
Fri, 2 Oct 2009 21:36:04 +0000 (14:36 -0700)
Makefile
pep8.py

index 887d5674c134e29ac2b3c1ed6d2bb0a06c373354..11e86f84583d3a7596560a3584afdbe4910fbd0b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,17 +1,24 @@
 test :
        python pep8.py --testsuite testsuite
 
-multitest :
-       python2.4 pep8.py --testsuite testsuite
-       python2.5 pep8.py --testsuite testsuite
-       python2.6 pep8.py --testsuite testsuite
-
 selftest :
-       python pep8.py --repeat pep8.py
-
+       python pep8.py --repeat --statistics pep8.py
 
 doctest :
        python pep8.py --doctest
 
-
 alltest : test selftest doctest
+
+multitest :
+       python2.3 pep8.py --testsuite testsuite
+       python2.4 pep8.py --testsuite testsuite
+       python2.5 pep8.py --testsuite testsuite
+       python2.6 pep8.py --testsuite testsuite
+       python3.0 pep8.py --testsuite testsuite
+       python3.1 pep8.py --testsuite testsuite
+       python2.3 pep8.py --repeat --statistics pep8.py
+       python2.4 pep8.py --repeat --statistics pep8.py
+       python2.5 pep8.py --repeat --statistics pep8.py
+       python2.6 pep8.py --repeat --statistics pep8.py
+       python3.0 pep8.py --repeat --statistics pep8.py
+       python3.1 pep8.py --repeat --statistics pep8.py
diff --git a/pep8.py b/pep8.py
index fe6023d59a34475ef1fc545790e765b148a02508..2f6c13ac1d4d32cedca1ec5a1dedb9af8b4c61dd 100755 (executable)
--- a/pep8.py
+++ b/pep8.py
@@ -84,6 +84,7 @@ text from PEP 8. It is printed if the user enables --show-pep8.
 import os
 import sys
 import re
+import types
 import time
 import inspect
 import tokenize
@@ -510,7 +511,7 @@ def message(text):
     """Print a message."""
     # print >> sys.stderr, options.prog + ': ' + text
     # print >> sys.stderr, text
-    print text
+    print(text)
 
 
 def find_checks(argument_name):
@@ -519,9 +520,8 @@ def find_checks(argument_name):
     starts with argument_name.
     """
     checks = []
-    function_type = type(find_checks)
-    for name, function in globals().iteritems():
-        if type(function) is function_type:
+    for name, function in globals().items():
+        if isinstance(function, types.FunctionType):
             args = inspect.getargspec(function)[0]
             if len(args) >= 1 and args[0].startswith(argument_name):
                 checks.append((name, function, args))
@@ -561,7 +561,7 @@ class Checker:
 
     def __init__(self, filename):
         self.filename = filename
-        self.lines = file(filename).readlines()
+        self.lines = open(filename).readlines()
         self.physical_checks = find_checks('physical_line')
         self.logical_checks = find_checks('logical_line')
         options.counters['physical lines'] = \
@@ -655,14 +655,14 @@ class Checker:
         self.previous_indent_level = self.indent_level
         self.indent_level = expand_indent(indent)
         if options.verbose >= 2:
-            print self.logical_line[:80].rstrip()
+            print(self.logical_line[:80].rstrip())
         for name, check, argument_names in self.logical_checks:
             if options.verbose >= 3:
-                print '   ', name
+                print('   ', name)
             result = self.run_check(check, argument_names)
             if result is not None:
                 offset, text = result
-                if type(offset) is tuple:
+                if isinstance(offset, tuple):
                     original_number, original_offset = offset
                 else:
                     for token_offset, token in self.mapping:
@@ -843,7 +843,7 @@ def get_statistics(prefix=''):
     prefix='E4' matches all errors that have to do with imports
     """
     stats = []
-    keys = options.messages.keys()
+    keys = list(options.messages.keys())
     keys.sort()
     for key in keys:
         if key.startswith(prefix):
@@ -854,7 +854,7 @@ def get_statistics(prefix=''):
 
 def get_count(prefix=''):
     """Return the total count of errors and warnings."""
-    keys = options.messages.keys()
+    keys = list(options.messages.keys())
     count = 0
     for key in keys:
         if key.startswith(prefix):
@@ -865,21 +865,21 @@ def get_count(prefix=''):
 def print_statistics(prefix=''):
     """Print overall statistics (number of errors and warnings)."""
     for line in get_statistics(prefix):
-        print line
+        print(line)
 
 
 def print_benchmark(elapsed):
     """
     Print benchmark numbers.
     """
-    print '%-7.2f %s' % (elapsed, 'seconds elapsed')
+    print('%-7.2f %s' % (elapsed, 'seconds elapsed'))
     keys = ['directories', 'files',
             'logical lines', 'physical lines']
     for key in keys:
         if key in options.counters:
-            print '%-7d %s per second (%d total)' % (
+            print('%-7d %s per second (%d total)' % (
                 options.counters[key] / elapsed, key,
-                options.counters[key])
+                options.counters[key]))
 
 
 def process_options(arglist=None):
@@ -956,7 +956,7 @@ def _main():
     if options.benchmark:
         print_benchmark(elapsed)
     if options.count:
-        print get_count()
+        print(get_count())
 
 
 if __name__ == '__main__':