Commit bb0c2d11 authored by Shane Hathaway's avatar Shane Hathaway

Enabled tests to run both from the command line and by testrunner.

parent ad4b3de1
...@@ -9,21 +9,15 @@ import unittest ...@@ -9,21 +9,15 @@ import unittest
from RestrictedPython import compile_restricted, PrintCollector from RestrictedPython import compile_restricted, PrintCollector
from RestrictedPython.Eval import RestrictionCapableEval from RestrictedPython.Eval import RestrictionCapableEval
import security_in_syntax import security_in_syntax
from types import FunctionType
def package_home(globals_dict): if __name__=='__main__':
__name__=globals_dict['__name__'] sys.path.append(os.path.join(os.pardir, os.pardir))
if __name__ == '__main__': here = os.curdir
return os.getcwd() else:
m=sys.modules[__name__] from App.Common import package_home
if hasattr(m,'__path__'): from RestrictedPython import tests
r=m.__path__[0] here = package_home(tests.__dict__)
elif "." in __name__:
r=sys.modules[__name__[:rfind(__name__,'.')]].__path__[0]
else:
r=__name__
return os.path.join(os.getcwd(), r)
FunctionType = type(package_home)
def _getindent(line): def _getindent(line):
"""Returns the indentation level of the given line.""" """Returns the indentation level of the given line."""
...@@ -55,7 +49,7 @@ def find_source(fn, func): ...@@ -55,7 +49,7 @@ def find_source(fn, func):
def create_rmodule(): def create_rmodule():
global rmodule global rmodule
fn = os.path.join(package_home(globals()), 'restricted_module.py') fn = os.path.join(here, 'restricted_module.py')
f = open(fn, 'r') f = open(fn, 'r')
source = f.read() source = f.read()
f.close() f.close()
...@@ -64,10 +58,11 @@ def create_rmodule(): ...@@ -64,10 +58,11 @@ def create_rmodule():
# Now compile it for real # Now compile it for real
code = compile_restricted(source, fn, 'exec') code = compile_restricted(source, fn, 'exec')
rmodule = {'__builtins__':None} rmodule = {'__builtins__':None}
builtins = getattr(__builtins__, '__dict__', __builtins__)
for name in ('map', 'reduce', 'int', 'pow', 'range', 'filter', for name in ('map', 'reduce', 'int', 'pow', 'range', 'filter',
'len', 'chr', 'ord', 'len', 'chr', 'ord',
): ):
rmodule[name] = getattr(__builtins__, name) rmodule[name] = builtins[name]
exec code in rmodule exec code in rmodule
create_rmodule() create_rmodule()
...@@ -202,7 +197,7 @@ class RestrictionTests(unittest.TestCase): ...@@ -202,7 +197,7 @@ class RestrictionTests(unittest.TestCase):
def checkSyntaxSecurity(self): def checkSyntaxSecurity(self):
# Ensures that each of the functions in security_in_syntax.py # Ensures that each of the functions in security_in_syntax.py
# throws a SyntaxError when using compile_restricted. # throws a SyntaxError when using compile_restricted.
fn = os.path.join(package_home(globals()), 'security_in_syntax.py') fn = os.path.join(here, 'security_in_syntax.py')
f = open(fn, 'r') f = open(fn, 'r')
source = f.read() source = f.read()
f.close() f.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment