def test_pymain_ver(runtime): from golang import b from gpython import _version_info_str as V import gevent vok = 'GPython %s' % golang.__version__ if runtime != 'threads': vok += ' [gevent %s]' % gevent.__version__ else: vok += ' [threads]' if is_cpython: vok += ' / CPython %s' % platform.python_version() elif is_pypy: vok += ' / PyPy %s / Python %s' % (V( sys.pypy_version_info), V(sys.version_info)) else: vok = sys.version vok += '\n' ret, out, err = _pyrun(['-V'], stdout=PIPE, stderr=PIPE, env=gpyenv(runtime)) assert (ret, out, b(err)) == (0, b'', b(vok)) ret, out, err = _pyrun(['--version'], stdout=PIPE, stderr=PIPE, env=gpyenv(runtime)) assert (ret, out, b(err)) == (0, b'', b(vok))
def test_new(): E = errors.New for x in ["мир", u"мир", b("мир")]: err = E(x) assert type(err) is error assertEeq(err, E("мир")) assertEne(err, E("def")) assertEeq(err, error("мир")) assertEeq(err, error(u"мир")) assertEeq(err, error(b("мир"))) with raises(TypeError): E(1)
def test_pymain__main__(): from golang import b check_main_py = readfile('%s/check_main.py' % testprog) pyrun(['testprog/check_main.py'], cwd=here) # file pyrun(['-m', 'check_main'], cwd=testprog) # -m pyrun(['-c', check_main_py]) # -c # stdin ret, out, err = _pyrun([], stdin=b(check_main_py), stdout=PIPE, stderr=PIPE) assert ret == 0, (out, err) assert b"Error" not in out, (out, err) assert b"Error" not in err, (out, err)
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 zodbdump(stor, tidmin, tidmax, hashonly=False, pretty='raw', out=asbinstream(sys.stdout)): def badpretty(): raise ValueError("invalid pretty format %s" % pretty) for txn in stor.iterator(tidmin, tidmax): # XXX .status not covered by IStorageTransactionInformation # XXX but covered by BaseStorage.TransactionRecord out.write(b"txn %s %s\nuser %s\ndescription %s\n" % (ashex( txn.tid), qq(txn.status), qq(txn.user), qq(txn.description))) # extension is saved by ZODB as either empty or as pickle dump of an object rawext = txn_raw_extension(stor, txn) if pretty == 'raw': out.write(b"extension %s\n" % qq(rawext)) elif pretty == 'zpickledis': if len(rawext) == 0: out.write(b'extension ""\n') else: out.write(b"extension\n") extf = BytesIO(rawext) disf = BytesIO() pickletools.dis(extf, disf) out.write(indent(disf.getvalue(), " ")) extra = extf.read() if len(extra) > 0: out.write(b" + extra data %s\n" % qq(extra)) else: badpretty() objv = txnobjv(txn) for obj in objv: entry = b"obj %s " % ashex(obj.oid) write_data = False if obj.data is None: entry += b"delete" # was undo and data taken from obj.data_txn elif obj.data_txn is not None: entry += b"from %s" % ashex(obj.data_txn) else: # XXX sha1 is hardcoded for now. Dump format allows other hashes. entry += b"%i sha1:%s" % (len(obj.data), ashex(sha1(obj.data))) write_data = True out.write(b(entry)) if write_data: if hashonly: out.write(b" -") else: out.write(b"\n") if pretty == 'raw': out.write(obj.data) elif pretty == 'zpickledis': # https://github.com/zopefoundation/ZODB/blob/5.6.0-55-g1226c9d35/src/ZODB/serialize.py#L24-L29 dataf = BytesIO(obj.data) disf = BytesIO() pickletools.dis(dataf, disf) # class pickletools.dis(dataf, disf) # state out.write(indent(disf.getvalue(), " ")) extra = dataf.read() if len(extra) > 0: out.write(b" + extra data %s\n" % qq(extra)) else: badpretty() out.write(b"\n") out.write(b"\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_parse_tid_time_format(fake_time, reference_time, reference_tid, input_time): assert b(reference_tid) == ashex(parse_tid(input_time)) # check that the reference_tid matches the reference time, mainly # to check that input is defined correctly. assert b(reference_tid) == ashex(parse_tid(reference_time))
def test_error(): assert error_mkchain([]) is None e = error_mkchain(["abc"]) assert type(e) is error assert e.Error() == "abc" assert str(e) == "abc" assert repr(e).endswith(' error="abc">') assert e.Unwrap() is None assertEeq(e, e) e1 = e e = error_mkchain(["привет", "abc"]) assert type(e) is error assert e.Error() == "привет: abc" assert str(e) == "привет: abc" assert repr(e).endswith(' error="привет: abc">') e1_ = e.Unwrap() assert type(e1_) is type(e1) assertEeq(e1_, e1) assert e1_.Unwrap() is None assertEeq(e, e) assertEne(e, e1) e2 = e # create an error from py via error() call with raises(ValueError): error() with raises(ValueError): error("hello", "world") for x in ["hello мир", u"hello мир", b("hello мир")]: e = error(x) assert type(e) is error assert e.Error() == "hello мир" assert str(e) == "hello мир" assert repr(e).endswith(' error="hello мир">') assert e.Unwrap() is None assertEeq(e, e) # create an error from py via error subclass class EErr(error): pass m = EErr("abc") assertEeq(m, m) assertEne(m, error("abc")) # EErr("abc") != error("abc") epy = EError("load", 3) assert type(epy) is EError assert epy.Error() == "my load: 3" assert str(epy) == "my load: 3" assert repr(epy) == "golang.errors_test.EError('load', 3)" assert epy.Unwrap() is None assertEeq(epy, epy) assertEeq(epy, EError("load", 3)) assertEne(epy, EError("load", 4)) wpy = EErrorWrap("mywrap", epy) assert type(wpy) is EErrorWrap assert wpy.Error() == "mywrap: my load: 3" assert str(wpy) == "mywrap: my load: 3" assert repr( wpy ) == "golang.errors_test.EErrorWrap('mywrap', golang.errors_test.EError('load', 3))" assert wpy.Unwrap() is epy assert epy.Unwrap() is None epy = RuntimeError("zzz") wpy = EErrorWrap("qqq", epy) assert type(wpy) is EErrorWrap assert wpy.Error() == "qqq: zzz" assert str(wpy) == "qqq: zzz" assert repr(wpy) == "golang.errors_test.EErrorWrap('qqq', %r)" % epy assert wpy.Unwrap() is epy with raises(AttributeError): epy.Unwrap
def main(): sb = b("привет b") su = u("привет u") print("print(qq(b)):", qq(sb)) print("print(qq(u)):", qq(su))
def test_pyx_os_fileio_cpp(tmp_path): _test_os_fileio_cpp(b(str(tmp_path)))