0

RestrictedPython module has a restricted compiler in which you can compile code and customize some python features. For example, you can replace the builtin print function.

That's what I need to do. I need to compile some code but defining my own print function. I can't use this restricted compiler because it has a lot of restrictions I don't need at now.

Do you know any other compiler in which I can define my own print function?

3
  • Why do you need to replace the print function? I'm pretty sure there's a better/easier way to achieve what you need. Commented Oct 1, 2015 at 16:08
  • I have to execute python code in runtime. I'm doing this by using the compile and exec statements. Something I'd like to do is to redirect all prints (from this code) to some predefined function. (security is unneeded) Commented Oct 1, 2015 at 16:15
  • More precisely, I'm developing a webpage and I like to redirect all prints to the browser. I've done it by using restricted python, but I can't use it anymore. Commented Oct 1, 2015 at 16:28

1 Answer 1

2

Just use regular Python then; in Python 2 use:

from __future__ import print_function

or use Python 3, and print() is then a function. You can redefine that function:

from __future__ import print_function
try:
    # Python 2
    from __builtin__ import print as builtin_print
except ImportError:
    from builtins import print as builtin_print

def print(*args, **kw):
    # do something extra with *args or **kw, etc.
    builtin_print(*args, **kw)

Like any other built-in function you can define your own function using the same name. In the above example I used the __builtin__ / builtins module to access the original.

If you are using exec(), you can pass in the print() function you defined as an extra name in the namespace you pass in:

exec(code_to_execute, {'print': your_print_function})

For Python 2, you do need to compile the code first to switch off the print statement and enable the print() function; use the compile() function to produce a code object to pass to the exec statement:

import __future__

code_to_execute = compile(
    code_to_execute_in_string, '', 'exec',
    flags=__future__.print_function.compiler_flag)

I used the __future__ module to obtain the right compiler flag.

Sign up to request clarification or add additional context in comments.

5 Comments

Not sure if that's a problem, but this will not change the behaviour of print in an imported module OP has no control over, right?
@tobias_k: it would not. You could use import builtins; builtins.print = your_print_function to alter the function everywhere (Python 3 spelling, adjust for Python 2 as needed).
I like to do it but just in code executed by using exec or eval.
@SebastianAlvarez: updated to include how to do this with a compiler flag in Python 2.
That's what I was looking for. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.