Skip to content

[in progress] python 2 and 3 compatibility without 2to3#2095

Closed
Carreau wants to merge 21 commits into
ipython:masterfrom
Carreau:2to3compat
Closed

[in progress] python 2 and 3 compatibility without 2to3#2095
Carreau wants to merge 21 commits into
ipython:masterfrom
Carreau:2to3compat

Conversation

@Carreau

@Carreau Carreau commented Jul 4, 2012

Copy link
Copy Markdown
Member

Logical step after #2064 where the discussion started.

Most of the fixes apply by 2to3 when converting the current source tree to python 3 can be applied and are valid python 2.6 and 2.7 (not 2.5 but we don't support it), with minimal changes.

The plan is here to apply theses changes as early as possible ans deactivate them when using 2to3 in order to get a python2 and python3 compatible IPython code.

For what I've seen right now, 2to3 is sometime a little overzealous with print, converting
print 'xxx', to print('xxx', end=' ')(space) but in some place we should have end=''(no space),
and it sometime also change print ('an'+'expression') to print(('an'+'expression')). I detected and fixed some by hand because of failling test, but might have missed a few.

The other 2 fixes I added was getting rid of apply in some places, and convert exec statement to a function.

My guess is the harder part will be unicode strings prefix that is not valid python 3.0 to 3.2 and valid again python 3.3. One of our option might be from __future__ import unicode_literals.

It would be really cool to be able to do python3 ipython.py

Note : This does not merge cleanly right now and is on top of #2064, we can either close #2064 then i'll rebase the all on top of master, or I'll rebase when #2064 is merged.

@takluyver

Copy link
Copy Markdown
Member

Thanks, I've been thinking of aiming for this.

I think exec is actually the trickiest bit - the function form won't work under Python 2 if you're specifying the namespaces, as far as I know. The workaround would be to declare our own exec_ function, like six does: https://bitbucket.org/gutworth/six/src/0c712d55e12c/six.py#cl-297 (In fact, we could use six, since our own py3compat module duplicates quite a bit of its functionality).

For unicode strings, I think the approach Django took was to use unicode_literals, and then use b'bytes', 'unicode' and n('native string'), as there are relatively few cases where the latter is needed. So long as your native strings are all ascii, n = str works.

@Carreau

Carreau commented Jul 4, 2012

Copy link
Copy Markdown
Member Author

I removed the exec fix, i can push this branch onto the main repository so that everyone can have acces to it.

@minrk

minrk commented Jul 4, 2012

Copy link
Copy Markdown
Member

Please do not do this. Maintaining Python3 compatible source in Python 2 is an ugly pain.

@takluyver

Copy link
Copy Markdown
Member

I'm not sure it's necessarily much worse, given that we're only
supporting >= 2.6 anyway, and we've already got compatibility
functions around. The compatible source approach is increasingly
popular.

I suggest we just progress one step at a time, as we've done with the
"except/as" syntax. As we get to the trickier changes, we can decide
whether the benefits outweigh the costs.

@minrk

minrk commented Jul 4, 2012

Copy link
Copy Markdown
Member

My main objection is the print function, which I really, really hate.

@minrk

minrk commented Jul 4, 2012

Copy link
Copy Markdown
Member

Counterpoints to my own argument:

  • python3 setupegg.py develop would be awesome.
  • Our current occasional print_function use is inconsistent.

But I still would vastly prefer to not apply print_function everywhere. At least not yet.

@takluyver

Copy link
Copy Markdown
Member

My own plan was to apply it first in places where we're using the
print >>foo syntax, because I find that particularly ugly. But in
other cases, the difference is just a couple of parentheses, so I
don't have a problem with using it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra parenthesis here (and a couple lines below) are not needed.

@bfroehle

bfroehle commented Jul 4, 2012

Copy link
Copy Markdown
Contributor

I'd definitely be in favor of this. Currently developing IPython for Python 3 is a pain as any change you make requires at least running python3 setup.py build.

As an aside, Python 3 is starting to reach such a critical mass of supported packages at the distribution level (i.e., in Debian Wheezy / Ubuntu Precise) that I'm now finding myself often using Python 3 as my go-to Python intepreter.

@takluyver

Copy link
Copy Markdown
Member

That's good to hear. I know Ubuntu is also pushing to port core
applications for Quantal, and with Python 3.3 on the way, it's picking
up speed rapidly.

@bfroehle

bfroehle commented Jul 4, 2012

Copy link
Copy Markdown
Contributor

Aside: if only matplotlib could get their act together and create a new release...

@minrk

minrk commented Jul 4, 2012

Copy link
Copy Markdown
Member

My pattern has been similar to @takluyver's - when more complicated behavior is called for, I add print_function, but not before. I think to do so across all of IPython is a huge net detriment.

@takluyver

Copy link
Copy Markdown
Member

Shall we start by doing these things that I think are uncontroversial:

  • backticks --> repr()
  • Remove the couple of uses of apply()
  • Use print_function in modules where we have the print >>foo syntax only.

I suggest we close this PR and open a new one for those changes - this one is unwieldy, and already has a merge conflict.

We could also express a preference that new modules added to the codebase should use print_function.

@minrk

minrk commented Jul 4, 2012

Copy link
Copy Markdown
Member

@takluyver's suggestion seems sensible to me. code-wide print_function is a major style change, and code-wide unicode_literals will likely be a big mess (and I expect not worth it, and we should just wait for 3.3 on that one). But this PR includes several helpful little changes, which will be dwarfed by the discussion of others.

@Carreau

Carreau commented Jul 5, 2012

Copy link
Copy Markdown
Member Author

ok, closing, and reopening as #2100 with @takluyver sugestion.

@fperez

fperez commented Jul 5, 2012

Copy link
Copy Markdown
Member

Closing (I think you forgot to actually close it, @Carreau :)

@fperez fperez closed this Jul 5, 2012
This was referenced Jul 13, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants