Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
robertwb opened this issue Sep 11, 2008 · 5 comments
Closed

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

robertwb opened this issue Sep 11, 2008 · 5 comments

Comments

@robertwb
Copy link
Contributor

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+.

Migrated from http://trac.cython.org/ticket/69

@robertwb
Copy link
Contributor Author

@dagss changed cc to stefan_ml@behnel.de, dagss@student.matnat.uio.no
commented

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(...): ...

print(...)

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

@robertwb
Copy link
Contributor Author

scoder changed description from

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+.

to

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+.
commented

@robertwb
Copy link
Contributor Author

robertwb commented Mar 10, 2010

scoder commented

The future import was implemented for ticket http://trac.cython.org/ticket/516, so this ticket is reduced to providing a fallback implementation for Python versions before 2.6.

@robertwb
Copy link
Contributor Author

robertwb commented Jun 10, 2010

@bhy changed cc from stefan_ml@behnel.de, dagss@student.matnat.uio.no to stefan_ml@behnel.de, dagss@student.matnat.uio.no, biahaoyu@gmail.com
commented

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 (http://trac.cython.org/ticket/87).

@robertwb
Copy link
Contributor Author

scoder changed resolution to wontfix
status from new to closed
commented

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

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

No branches or pull requests

1 participant