Beispiel #1
0
 def test_CompiledCode_explicit_writable(self):
     import StringIO
     f = StringIO.StringIO()
     code = rcompile.compile("print >> f, 'hi',\nprint >> f, 'world'", '',
                             'exec')
     exec code in {'f': f}
     self.assertEqual(f.getvalue(), 'hi world\n')
Beispiel #2
0
    def test_CompiledCode_default_output(self):
        def _exec(code, locals):
            exec code in locals

        code = rcompile.compile("print 'hi',\nprint 'world'", '', 'exec')
        self.assertRaises(NameError, _exec, code, {})
        import StringIO
        f = StringIO.StringIO()
        _exec(code, {'untrusted_output': f})
        self.assertEqual(f.getvalue(), 'hi world\n')
 def __init__(self, source, filename='<string>'):
     self.source = source
     self.code = compile(source, filename, 'exec')
def exec_src(source, globals, locals=None):
    globals['__builtins__'] = SafeBuiltins
    code = compile(source, '<string>', 'exec')
    exec code in globals, locals
Beispiel #5
0
 def test_CompiledCode_statements(self):
     exec rcompile.compile("x = 1", "<string>", "exec")
     self.assertEqual(x, 1)
Beispiel #6
0
 def test_CompiledCode_simple(self):
     code = rcompile.compile("21 * 2", "<string>", "eval")
     self.assertEqual(eval(code), 42)
     self.assertRaises(TypeError, rcompile.compile, object(), '<string>',
                       'eval')
Beispiel #7
0
 def _compile(self, text, filename):
     return rcompile.compile(text, filename, 'eval')
Beispiel #8
0
 def _compile(self, text, filename):
     return rcompile.compile(text, filename, 'eval')
Beispiel #9
0
 def __init__(self, source, filename='<string>'):
     self.source = source
     self.code = compile(source, filename, 'exec')
Beispiel #10
0
def exec_src(source, globals, locals=None):
    globals['__builtins__'] = SafeBuiltins
    code = compile(source, '<string>', 'exec')
    exec code in globals, locals