Skip to content

Commit

Permalink
added more elements for print
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Sep 29, 2016
1 parent bb7ab55 commit 28794e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/RestrictedPython/PrintCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
from __future__ import print_function

import sys


version = sys.version_info


class PrintCollector(object):
Expand All @@ -23,3 +29,20 @@ def write(self, text):

def __call__(self):
return ''.join(self.txt)


printed = PrintCollector()


def safe_print(sep=' ', end='\n', file=printed, flush=False, *objects):
"""
"""
# TODO: Reorder method args so that *objects is first
# This could first be done if we drop Python 2 support
if file is None or file is sys.stdout or file is sys.stderr:
file = printed
if version >= (3, 3):
print(self, objects, sep=sep, end=end, file=file, flush=flush)
else:
print(self, objects, sep=sep, end=end, file=file)
17 changes: 12 additions & 5 deletions tests/test_print.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from RestrictedPython import compile_restricted
from RestrictedPython import compile_restricted_exec
from RestrictedPython import compile_restricted_eval
from RestrictedPython import compile_restricted_exec
from RestrictedPython import compile_restricted_function
from RestrictedPython.PrintCollector import PrintCollector
from RestrictedPython.PrintCollector import printed
from RestrictedPython.PrintCollector import safe_print

import pytest
import sys


ALLOWED_PRINT_STATEMENT = """\
Expand Down Expand Up @@ -52,8 +58,9 @@
print('Hello World!', file=sys.stderr)
"""


@pytest.mark.skipif(sys.version_info >= (3, 0),
reason="print statement no longer exists in Python 3")
def test_print__simple_print_statement():
#code, err, warn, use = compile_restricted_exec(ALLOWED_PRINT, '<undefined>')
#exec(code)
#assert 'code' == code
pass
code, err, warn, use = compile_restricted_exec(ALLOWED_PRINT_STATEMENT, '<undefined>')
exec(code)
4 changes: 2 additions & 2 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from RestrictedPython import compile_restricted
from RestrictedPython import compile_restricted_exec
from RestrictedPython import compile_restricted_eval
#from RestrictedPython import compile_restricted_single
from RestrictedPython import compile_restricted_exec
from RestrictedPython import compile_restricted_function
from RestrictedPython import compile_restricted_single

import pytest
import sys
Expand Down

0 comments on commit 28794e1

Please sign in to comment.