★ wanayoo — archive 1999 https://github.com/ipython/ipython/pull/2134/Nouvelle recherche | Portail wanayoo
Skip to content
This repository

Apply 2to3 `next` fix. #2134

Merged
merged 1 commit into from almost 2 years ago

3 participants

Bradley M. Froehle Thomas Kluyver Min RK
Bradley M. Froehle
Collaborator

Continuing on the Python 2/3 compatibility (which I'll refer to as '2and3') from #2095 and #2100, I propose to include the changes from running 2to3 -f next:

Converts the use of iterator’s next() methods to the next() function. It also renames next() methods to __next__().

One small issue is the behavior of next(x) on user defined classes:

Consider test_next.py

class X(object):
    def __init__(self, iterable):
        self.iterator = iter(iterable)
    def __iter__(self):
        return self
    def __next__(self):
        print("__next__ called")
        return next(self.iterator)
    def next(self):
        print("next called")
        return next(self.iterator)

x = X([1,2,3])
next(x)

And run it in Python 2.7 and 3.2:

$ python2.7 next_test.py
next called
$ python3.2 next_test.py
__next__ called

To work around this difference, I've just set next = __next__ manually after applying the 2to3 fix. This also maintains compatibility with any third party code which calls x.next().

Thomas Kluyver
Collaborator

Test results for commit 360c5cd merged into master
Platform: linux2

  • python2.7: OK (libraries not available: oct2py rpy2)
  • python3.1: OK (libraries not available: cython matplotlib numpy oct2py pymongo qt rpy2 wx wx.aui zmq)
  • python3.2: OK (libraries not available: cython oct2py pymongo rpy2 wx wx.aui)

Not available for testing: python2.6

IPython/external/pexpect/_pexpect.py
... ...
@@ -940,6 +940,8 @@ def next (self):    # File-like object.
940 940
             raise StopIteration
941 941
         return result
942 942
 
  943
+    next = __next__    # File-like object. (Python 2).
3
Bradley M. Froehle Collaborator
bfroehle added a note

Alternatively we could treat this as:

    def __next(self)__:
        # ...

    if not py3compat.PY3:
        next = __next__

This would make it more obvious what needs to be ripped out if Python 2 support was ever dropped...

Thomas Kluyver Collaborator
takluyver added a note

I think I'd side with doing that.

Bradley M. Froehle Collaborator
bfroehle added a note

I agree. I've made the changes and pushed a new commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Bradley M. Froehle bfroehle Apply 2to3 `next` fix. …
Manually set `next = __next__` for Python 2 support.
30e4a14
Thomas Kluyver
Collaborator

Test results for commit 30e4a14 merged into master
Platform: linux2

  • python2.7: OK (libraries not available: oct2py rpy2)
  • python3.1: OK (libraries not available: cython matplotlib numpy oct2py pymongo qt rpy2 wx wx.aui zmq)
  • python3.2: OK (libraries not available: cython oct2py pymongo rpy2 wx wx.aui)

Not available for testing: python2.6

Thomas Kluyver
Collaborator

This looks OK to me. If no-one objects in about a day, we can merge it.

Min RK minrk commented on the diff
IPython/zmq/iostream.py
((6 lines not shown))
58 59
         raise IOError('Read not supported on a write only stream.')
59 60
 
  61
+    if not py3compat.PY3:
  62
+        next = __next__
  63
+
1
Min RK Owner
minrk added a note

Is there a good reason that this method is defined in a conditional, and not just next = __next__? I know the method is not required in py3, but it seems cleaner to just define it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Min RK
Owner
minrk commented

Ah, now I just saw the conversation above. I don't really agree with the decision, but I don't feel strongly, so I'll go with you guys. Also, Python 2.6 testing is the most important here before merge.

Min RK
Owner
minrk commented

Test results for commit 30e4a14 merged into master
Platform: darwin

  • python2.6: OK (libraries not available: cython matplotlib oct2py pygments pymongo qt rpy2 tornado wx wx.aui)
  • python2.7: OK (libraries not available: oct2py wx wx.aui)
  • python3.2: OK (libraries not available: cython matplotlib oct2py pymongo qt rpy2 wx wx.aui)

Not available for testing: python3.1

Min RK
Owner
minrk commented

2.6 is happy, +1 to merge.

Thomas Kluyver takluyver merged commit 87d86c1 into from
Thomas Kluyver takluyver closed this
Thomas Kluyver
Collaborator

Merged - thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jul 13, 2012
Bradley M. Froehle bfroehle Apply 2to3 `next` fix. …
Manually set `next = __next__` for Python 2 support.
30e4a14
This page is out of date. Refresh to see the latest.
4 IPython/core/completer.py
@@ -718,10 +718,10 @@ def python_func_kw_matches(self,text):
718 718
         isId = re.compile(r'\w+$').match
719 719
         while True:
720 720
             try:
721  
-                ids.append(iterTokens.next())
  721
+                ids.append(next(iterTokens))
722 722
                 if not isId(ids[-1]):
723 723
                     ids.pop(); break
724  
-                if not iterTokens.next() == '.':
  724
+                if not next(iterTokens) == '.':
725 725
                     break
726 726
             except StopIteration:
727 727
                 break
5 IPython/external/pexpect/_pexpect.py
@@ -930,7 +930,7 @@ def __iter__ (self):    # File-like object.
930 930
 
931 931
         return self
932 932
 
933  
-    def next (self):    # File-like object.
  933
+    def __next__ (self):    # File-like object.
934 934
 
935 935
         """This is to support iterators over a file-like object.
936 936
         """
@@ -940,6 +940,9 @@ def next (self):    # File-like object.
940 940
             raise StopIteration
941 941
         return result
942 942
 
  943
+    if not PY3:
  944
+        next = __next__    # File-like object.
  945
+
943 946
     def readlines (self, sizehint = -1):    # File-like object.
944 947
 
945 948
         """This reads until EOF using readline() and returns a list containing
2  IPython/frontend/qt/console/pygments_highlighter.py
@@ -174,7 +174,7 @@ def _get_format(self, token):
174 174
     def _get_format_from_document(self, token, document):
175 175
         """ Returns a QTextCharFormat for token by
176 176
         """
177  
-        code, html = self._formatter._format_lines([(token, u'dummy')]).next()
  177
+        code, html = next(self._formatter._format_lines([(token, u'dummy')]))
178 178
         self._document.setHtml(html)
179 179
         return QtGui.QTextCursor(self._document).charFormat()
180 180
 
2  IPython/testing/_paramtestpy2.py
@@ -51,7 +51,7 @@ def run_parametric(self, result, testMethod):
51 51
                 # Test execution
52 52
                 ok = False
53 53
                 try:
54  
-                    testgen.next()
  54
+                    next(testgen)
55 55
                     ok = True
56 56
                 except StopIteration:
57 57
                     # We stop the loop
2  IPython/utils/_process_common.py
@@ -179,7 +179,7 @@ def arg_split(s, posix=False, strict=True):
179 179
     tokens = []
180 180
     while True:
181 181
         try:
182  
-            tokens.append(lex.next())
  182
+            tokens.append(next(lex))
183 183
         except StopIteration:
184 184
             break
185 185
         except ValueError:
6 IPython/zmq/iostream.py
@@ -5,6 +5,7 @@
5 5
 from session import extract_header, Message
6 6
 
7 7
 from IPython.utils import io, text, encoding
  8
+from IPython.utils import py3compat
8 9
 
9 10
 #-----------------------------------------------------------------------------
10 11
 # Globals
@@ -54,9 +55,12 @@ def flush(self):
54 55
     def isatty(self):
55 56
         return False
56 57
 
57  
-    def next(self):
  58
+    def __next__(self):
58 59
         raise IOError('Read not supported on a write only stream.')
59 60
 
  61
+    if not py3compat.PY3:
  62
+        next = __next__
  63
+
60 64
     def read(self, size=-1):
61 65
         raise IOError('Read not supported on a write only stream.')
62 66
 
1  setup.py
@@ -272,6 +272,7 @@ def run(self):
272 272
                 'lib2to3.fixes.fix_except',
273 273
                 'lib2to3.fixes.fix_apply',
274 274
                 'lib2to3.fixes.fix_repr',
  275
+                'lib2to3.fixes.fix_next',
275 276
                 ]
276 277
         from setuptools.command.build_py import build_py
277 278
         setup_args['cmdclass'] = {'build_py': record_commit_info('IPython', build_cmd=build_py)}

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.