Opened 7 years ago

Closed 11 months ago

#69 closed enhancement (wontfix)

Cython should emulate the Py3 print() function in Py2 < 2.6

Reported by: Stefan Behnel Owned by: somebody
Priority: minor Milestone: wishlist
Component: Code Generation Keywords:
Cc: stefan_ml@…, dagss@…, biahaoyu@…

Description (last modified by scoder)

Given the whitespace semantics of the Py2 print statement which are hard to emulate in Py3, it would be nice if Cython could provide the print() function in a portable way, depending on the "print_function" future import. Code that requires specific semantics could then express them in a portable way using the print function.

This is not trivial, as it would require different code in Py2 versions before 2.6. Py2.6 and later should delegate directly to the normal __builtins__.print function, wheres earlier Py2 versions must delegate to an equivalent function provided by the module itself. Python's print() function uses an arbitrary number of positional arguments and a number of keyword-only arguments. This cannot currently be expressed using the normal builtins declaration mechanism. On the other hand, it is rather trivial to implement the print() function in pure Cython.

If emulation also under Py2.6+ is acceptable, we could inline the print function call using a transform. However, maybe we could just provide a Cython-implemented builtin-function as a private Python function in the module. In this case, some special casing could take care of replacing the function by the real builtin in Py2.6+.

Change History (5)

comment:1 Changed 7 years ago by dagss@…

  • Cc stefan_ml@… dagss@… added

There are problems with using using transforms for this.

a) If you want to do it prior to scope analysis, code like this likely won't work properly:

def print(...): ...


b) So, it should be done after scope analysis. But then the scope analysis won't find the entry in the first place.

comment:2 Changed 7 years ago by scoder

  • Description modified (diff)

comment:3 Changed 5 years ago by scoder

The future import was implemented for ticket #516, so this ticket is reduced to providing a fallback implementation for Python versions before 2.6.

comment:4 Changed 5 years ago by haoyu

  • Cc biahaoyu@… added

I'm trying to insert a print function before the actual code, like this:

if __import__('sys').version_info < (2,6):
    def print(*args, sep=' ', end='\n', file=None):

but the problem is def in control structure cannot work properly yet (#87).

comment:5 Changed 11 months ago by scoder

  • Resolution set to wontfix
  • Status changed from new to closed

Waiting long enough can help, Cython is Py2.6+ now.

Note: See TracTickets for help on using tickets.