| 1 |
|
| 2 |
from cStringIO import StringIO |
| 3 |
|
| 4 |
import py |
| 5 |
from rpython.tool.udir import udir |
| 6 |
from pypy.interpreter.baseobjspace import OperationError |
| 7 |
from pypy.interpreter import main |
| 8 |
|
| 9 |
testcode = """\ |
| 10 |
def main(): |
| 11 |
aStr = 'hello world' |
| 12 |
print(len(aStr)) |
| 13 |
|
| 14 |
main() |
| 15 |
""" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
testmodulecode = """ |
| 20 |
import sys |
| 21 |
if __name__ == '__main__': |
| 22 |
aStr = sys.argv[1] |
| 23 |
print(len(aStr)) |
| 24 |
""" |
| 25 |
|
| 26 |
testresultoutput = '11\n' |
| 27 |
|
| 28 |
|
| 29 |
def checkoutput(space, expected_output, f, *args): |
| 30 |
w_oldout = space.sys.get('stdout') |
| 31 |
capturefn = udir.join('capturefile') |
| 32 |
w_capturefile = space.call_method(space.builtin, "open", space.wrap(str(capturefn)), space.wrap("w")) |
| 33 |
w_sys = space.sys.getmodule('sys') |
| 34 |
space.setattr(w_sys, space.wrap("stdout"), w_capturefile) |
| 35 |
try: |
| 36 |
f(*(args + (space,))) |
| 37 |
finally: |
| 38 |
space.setattr(w_sys, space.wrap("stdout"), w_oldout) |
| 39 |
space.call_method(w_capturefile, "close") |
| 40 |
assert capturefn.read(mode='rU') == expected_output |
| 41 |
|
| 42 |
testfn = udir.join('tmp_hello_world.py') |
| 43 |
testmodule = 'tmp_hello_module' |
| 44 |
testpackage = 'tmp_package' |
| 45 |
|
| 46 |
class TestMain: |
| 47 |
def setup_class(cls): |
| 48 |
testfn.write(testcode, 'w') |
| 49 |
udir.join(testmodule + '.py').write(testmodulecode, 'w') |
| 50 |
udir.ensure(testpackage, '__init__.py') |
| 51 |
udir.join(testpackage, testmodule + '.py').write(testmodulecode, 'w') |
| 52 |
space = cls.space |
| 53 |
cls.w_oldsyspath = space.appexec([space.wrap(str(udir))], """(udir): |
| 54 |
import sys |
| 55 |
old = sys.path[:] |
| 56 |
sys.path.insert(0, udir) |
| 57 |
return old |
| 58 |
""") |
| 59 |
|
| 60 |
def teardown_class(cls): |
| 61 |
cls.space.appexec([cls.w_oldsyspath], """(old): |
| 62 |
import sys |
| 63 |
sys.path[:] = old |
| 64 |
""") |
| 65 |
|
| 66 |
def test_run_file(self): |
| 67 |
checkoutput(self.space, testresultoutput, main.run_file, str(testfn)) |
| 68 |
|
| 69 |
def test_run_string(self): |
| 70 |
checkoutput(self.space, testresultoutput, |
| 71 |
main.run_string, testcode, str(testfn)) |
| 72 |
|
| 73 |
def test_eval_string(self): |
| 74 |
w_x = main.eval_string('2+2', space=self.space) |
| 75 |
assert self.space.eq_w(w_x, self.space.wrap(4)) |
| 76 |
|
| 77 |
def test_run_module(self): |
| 78 |
checkoutput(self.space, testresultoutput, main.run_module, |
| 79 |
testmodule, ['hello world']) |
| 80 |
checkoutput(self.space, testresultoutput, main.run_module, |
| 81 |
testpackage + '.' + testmodule, ['hello world']) |
| 82 |
|
| 83 |
|
| 84 |
class TestMainPEP3120: |
| 85 |
def setup_class(cls): |
| 86 |
|
| 87 |
|
| 88 |
testfn.write("""print('日本'.encode('utf-8'), end='')""", 'wb') |
| 89 |
space = cls.space |
| 90 |
cls.w_oldsyspath = space.appexec([space.wrap(str(udir))], """(udir): |
| 91 |
import sys |
| 92 |
old = sys.path[:] |
| 93 |
sys.path.insert(0, udir) |
| 94 |
return old |
| 95 |
""") |
| 96 |
|
| 97 |
def teardown_class(cls): |
| 98 |
cls.space.appexec([cls.w_oldsyspath], """(old): |
| 99 |
import sys |
| 100 |
sys.path[:] = old |
| 101 |
""") |
| 102 |
|
| 103 |
def test_pep3120(self): |
| 104 |
tf-8 |
| 105 |
|
| 106 |
checkoutput(self.space, 'b' + repr(u'日本'.encode('utf-8')), |
| 107 |
main.run_file, str(testfn)) |