This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: from __future__ import print_function
Type: Stage:
Components: Interpreter Core Versions: Python 2.6
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: anthonybaxter, eric.smith, gvanrossum, loewis, nnorwitz, rhettinger, twouters
Priority: normal Keywords: patch

Created on 2007-01-12 07:13 by anthonybaxter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
print_function.patch anthonybaxter, 2007-01-12 07:13 print_function.patch
print_function.patch anthonybaxter, 2007-01-12 07:31 print_function.patch, version 2
print_function.patch3 anthonybaxter, 2007-01-29 12:37 print_function.patch, version 3
Messages (11)
msg51724 - (view) Author: Anthony Baxter (anthonybaxter) (Python triager) Date: 2007-01-12 07:13
This was done partly as a learning exercise, partly just as a vague idea that might prove to be practical (chatting with Neal at the time, but all blame is with me, not him!)

The following adds 'from __future__ import print_function' to 2.x. When this is enabled, 'print' is no longer a statement. Combined with copying bltinmodule.c:builtin_print() from the p3yk trunk, this should give some compatibility options for 2.6 <-> 3.0

Note that for some reason I don't fully understand, this doesn't work in interactive mode. For some reason, in interactive mode, the parser flags get reset for each line. Wah. 
msg51725 - (view) Author: Anthony Baxter (anthonybaxter) (Python triager) Date: 2007-01-12 07:31
Updated version of patch - fixes interactive mode, adds builtins.print

File Added: print_function.patch
msg51726 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-01-17 07:42
Guido, this is the patch I was talking about wrt supporting a print function in 2.6.  exec could get similar treatment.  You mentioned in mail that things like except E as V: can go in without a future stmt.  I agree.
msg51727 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-01-17 15:24
I don't think we need to do anything special for exec, as the exec(s, locals, globals) syntax is already (still :-) supported in 2.x with identical semantics as in 3.0.

except E as V *syntax* can go in without a future stmt; and (only when that syntax is used) it should also enforce the new semantics (V must be a simple name and is deleted at the end of the except clause).

I think Anthony's patch is a great idea, but I'll refrain from reviewing it.  I'd say "just do it". :-)
msg51728 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2007-01-17 15:58
You seem to have '#if 0'ed-out some code related to the with/as-statement warnings; I suggest just removing them. Since you're in this code now, it might make sense to provide a commented out warning about the use of the print statement, so we won't have to figure it out later (in Python 2.9 or when we add -Wp3yk.)

It needs a test, and probably a doc change somewhere.
msg51729 - (view) Author: Anthony Baxter (anthonybaxter) (Python triager) Date: 2007-01-29 12:37
Attached version 3 of the patch.

I've added an '#if 0'd warning in ast.c - for instance, when enabled, you get
./ SyntaxWarning: print no longer a statement in Py3.0
I'll make a new version of a -W py3k patch that enables this as well. 

I've made the other cleanup suggested by twouters. I'm not clear on the best way to do the tests for this - the from __future__ needs to be at the start of the file. My concern is that anything that tries to compile the whole test file with this under a previous version will choke and die on the print-as-function. Not sure if this is a hugely bad problem or not. 

Docs will follow once I bother wrapping my head around LaTeX and figuring out the best way to do the docs. I'm guessing we need a note in ref/ref6.tex in the section on the print statement, another bit in the same file in the subsection on Future statements, and something in lib/libbltin.tex. Did I miss anywhere?

In current 3.0, the builtin is called Print, not print. Is there a reason for this? Is it just a matter of updating all the tests and ripping out the support for the print statement and the related opcodes? If so, I'll tackle that next. Doing this does mean that the docs and the stdlib and the tests will all need a huge amount of updating, and it will make merging from the trunk to the p3yk branch much more painful.

While I'm in the vague area - why is PRINT_ITEM inlined in ceval.c? Couldn't it be punted out to a separate function, making the main switch statement just that little bit smaller? I can't imagine that making 'print' that little tiny bit faster is actually worthwhile, compared to shrinking the main switch statement.

except E as V, I'll look at later for a different patch. My tree is already getting quite cluttered already with uncommitted patches :-)
File Added: print_function.patch3
msg51730 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-01-29 17:07
Instead of __future__ imports, it would be better to put all of this Py3.0 stuff in a single compatability module and keep the rest of Py2.x as clean as possible.
msg51731 - (view) Author: Anthony Baxter (anthonybaxter) (Python triager) Date: 2007-01-30 06:19
That only works for a very small number of the changes. I can't see how we could change the parser using a module.

from __future__ import foo is the standard way we make forwards-compatible changes to Python. It's been that way for quite a while now - I don't see it as being controversial at all.
msg51732 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-02-07 08:32
rhettinger: I agree with anthonybaxter; implementing this in a module to import is not possible (unless you want *another* module that works like __future__ imports, i.e. where importing it isn't just a plain import - you could then just as well require to put "this is py3k" as the first line of the file). I wish PEP 244 would have been adopted, then you could "directive python3" at the top.

As for testing: I don't see it as a problem if the test suite only works with the version where the feature is introduced. See test_future* and test_with.

As for PRINT_ITEM: it was always inlined, since day 1. It has grown since, but apparently nobody ever bothered refactoring (on day 1, it was smaller, and the bulk of it was, and still is, in writeobject aka PyFile_WriteObject)
msg63829 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2008-03-18 00:48
I'm going to review Anthony's patch and attempt to get it working in the
current trunk.

I'm going to start by adding some print tests to 3.0, then backport.
msg64018 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2008-03-18 23:47
Checked in as r61577.
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44448
2008-03-18 23:47:20eric.smithsetstatus: open -> closed
resolution: accepted
messages: + msg64018
2008-03-18 00:48:28eric.smithsetmessages: + msg63829
2008-03-18 00:40:41eric.smithsetnosy: + eric.smith
2007-01-12 07:13:37anthonybaxtercreate