def test_main_two(self): udir.ensure("js_two.py").write(py.code.Source(""" def f(): pass """)) self._test_not_raises(udir.join("js_two.py"), ["f"]) self._test_raises(udir.join("js_two.py"), [])
def test_two_files(self): cfile = udir.join("test_two_files.c") cfile.write( """ #include <stdio.h> int func(); int main() { printf("%d\\n", func()); return 0; } """ ) cfile2 = udir.join("implement1.c") cfile2.write( """ int func() { return 42; } """ ) executable = self.platform.compile([cfile, cfile2], ExternalCompilationInfo()) res = self.platform.execute(executable) self.check_res(res)
def test_gcc_exec(): f = udir.join("x.c") f.write(""" #include <stdio.h> #include <test_gcc_exec.h> int main() { printf("%d\\n", ANSWER); return 0; } """) dir1 = udir.join('test_gcc_exec_dir1').ensure(dir=1) dir2 = udir.join('test_gcc_exec_dir2').ensure(dir=1) dir1.join('test_gcc_exec.h').write('#define ANSWER 3\n') dir2.join('test_gcc_exec.h').write('#define ANSWER 42\n') eci = ExternalCompilationInfo(include_dirs=[str(dir1)]) # remove cache path = cache_file_path([f], eci, 'build_executable_cache') if path.check(): path.remove() res = build_executable_cache([f], eci) assert res == "3\n" assert build_executable_cache([f], eci) == "3\n" eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)]) assert build_executable_cache([f], eci2) == "42\n" f.write("#error BOOM\n") err = py.test.raises(CompilationError, build_executable_cache, [f], eci2) print '<<<' print err print '>>>'
def test_gcc_ask(): f = udir.join("y.c") f.write(""" #include <stdio.h> #include <test_gcc_ask.h> int main() { printf("hello\\n"); return 0; } """) dir1 = udir.join('test_gcc_ask_dir1').ensure(dir=1) dir2 = udir.join('test_gcc_ask_dir2').ensure(dir=1) dir1.join('test_gcc_ask.h').write('/* hello world */\n') dir2.join('test_gcc_ask.h').write('#error boom\n') eci = ExternalCompilationInfo(include_dirs=[str(dir1)]) # remove cache path = cache_file_path([f], eci, 'try_compile_cache') if path.check(): path.remove() assert try_compile_cache([f], eci) assert try_compile_cache([f], eci) assert build_executable_cache([f], eci) == "hello\n" eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)]) err = py.test.raises(CompilationError, try_compile_cache, [f], eci2) print '<<<' print err print '>>>'
def test_32bit_makefile(self): if platform.machine() != "i386": py.test.skip("i386 only") plat32 = Darwin_i386() plat64 = Darwin_x86_64() eci = ExternalCompilationInfo() cfile_content = r""" #include <stdio.h> #include <limits.h> int main() { printf("%d\n", INT_MAX < LONG_MAX); return 0; } """ tmpdir = udir.join("32_makefile" + self.__class__.__name__).ensure(dir=1) cfile = tmpdir.join("test_int_size.c") cfile.write(cfile_content) mk = plat32.gen_makefile([cfile], ExternalCompilationInfo(), path=tmpdir) mk.write() plat32.execute_makefile(mk) res = plat32.execute(tmpdir.join("test_int_size")) self.check_res(res, "0\n") if host_factory == Darwin_x86_64: tmpdir = udir.join("64_makefile" + self.__class__.__name__).ensure(dir=1) cfile = tmpdir.join("test_int_size.c") cfile.write(cfile_content) mk = plat64.gen_makefile([cfile], ExternalCompilationInfo(), path=tmpdir) mk.write() plat64.execute_makefile(mk) res = plat64.execute(tmpdir.join("test_int_size")) self.check_res(res, "1\n")
def compile_shared_lib(self, outputfilename=None): self = self.convert_sources_to_files() if not self.separate_module_files: if sys.platform != 'win32': return self if not self.export_symbols: return self basepath = udir.join('module_cache') else: #basepath = py.path.local(self.separate_module_files[0]).dirpath() basepath = udir.join('shared_cache') if outputfilename is None: # find more or less unique name there pth = basepath.join('externmod').new(ext=host.so_ext) num = 0 while pth.check(): pth = basepath.join( 'externmod_%d' % (num,)).new(ext=host.so_ext) num += 1 basepath.ensure(dir=1) outputfilename = str(pth.dirpath().join(pth.purebasename)) lib = str(host.compile([], self, outputfilename=outputfilename, standalone=False)) d = self._copy_attributes() d['libraries'] += (lib,) d['separate_module_files'] = () d['separate_module_sources'] = () return ExternalCompilationInfo(**d)
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 setup_module(mod): if os.name != 'nt': mod.space = gettestobjspace(usemodules=['posix', 'fcntl']) else: # On windows, os.popen uses the subprocess module mod.space = gettestobjspace(usemodules=['posix', '_rawffi', 'thread']) mod.path = udir.join('posixtestfile.txt') mod.path.write("this is a test") mod.path2 = udir.join('test_posix2-') pdir = udir.ensure('posixtestdir', dir=True) pdir.join('file1').write("test1") os.chmod(str(pdir.join('file1')), 0600) pdir.join('file2').write("test2") pdir.join('another_longer_file_name').write("test3") mod.pdir = pdir unicode_dir = udir.ensure('fi\xc5\x9fier.txt', dir=True) unicode_dir.join('somefile').write('who cares?') mod.unicode_dir = unicode_dir # in applevel tests, os.stat uses the CPython os.stat. # Be sure to return times with full precision # even when running on top of CPython 2.4. os.stat_float_times(True) # Initialize sys.filesystemencoding space.call_method(space.getbuiltinmodule('sys'), 'getfilesystemencoding')
def test_dont_inherit_across_import(self): from pypy.tool.udir import udir udir.join("test_dont_inherit_across_import.py").write("x = 1/2\n") space = self.space s1 = str( py.code.Source( """ from __future__ import division from test_dont_inherit_across_import import x """ ) ) w_result = space.appexec( [space.wrap(str(udir)), space.wrap(s1)], """(udir, s1): import sys copy = sys.path[:] sys.path.insert(0, udir) try: exec s1 finally: sys.path[:] = copy return x """, ) assert space.float_w(w_result) == 0
def setup_class(cls): compiler = ccompiler.new_compiler() c_file = udir.join('rffilib.c') c_file.write(c_source) compiler.compile([str(c_file)], output_dir='/') compiler.link_shared_lib([str(udir.join('rffilib.o'))], 'rffi', output_dir=str(udir)) cls.lib = ctypes.CDLL(str(udir.join('librffi.so')))
def test_os_rename(): tmpfile1 = str(udir.join('test_os_rename_1.txt')) tmpfile2 = str(udir.join('test_os_rename_2.txt')) f = open(tmpfile1, 'w') f.close() def does_stuff(): os.rename(tmpfile1, tmpfile2) f1 = compile(does_stuff, []) f1() assert os.path.exists(tmpfile2) assert not os.path.exists(tmpfile1)
def setup_module(mod): mod.space = gettestobjspace(usemodules=['posix']) mod.path = udir.join('posixtestfile.txt') mod.path.write("this is a test") mod.path2 = udir.join('posixtestlargefile') pdir = udir.ensure('posixtestdir', dir=True) pdir.join('file1').write("test1") os.chmod(str(pdir.join('file1')), 0600) pdir.join('file2').write("test2") pdir.join('another_longer_file_name').write("test3") mod.pdir = pdir
def setup_class(cls): testfn.write(testcode, 'w') udir.join(testmodule + '.py').write(testmodulecode, 'w') udir.ensure(testpackage, '__init__.py') udir.join(testpackage, testmodule + '.py').write(testmodulecode, 'w') space = cls.space cls.w_oldsyspath = space.appexec([space.wrap(str(udir))], """(udir): import sys old = sys.path[:] sys.path.insert(0, udir) return old """)
def safe_filename(self): name = self.safe_name(self.listnames()) num = 0 while udir.join(name + '.py').check(): num += 1 name = self.safe_name(self.listnames()) + "_" + str(num) return name + '.py'
def setup_class(cls): if '__pypy__' not in sys.builtin_module_names: py.test.skip("must run this test with pypy") if not sys.pypy_translation_info['translation.jit']: py.test.skip("must give a pypy-c with the jit enabled") cls.tmpdir = udir.join('test-pypy-jit') cls.tmpdir.ensure(dir=True)
def test_spawnve(): filename = str(udir.join('test_spawnve.txt')) progname = str(sys.executable) scriptpath = udir.join('test_spawnve.py') scriptpath.write('import os\n' + 'f=open(%r,"w")\n' % filename + 'f.write(os.environ["FOOBAR"])\n' + 'f.close\n') scriptname = str(scriptpath) def does_stuff(): l = [progname, scriptname] pid = os.spawnve(os.P_NOWAIT, progname, l, {'FOOBAR': '42'}) os.waitpid(pid, 0) func = compile(does_stuff, []) func() assert open(filename).read() == "42"
def return_char(self, signed): ctype_pref = ["un", ""][signed] rffi_type = [UCHAR, SIGNEDCHAR][signed] h_source = py.code.Source(""" %ssigned char returnchar(void) { return 42; } """ % (ctype_pref, )) h_file = udir.join("opaque2%s.h" % (ctype_pref, )) h_file.write(h_source) from pypy.rpython.tool import rffi_platform eci = ExternalCompilationInfo( includes=[h_file.basename], include_dirs=[str(udir)] ) ll_returnchar = llexternal('returnchar', [], rffi_type, compilation_info=eci) def f(): result = ll_returnchar() return result f1 = self.compile(f, []) assert f1() == chr(42)
def test_frameworks(self): objcfile = udir.join("test_simple.m") objcfile.write( r""" #import <Foundation/Foundation.h> #include "test.h" int main (int argc, const char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSArray *args = [[NSProcessInfo processInfo] arguments]; NSCountedSet *cset = [[NSCountedSet alloc] initWithArray:args]; printf("%d\n", XXX_STUFF); [cset release]; [pool release]; return 0; } """ ) includedir = py.path.local(__file__).dirpath().join("include") eci = ExternalCompilationInfo(frameworks=("Cocoa",), include_dirs=(includedir,)) executable = self.platform.compile([objcfile], eci) res = self.platform.execute(executable) self.check_res(res)
def test_get_osfhandle(): fid = open(str(udir.join('validate_test.txt')), 'w') fd = fid.fileno() rwin32.get_osfhandle(fd) fid.close() raises(OSError, rwin32.get_osfhandle, fd) rwin32.get_osfhandle(0)
def instrument_result(self, args): backend, ts = self.get_backend_and_type_system() if backend != 'c' or sys.platform == 'win32': raise Exception("instrumentation requires the c backend" " and unix for now") from pypy.tool.udir import udir datafile = udir.join('_instrument_counters') makeProfInstrument = lambda compiler: ProfInstrument(datafile, compiler) pid = os.fork() if pid == 0: # child compiling and running with instrumentation self.config.translation.instrument = True self.config.translation.instrumentctl = (makeProfInstrument, args) raise Instrument else: pid, status = os.waitpid(pid, 0) if os.WIFEXITED(status): status = os.WEXITSTATUS(status) if status != 0: raise Exception, "instrumentation child failed: %d" % status else: raise Exception, "instrumentation child aborted" import array, struct n = datafile.size()//struct.calcsize('L') datafile = datafile.open('rb') counters = array.array('L') counters.fromfile(datafile, n) datafile.close() return counters
def test_prebuilt_constant(self): py.test.skip("Think how to do it sane") h_source = py.code.Source(""" int x = 3; char** z = NULL; #endif """) h_include = udir.join('constants.h') h_include.write(h_source) eci = ExternalCompilationInfo(includes=['stdio.h', str(h_include.basename)], include_dirs=[str(udir)]) get_x, set_x = CExternVariable(lltype.Signed, 'x', eci) get_z, set_z = CExternVariable(CCHARPP, 'z', eci) def f(): one = get_x() set_x(13) return one + get_x() def g(): l = liststr2charpp(["a", "b", "c"]) try: set_z(l) return charp2str(get_z()[2]) finally: free_charpp(l) fn = self.compile(f, []) assert fn() == 16 gn = self.compile(g, []) assert gn() == "c"
def convert_sources_to_files(self, cache_dir=None, being_main=False): if not self.separate_module_sources: return self if cache_dir is None: cache_dir = udir.join('module_cache').ensure(dir=1) num = 0 files = [] for source in self.separate_module_sources: while 1: filename = cache_dir.join('module_%d.c' % num) num += 1 if not filename.check(): break f = filename.open("w") if being_main: f.write("#define PYPY_NOT_MAIN_FILE\n") if sys.platform == 'win32': f.write("#define WIN32_LEAN_AND_MEAN\n") self.write_c_header(f) source = str(source) f.write(source) if not source.endswith('\n'): f.write('\n') f.close() files.append(str(filename)) d = self._copy_attributes() d['separate_module_sources'] = () d['separate_module_files'] += tuple(files) return ExternalCompilationInfo(**d)
def test_mkdir(): filename = str(udir.join('test_mkdir.dir')) getllimpl(os.mkdir)(filename, 0) exc = raises(OSError, getllimpl(os.mkdir), filename, 0) assert exc.value.errno == errno.EEXIST if sys.platform == 'win32': assert exc.type is WindowsError
def setup_class(cls): if option.pypy_c is None: py.test.skip("pass --pypy-c!") cls.tmpdir = udir.join("pypy-jit") cls.tmpdir.ensure(dir=1) cls.counter = 0 cls.pypy_c = option.pypy_c
def test_counters(self): from pypy.rpython.lltypesystem import lltype from pypy.rpython.lltypesystem.lloperation import llop def entry_point(argv): llop.instrument_count(lltype.Void, 'test', 2) llop.instrument_count(lltype.Void, 'test', 1) llop.instrument_count(lltype.Void, 'test', 1) llop.instrument_count(lltype.Void, 'test', 2) llop.instrument_count(lltype.Void, 'test', 1) return 0 t = TranslationContext(self.config) t.config.translation.instrument = True t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, config=t.config) # xxx cbuilder.generate_source() cbuilder.compile() counters_fname = udir.join("_counters_") os.putenv('_INSTRUMENT_COUNTERS', str(counters_fname)) try: data = cbuilder.cmdexec() finally: os.unsetenv('_INSTRUMENT_COUNTERS') f = counters_fname.open('rb') counters_data = f.read() f.close() import struct counters = struct.unpack("LLL", counters_data) assert counters == (0,3,2)
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_system(): filename = str(udir.join('test_system.txt')) arg = '%s -c "print 1+1" > %s' % (sys.executable, filename) data = getllimpl(os.system)(arg) assert data == 0 assert file(filename).read().strip() == '2' os.unlink(filename)
def rundemo(entrypoint, *args): view = conftest.option.view seed = demo_conftest.option.randomseed benchmark = bench_conftest.option.benchmark logfile = str(udir.join('%s.log' % (entrypoint.__name__,))) try: os.unlink(logfile) except OSError: pass os.environ['PYPYJITLOG'] = logfile if benchmark: py.test.skip("benchmarking: working in progress") arglist = ', '.join(['a%d' % i for i in range(len(args))]) miniglobals = {'Benchmark': bench_conftest.Benchmark, 'original_entrypoint': entrypoint} exec py.code.Source(""" def benchmark_runner(%s): bench = Benchmark() while 1: res = original_entrypoint(%s) if bench.stop(): break return res """ % (arglist, arglist)).compile() in miniglobals entrypoint = miniglobals['benchmark_runner'] nb_args = len(args) # XXX ints only for now if machine_code_dumper: machine_code_dumper._freeze_() # clean up state rgenop = RGenOp() gv_entrypoint = rcompile(rgenop, entrypoint, [int]*nb_args, random_seed=seed) if machine_code_dumper: machine_code_dumper._freeze_() # clean up state print print 'Random seed value: %d' % (seed,) print print 'Running %s(%s)...' % (entrypoint.__name__, ', '.join(map(repr, args))) expected = entrypoint(*args) print 'Python ===>', expected F1 = lltype.FuncType([lltype.Signed] * nb_args, lltype.Signed) fp = RGenOp.get_python_callable(lltype.Ptr(F1), gv_entrypoint) res = runfp(fp, *args) print '%-6s ===>' % RGenOp.__name__, res print if res != expected: raise AssertionError( "expected return value is %s, got %s\nseed = %s" % ( expected, res, seed)) if view and machine_code_dumper: from pypy.jit.codegen.i386.viewcode import World world = World() world.parse(open(logfile)) world.show()
def test_64_32_results(self): if platform.machine() != "i386": py.test.skip("i386 only") plat32 = Darwin_i386() plat64 = Darwin_x86_64() cfile = udir.join("test_int_size.c") cfile.write( r""" #include <stdio.h> #include <limits.h> int main() { printf("%d\n", INT_MAX < LONG_MAX); return 0; } """ ) eci = ExternalCompilationInfo() executable = plat32.compile([cfile], eci) res = plat32.execute(executable) self.check_res(res, "0\n") if host_factory == Darwin_x86_64: executable = plat64.compile([cfile], eci) res = plat64.execute(executable) self.check_res(res, "1\n")
def eating_callback(self): h_source = py.code.Source(""" #ifndef _CALLBACK_H #define _CALLBACK_H extern Signed eating_callback(Signed arg, Signed(*call)(Signed)); #endif /* _CALLBACK_H */ """) h_include = udir.join('callback.h') h_include.write(h_source) c_source = py.code.Source(""" Signed eating_callback(Signed arg, Signed(*call)(Signed)) { Signed res = call(arg); if (res == -1) return -1; return res; } """) eci = ExternalCompilationInfo(includes=['callback.h'], include_dirs=[str(udir)], separate_module_sources=[c_source], export_symbols=['eating_callback']) args = [SIGNED, CCallback([SIGNED], SIGNED)] eating_callback = llexternal('eating_callback', args, SIGNED, compilation_info=eci) return eating_callback
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 load_and_cache_assembly(name, outfile): tmpfile = udir.join(name) arglist = SDK.runtime() + [Query.get(), name, str(tmpfile)] retcode = subprocess.call(arglist) assert retcode == 0 mydict = {} execfile(str(tmpfile), mydict) types = mydict['types'] f = outfile.open('wb') pickle.dump(types, f, pickle.HIGHEST_PROTOCOL) f.close() return types
def test_spawnv(): filename = str(udir.join('test_spawnv.txt')) progname = str(sys.executable) scriptpath = udir.join('test_spawnv.py') scriptpath.write('f=open(%r,"w")\nf.write("2")\nf.close\n' % filename) scriptname = str(scriptpath) def does_stuff(): # argument quoting on Windows is completely ill-defined. # don't let yourself be fooled by the idea that if os.spawnv() # takes a list of strings, then the receiving program will # nicely see these strings as arguments with no further quote # processing. Achieving this is nearly impossible - even # CPython doesn't try at all. l = [progname, scriptname] pid = os.spawnv(os.P_NOWAIT, progname, l) os.waitpid(pid, 0) func = compile(does_stuff, []) func() assert open(filename).read() == "2"
def test_dont_inherit_across_import(self): from pypy.tool.udir import udir udir.join('test_dont_inherit_across_import.py').write('x = 1/2\n') space = self.space s1 = str( py.code.Source(""" from __future__ import division from test_dont_inherit_across_import import x """)) w_result = space.appexec( [space.wrap(str(udir)), space.wrap(s1)], """(udir, s1): import sys copy = sys.path[:] sys.path.insert(0, udir) try: exec s1 finally: sys.path[:] = copy return x """) assert space.float_w(w_result) == 0
def setup_class(cls): ufilename = (unicode(udir.join('test_unicode_filename_')) + u'\u65e5\u672c.txt') # "Japan" try: f = file(ufilename, 'w') except UnicodeEncodeError: py.test.skip("encoding not good enough") f.write("test") f.close() cls.space = space cls.w_filename = space.wrap(ufilename) cls.w_posix = space.appexec([], GET_POSIX)
def test_execfile(self, space): from pypy.tool.udir import udir fn = str(udir.join('test_execfile')) f = open(fn, 'w') print >> f, "i=42" f.close() w_execfile = space.builtin.get("execfile") w_dict = space.newdict() space.call_function(w_execfile, space.wrap(fn), w_dict, space.w_None) w_value = space.getitem(w_dict, space.wrap('i')) assert space.eq_w(w_value, space.wrap(42))
def generate_decls_and_callbacks(db, export_symbols, api_struct=True): "NOT_RPYTHON" # implement function callbacks and generate function decls functions = [] pypy_decls = [] pypy_decls.append("#ifndef PYPY_STANDALONE\n") pypy_decls.append("#ifdef __cplusplus") pypy_decls.append("extern \"C\" {") pypy_decls.append("#endif\n") pypy_decls.append( '#define Signed long /* xxx temporary fix */\n') pypy_decls.append( '#define Unsigned unsigned long /* xxx temporary fix */\n') for decl in FORWARD_DECLS: pypy_decls.append("%s;" % (decl, )) for name, func in sorted(FUNCTIONS.iteritems()): restype, args = c_function_signature(db, func) pypy_decls.append("PyAPI_FUNC(%s) %s(%s);" % (restype, name, args)) if api_struct: callargs = ', '.join('arg%d' % (i, ) for i in range(len(func.argtypes))) if func.restype is lltype.Void: body = "{ _pypyAPI.%s(%s); }" % (name, callargs) else: body = "{ return _pypyAPI.%s(%s); }" % (name, callargs) functions.append('%s %s(%s)\n%s' % (restype, name, args, body)) for name in VA_TP_LIST: name_no_star = process_va_name(name) header = ('%s pypy_va_get_%s(va_list* vp)' % (name, name_no_star)) pypy_decls.append(header + ';') functions.append(header + '\n{return va_arg(*vp, %s);}\n' % name) export_symbols.append('pypy_va_get_%s' % (name_no_star, )) for name, (typ, expr) in GLOBALS.iteritems(): if name.endswith('#'): name = name.replace("#", "") typ = typ.replace("*", "") elif name.startswith('PyExc_'): typ = 'PyObject*' pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name)) pypy_decls.append('#undef Signed /* xxx temporary fix */\n') pypy_decls.append('#undef Unsigned /* xxx temporary fix */\n') pypy_decls.append("#ifdef __cplusplus") pypy_decls.append("}") pypy_decls.append("#endif") pypy_decls.append("#endif /*PYPY_STANDALONE*/\n") pypy_decl_h = udir.join('pypy_decl.h') pypy_decl_h.write('\n'.join(pypy_decls)) return functions
def test_mkdir_rmdir(): def does_stuff(path, delete): if delete: os.rmdir(path) else: os.mkdir(path, 0777) f1 = compile(does_stuff, [str, bool]) dirname = str(udir.join('test_mkdir_rmdir')) f1(dirname, False) assert os.path.exists(dirname) and os.path.isdir(dirname) f1(dirname, True) assert not os.path.exists(dirname)
def test_setupterm(self): source = py.code.Source(""" import _curses try: _curses.tigetstr('cup') except _curses.error: print 'ok!' """) f = udir.join("test_setupterm.py") f.write(source) child = self.spawn(['--withmod-_curses', str(f)]) child.expect('ok!')
def test_preprocess_localbase(self): tmpdir = udir.join('test_preprocess_localbase').ensure(dir=1) eci = ExternalCompilationInfo() os.environ['PYPY_LOCALBASE'] = '/foo/baz' try: mk = self.platform.gen_makefile(['blip.c'], eci, path=tmpdir) mk.write() finally: del os.environ['PYPY_LOCALBASE'] Makefile = tmpdir.join('Makefile').read() assert 'INCLUDEDIRS = -I/foo/baz/include' in Makefile assert 'LIBDIRS = -L/foo/baz/lib' in Makefile
def test_tb_normalization(): if sys.platform == "win32": py.test.skip("cannot detect process exit code for now") tmpfilepath = str(udir.join("test_py_script.py")) tmpfile = file(tmpfilepath, "w") tmpfile.write(TB_NORMALIZATION_CHK) tmpfile.close() popen = subprocess.Popen( [sys.executable, str(pypypath), '-S', tmpfilepath], stderr=subprocess.PIPE) _, stderr = popen.communicate() assert stderr.endswith('KeyError: <normalized>\n')
def test_os_chown_lchown(): path1 = udir.join('test_os_chown_lchown-1.txt') path2 = udir.join('test_os_chown_lchown-2.txt') path1.write('foobar') path2.mksymlinkto('some-broken-symlink') tmpfile1 = str(path1) tmpfile2 = str(path2) def does_stuff(): # xxx not really a test, just checks that they are callable os.chown(tmpfile1, os.getuid(), os.getgid()) os.lchown(tmpfile1, os.getuid(), os.getgid()) os.lchown(tmpfile2, os.getuid(), os.getgid()) try: os.chown(tmpfile2, os.getuid(), os.getgid()) except OSError: pass else: raise AssertionError("os.chown(broken symlink) should raise") f1 = compile(does_stuff, []) f1()
def test_get_total_memory_linux2(): filepath = udir.join('get_total_memory_linux2') filepath.write("""\ MemTotal: 1976804 kB MemFree: 32200 kB Buffers: 144092 kB Cached: 1385196 kB SwapCached: 8408 kB Active: 1181436 kB etc. """) result = env.get_total_memory_linux2(str(filepath)) assert result == 1976804 * 1024
def test_two_files(self): cfile = udir.join('test_two_files.c') cfile.write(''' #include <stdio.h> int func(); int main() { printf("%d\\n", func()); return 0; } ''') cfile2 = udir.join('implement1.c') cfile2.write(''' int func() { return 42; } ''') executable = self.platform.compile([cfile, cfile2], ExternalCompilationInfo()) res = self.platform.execute(executable) self.check_res(res)
def copy_header_files(dstdir): assert dstdir.check(dir=True) headers = include_dir.listdir('*.h') + include_dir.listdir('*.inl') for name in ("pypy_decl.h", "pypy_macros.h"): headers.append(udir.join(name)) for header in headers: target = dstdir.join(header.basename) try: header.copy(dstdir) except py.error.EACCES: target.remove() # maybe it was a read-only file header.copy(dstdir) target.chmod(0444) # make the file read-only, to make sure that nobody
def test_simple_enough(self): cfile = udir.join('test_simple_enough.c') cfile.write(''' #include <stdio.h> int main() { printf("42\\n"); return 0; } ''') executable = self.platform.compile([cfile], ExternalCompilationInfo()) res = self.platform.execute(executable) self.check_res(res)
def test_os_path_exists(): if sys.maxint != 2**31 - 1: py.test.skip("WIP on 64 bit architectures") tmpfile = str(udir.join('test_os_path_exists.TMP')) def fn(): return os.path.exists(tmpfile) f = compile_function(fn, []) open(tmpfile, 'w').close() assert f() == True os.unlink(tmpfile) assert f() == False
def test_execv(): progname = str(sys.executable) filename = str(udir.join('test_execv.txt')) def does_stuff(): l = [progname, '-c', 'open(%r,"w").write("1")' % filename] pid = os.fork() if pid == 0: os.execv(progname, l) else: os.waitpid(pid, 0) func = compile(does_stuff, [], backendopt=False) func() assert open(filename).read() == "1"
def test_big_read(): filename = str(udir.join('test_open_read_write_close.txt')) def does_stuff(): fd = os.open(filename, os.O_WRONLY | os.O_CREAT, 0777) count = os.write(fd, "hello world\n") os.close(fd) fd = os.open(filename, os.O_RDONLY, 0777) data = os.read(fd, 500000) os.close(fd) f1 = compile(does_stuff, []) f1() os.unlink(filename)
def test_os_write(): #Same as test in rpython/test/test_rbuiltin fname = str(udir.join('os_test.txt')) fd = os.open(fname, os.O_WRONLY | os.O_CREAT, 0777) assert fd >= 0 f = getllimpl(os.write) f(fd, 'Hello world') os.close(fd) with open(fname) as fid: assert fid.read() == "Hello world" fd = os.open(fname, os.O_WRONLY | os.O_CREAT, 0777) os.close(fd) raises(OSError, f, fd, 'Hello world')
def test_write_compiled_module(self): space = self.space pathname = _testfilesource() stream = streamio.open_file_as_stream(pathname, "r") try: w_ret = importing.parse_source_module(space, pathname, stream.readall()) finally: stream.close() pycode = space.interpclass_w(w_ret) assert type(pycode) is pypy.interpreter.pycode.PyCode cpathname = str(udir.join('cpathname.pyc')) mtime = 12345 importing.write_compiled_module(space, pycode, cpathname, mtime) # check pathname = str(udir.join('cpathname.py')) ret = importing.check_compiled_module(space, pathname, mtime, cpathname) assert ret == 1 # read compile module stream = streamio.open_file_as_stream(cpathname, "r") try: stream.seek(8, 0) w_code = importing.read_compiled_module(space, cpathname, stream.readall()) pycode = space.interpclass_w(w_code) finally: stream.close() # check value of load w_dic = space.newdict() pycode.exec_code(space, w_dic, w_dic) w_ret = space.getitem(w_dic, space.wrap('x')) ret = space.int_w(w_ret) assert ret == 42
def test_tb_normalization(): if sys.platform == "win32": py.test.skip("cannot detect process exit code for now") tmpfilepath = str(udir.join("test_py_script.py")) tmpfile = file(tmpfilepath, "w") tmpfile.write(TB_NORMALIZATION_CHK) tmpfile.close() e = None try: output = py.process.cmdexec('''"%s" "%s" "%s" ''' % (sys.executable, pypypath, tmpfilepath)) except py.process.cmdexec.Error, e: pass
def start(self): # start of a dump file from pypy.tool.udir import udir n = Tracer.Counter Tracer.Counter += 1 filename = 'llinterp_trace_%d.html' % n self.file = udir.join(filename).open('w') print >> self.file, self.HEADER linkname = str(udir.join('llinterp_trace.html')) try: os.unlink(linkname) except OSError: pass try: os.symlink(filename, linkname) except (AttributeError, OSError): pass self.count = 0 self.indentation = '' self.depth = 0 self.latest_call_chain = []
def __init__(self, translator, functions=[], stackless=False, compress=False, \ logging=False, use_debug=False): if not isinstance(functions, list): functions = [functions] GenOO.__init__(self, udir, translator, None) pending_graphs = [translator.annotator.bookkeeper.getdesc(f).cachedgraph(None) for f in functions ] for graph in pending_graphs: self.db.pending_function(graph) self.db.translator = translator self.use_debug = use_debug self.assembly_name = self.translator.graphs[0].name self.tmpfile = udir.join(self.assembly_name + '.js')
def setup_class(cls): # simply compile snippets just once src = str(Source(snippet)) # truncate non-compilable stuff for now: p = src.index('Non compilable Functions') src = src[:p] + '\n' # put our ad into snippet exec cls.snippet_ad in snippet.__dict__ src += cls.snippet_ad # just in case of trouble, we produce a tempfile ini, newsrc = translate_as_module(src, tmpname=str( udir.join("_geninterp_test.py"))) cls.w_glob = ini(cls.space)
def test_convert_sources_to_c_files(self): eci = ExternalCompilationInfo( separate_module_sources=['xxx'], separate_module_files=['x.c'], ) cache_dir = udir.join('test_convert_sources').ensure(dir=1) neweci = eci.convert_sources_to_files(cache_dir) assert not neweci.separate_module_sources res = neweci.separate_module_files assert len(res) == 2 assert res[0] == 'x.c' assert str(res[1]).startswith(str(cache_dir)) e = ExternalCompilationInfo() assert e.convert_sources_to_files() is e
def test_os_chmod(): tmpfile = str(udir.join('test_os_chmod.txt')) f = open(tmpfile, 'w') f.close() def does_stuff(mode): os.chmod(tmpfile, mode) return 0 f1 = compile_function(does_stuff, [int]) f1(0000) assert os.stat(tmpfile).st_mode & 0777 == 0000 f1(0644) assert os.stat(tmpfile).st_mode & 0777 == 0644
def test_os_fdatasync(): try: f = getllimpl(os.fdatasync) except: skip('No fdatasync in os') fname = str(udir.join('os_test.txt')) fd = os.open(fname, os.O_WRONLY | os.O_CREAT, 0777) assert fd >= 0 os.write(fd, 'Hello world') f(fd) fid = open(fname) assert fid.read() == 'Hello world' os.close(fd) raises(OSError, f, fd)
def test_separate_files(self): # One file in translator/c/src fname = py.path.local(pypydir).join('translator', 'c', 'src', 'll_strtod.h') # One file in (another) subdir of the temp directory dirname = udir.join("test_dir").ensure(dir=1) fname2 = dirname.join("test_genc.c") fname2.write(""" void f() { LL_strtod_formatd("%5f", 12.3); }""") files = [fname, fname2] def entry_point(argv): return 0 t = TranslationContext(self.config) t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, t.config) cbuilder.eci = cbuilder.eci.merge( ExternalCompilationInfo(separate_module_files=files)) cbuilder.generate_source() makefile = udir.join(cbuilder.modulename, 'Makefile').read() # generated files are compiled in the same directory assert " ../test_dir/test_genc.c" in makefile assert " ../test_dir/test_genc.o" in makefile # but files from pypy source dir must be copied assert "translator/c/src" not in makefile assert " ll_strtod.h" in makefile assert " ll_strtod.o" in makefile
def setuppkg(pkgname, **entries): p = udir.join('impsubdir') if pkgname: p = p.join(*pkgname.split('.')) p.ensure(dir=1) f = p.join("__init__.py").open('w') print >> f, "# package" f.close() for filename, content in entries.items(): filename += '.py' f = p.join(filename).open('w') print >> f, '#', filename print >> f, content f.close() return p