def test_translate(): from pypy.translator.c.test.test_genc import compile def func(): poll({}) compile(func, [])
def test_translated(self): from pypy.translator.c.test.test_genc import compile def func(no): m = mmap.mmap(no, 1) r = m.read_byte() m.close() return r compile(func, [int])
def test_os_path_isdir(): directory = "./." def fn(): return os.path.isdir(directory) f = compile(fn, []) assert f() == True directory = "some/random/name" def fn(): return os.path.isdir(directory) f = compile(fn, []) assert f() == False
def test_utimes(): if os.name != 'nt': py.test.skip('Windows specific feature') # Windows support centiseconds def f(fname, t1): os.utime(fname, (t1, t1)) fname = udir.join('test_utimes.txt') fname.ensure() t1 = 1159195039.25 compile(f, (str, float))(str(fname), t1) assert t1 == os.stat(str(fname)).st_mtime
def test_rarith_float_to_str(): def fn(f): return str(f) f = compile(fn, [float]) res = f(1.5) assert eval(res) == 1.5
def test_os_isatty(): def call_isatty(fd): return os.isatty(fd) f = compile(call_isatty, [int]) assert f(0) == os.isatty(0) assert f(1) == os.isatty(1) assert f(2) == os.isatty(2)
def test_os_setpgrp(): def does_stuff(): return os.setpgrp() f1 = compile(does_stuff, []) res = f1() assert res == os.setpgrp()
def test_os_stat(): filename = str(py.magic.autopath()) has_blksize = hasattr(os.stat_result, 'st_blksize') has_blocks = hasattr(os.stat_result, 'st_blocks') def call_stat(): st = os.stat(filename) res = (st[0], st.st_ino, st.st_ctime) if has_blksize: res += (st.st_blksize,) if has_blocks: res += (st.st_blocks,) return res f = compile(call_stat, []) res = f() assert res[0] == os.stat(filename).st_mode assert res[1] == os.stat(filename).st_ino if sys.platform.startswith('win'): py.test.skip("in-progress - bogus stat().st_time") st_ctime = res[2] if isinstance(st_ctime, float): assert st_ctime == os.stat(filename).st_ctime else: assert st_ctime == int(os.stat(filename).st_ctime) if has_blksize: assert res[3] == os.stat(filename).st_blksize if has_blocks: assert res[4] == os.stat(filename).st_blocks
def test_largefile(): if not hasattr(os, 'ftruncate'): py.test.skip("this os has no ftruncate :-(") from pypy.module.posix.test.test_posix2 import need_sparse_files need_sparse_files() filename = str(udir.join('test_largefile')) r4800000000 = r_longlong(4800000000L) r4900000000 = r_longlong(4900000000L) r5000000000 = r_longlong(5000000000L) r5200000000 = r_longlong(5200000000L) r9900000000 = r_longlong(9900000000L) r10000000000 = r_longlong(10000000000L) def does_stuff(): fd = os.open(filename, os.O_RDWR | os.O_CREAT, 0666) os.ftruncate(fd, r10000000000) res = os.lseek(fd, r9900000000, 0) assert res == r9900000000 res = os.lseek(fd, -r5000000000, 1) assert res == r4900000000 res = os.lseek(fd, -r5200000000, 2) assert res == r4800000000 os.close(fd) st = os.stat(filename) assert st.st_size == r10000000000 does_stuff() os.unlink(filename) f1 = compile(does_stuff, []) f1() os.unlink(filename)
def test_os_getpid(): def does_stuff(): return os.getpid() f1 = compile(does_stuff, []) res = f1() assert res == os.getpid()
def test_stringstar(): c_source = """ #include <string.h> int f(char *args[]) { char **p = args; int l = 0; while (*p) { l += strlen(*p); p++; } return (l); } """ c_file = udir.join("stringstar.c") c_file.write(c_source) z = llexternal('f', [CCHARPP], Signed, sources=[str(c_file)]) def f(): l = ["xxx", "x", "xxxx"] ss = liststr2charpp(l) result = z(ss) free_charpp(ss) return result xf = compile(f, [], backendopt=False) assert xf() == 8
def test_system(): def does_stuff(cmd): return os.system(cmd) f1 = compile(does_stuff, [str]) res = f1("echo hello") assert res == 0
def test_simple_start_new_thread(): import thread import pypy.module.thread.rpython.exttable # for declare()/declaretype() class Arg: pass def mythreadedfunction(arg): assert arg.value == 42 def myotherthreadedfunction(arg): assert arg.value == 43 a42 = Arg() a42.value = 42 a43 = Arg() a43.value = 43 def fn(i): thread.start_new_thread(mythreadedfunction, (a42,)) thread.start_new_thread(myotherthreadedfunction, (a43,)) if i == 1: x = mythreadedfunction a = a42 else: x = myotherthreadedfunction a = a43 thread.start_new_thread(x, (a,)) return 42 f = compile(fn, [int]) res = f(1) assert res == 42
def test_os_stat(): filename = str(py.path.local(__file__)) has_blksize = hasattr(os.stat_result, "st_blksize") has_blocks = hasattr(os.stat_result, "st_blocks") def call_stat(): st = os.stat(filename) res = (st[0], st.st_ino, st.st_ctime) if has_blksize: res += (st.st_blksize,) if has_blocks: res += (st.st_blocks,) return res f = compile(call_stat, []) res = f() assert res[0] == os.stat(filename).st_mode assert res[1] == os.stat(filename).st_ino st_ctime = res[2] if isinstance(st_ctime, float): assert st_ctime == os.stat(filename).st_ctime else: assert st_ctime == int(os.stat(filename).st_ctime) if has_blksize: assert res[3] == os.stat(filename).st_blksize if has_blocks: assert res[4] == os.stat(filename).st_blocks
def test_strerror(): def does_stuff(): return os.strerror(2) f1 = compile(does_stuff, []) res = f1() assert res == os.strerror(2)
def test_math(): f = compile(fn, []) res = f() if res >= 0: py.test.fail(repr(test_direct.MathTests.TESTCASES[res])) else: assert res == -42
def test_compile_pyerrchecker(self): from pypy.rpython.rctypes import apyobject class W_Object(py_object): pass apyobject.register_py_object_subclass(W_Object) def mypyerrchecker(): # for this test, always raises raise ZeroDivisionError PyNumber_Add = pythonapi.PyNumber_Add PyNumber_Add.argtypes = [W_Object, W_Object] PyNumber_Add.restype = W_Object assert PyNumber_Add._flags_ & _FUNCFLAG_PYTHONAPI PyNumber_Add._rctypes_pyerrchecker_ = mypyerrchecker # special extension ^^^ to support the CPyObjSpace try: def fn1(n): if n < 0: # for this test, force mypyerrchecker() to be annotated # using this trick mypyerrchecker() pyobj = W_Object(n) return PyNumber_Add(pyobj, pyobj) fn = compile(fn1, [int]) py.test.raises(ZeroDivisionError, fn, 64) finally: del PyNumber_Add._rctypes_pyerrchecker_
def test_compile_pythonapi(self): from pypy.rpython.rctypes import apyobject class W_Object(py_object): pass apyobject.register_py_object_subclass(W_Object) PyInt_AsLong = pythonapi.PyInt_AsLong PyInt_AsLong.argtypes = [W_Object] PyInt_AsLong.restype = c_long assert PyInt_AsLong._flags_ & _FUNCFLAG_PYTHONAPI PyNumber_Add = pythonapi.PyNumber_Add PyNumber_Add.argtypes = [W_Object, W_Object] PyNumber_Add.restype = W_Object def fn1(x, crash): pyobj = W_Object(x) pyobj = PyNumber_Add(pyobj, pyobj) x = PyInt_AsLong(pyobj) if crash: # fn(sys.maxint, 1) should crash on PyInt_AsLong before # it arrives here. If by mistake it arrives here then # we get a TypeError instead of the OverflowError PyNumber_Add(W_Object(5), W_Object("x")) return x fn = compile(fn1, [int, int]) res = fn(17, 0) assert res == 34 py.test.raises(OverflowError, 'fn(sys.maxint, 1)')
def test_os_stat(): filename = str(py.path.local(__file__)) has_blksize = hasattr(os.stat_result, 'st_blksize') has_blocks = hasattr(os.stat_result, 'st_blocks') def call_stat(): st = os.stat(filename) res = (st[0], st.st_ino, st.st_ctime) if has_blksize: res += (st.st_blksize, ) if has_blocks: res += (st.st_blocks, ) return res f = compile(call_stat, []) res = f() assert res[0] == os.stat(filename).st_mode assert res[1] == os.stat(filename).st_ino st_ctime = res[2] if isinstance(st_ctime, float): assert st_ctime == os.stat(filename).st_ctime else: assert st_ctime == int(os.stat(filename).st_ctime) if has_blksize: assert res[3] == os.stat(filename).st_blksize if has_blocks: assert res[4] == os.stat(filename).st_blocks
def test_interp_attrproperty(): W_MyType.typedef = TypeDef("MyType", x = interp_attrproperty("x", W_MyType)) space = CPyObjSpace() def mytest(w_myobj): myobj = space.interp_w(W_MyType, w_myobj, can_be_None=True) if myobj is None: myobj = W_MyType(space) myobj.x = 1 myobj.x *= 2 w_myobj = space.wrap(myobj) w_x = space.wrap(myobj.x) return space.newtuple([w_myobj, w_x]) def fn(obj): w_obj = W_Object(obj) w_res = mytest(w_obj) return w_res.value fn.allow_someobjects = True fn = compile(fn, [object], annotatorpolicy = CPyAnnotatorPolicy(space)) res, x = fn(None, expected_extra_mallocs=1) assert type(res).__name__ == 'MyType' assert x == 2 assert res.x == 2 res2, x = fn(res, expected_extra_mallocs=1) assert res2 is res assert x == 4 assert res.x == 4
def test_start_new_thread(): py.test.skip("XXX out-of-date, and should not be here") import thread import pypy.module.thread.rpython.exttable # for declare()/declaretype() class Arg: pass a = Arg() a.x = 5 a.lock = thread.allocate_lock() def mythreadedfunction(arg): arg.x += 37 arg.myident = thread.get_ident() arg.lock.release() def fn(): a.lock.acquire(True) ident = thread.start_new_thread(mythreadedfunction, (a, )) assert ident != thread.get_ident() a.lock.acquire(True) # wait for the thread to finish assert a.myident == ident return a.x f = compile(fn, []) res = f() assert res == 42
def test_with_new_with_allocate_instance(): def mytype_new(space, w_subtype, x): w_obj = space.allocate_instance(W_MyType, w_subtype) W_MyType.__init__(space.interp_w(W_MyType, w_obj), space, x) return w_obj mytype_new.unwrap_spec = [ObjSpace, W_Root, int] W_MyType.typedef = TypeDef("MyType", __new__ = interp2app(mytype_new), x = interp_attrproperty("x", W_MyType)) space = CPyObjSpace() def build(): w_type = space.gettypefor(W_MyType) return space.call_function(w_type, space.wrap(42)) w_obj = build() w_name = space.getattr(space.type(w_obj), space.wrap('__name__')) assert space.unwrap(w_name) == 'MyType' assert space.int_w(space.getattr(w_obj, space.wrap('x'))) == 42 fn = compile(build, [], annotatorpolicy = CPyAnnotatorPolicy(space)) res = fn(expected_extra_mallocs=1) assert type(res).__name__ == 'MyType' assert res.x == 42
def test_string_reverse(): c_source = py.code.Source(""" #include <string.h> char *f(char* arg) { char *ret; ret = (char*)malloc(strlen(arg) + 1); strcpy(ret, arg); return ret; } """) c_file = udir.join("stringrev.c") c_file.write(c_source) z = llexternal('f', [CCHARP], CCHARP, sources=[str(c_file)]) def f(): s = str2charp("xxx") l_res = z(s) res = charp2str(l_res) lltype.free(l_res, flavor='raw') free_charp(s) return len(res) xf = compile(f, [], backendopt=False) assert xf(expected_extra_mallocs=-1) == 3
def test_stat_result(): import os from pypy.translator.c.test.test_genc import compile from pypy.rpython.module.ll_os_stat import s_StatResult marshal_stat_result = get_marshaller(s_StatResult) unmarshal_stat_result = get_unmarshaller(s_StatResult) def f(path): st = os.stat(path) buf = [] marshal_stat_result(buf, st) buf = "".join(buf) st2 = unmarshal_stat_result(buf) assert st2.st_mode == st.st_mode assert st2[9] == st[9] return buf fn = compile(f, [str]) res = fn(".") st = os.stat(".") sttuple = marshal.loads(res) assert sttuple[0] == st[0] assert sttuple[1] == st[1] assert sttuple[2] == st[2] assert sttuple[3] == st[3] assert sttuple[4] == st[4] assert sttuple[5] == st[5] assert len(sttuple) == 10
def test_expose_types(): W_MyType.typedef = TypeDef("MyType") class W_MyType2(Wrappable): def __init__(self, space, x=1): self.space = space self.x = x W_MyType2.typedef = TypeDef("MyType2") space = CPyObjSpace() def get_mytype(n): if n: return space.gettypefor(W_MyType2) else: return space.gettypefor(W_MyType) return None fn = compile(get_mytype, [int], annotatorpolicy = CPyAnnotatorPolicy(space)) w_mytype2 = get_mytype(1) w_name = space.getattr(w_mytype2, space.wrap('__name__')) assert space.unwrap(w_name) == 'MyType2' w_mytype = get_mytype(0) w_name = space.getattr(w_mytype, space.wrap('__name__')) assert space.unwrap(w_name) == 'MyType' res2 = fn(1) assert type(res2) == type assert res2.__name__ == 'MyType2' res = fn(0) assert res.__name__ == 'MyType'
def test_with_new_with_allocate_instance_subclass(): py.test.skip("dealloction for now segfaults") def mytype_new(space, w_subtype, x): w_obj = space.allocate_instance(W_MyType, w_subtype) W_MyType.__init__(space.interp_w(W_MyType, w_obj), space, x) return w_obj mytype_new.unwrap_spec = [ObjSpace, W_Root, int] W_MyType.typedef = TypeDef("MyType", __new__ = interp2app(mytype_new), x = interp_attrproperty("x", W_MyType)) space = CPyObjSpace() def build(): w_type = space.gettypefor(W_MyType) return space.call_function(w_type, space.wrap(42)) fn = compile(build, [], annotatorpolicy = CPyAnnotatorPolicy(space)) res = fn(expected_extra_mallocs=1) assert type(res).__name__ == 'MyType' assert res.x == 42 class MyType2(type(res)): pass res2 = MyType2(42) assert type(res2) is MyType2 assert res2.x == 42 del res2 import gc gc.collect()
def test_os_stat(): filename = str(py.magic.autopath()) has_blksize = hasattr(os.stat_result, 'st_blksize') has_blocks = hasattr(os.stat_result, 'st_blocks') def call_stat(): st = os.stat(filename) res = (st[0], st.st_ino, st.st_ctime) if has_blksize: res += (st.st_blksize, ) if has_blocks: res += (st.st_blocks, ) return res f = compile(call_stat, []) res = f() assert res[0] == os.stat(filename).st_mode assert res[1] == os.stat(filename).st_ino if sys.platform.startswith('win'): py.test.skip("in-progress - bogus stat().st_time") st_ctime = res[2] if isinstance(st_ctime, float): assert st_ctime == os.stat(filename).st_ctime else: assert st_ctime == int(os.stat(filename).st_ctime) if has_blksize: assert res[3] == os.stat(filename).st_blksize if has_blocks: assert res[4] == os.stat(filename).st_blocks
def setup_class(self): if not test_c_compile: py.test.skip("c compilation disabled") from pypy.translator.c.test.test_genc import compile self.compile = lambda s, x, y: compile(x, y, backendopt=True)
def test_os_access(): filename = str(py.path.local(__file__)) def call_access(path, mode): return os.access(path, mode) f = compile(call_access, [str, int]) for mode in os.R_OK, os.W_OK, os.X_OK, (os.R_OK | os.W_OK | os.X_OK): assert f(filename, mode) == os.access(filename, mode)
def test_simple_start_new_thread(): py.test.skip("XXX out-of-date, and should not be here") import thread import pypy.module.thread.rpython.exttable # for declare()/declaretype() class Arg: pass def mythreadedfunction(arg): assert arg.value == 42 def myotherthreadedfunction(arg): assert arg.value == 43 a42 = Arg() a42.value = 42 a43 = Arg() a43.value = 43 def fn(i): thread.start_new_thread(mythreadedfunction, (a42, )) thread.start_new_thread(myotherthreadedfunction, (a43, )) if i == 1: x = mythreadedfunction a = a42 else: x = myotherthreadedfunction a = a43 thread.start_new_thread(x, (a, )) return 42 f = compile(fn, [int]) res = f(1) assert res == 42
def test_gcref(): if sys.platform == 'darwin': py.test.skip("'boehm' may crash") S = lltype.GcStruct("S", ("x", lltype.Signed)) s = lltype.malloc(S) s.x = 123 g1 = lltype.cast_opaque_ptr(GCREF, s) g2 = lltype.cast_opaque_ptr(GCREF, lltype.nullptr(S)) def f2(x): if x > 0: return g1 else: return g2 def f(x): gref = f2(x) g = lltype.cast_opaque_ptr(lltype.Ptr(S), gref) if g: return g.x else: return -42 fn = compile(f, [int], gcpolicy='boehm') assert fn(3) == 123 assert fn(-3) == -42
def test_catches(): def one(x): if x == 1: raise ValueError() elif x == 2: raise TypeError() return x - 5 def foo(x): x = one(x) try: x = one(x) except ValueError: return 1 + x except TypeError: return 2 + x except: return 3 + x return 4 + x t, g = transform_func(foo, [int]) assert len(list(g.iterblocks())) == 9 f = compile(foo, [int]) result = interpret(foo, [6]) assert result == 2 result = f(6) assert result == 2 result = interpret(foo, [7]) assert result == 4 result = f(7) assert result == 4 result = interpret(foo, [8]) assert result == 2 result = f(8) assert result == 2
def test_links(): import stat tmpfile1 = str(udir.join('test_links_1.txt')) tmpfile2 = str(udir.join('test_links_2.txt')) tmpfile3 = str(udir.join('test_links_3.txt')) f = open(tmpfile1, 'w') f.close() def does_stuff(): os.symlink(tmpfile1, tmpfile2) os.link(tmpfile1, tmpfile3) assert os.readlink(tmpfile2) == tmpfile1 flag= 0 st = os.lstat(tmpfile1) flag = flag*10 + stat.S_ISREG(st[0]) flag = flag*10 + stat.S_ISLNK(st[0]) st = os.lstat(tmpfile2) flag = flag*10 + stat.S_ISREG(st[0]) flag = flag*10 + stat.S_ISLNK(st[0]) st = os.lstat(tmpfile3) flag = flag*10 + stat.S_ISREG(st[0]) flag = flag*10 + stat.S_ISLNK(st[0]) return flag f1 = compile(does_stuff, []) res = f1() assert res == 100110 assert os.path.islink(tmpfile2) assert not os.path.islink(tmpfile3)
def test_pipe_dup_dup2(): def does_stuff(): a, b = os.pipe() c = os.dup(a) d = os.dup(b) assert a != b assert a != c assert a != d assert b != c assert b != d assert c != d os.close(c) os.dup2(d, c) e, f = os.pipe() assert e != a assert e != b assert e != c assert e != d assert f != a assert f != b assert f != c assert f != d assert f != e os.close(a) os.close(b) os.close(c) os.close(d) os.close(e) os.close(f) return 42 f1 = compile(does_stuff, []) res = f1() assert res == 42
def test_strerror(): def does_stuff(n): return os.strerror(n) f1 = compile(does_stuff, [int]) for i in range(4): res = f1(i) assert res == os.strerror(i)
def test_math_hypot(): from math import hypot def fn(x, y): return hypot(x, y) f = compile(fn, [float, float]) assert f(9812.231, 1234) == hypot(9812.231, 1234)