[Python-checkins] r79308 - in python/branches/release26-maint: Doc/library/2to3.rst Lib/lib2to3 Lib/lib2to3/fixes/fix_exitfunc.py Lib/lib2to3/pgen2/tokenize.py Lib/lib2to3/tests/test_fixers.py Lib/lib2to3/tests/test_parser.py

benjamin.peterson python-checkins at python.org
Mon Mar 22 23:47:21 CET 2010


Author: benjamin.peterson
Date: Mon Mar 22 23:47:20 2010
New Revision: 79308

Log:
Merged revisions 79138-79139,79306 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

................
  r79138 | benjamin.peterson | 2010-03-20 11:16:44 -0500 (Sat, 20 Mar 2010) | 1 line
  
  document exitfunc fixer
................
  r79139 | benjamin.peterson | 2010-03-20 11:17:37 -0500 (Sat, 20 Mar 2010) | 1 line
  
  wrap
................
  r79306 | benjamin.peterson | 2010-03-22 17:40:06 -0500 (Mon, 22 Mar 2010) | 21 lines
  
  Merged revisions 79077,79137,79304-79305 via svnmerge from 
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
  
  ........
    r79077 | benjamin.peterson | 2010-03-18 18:05:29 -0500 (Thu, 18 Mar 2010) | 1 line
    
    port detect_encoding improvements from py3k
  ........
    r79137 | benjamin.peterson | 2010-03-20 11:12:53 -0500 (Sat, 20 Mar 2010) | 1 line
    
    add a fixer for setting sys.exitfunc #2356
  ........
    r79304 | benjamin.peterson | 2010-03-22 17:20:22 -0500 (Mon, 22 Mar 2010) | 1 line
    
    fix test_parser when it's run in a path with spaces #7666
  ........
    r79305 | benjamin.peterson | 2010-03-22 17:27:07 -0500 (Mon, 22 Mar 2010) | 1 line
    
    normalize whitespace
  ........
................


Added:
   python/branches/release26-maint/Lib/lib2to3/fixes/fix_exitfunc.py
      - copied unchanged from r79306, /python/trunk/Lib/lib2to3/fixes/fix_exitfunc.py
Modified:
   python/branches/release26-maint/   (props changed)
   python/branches/release26-maint/Doc/library/2to3.rst
   python/branches/release26-maint/Lib/lib2to3/   (props changed)
   python/branches/release26-maint/Lib/lib2to3/pgen2/tokenize.py
   python/branches/release26-maint/Lib/lib2to3/tests/test_fixers.py
   python/branches/release26-maint/Lib/lib2to3/tests/test_parser.py

Modified: python/branches/release26-maint/Doc/library/2to3.rst
==============================================================================
--- python/branches/release26-maint/Doc/library/2to3.rst	(original)
+++ python/branches/release26-maint/Doc/library/2to3.rst	Mon Mar 22 23:47:20 2010
@@ -120,9 +120,11 @@
 
    Fixes dictionary iteration methods.  :meth:`dict.iteritems` is converted to
    :meth:`dict.items`, :meth:`dict.iterkeys` to :meth:`dict.keys`, and
-   :meth:`dict.itervalues` to :meth:`dict.values`.  It also wraps existing
-   usages of :meth:`dict.items`, :meth:`dict.keys`, and :meth:`dict.values` in a
-   call to :class:`list`.
+   :meth:`dict.itervalues` to :meth:`dict.values`.  Similarly,
+   :meth:`dict.viewitems`, :meth:`dict.viewkeys` and :meth:`dict.viewvalues` are
+   converted respectively to :meth:`dict.items`, :meth:`dict.keys` and
+   :meth:`dict.values`.  It also wraps existing usages of :meth:`dict.items`,
+   :meth:`dict.keys`, and :meth:`dict.values` in a call to :class:`list`.
 
 .. 2to3fixer:: except
 
@@ -137,6 +139,11 @@
    Removes usage of :func:`execfile`.  The argument to :func:`execfile` is
    wrapped in calls to :func:`open`, :func:`compile`, and :func:`exec`.
 
+.. 2to3fixer:: exitfunc
+
+   Changes assignment of :attr:`sys.exitfunc` to use of the :mod:`atexit`
+   module.
+
 .. 2to3fixer:: filter
 
    Wraps :func:`filter` usage in a :class:`list` call.

Modified: python/branches/release26-maint/Lib/lib2to3/pgen2/tokenize.py
==============================================================================
--- python/branches/release26-maint/Lib/lib2to3/pgen2/tokenize.py	(original)
+++ python/branches/release26-maint/Lib/lib2to3/pgen2/tokenize.py	Mon Mar 22 23:47:20 2010
@@ -253,14 +253,16 @@
     in.
 
     It detects the encoding from the presence of a utf-8 bom or an encoding
-    cookie as specified in pep-0263. If both a bom and a cookie are present,
-    but disagree, a SyntaxError will be raised. If the encoding cookie is an
-    invalid charset, raise a SyntaxError.
+    cookie as specified in pep-0263. If both a bom and a cookie are present, but
+    disagree, a SyntaxError will be raised. If the encoding cookie is an invalid
+    charset, raise a SyntaxError.  Note that if a utf-8 bom is found,
+    'utf-8-sig' is returned.
 
     If no encoding is specified, then the default of 'utf-8' will be returned.
     """
     bom_found = False
     encoding = None
+    default = 'utf-8'
     def read_or_stop():
         try:
             return readline()
@@ -287,17 +289,16 @@
             if codec.name != 'utf-8':
                 # This behaviour mimics the Python interpreter
                 raise SyntaxError('encoding problem: utf-8')
-            else:
-                # Allow it to be properly encoded and decoded.
-                encoding = 'utf-8-sig'
+            encoding += '-sig'
         return encoding
 
     first = read_or_stop()
     if first.startswith(BOM_UTF8):
         bom_found = True
         first = first[3:]
+        default = 'utf-8-sig'
     if not first:
-        return 'utf-8', []
+        return default, []
 
     encoding = find_cookie(first)
     if encoding:
@@ -305,13 +306,13 @@
 
     second = read_or_stop()
     if not second:
-        return 'utf-8', [first]
+        return default, [first]
 
     encoding = find_cookie(second)
     if encoding:
         return encoding, [first, second]
 
-    return 'utf-8', [first, second]
+    return default, [first, second]
 
 def untokenize(iterable):
     """Transform tokens back into Python source code.

Modified: python/branches/release26-maint/Lib/lib2to3/tests/test_fixers.py
==============================================================================
--- python/branches/release26-maint/Lib/lib2to3/tests/test_fixers.py	(original)
+++ python/branches/release26-maint/Lib/lib2to3/tests/test_fixers.py	Mon Mar 22 23:47:20 2010
@@ -4285,3 +4285,91 @@
     def test_bare_sequenceIncludes(self):
         s = "sequenceIncludes(x, y)"
         self.warns_unchanged(s, "You should use operator.contains here.")
+
+
+class Test_exitfunc(FixerTestCase):
+
+    fixer = "exitfunc"
+
+    def test_simple(self):
+        b = """
+            import sys
+            sys.exitfunc = my_atexit
+            """
+        a = """
+            import sys
+            import atexit
+            atexit.register(my_atexit)
+            """
+        self.check(b, a)
+
+    def test_names_import(self):
+        b = """
+            import sys, crumbs
+            sys.exitfunc = my_func
+            """
+        a = """
+            import sys, crumbs, atexit
+            atexit.register(my_func)
+            """
+        self.check(b, a)
+
+    def test_complex_expression(self):
+        b = """
+            import sys
+            sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression
+            """
+        a = """
+            import sys
+            import atexit
+            atexit.register(do(d)/a()+complex(f=23, g=23)*expression)
+            """
+        self.check(b, a)
+
+    def test_comments(self):
+        b = """
+            import sys # Foo
+            sys.exitfunc = f # Blah
+            """
+        a = """
+            import sys
+            import atexit # Foo
+            atexit.register(f) # Blah
+            """
+        self.check(b, a)
+
+        b = """
+            import apples, sys, crumbs, larry # Pleasant comments
+            sys.exitfunc = func
+            """
+        a = """
+            import apples, sys, crumbs, larry, atexit # Pleasant comments
+            atexit.register(func)
+            """
+        self.check(b, a)
+
+    def test_in_a_function(self):
+        b = """
+            import sys
+            def f():
+                sys.exitfunc = func
+            """
+        a = """
+            import sys
+            import atexit
+            def f():
+                atexit.register(func)
+             """
+        self.check(b, a)
+
+    def test_no_sys_import(self):
+        b = """sys.exitfunc = f"""
+        a = """atexit.register(f)"""
+        msg = ("Can't find sys import; Please add an atexit import at the "
+            "top of your file.")
+        self.warns(b, a, msg)
+
+
+    def test_unchanged(self):
+        s = """f(sys.exitfunc)"""
+        self.unchanged(s)

Modified: python/branches/release26-maint/Lib/lib2to3/tests/test_parser.py
==============================================================================
--- python/branches/release26-maint/Lib/lib2to3/tests/test_parser.py	(original)
+++ python/branches/release26-maint/Lib/lib2to3/tests/test_parser.py	Mon Mar 22 23:47:20 2010
@@ -209,6 +209,7 @@
     finally:
         f.close()
     try:
-        return os.system("diff -u %r @" % fn)
+        fn = fn.replace('"', '\\"')
+        return os.system('diff -u "%s" @' % fn)
     finally:
         os.remove("@")


More information about the Python-checkins mailing list