def test_pymain_print_function_future(): if PY2: _ = pyout([], stdin=b'print "print", "is", "a", "statement"\n') assert _ == b"print is a statement\n" _ = pyout(['-c', 'print "print", "is", "a", "statement"']) assert _ == b"print is a statement\n" _ = pyout(['print_statement.py'], cwd=testprog) assert _ == b"print is a statement\n" _ = pyout(['future_print_function.py'], cwd=testprog) assert _ == b"print is a function with print_function future\n"
def check_gpy_vs_py(argv, postprocessf=None, **kw): gpyout = u(pyout(argv, **kw)) stdpyout = u(pyout(argv, pyexe=sys._gpy_underlying_executable, **kw)) gpyoutv = gpyout.splitlines() stdpyoutv = stdpyout.splitlines() if postprocessf is not None: postprocessf(gpyoutv, stdpyoutv) assert gpyoutv == stdpyoutv
def test_pymain_run_via_relpath(): from gpython import _is_buildout_script if _is_buildout_script(sys.executable): pytest.xfail( "with buildout raw underlying interpreter does not have " + "access to installed eggs") argv = ['-c', 'import sys; print(sys.version)'] out1 = pyout(argv, pyexe=sys.executable) out2 = pyout(['./__init__.py'] + argv, pyexe=sys._gpy_underlying_executable, cwd=here) assert out1 == out2
def test_executable(): # sys.executable must point to gpython and we must be able to execute it. import gevent assert 'gpython' in sys.executable out = pyout(['-c', 'import sys; print(sys.version)']) assert ('[GPython %s]' % golang.__version__) in str(out) assert ('[gevent %s]' % gevent.__version__) in str(out)
def test_dso_build(): dsouser = testprog + "/golang_dso_user" pyrun(["setup.py", "build_dso", "-i"], cwd=dsouser) pyrun(["setup.py", "build_ext", "-i"], cwd=dsouser) # run built test. _ = pyout(["-c", # XXX `import golang` is a hack - see test_pyx_build for details. "import golang;" + "from dsouser import test; test.main()"], cwd=dsouser) assert _ == b"dso.cpp: OK\n"
def test_executable(runtime): # sys.executable must point to gpython and we must be able to execute it. import gevent assert 'gpython' in sys.executable ver = pyout(['-c', 'import sys; print(sys.version)'], env=gpyenv(runtime)) ver = str(ver) assert ('[GPython %s]' % golang.__version__) in ver if runtime != 'threads': assert ('[gevent %s]' % gevent.__version__) in ver assert ('[threads]') not in ver else: assert ('[gevent ') not in ver assert ('[threads]') in ver
def test_pyx_build(): pyxuser = testprog + "/golang_pyx_user" pyrun(["setup.py", "build_ext", "-i"], cwd=pyxuser) # run built test. _ = pyout(["-c", # XXX `import golang` is a hack: it dynamically loads _golang.so -> libgolang.so, # and this way dynamic linker already has libgolang.so DSO found and in # memory for all further imports. If we don't, current state of setuptools_dso # is that `import pyxuser.test` will fail finding libgolang.so. "import golang;" + "from pyxuser import test; test.main()"], cwd=pyxuser) assert _ == b"test.pyx: OK\n"
def test_pymain(): from golang import b from os.path import join, dirname, realpath here = dirname(__file__) testdata = join(dirname(__file__), 'testdata') # interactive _ = pyout([], stdin=b'import hello\n', cwd=testdata) assert _ == b"hello\nworld\n['']\n" # -c _ = pyout(['-c', 'import hello', 'abc', 'def'], cwd=testdata) assert _ == b"hello\nworld\n['-c', 'abc', 'def']\n" # -m _ = pyout(['-m', 'hello', 'abc', 'def'], cwd=testdata) # realpath rewrites e.g. `local/lib -> lib` if local/lib is symlink hellopy = realpath(join(testdata, 'hello.py')) assert _ == b"hello\nworld\n['%s', 'abc', 'def']\n" % b(hellopy) # file _ = pyout(['testdata/hello.py', 'abc', 'def'], cwd=here) assert _ == b"hello\nworld\n['testdata/hello.py', 'abc', 'def']\n"
def test_Xruntime(runtime): env = os.environ.copy() env.pop('GPYTHON_RUNTIME', None) # del argv = [] if runtime != '': argv += ['-X', 'gpython.runtime=' + runtime] prog = 'from gpython import gpython_test as t; ' if runtime != 'threads': prog += 't.assert_gevent_activated(); ' else: prog += 't.assert_gevent_not_activated(); ' prog += 'print("ok")' argv += ['-c', prog] out = pyout(argv, env=env) assert out == b'ok\n'
def test_pymain(): from golang import b # stdin _ = pyout([], stdin=b'import hello\n', cwd=testdata) assert _ == b"hello\nworld\n['']\n" _ = pyout(['-'], stdin=b'import hello\n', cwd=testdata) assert _ == b"hello\nworld\n['-']\n" _ = pyout(['-', 'zzz'], stdin=b'import hello\n', cwd=testdata) assert _ == b"hello\nworld\n['-', 'zzz']\n" # -c <command> _ = pyout(['-c', 'import hello', 'abc', 'def'], cwd=testdata) assert _ == b"hello\nworld\n['-c', 'abc', 'def']\n" # -c<command> should also work __ = pyout(['-cimport hello', 'abc', 'def'], cwd=testdata) assert __ == _ # -m <module> _ = pyout(['-m', 'hello', 'abc', 'def'], cwd=testdata) # realpath rewrites e.g. `local/lib -> lib` if local/lib is symlink hellopy = realpath(join(testdata, 'hello.py')) assert _ == b"hello\nworld\n['%s', 'abc', 'def']\n" % b(hellopy) # -m<module> __ = pyout(['-mhello', 'abc', 'def'], cwd=testdata) assert __ == _ # file _ = pyout(['testdata/hello.py', 'abc', 'def'], cwd=here) assert _ == b"hello\nworld\n['testdata/hello.py', 'abc', 'def']\n" # -i after stdin (also tests interactive mode as -i forces interactive even on non-tty) d = { b'hellopy': b(hellopy), b'ps1': b'' # cpython emits prompt to stderr } if is_pypy and not is_gpython: d[b'ps1'] = b'>>>> ' # native pypy emits prompt to stdout and >>>> instead of >>> _ = pyout(['-i'], stdin=b'import hello\n', cwd=testdata) assert _ == b"%(ps1)shello\nworld\n['']\n%(ps1)s" % d _ = pyout(['-i', '-'], stdin=b'import hello\n', cwd=testdata) assert _ == b"%(ps1)shello\nworld\n['-']\n%(ps1)s" % d _ = pyout(['-i', '-', 'zzz'], stdin=b'import hello\n', cwd=testdata) assert _ == b"%(ps1)shello\nworld\n['-', 'zzz']\n%(ps1)s" % d # -i after -c _ = pyout(['-i', '-c', 'import hello'], stdin=b'hello.tag', cwd=testdata) assert _ == b"hello\nworld\n['-c']\n%(ps1)s'~~HELLO~~'\n%(ps1)s" % d # -i after -m _ = pyout(['-i', '-m', 'hello'], stdin=b'world.tag', cwd=testdata) assert _ == b"hello\nworld\n['%(hellopy)s']\n%(ps1)s'~~WORLD~~'\n%(ps1)s" % d # -i after file _ = pyout(['-i', 'testdata/hello.py'], stdin=b'tag', cwd=here) assert _ == b"hello\nworld\n['testdata/hello.py']\n%(ps1)s'~~HELLO~~'\n%(ps1)s" % d # -W <opt> _ = pyout([ '-Werror', '-Whello', '-W', 'ignore::DeprecationWarning', 'testprog/print_warnings_setup.py' ], cwd=here) if PY2: # py2 threading, which is imported after gpython startup, adds ignore # for sys.exc_clear _ = grepv(r'ignore:sys.exc_clear:DeprecationWarning:threading:*', _) assert _.startswith( b"sys.warnoptions: ['error', 'hello', 'ignore::DeprecationWarning']\n\n" + \ b"warnings.filters:\n" + \ b"- ignore::DeprecationWarning::*\n" + \ b"- error::Warning::*\n"), _ # $PYTHONWARNINGS _ = pyout(['testprog/print_warnings_setup.py'], cwd=here, envadj={'PYTHONWARNINGS': 'ignore,world,error::SyntaxWarning'}) if PY2: # see ^^^ _ = grepv(r'ignore:sys.exc_clear:DeprecationWarning:threading:*', _) assert _.startswith( b"sys.warnoptions: ['ignore', 'world', 'error::SyntaxWarning']\n\n" + \ b"warnings.filters:\n" + \ b"- error::SyntaxWarning::*\n" + \ b"- ignore::Warning::*\n"), _
def test_pyx_build_cmdclass(): _ = pyout(["cmdclass_custom.py", "build_ext"], cwd=testprog) assert b"pyx.build:RUN_BUILD_EXT" in _