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 setup_module(mod): if os.name != 'nt': mod.space = gettestobjspace(usemodules=['posix', 'fcntl', 'struct']) else: # On windows, os.popen uses the subprocess module mod.space = gettestobjspace( usemodules=['posix', '_rawffi', 'thread', 'struct']) 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?') unicode_dir.join('caf\xe9').write('who knows?') 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_main_one(self): udir.ensure("js_one.py").write(py.code.Source(""" def f(): pass f._client = True """)) self._test_not_raises(udir.join("js_one.py"))
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_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_main_one(self): udir.ensure("js_one.py").write( py.code.Source(""" def f(): pass f._client = True """)) self._test_not_raises(udir.join("js_one.py"))
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 test_safe_filename(): source = py.code.Source(""" class ExpectTestOne: def test_one(self): pass """) dest = udir.join("test_expect_safefilename.py") dest.write(source) from pypy import conftest col = conftest.Module(dest) methcol = col.join('ExpectTestOne').join('()').join('test_one') name = 'test_expect_safefilename_ExpectTestOne_paren_test_one.py' assert methcol.safe_filename() == name udir.ensure(name) assert methcol.safe_filename() == name[:-3] + '_1.py'
def prepare_c_example(cls): from pypy.tool.udir import udir from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform c_file = udir.ensure("test__ffi", dir=1).join("foolib.c") # automatically collect the C source from the docstrings of the tests snippets = [""" #ifdef _WIN32 #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT #endif """] for name in dir(cls): if name.startswith('test_'): meth = getattr(cls, name) # the heuristic to determine it it's really C code could be # improved: so far we just check that there is a '{' :-) if meth.__doc__ is not None and '{' in meth.__doc__: snippets.append(meth.__doc__) # c_file.write(py.code.Source('\n'.join(snippets))) eci = ExternalCompilationInfo(export_symbols=[]) return str(platform.compile([c_file], eci, 'x', standalone=False))
def setup_class(cls): from pypy.tool.udir import udir from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform BaseFfiTest.setup_class() # prepare C code as an example, so we can load it and call # it via rlib.libffi c_file = udir.ensure("test_libffi", dir=1).join("foolib.c") # automatically collect the C source from the docstrings of the tests snippets = [] exports = [] for name in dir(cls): if name.startswith('test_'): meth = getattr(cls, name) # the heuristic to determine it it's really C code could be # improved: so far we just check that there is a '{' :-) if meth.__doc__ is not None and '{' in meth.__doc__: snippets.append(meth.__doc__) import re for match in re.finditer(" ([a-z_]+)\(", meth.__doc__): exports.append(match.group(1)) # c_file.write(py.code.Source('\n'.join(snippets))) eci = ExternalCompilationInfo(export_symbols=exports) cls.libfoo_name = str(platform.compile([c_file], eci, 'x', standalone=False))
def test_cdll_life_time(self): from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source(''' long fun(long i) { return i + 42; } ''')) eci = ExternalCompilationInfo(export_symbols=['fun']) lib_name = str(platform.compile([c_file], eci, 'x', standalone=False)) lib = CDLL(lib_name) slong = cast_type_to_ffitype(rffi.LONG) fun = lib.getrawpointer('fun', [slong], slong) del lib # already delete here buffer = lltype.malloc(rffi.LONGP.TO, 2, flavor='raw') buffer[0] = 200 buffer[1] = -1 fun.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1))) assert buffer[1] == 242 lltype.free(buffer, flavor='raw') del fun assert not ALLOCATED
def test_execfile_different_lineendings(self): space = self.space from pypy.tool.udir import udir d = udir.ensure('lineending', dir=1) dos = d.join('dos.py') f = dos.open('wb') f.write("x=3\r\n\r\ny=4\r\n") f.close() space.appexec([space.wrap(str(dos))], """ (filename): d = {} execfile(filename, d) assert d['x'] == 3 assert d['y'] == 4 """) unix = d.join('unix.py') f = unix.open('wb') f.write("x=5\n\ny=6\n") f.close() space.appexec([space.wrap(str(unix))], """ (filename): d = {} execfile(filename, d) assert d['x'] == 5 assert d['y'] == 6 """)
def make_class(cls): # XXX: this is (mostly) wrong: .compile() compiles the code object # using the host python compiler, but then in the tests we load it # with py.py. It works (mostly by chance) because the two functions # are very simple and the bytecodes are compatible enough. co = py.code.Source(""" def get_name(): return __name__ def get_file(): return __file__ """).compile() if cls.compression == ZIP_DEFLATED: space = gettestobjspace( usemodules=['zipimport', 'zlib', 'rctime', 'struct']) else: space = gettestobjspace( usemodules=['zipimport', 'rctime', 'struct']) cls.space = space tmpdir = udir.ensure('zipimport_%s' % cls.__name__, dir=1) now = time.time() cls.w_now = space.wrap(now) test_pyc = cls.make_pyc(space, co, now) cls.w_test_pyc = space.wrap(test_pyc) cls.w_compression = space.wrap(cls.compression) cls.w_pathsep = space.wrap(cls.pathsep) #ziptestmodule = tmpdir.ensure('ziptestmodule.zip').write( ziptestmodule = tmpdir.join("somezip.zip") cls.w_tmpzip = space.wrap(str(ziptestmodule)) cls.w_co = space.wrap(co) cls.tmpdir = tmpdir
def setup_class(cls): from pypy.tool.udir import udir from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.tool.cbuild import STANDARD_DEFINES from pypy.translator.platform import platform BaseFfiTest.setup_class() # prepare C code as an example, so we can load it and call # it via rlib.libffi c_file = udir.ensure("test_libffi", dir=1).join("foolib.c") # automatically collect the C source from the docstrings of the tests snippets = [] exports = [] for name in dir(cls): if name.startswith('test_'): meth = getattr(cls, name) # the heuristic to determine it it's really C code could be # improved: so far we just check that there is a '{' :-) if meth.__doc__ is not None and '{' in meth.__doc__: snippets.append(meth.__doc__) import re for match in re.finditer(" ([a-z_]+)\(", meth.__doc__): exports.append(match.group(1)) # c_file.write(STANDARD_DEFINES + str(py.code.Source('\n'.join(snippets)))) eci = ExternalCompilationInfo(export_symbols=exports) cls.libfoo_name = str( platform.compile([c_file], eci, 'x', standalone=False)) cls.dll = cls.CDLL(cls.libfoo_name)
def prepare_c_example(cls): from pypy.tool.udir import udir from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform c_file = udir.ensure("test__ffi", dir=1).join("foolib.c") # automatically collect the C source from the docstrings of the tests snippets = [ """ #ifdef _WIN32 #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT #endif """ ] for name in dir(cls): if name.startswith('test_'): meth = getattr(cls, name) # the heuristic to determine it it's really C code could be # improved: so far we just check that there is a '{' :-) if meth.__doc__ is not None and '{' in meth.__doc__: snippets.append(meth.__doc__) # c_file.write(py.code.Source('\n'.join(snippets))) eci = ExternalCompilationInfo(export_symbols=[]) return str(platform.compile([c_file], eci, 'x', standalone=False))
def test_gcc_options(self): # check that the env var CC is correctly interpreted, even if # it contains the compiler name followed by some options. if sys.platform == 'win32': py.test.skip("only for gcc") from pypy.rpython.lltypesystem import lltype, rffi dir = udir.ensure('test_gcc_options', dir=1) dir.join('someextraheader.h').write('#define someextrafunc() 42\n') eci = ExternalCompilationInfo(includes=['someextraheader.h']) someextrafunc = rffi.llexternal('someextrafunc', [], lltype.Signed, compilation_info=eci) def entry_point(argv): return someextrafunc() old_cc = os.environ.get('CC') try: os.environ['CC'] = 'gcc -I%s' % dir t, cbuilder = self.compile(entry_point) finally: if old_cc is None: del os.environ['CC'] else: os.environ['CC'] = old_cc
def make_class(cls): # XXX: this is (mostly) wrong: .compile() compiles the code object # using the host python compiler, but then in the tests we load it # with py.py. It works (mostly by chance) because the two functions # are very simple and the bytecodes are compatible enough. co = py.code.Source( """ def get_name(): return __name__ def get_file(): return __file__ """ ).compile() if cls.compression == ZIP_DEFLATED: space = gettestobjspace(usemodules=["zipimport", "zlib", "rctime", "struct"]) else: space = gettestobjspace(usemodules=["zipimport", "rctime", "struct"]) cls.space = space tmpdir = udir.ensure("zipimport_%s" % cls.__name__, dir=1) now = time.time() cls.w_now = space.wrap(now) test_pyc = cls.make_pyc(space, co, now) cls.w_test_pyc = space.wrap(test_pyc) cls.w_compression = space.wrap(cls.compression) cls.w_pathsep = space.wrap(cls.pathsep) # ziptestmodule = tmpdir.ensure('ziptestmodule.zip').write( ziptestmodule = tmpdir.join("somezip.zip") cls.w_tmpzip = space.wrap(str(ziptestmodule)) cls.w_co = space.wrap(co) cls.tmpdir = tmpdir
def test_cdll_life_time(self): from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source( """ long fun(long i) { return i + 42; } """ ) ) eci = ExternalCompilationInfo(export_symbols=["fun"]) lib_name = str(platform.compile([c_file], eci, "x", standalone=False)) lib = CDLL(lib_name) slong = cast_type_to_ffitype(rffi.LONG) fun = lib.getrawpointer("fun", [slong], slong) del lib # already delete here buffer = lltype.malloc(rffi.LONGP.TO, 2, flavor="raw") buffer[0] = 200 buffer[1] = -1 fun.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1))) assert buffer[1] == 242 lltype.free(buffer, flavor="raw") del fun assert not ALLOCATED
def test_struct_by_val(self): import platform if platform.machine() == 'x86_64': py.test.skip("Segfaults on x86_64 because small structures " "may be passed in registers and " "c_elements must not be null") from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source(''' #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; long sum_x_y(struct x_y s) { return s.x + s.y; } long sum_x_y_p(struct x_y *p) { return p->x + p->y; } ''')) eci = ExternalCompilationInfo(export_symbols=['sum_x_y']) lib_name = str(platform.compile([c_file], eci, 'x', standalone=False)) lib = CDLL(lib_name) slong = cast_type_to_ffitype(rffi.LONG) size = slong.c_size * 2 alignment = slong.c_alignment tp = make_struct_ffitype(size, alignment) sum_x_y = lib.getrawpointer('sum_x_y', [tp], slong) buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw') buffer[0] = 200 buffer[1] = 220 buffer[2] = 666 sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) assert buffer[2] == 420 lltype.free(buffer, flavor='raw') del sum_x_y lltype.free(tp, flavor='raw') del lib assert not ALLOCATED
def setup_module(mod): mod.space = gettestobjspace(usemodules=['posix']) mod.path = udir.join('posixtestfile.txt') mod.path.write("this is a test") 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 test_struct_by_val(self): import platform if platform.machine() == 'x86_64': py.test.skip("Segfaults on x86_64 because small structures " "may be passed in registers and " "c_elements must not be null") from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write(py.code.Source(''' #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; long sum_x_y(struct x_y s) { return s.x + s.y; } long sum_x_y_p(struct x_y *p) { return p->x + p->y; } ''')) eci = ExternalCompilationInfo(export_symbols=['sum_x_y']) lib_name = str(platform.compile([c_file], eci, 'x', standalone=False)) lib = CDLL(lib_name) slong = cast_type_to_ffitype(rffi.LONG) size = slong.c_size*2 alignment = slong.c_alignment tp = make_struct_ffitype(size, alignment) sum_x_y = lib.getrawpointer('sum_x_y', [tp], slong) buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw') buffer[0] = 200 buffer[1] = 220 buffer[2] = 666 sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) assert buffer[2] == 420 lltype.free(buffer, flavor='raw') del sum_x_y lltype.free(tp, flavor='raw') del lib assert not ALLOCATED
def test_extra_include_dirs(): udir.ensure("incl", dir=True) udir.join("incl", "incl.h").write("#define C 3") c_file = udir.join("test_extra_include_dirs.c") c_source = """ #include <incl.h> int fun () { return (C); } """ c_file.write(c_source) z = llexternal('fun', [], Signed, sources=[str(c_file)], include_dirs= [str(udir.join("incl"))]) def f(): return z() res = compile(f, []) assert res() == 3
def setup_class(cls): tmpdir = udir.ensure('testeci', dir=1) c_file = tmpdir.join('module.c') c_file.write(py.code.Source(''' int sum(int x, int y) { return x + y; } ''')) cls.modfile = c_file cls.tmpdir = tmpdir
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('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
def test_extra_include_dirs(self): udir.ensure("incl", dir=True) udir.join("incl", "incl.h").write("#define C 3") c_source = py.code.Source(""" #include <incl.h> int fun () { return (C); } """) eci = ExternalCompilationInfo(includes=['incl.h'], include_dirs=[str(udir.join('incl'))], separate_module_sources=[c_source]) z = llexternal('fun', [], Signed, compilation_info=eci) def f(): return z() res = self.compile(f, []) assert res() == 3
def test_load_html(): tmpdir = udir.ensure("docloader", dir=1) help = tmpdir.ensure("one.html").write("<a href='dupa'>%s</a>") code = tmpdir.ensure("test_snippets2.py").write(str(py.code.Source(''' class AppTest_one(object): def test_snippet_1(self): x = 1 ''')) + '\n') ld = DocLoader(docdir=tmpdir, consoles=['one'], testfile=tmpdir.join('test_snippets2.py')) assert ld.get_html('one') == "<a href='dupa'>x = 1</a>" assert ld.get_snippet('one', 0) == 'x = 1'
def setup_module(mod): d = udir.ensure('test_vfs', dir=1) d.join('file1').write('somedata1') d.join('file2').write('somelongerdata2') os.chmod(str(d.join('file2')), stat.S_IWUSR) # unreadable d.join('.hidden').write('secret') d.ensure('subdir1', dir=1).join('subfile1').write('spam') d.ensure('.subdir2', dir=1).join('subfile2').write('secret as well') if HASLINK: d.join('symlink1').mksymlinkto(str(d.join('subdir1'))) d.join('symlink2').mksymlinkto('.hidden') d.join('symlink3').mksymlinkto('BROKEN')
def test_struct_by_val(self): from pypy.translator.tool.cbuild import ExternalCompilationInfo from pypy.translator.platform import platform from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source( """ #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; long sum_x_y(struct x_y s) { return s.x + s.y; } long sum_x_y_p(struct x_y *p) { return p->x + p->y; } """ ) ) eci = ExternalCompilationInfo(export_symbols=["sum_x_y"]) lib_name = str(platform.compile([c_file], eci, "x", standalone=False)) lib = CDLL(lib_name) slong = cast_type_to_ffitype(rffi.LONG) size = slong.c_size * 2 alignment = slong.c_alignment tpe = make_struct_ffitype_e(size, alignment, [slong, slong]) sum_x_y = lib.getrawpointer("sum_x_y", [tpe.ffistruct], slong) buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor="raw") buffer[0] = 200 buffer[1] = 220 buffer[2] = 666 sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) assert buffer[2] == 420 lltype.free(buffer, flavor="raw") del sum_x_y lltype.free(tpe, flavor="raw") del lib assert not ALLOCATED
def setup_class(cls): tmpdir = udir.ensure("zipimport_%s" % cls.__name__, dir=1) zipname = str(tmpdir.join("somezip.zip")) cls.zipname = zipname zipfile = ZipFile(zipname, "w", compression=cls.compression) cls.year = time.localtime(time.time())[0] zipfile.writestr("one", "stuff\n") zipfile.writestr("dir" + os.path.sep + "two", "otherstuff") # Value selected to produce a CRC32 which is negative if # interpreted as a signed 32 bit integer. This exercises the # masking behavior necessary on 64 bit platforms. zipfile.writestr("three", "hello, world") zipfile.close()
def setup_class(cls): tmpdir = udir.ensure('zipimport_%s' % cls.__name__, dir=1) zipname = str(tmpdir.join("somezip.zip")) cls.zipname = zipname zipfile = ZipFile(zipname, "w", compression=cls.compression) cls.year = time.localtime(time.time())[0] zipfile.writestr("one", "stuff") zipfile.writestr("dir" + os.path.sep + "two", "otherstuff") # Value selected to produce a CRC32 which is negative if # interpreted as a signed 32 bit integer. This exercises the # masking behavior necessary on 64 bit platforms. zipfile.writestr("three", "hello, world") zipfile.close()
def test_extra_include_dirs(self): udir.ensure("incl", dir=True) udir.join("incl", "incl.h").write("#define C 3") c_source = py.code.Source(""" #include <incl.h> int fun () { return (C); } """) eci = ExternalCompilationInfo( includes=['incl.h'], include_dirs=[str(udir.join('incl'))], separate_module_sources=[c_source] ) z = llexternal('fun', [], Signed, compilation_info=eci) def f(): return z() res = self.compile(f, []) assert res() == 3
def test_struct_by_val(self): from pypy.translator.tool.cbuild import compile_c_module, \ ExternalCompilationInfo from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source(''' #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; long sum_x_y(struct x_y s) { return s.x + s.y; } long sum_x_y_p(struct x_y *p) { return p->x + p->y; } ''')) eci = ExternalCompilationInfo(export_symbols=['sum_x_y']) lib_name = compile_c_module([c_file], 'x', eci) lib = CDLL(lib_name) slong = cast_type_to_ffitype(rffi.LONG) size = slong.c_size * 2 alignment = slong.c_alignment tp = make_struct_ffitype(size, alignment) sum_x_y = lib.getrawpointer('sum_x_y', [tp], slong) buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw') buffer[0] = 200 buffer[1] = 220 buffer[2] = 666 sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) assert buffer[2] == 420 lltype.free(buffer, flavor='raw') del sum_x_y lltype.free(tp, flavor='raw') del lib assert not ALLOCATED
def test_load_html(): tmpdir = udir.ensure("docloader", dir=1) help = tmpdir.ensure("one.html").write("<a href='dupa'>%s</a>") code = tmpdir.ensure("test_snippets2.py").write( str( py.code.Source(''' class AppTest_one(object): def test_snippet_1(self): x = 1 ''')) + '\n') ld = DocLoader(docdir=tmpdir, consoles=['one'], testfile=tmpdir.join('test_snippets2.py')) assert ld.get_html('one') == "<a href='dupa'>x = 1</a>" assert ld.get_snippet('one', 0) == 'x = 1'
def test_PYTHONPATH_takes_precedence(space): if sys.platform == "win32": py.test.skip("unresolved issues with win32 shell quoting rules") from pypy.interpreter.test.test_zpy import pypypath extrapath = udir.ensure("pythonpath", dir=1) extrapath.join("urllib.py").write("print 42\n") old = os.environ.get('PYTHONPATH', None) try: os.environ['PYTHONPATH'] = str(extrapath) output = py.process.cmdexec('''"%s" "%s" -c "import urllib"''' % (sys.executable, pypypath) ) assert output.strip() == '42' finally: if old: os.environ['PYTHONPATH'] = old
def test_realdir_exclude(): xdir = udir.ensure('test_realdir_exclude', dir=1) xdir.ensure('test_realdir_exclude.yes') xdir.ensure('test_realdir_exclude.no') v_udir = RealDir(str(udir), exclude=['.no']) v_xdir = v_udir.join('test_realdir_exclude') assert 'test_realdir_exclude.yes' in v_xdir.keys() assert 'test_realdir_exclude.no' not in v_xdir.keys() v_xdir.join('test_realdir_exclude.yes') # works py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.no') # Windows and Mac tests, for the case py.test.raises(OSError, v_xdir.join, 'Test_RealDir_Exclude.no') py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.No') py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.nO') py.test.raises(OSError, v_xdir.join, 'test_realdir_exclude.NO')
def generate_source(self, db=None, defines={}, exe_name=None): assert self.c_source_filename is None translator = self.translator if db is None: db = self.build_database() pf = self.getentrypointptr() if self.modulename is None: self.modulename = uniquemodulename('testing') modulename = self.modulename targetdir = udir.ensure(modulename, dir=1) if self.config.translation.dont_write_c_files: targetdir = NullPyPathLocal(targetdir) self.targetdir = targetdir defines = defines.copy() if self.config.translation.countmallocs: defines['COUNT_OP_MALLOCS'] = 1 if self.config.translation.sandbox: defines['RPY_SANDBOXED'] = 1 if CBuilder.have___thread is None: CBuilder.have___thread = self.translator.platform.check___thread() if not self.standalone: assert not self.config.translation.instrument if self.cpython_extension: defines['PYPY_CPYTHON_EXTENSION'] = 1 else: defines['PYPY_STANDALONE'] = db.get(pf) if self.config.translation.instrument: defines['INSTRUMENT'] = 1 if CBuilder.have___thread: if not self.config.translation.no__thread: defines['USE___THREAD'] = 1 if self.config.translation.shared: defines['PYPY_MAIN_FUNCTION'] = "pypy_main_startup" self.eci = self.eci.merge( ExternalCompilationInfo( export_symbols=["pypy_main_startup"])) self.eci, cfile, extra = gen_source(db, modulename, targetdir, self.eci, defines=defines, split=self.split) self.c_source_filename = py.path.local(cfile) self.extrafiles = self.eventually_copy(extra) self.gen_makefile(targetdir, exe_name=exe_name) return cfile
def generate_source(self, db=None, defines={}): assert self.c_source_filename is None translator = self.translator if db is None: db = self.build_database() pf = self.getentrypointptr() pfname = db.get(pf) if self.modulename is None: self.modulename = uniquemodulename('testing') modulename = self.modulename targetdir = udir.ensure(modulename, dir=1) self.targetdir = targetdir defines = defines.copy() if self.config.translation.countmallocs: defines['COUNT_OP_MALLOCS'] = 1 if self.config.translation.sandbox: defines['RPY_SANDBOXED'] = 1 if CBuilder.have___thread is None: CBuilder.have___thread = check_under_under_thread() if not self.standalone: assert not self.config.translation.instrument cfile, extra = gen_source(db, modulename, targetdir, self.eci, defines = defines) else: if self.config.translation.instrument: defines['INSTRUMENT'] = 1 if CBuilder.have___thread: if not self.config.translation.no__thread: defines['USE___THREAD'] = 1 # explicitely include python.h and exceptions.h # XXX for now, we always include Python.h from distutils import sysconfig python_inc = sysconfig.get_python_inc() pypy_include_dir = autopath.this_dir self.eci = self.eci.merge(ExternalCompilationInfo( include_dirs=[python_inc, pypy_include_dir], )) cfile, extra = gen_source_standalone(db, modulename, targetdir, self.eci, entrypointname = pfname, defines = defines) self.c_source_filename = py.path.local(cfile) self.extrafiles = extra if self.standalone: self.gen_makefile(targetdir) return cfile
def test_struct_by_val(self): from pypy.translator.tool.cbuild import compile_c_module, \ ExternalCompilationInfo from pypy.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write(py.code.Source(''' #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; long sum_x_y(struct x_y s) { return s.x + s.y; } long sum_x_y_p(struct x_y *p) { return p->x + p->y; } ''')) lib_name = compile_c_module([c_file], 'x', ExternalCompilationInfo()) lib = CDLL(lib_name) size = ffi_type_slong.c_size*2 alignment = ffi_type_slong.c_alignment tp = make_struct_ffitype(size, alignment) sum_x_y = lib.getrawpointer('sum_x_y', [tp], ffi_type_slong) buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw') buffer[0] = 200 buffer[1] = 220 buffer[2] = 666 sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) assert buffer[2] == 420 lltype.free(buffer, flavor='raw') del sum_x_y lltype.free(tp, flavor='raw') del lib assert not ALLOCATED
def run(filename, outputname): filepath = dirpath.join(filename) tmpdir = udir.ensure('testcache-' + filename, dir=True) outputpath = tmpdir.join(outputname) d = {'__file__': str(outputpath)} path = sys.path[:] try: sys.path.insert(0, str(dirpath)) execfile(str(filepath), d) finally: sys.path[:] = path # assert outputpath.check(exists=1) d = {} execfile(str(outputpath), d) return d
def generate_source(self, db=None, defines={}, exe_name=None): assert self.c_source_filename is None translator = self.translator if db is None: db = self.build_database() pf = self.getentrypointptr() if self.modulename is None: self.modulename = uniquemodulename('testing') modulename = self.modulename targetdir = udir.ensure(modulename, dir=1) self.targetdir = targetdir defines = defines.copy() if self.config.translation.countmallocs: defines['COUNT_OP_MALLOCS'] = 1 if self.config.translation.sandbox: defines['RPY_SANDBOXED'] = 1 if CBuilder.have___thread is None: CBuilder.have___thread = self.translator.platform.check___thread() if not self.standalone: assert not self.config.translation.instrument self.eci, cfile, extra = gen_source(db, modulename, targetdir, self.eci, defines = defines, split=self.split) else: pfname = db.get(pf) if self.config.translation.instrument: defines['INSTRUMENT'] = 1 if CBuilder.have___thread: if not self.config.translation.no__thread: defines['USE___THREAD'] = 1 if self.config.translation.shared: defines['PYPY_MAIN_FUNCTION'] = "pypy_main_startup" self.eci = self.eci.merge(ExternalCompilationInfo( export_symbols=["pypy_main_startup"])) self.eci, cfile, extra = gen_source_standalone(db, modulename, targetdir, self.eci, entrypointname = pfname, defines = defines) self.c_source_filename = py.path.local(cfile) self.extrafiles = self.eventually_copy(extra) self.gen_makefile(targetdir, exe_name=exe_name) return cfile
def test_expectcollect(): try: import pexpect except ImportError: py.test.skip("pexpect not found") source = py.code.Source(""" class ExpectTestOne: def test_one(self): pass """) from pypy import conftest tdir = udir.ensure("t", dir=True) dest = tdir.join("test_expect.py") dest.write(source) col = conftest.Module(dest) result = col.run() assert len(result) == 1
def generate_source(self, db=None, defines={}): assert self.c_source_filename is None translator = self.translator if db is None: db = self.build_database() pf = self.getentrypointptr() if self.modulename is None: self.modulename = uniquemodulename('testing') modulename = self.modulename targetdir = udir.ensure(modulename, dir=1) self.targetdir = targetdir defines = defines.copy() if self.config.translation.countmallocs: defines['COUNT_OP_MALLOCS'] = 1 if self.config.translation.sandbox: defines['RPY_SANDBOXED'] = 1 if CBuilder.have___thread is None: CBuilder.have___thread = self.translator.platform.check___thread() if not self.standalone: assert not self.config.translation.instrument self.eci, cfile, extra = gen_source(db, modulename, targetdir, self.eci, defines=defines, split=self.split) else: pfname = db.get(pf) if self.config.translation.instrument: defines['INSTRUMENT'] = 1 if CBuilder.have___thread: if not self.config.translation.no__thread: defines['USE___THREAD'] = 1 self.eci, cfile, extra = gen_source_standalone( db, modulename, targetdir, self.eci, entrypointname=pfname, defines=defines) self.c_source_filename = py.path.local(cfile) self.extrafiles = self.eventually_copy(extra) self.gen_makefile(targetdir) return cfile
def test_utime(): path = str(udir.ensure("test_utime.txt")) from time import time, sleep t0 = time() sleep(1) def does_stuff(flag): if flag: os.utime(path, None) else: os.utime(path, (int(t0), int(t0))) func = compile(does_stuff, [int]) func(1) assert os.stat(path).st_atime > t0 func(0) assert int(os.stat(path).st_atime) == int(t0)
def make_class(cls): co = py.code.Source(""" def get_name(): return __name__ def get_file(): return __file__ """).compile() if cls.compression == ZIP_DEFLATED: space = gettestobjspace(usemodules=['zipimport', 'zlib', 'rctime']) else: space = gettestobjspace(usemodules=['zipimport', 'rctime']) cls.space = space tmpdir = udir.ensure('zipimport_%s' % cls.__name__, dir=1) now = time.time() cls.w_now = space.wrap(now) test_pyc = cls.make_pyc(space, co, now) cls.w_test_pyc = space.wrap(test_pyc) cls.w_compression = space.wrap(cls.compression) #ziptestmodule = tmpdir.ensure('ziptestmodule.zip').write( ziptestmodule = tmpdir.join("somezip.zip") cls.w_tmpzip = space.wrap(str(ziptestmodule)) cls.w_co = space.wrap(co) cls.tmpdir = tmpdir cls.w_writefile = space.appexec([], """(): def writefile (self, filename, data): import sys import time from zipfile import ZipFile, ZipInfo z = ZipFile(self.zipfile, 'w') write_files = self.write_files write_files.append((filename, data)) for filename, data in write_files: zinfo = ZipInfo(filename, time.localtime(self.now)) zinfo.compress_type = self.compression z.writestr(zinfo, data) self.write_files = write_files # XXX populates sys.path, but at applevel if sys.path[0] != self.zipfile: sys.path.insert(0, self.zipfile) z.close() return writefile """)
def generate_source(self, db=None, defines={}): assert self.c_source_filename is None translator = self.translator if db is None: db = self.build_database() pf = self.getentrypointptr() pfname = db.get(pf) if self.modulename is None: self.modulename = uniquemodulename('testing') modulename = self.modulename targetdir = udir.ensure(modulename, dir=1) self.targetdir = targetdir defines = defines.copy() if self.config.translation.countmallocs: defines['COUNT_OP_MALLOCS'] = 1 if CBuilder.have___thread is None: CBuilder.have___thread = check_under_under_thread() if not self.standalone: assert not self.config.translation.instrument from pypy.translator.c.symboltable import SymbolTable # XXX fix symboltable #self.symboltable = SymbolTable() cfile, extra, extraincludes = gen_source(db, modulename, targetdir, defines = defines, exports = self.exports, symboltable = self.symboltable) else: if self.config.translation.instrument: defines['INSTRUMENT'] = 1 if CBuilder.have___thread: if not self.config.translation.no__thread: defines['USE___THREAD'] = 1 cfile, extra, extraincludes = \ gen_source_standalone(db, modulename, targetdir, entrypointname = pfname, defines = defines) self.c_source_filename = py.path.local(cfile) self.extrafiles = extra self.extraincludes = extraincludes.keys() if self.standalone: self.gen_makefile(targetdir) return cfile
def setup_module(mod): if os.name != 'nt': mod.space = gettestobjspace(usemodules=['posix']) 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 # 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)
def print_ssa_repr(self, ssarepr, portal_jitdriver, verbose): if verbose: print '%s:' % (ssarepr.name, ) print format_assembler(ssarepr) else: log.dot() dir = udir.ensure("jitcodes", dir=1) if portal_jitdriver: name = "%02d_portal_runner" % (portal_jitdriver.index, ) elif ssarepr.name and ssarepr.name != '?': name = ssarepr.name else: name = 'unnamed' % id(ssarepr) i = 1 # escape <lambda> names for windows name = name.replace('<lambda>', '_(lambda)_') extra = '' while name + extra in self._seen_files: i += 1 extra = '.%d' % i self._seen_files.add(name + extra) dir.join(name + extra).write(format_assembler(ssarepr))
def main(basedir, name='pypy-nightly'): basedir = py.path.local(basedir) pypy_c = basedir.join('pypy', 'translator', 'goal', 'pypy-c') if not pypy_c.check(): raise PyPyCNotFound('Please compile pypy first, using translate.py') builddir = udir.ensure("build", dir=True) pypydir = builddir.ensure("pypy", dir=True) shutil.copytree(str(basedir.join('lib-python')), str(pypydir.join('lib-python')), ignore=ignore_patterns('.svn')) pypydir.ensure('pypy', dir=True) shutil.copytree(str(basedir.join('pypy', 'lib')), str(pypydir.join('pypy', 'lib')), ignore=ignore_patterns('.svn')) pypydir.ensure('bin', dir=True) shutil.copy(str(pypy_c), str(pypydir.join('bin', 'pypy-c'))) old_dir = os.getcwd() try: os.chdir(str(builddir)) os.system('tar cvjf ' + str(builddir.join(name + '.tar.bz2')) + " pypy") finally: os.chdir(old_dir) return builddir # for tests