def setup_module(mod): usemodules = ["binascii", "posix", "signal", "struct", "time"] # py3k os.open uses subprocess, requiring the following per platform if os.name != "nt": usemodules += ["fcntl", "select"] else: usemodules += ["_rawffi", "thread"] mod.space = gettestobjspace(usemodules=usemodules) 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")), 0o600) pdir.join("file2").write("test2") pdir.join("another_longer_file_name").write("test3") mod.pdir = pdir bytes_dir = udir.ensure("fi\xc5\x9fier.txt", dir=True) bytes_dir.join("somefile").write("who cares?") bytes_dir.join("caf\xe9").write("who knows?") mod.bytes_dir = bytes_dir # an escaped surrogate mod.esurrogate_dir = udir.ensure("foo\x80", dir=True) # 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 setup_module(mod): usemodules = ['binascii', 'posix', 'struct', 'time'] if os.name != 'nt': usemodules += ['fcntl'] else: # On windows, os.popen uses the subprocess module usemodules += ['_rawffi', 'thread', 'signal'] mod.space = gettestobjspace(usemodules=usemodules) 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)
def setup_module(mod): mod.space = gettestobjspace(usemodules=USEMODULES) 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 if sys.platform == 'darwin': # see issue https://bugs.python.org/issue31380 unicode_dir = udir.ensure('fixc5x9fier.txt', dir=True) file_name = 'cafxe9' else: unicode_dir = udir.ensure('fi\xc5\x9fier.txt', dir=True) file_name = 'caf\xe9' unicode_dir.join('somefile').write('who cares?') unicode_dir.join(file_name).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)
def setup_module(mod): usemodules = ['binascii', 'posix', 'signal', 'struct', 'time'] # py3k os.open uses subprocess, requiring the following per platform if os.name != 'nt': usemodules += ['fcntl', 'select'] else: usemodules += ['_rawffi', 'thread'] mod.space = gettestobjspace(usemodules=usemodules) 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')), 0o600) pdir.join('file2').write("test2") pdir.join('another_longer_file_name').write("test3") mod.pdir = pdir bytes_dir = udir.ensure('fi\xc5\x9fier.txt', dir=True) bytes_dir.join('somefile').write('who cares?') bytes_dir.join('caf\xe9').write('who knows?') mod.bytes_dir = bytes_dir # an escaped surrogate mod.esurrogate_dir = udir.ensure('foo\x80', dir=True) # 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 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 setup_class(cls): from rpython.tool.udir import udir from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.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 = [] 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__) # INCLUDE = '#include "src/precommondefs.h"\n' c_file.write(INCLUDE + str(py.code.Source('\n'.join(snippets)))) eci = ExternalCompilationInfo(include_dirs=[cdir]) 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 rpython.tool.udir import udir from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.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 = [ """ #include "src/precommondefs.h" #define DLLEXPORT RPY_EXPORTED """ ] 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(include_dirs=[cdir]) # Windows note: can't reuse the same file name 'x.dll', because # the previous one is likely still opened return str( platform.compile([c_file], eci, 'x' + cls.__name__, standalone=False))
def prepare_c_example(cls): from rpython.tool.udir import udir from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.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 rpython.tool.udir import udir from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.tool.cbuild import STANDARD_DEFINES from rpython.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-Za-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 test_cdll_life_time(self): from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source(''' #include "src/precommondefs.h" RPY_EXPORTED long fun(long i) { return i + 42; } ''')) eci = ExternalCompilationInfo(include_dirs=[cdir]) 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 gc.collect() assert not ALLOCATED
def test_cdll_life_time(self): from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.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): from rpython.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 prepare_c_example(cls): from rpython.tool.udir import udir from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.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 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() space = cls.space tmpdir = udir.ensure('zipimport_%s_%s' % (__name__, 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.tmpdir = tmpdir
def test_options(self, test='testoptions'): builddir = udir.ensure("build", dir=True) retval, builddir = package.package( '--without-cffi', '--builddir', str(builddir), '--archive-name', test, '--rename_pypy_c', self.rename_pypy_c, _fake=True)
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 rpython.rtyper.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 test_cdll_life_time(self): from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source( """ #include "src/precommondefs.h" RPY_EXPORTED long fun(long i) { return i + 42; } """ ) ) eci = ExternalCompilationInfo(include_dirs=[cdir]) 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 setup_class(cls): from rpython.tool.udir import udir from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.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 = [] 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__) # INCLUDE = '#include "src/precommondefs.h"\n' c_file.write(INCLUDE + str(py.code.Source('\n'.join(snippets)))) eci = ExternalCompilationInfo(include_dirs=[cdir]) cls.libfoo_name = str( platform.compile([c_file], eci, 'x', standalone=False)) cls.dll = cls.CDLL(cls.libfoo_name)
def package(*args): try: import argparse except ImportError: import imp argparse = imp.load_source('argparse', 'lib-python/2.7/argparse.py') if sys.platform == 'win32': pypy_exe = 'pypy.exe' license_base = os.path.join(basedir, r'..\..\..\local') # as on buildbot YMMV else: pypy_exe = 'pypy' license_base = '/usr/share/doc' parser = argparse.ArgumentParser() args = list(args) args[0] = str(args[0]) parser.add_argument('--without-tk', dest='no_tk', action='store_true', help='build and package the cffi tkinter module') parser.add_argument('--without-cffi', dest='no_cffi', action='store_true', help='do not pre-import any cffi modules') parser.add_argument('--nostrip', dest='nostrip', action='store_true', help='do not strip the exe, making it ~10MB larger') parser.add_argument('--rename_pypy_c', dest='pypy_c', type=str, default=pypy_exe, help='target executable name, defaults to "pypy"') parser.add_argument('--archive-name', dest='name', type=str, default='', help='pypy-VER-PLATFORM') parser.add_argument('--license_base', type=str, default=license_base, help='where to start looking for third party upstream licensing info') parser.add_argument('--builddir', type=str, default='', help='tmp dir for packaging') parser.add_argument('--targetdir', type=str, default='', help='destination dir for archive') parser.add_argument('--override_pypy_c', type=str, default='', help='use as pypy exe instead of pypy/goal/pypy-c') # Positional arguments, for backward compatability with buldbots parser.add_argument('extra_args', help='optional interface to positional arguments', nargs=argparse.REMAINDER, metavar='[root-pypy-dir] [name-of-archive] [name-of-pypy-c] [destination-for-tarball] [pypy-c-path]', ) options = parser.parse_args(args) # Handle positional arguments, choke if both methods are used for i,target, default in ([1, 'name', ''], [2, 'pypy_c', pypy_exe], [3, 'targetdir', ''], [4,'override_pypy_c', '']): if len(options.extra_args)>i: if getattr(options, target) != default: print 'positional argument',i,target,'already has value',getattr(options, target) parser.print_help() return setattr(options, target, options.extra_args[i]) if os.environ.has_key("PYPY_PACKAGE_NOSTRIP"): options.nostrip = True if os.environ.has_key("PYPY_PACKAGE_WITHOUTTK"): options.tk = True if not options.builddir: # The import actually creates the udir directory from rpython.tool.udir import udir options.builddir = udir.ensure("build", dir=True) assert '/' not in options.pypy_c return create_package(basedir, options)
def _dump_trace(self, addr, name, formatter=-1): if not we_are_translated(): if formatter != -1: name = name % formatter dir = udir.ensure('asm', dir=True) f = dir.join(name).open('wb') data = rffi.cast(rffi.CCHARP, addr) for i in range(self.currpos()): f.write(data[i]) f.close()
def test_struct_by_val(self): from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source( """ #include "src/precommondefs.h" #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; RPY_EXPORTED 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(include_dirs=[cdir]) 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 package(*args): try: import argparse except ImportError: import imp argparse = imp.load_source('argparse', 'lib-python/2.7/argparse.py') if sys.platform == 'win32': pypy_exe = 'pypy.exe' else: pypy_exe = 'pypy' parser = argparse.ArgumentParser() args = list(args) args[0] = str(args[0]) parser.add_argument('--without-tk', dest='no_tk', action='store_true', help='build and package the cffi tkinter module') parser.add_argument('--without-cffi', dest='no_cffi', action='store_true', help='do not pre-import any cffi modules') parser.add_argument('--nostrip', dest='nostrip', action='store_true', help='do not strip the exe, making it ~10MB larger') parser.add_argument('--rename_pypy_c', dest='pypy_c', type=str, default=pypy_exe, help='target executable name, defaults to "pypy"') parser.add_argument('--archive-name', dest='name', type=str, default='', help='pypy-VER-PLATFORM') parser.add_argument('--builddir', type=str, default='', help='tmp dir for packaging') parser.add_argument('--targetdir', type=str, default='', help='destination dir for archive') parser.add_argument('--override_pypy_c', type=str, default='', help='use as pypy exe instead of pypy/goal/pypy-c') # Positional arguments, for backward compatability with buldbots parser.add_argument('extra_args', help='optional interface to positional arguments', nargs=argparse.REMAINDER, metavar='[archive-name] [rename_pypy_c] [targetdir] [override_pypy_c]', ) options = parser.parse_args(args) # Handle positional arguments, choke if both methods are used for i,target, default in ([1, 'name', ''], [2, 'pypy_c', pypy_exe], [3, 'targetdir', ''], [4,'override_pypy_c', '']): if len(options.extra_args)>i: if getattr(options, target) != default: print 'positional argument',i,target,'already has value',getattr(options, target) parser.print_help() return setattr(options, target, options.extra_args[i]) if os.environ.has_key("PYPY_PACKAGE_NOSTRIP"): options.nostrip = True if os.environ.has_key("PYPY_PACKAGE_WITHOUTTK"): options.tk = True if not options.builddir: # The import actually creates the udir directory from rpython.tool.udir import udir options.builddir = udir.ensure("build", dir=True) assert '/' not in options.pypy_c return create_package(basedir, options)
def package(*args, **kwds): try: import argparse except ImportError: import imp argparse = imp.load_source("argparse", "lib-python/2.7/argparse.py") if sys.platform == "win32": pypy_exe = "pypy.exe" else: pypy_exe = "pypy" parser = argparse.ArgumentParser() args = list(args) if args: args[0] = str(args[0]) else: args.append("--help") for key, module in sorted(cffi_build_scripts.items()): if module is not None: parser.add_argument( "--without-" + key, dest="no_" + key, action="store_true", help="do not build and package the %r cffi module" % (key,), ) parser.add_argument( "--without-cffi", dest="no_cffi", action="store_true", help="skip building *all* the cffi modules listed above" ) parser.add_argument( "--nostrip", dest="nostrip", action="store_true", help="do not strip the exe, making it ~10MB larger" ) parser.add_argument( "--rename_pypy_c", dest="pypy_c", type=str, default=pypy_exe, help='target executable name, defaults to "pypy"' ) parser.add_argument("--archive-name", dest="name", type=str, default="", help="pypy-VER-PLATFORM") parser.add_argument("--builddir", type=str, default="", help="tmp dir for packaging") parser.add_argument("--targetdir", type=str, default="", help="destination dir for archive") parser.add_argument("--override_pypy_c", type=str, default="", help="use as pypy exe instead of pypy/goal/pypy-c") options = parser.parse_args(args) if os.environ.has_key("PYPY_PACKAGE_NOSTRIP"): options.nostrip = True if os.environ.has_key("PYPY_PACKAGE_WITHOUTTK"): options.no_tk = True if not options.builddir: # The import actually creates the udir directory from rpython.tool.udir import udir options.builddir = udir.ensure("build", dir=True) else: # if a user provides a path it must be converted to a local file system path # otherwise ensure in create_package will fail options.builddir = py.path.local(options.builddir) assert "/" not in options.pypy_c return create_package(basedir, options, **kwds)
def run_test(self, methname, instrname, argmodes, args_lists, instr_suffix=None): global labelcount labelcount = 0 oplist = [] testdir = udir.ensure(self.TESTDIR, dir=1) inputname = str(testdir.join(INPUTNAME % methname)) filename = str(testdir.join(FILENAME % methname)) with open(inputname, 'w') as g: g.write('\x09.string "%s"\n' % BEGIN_TAG) # for args in args_lists: suffix = "" if instr_suffix is not None: suffix = instr_suffix # overwrite # ops = self.operand_combinations(methname, argmodes, args) op = '\t%s%s %s' % (instrname.lower(), suffix, ', '.join(ops)) g.write('%s\n' % op) oplist.append(op) g.write('\t.string "%s"\n' % END_TAG) proc = subprocess.Popen([ 'as', '-m64', '-mzarch', '-march=z196', inputname, '-o', filename ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() if proc.returncode or stderr: raise Exception("could not execute assembler. error:\n%s" % (stderr)) with open(inputname, 'r') as g: got = g.read() error = [line for line in got.splitlines() if 'error' in line.lower()] if error: raise Exception("Assembler got an error: %r" % error[0]) error = [ line for line in got.splitlines() if 'warning' in line.lower() ] if error: raise Exception("Assembler got a warning: %r" % error[0]) try: with open(filename, 'rb') as f: data = f.read() i = data.find(BEGIN_TAG) assert i >= 0 j = data.find(END_TAG, i) assert j >= 0 as_code = data[i + len(BEGIN_TAG) + 1:j] except IOError: raise Exception("Assembler did not produce output?") return oplist, as_code
def setup_module(mod): mod.space = gettestobjspace(usemodules=USEMODULES) 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)
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 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 rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write( py.code.Source(''' #include "src/precommondefs.h" #include <stdlib.h> #include <stdio.h> struct x_y { long x; long y; }; RPY_EXPORTED 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(include_dirs=[cdir]) 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 gc.collect() assert not ALLOCATED
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 test_options(self, test="testoptions"): builddir = udir.ensure("build", dir=True) retval, builddir = package.package( "--without-cffi", "--builddir", str(builddir), "--archive-name", test, "--rename_pypy_c", self.rename_pypy_c, _fake=True, )
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 package(*args, **kwds): try: import argparse except ImportError: import imp argparse = imp.load_source('argparse', 'lib-python/2.7/argparse.py') if sys.platform == 'win32': pypy_exe = 'pypy.exe' else: pypy_exe = 'pypy' parser = argparse.ArgumentParser() args = list(args) if args: args[0] = str(args[0]) else: args.append('--help') for key, module in sorted(cffi_build_scripts.items()): if module is not None: parser.add_argument('--without-' + key, dest='no_' + key, action='store_true', help='do not build and package the %r cffi module' % (key,)) parser.add_argument('--without-cffi', dest='no_cffi', action='store_true', help='skip building *all* the cffi modules listed above') parser.add_argument('--no-keep-debug', dest='keep_debug', action='store_false', help='do not keep debug symbols') parser.add_argument('--rename_pypy_c', dest='pypy_c', type=str, default=pypy_exe, help='target executable name, defaults to "pypy"') parser.add_argument('--archive-name', dest='name', type=str, default='', help='pypy-VER-PLATFORM') parser.add_argument('--builddir', type=str, default='', help='tmp dir for packaging') parser.add_argument('--targetdir', type=str, default='', help='destination dir for archive') parser.add_argument('--override_pypy_c', type=str, default='', help='use as pypy exe instead of pypy/goal/pypy-c') options = parser.parse_args(args) if os.environ.has_key("PYPY_PACKAGE_NOKEEPDEBUG"): options.keep_debug = False if os.environ.has_key("PYPY_PACKAGE_WITHOUTTK"): options.no_tk = True if not options.builddir: # The import actually creates the udir directory from rpython.tool.udir import udir options.builddir = udir.ensure("build", dir=True) else: # if a user provides a path it must be converted to a local file system path # otherwise ensure in create_package will fail options.builddir = py.path.local(options.builddir) assert '/' not in options.pypy_c return create_package(basedir, options, **kwds)
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 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_module(mod): mod.path = udir.join('posixtestfile.txt') mod.path.write("this is a test") mod.path2 = udir.join('test_posix2-') mod.path3 = udir.join('unlinktestfile.txt') mod.path3.write("delete me!") pdir = udir.ensure('posixtestdir', dir=True) pdir = udir.ensure('posixtestdir', dir=True) pdir.join('file1').write("test1") os.chmod(str(pdir.join('file1')), 0o600) pdir.join('file2').write("test2") pdir.join('another_longer_file_name').write("test3") mod.pdir = pdir if sys.platform == 'darwin': # see issue https://bugs.python.org/issue31380 unicode_dir = udir.ensure('fixc5x9fier.txt', dir=True) file_name = 'cafxe9' else: unicode_dir = udir.ensure('fi\xc5\x9fier.txt', dir=True) file_name = 'caf\xe9' unicode_dir.join('somefile').write('who cares?') unicode_dir.join(file_name).write('who knows?') mod.unicode_dir = unicode_dir
def run_test(self, methname, instrname, argmodes, args_lists, instr_suffix=None): global labelcount labelcount = 0 oplist = [] testdir = udir.ensure(self.TESTDIR, dir=1) inputname = str(testdir.join(INPUTNAME % methname)) filename = str(testdir.join(FILENAME % methname)) with open(inputname, 'w') as g: g.write('\x09.string "%s"\n' % BEGIN_TAG) # for args in args_lists: suffix = "" if instr_suffix is not None: suffix = instr_suffix # overwrite # ops = self.operand_combinations(methname, argmodes, args) op = '\t%s%s %s' % (instrname.lower(), suffix, ', '.join(ops)) g.write('%s\n' % op) oplist.append(op) g.write('\t.string "%s"\n' % END_TAG) proc = subprocess.Popen(['as', '-m64', '-mzarch', '-march=z196', inputname, '-o', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() if proc.returncode or stderr: raise Exception("could not execute assembler. error:\n%s" % (stderr)) with open(inputname, 'r') as g: got = g.read() error = [line for line in got.splitlines() if 'error' in line.lower()] if error: raise Exception("Assembler got an error: %r" % error[0]) error = [line for line in got.splitlines() if 'warning' in line.lower()] if error: raise Exception("Assembler got a warning: %r" % error[0]) try: with open(filename, 'rb') as f: data = f.read() i = data.find(BEGIN_TAG) assert i>=0 j = data.find(END_TAG, i) assert j>=0 as_code = data[i+len(BEGIN_TAG)+1:j] except IOError: raise Exception("Assembler did not produce output?") return oplist, as_code
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 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.countfieldaccess: defines['RPY_COUNT_FIELDACCESS'] = 1 if self.config.translation.sandbox: defines['RPY_SANDBOXED'] = 1 if self.config.translation.reverse_debugger: defines['RPY_REVERSE_DEBUGGER'] = 1 if self.config.translation.rpython_translate: defines['RPY_TRANSLATE'] = 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 else: defines['PYPY_STANDALONE'] = db.get(pf) if self.config.translation.instrument: defines['PYPY_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, cfile, extra, headers_to_precompile = \ 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, headers_to_precompile=headers_to_precompile) 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 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) oldlang = os.environ.pop('LANG', 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 if oldlang: os.environ['LANG'] = oldlang
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("sched.py").write("print(42)\n") old = os.environ.get('PYTHONPATH', None) oldlang = os.environ.pop('LANG', None) try: os.environ['PYTHONPATH'] = str(extrapath) output = py.process.cmdexec('''"%s" "%s" -c "import sched"''' % (sys.executable, pypypath)) assert output.strip() == '42' finally: if old: os.environ['PYTHONPATH'] = old if oldlang: os.environ['LANG'] = oldlang
def test_variadic_args(self): from rpython.translator.tool.cbuild import ExternalCompilationInfo from rpython.translator.platform import platform from rpython.tool.udir import udir c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") c_file.write(py.code.Source(''' #include "src/precommondefs.h" #include <stdarg.h> #include <stdio.h> RPY_EXPORTED Signed fun(Signed n, ...) { va_list ptr; int sum = 0; printf("n: %ld\\n", n); va_start(ptr, n); for (int i = 0; i < n; i++) { Signed foo = va_arg(ptr, Signed); sum += foo; printf("Arg %d: %ld\\n", i, foo); } va_end(ptr); return sum; } ''')) eci = ExternalCompilationInfo(include_dirs=[cdir]) lib_name = str(platform.compile([c_file], eci, 'x4', standalone=False)) lib = CDLL(lib_name) signed = cast_type_to_ffitype(rffi.SIGNED) fun = lib.getrawpointer('fun', [signed, signed, signed], signed, variadic_args=2) buffer = lltype.malloc(rffi.SIGNEDP.TO, 4, flavor='raw') buffer[0] = 2 buffer[1] = 3 buffer[2] = 15 buffer[3] = 100 fun.call([rffi.cast(rffi.VOIDP, buffer), rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1)), rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))], rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 3))) assert buffer[3] == 3 + 15 lltype.free(buffer, flavor='raw')
def make_class(cls): BaseImportTest.setup_class.im_func(cls) space = cls.space w = space.wrap cls.w_appdirect = w(cls.runappdirect) cls.w_now = w(time.time()) cls.w_compression = w(cls.compression) cls.w_pathsep = w(cls.pathsep) cls.tmpdir = udir.ensure('zipimport_%s_%s' % (__name__, cls.__name__), dir=1) ziptestmodule = cls.tmpdir.join("somezip.zip") cls.w_tmpzip = w(str(ziptestmodule)) # Cache get_pyc() get_pyc_source = inspect.getsource( cls.w__get_pyc.im_func).splitlines()[1:] get_pyc_source.insert(0, ' (mtime):') cls.w__test_pyc = space.appexec([cls.w_now], '\n'.join(get_pyc_source))
def generate_source(self, db=None, defines={}, exe_name=None): assert self.c_source_filename is None 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 else: defines['PYPY_STANDALONE'] = db.get(pf) if self.config.translation.instrument: defines['PYPY_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", "pypy_debug_file"])) self.eci, cfile, extra, headers_to_precompile = \ 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, headers_to_precompile=headers_to_precompile) return cfile
def _make_dir(dirname, content): d = os.path.join(str(udir), dirname) os.mkdir(d) for key, value in content.items(): filename = os.path.join(d, key) if value == 'dir': os.mkdir(filename) elif value == 'file': with open(filename, 'w') as f: pass elif value == 'symlink-file': os.symlink(str(udir.ensure('some_file')), filename) elif value == 'symlink-dir': os.symlink(str(udir), filename) elif value == 'symlink-broken': os.symlink(filename + '-broken', filename) elif value == 'symlink-error': os.symlink(filename, filename) else: raise NotImplementedError(repr(value)) return d.decode(sys.getfilesystemencoding())
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 dir.join(name+extra).check(): i += 1 extra = '.%d' % i dir.join(name+extra).write(format_assembler(ssarepr))
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 dir.join(name + extra).check(): i += 1 extra = '.%d' % i dir.join(name + extra).write(format_assembler(ssarepr))
def run(filename, outputname): filepath = dirpath.join(filename) tmpdir = udir.ensure('testcache-' + os.path.splitext(filename)[0], dir=True) tmpdir.join('dumpcache.py').write(dirpath.join('dumpcache.py').read()) path = sys.path[:] sys.modules.pop('dumpcache', None) try: sys.path.insert(0, str(tmpdir)) execfile(str(filepath), {}) finally: sys.path[:] = path sys.modules.pop('dumpcache', None) # outputpath = tmpdir.join(outputname) assert outputpath.check(exists=1) modname = os.path.splitext(outputname)[0] try: sys.path.insert(0, str(tmpdir)) d = {} execfile(str(outputpath), d) finally: sys.path[:] = path return d
def setup_class(cls): GenericTestThread.setup_class.im_func(cls) tmpdir = str(udir.ensure('test_import_lock', dir=1)) cls.w_tmpdir = cls.space.wrap(tmpdir)