def generate_source_for_function(func, annotation, backendopt=False): """ Given a Python function and some hints about its argument types, generates JVM sources that call it and print the result. Returns the JvmGeneratedSource object. """ if hasattr(func, "im_func"): func = func.im_func t = TranslationContext() ann = t.buildannotator() ann.build_types(func, annotation) t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption("view"): t.view() if getoption("wd"): tmpdir = py.path.local(".") else: tmpdir = udir jvm = GenJvm(tmpdir, t, EntryPoint(main_graph, True, True)) return jvm.generate_source()
def _check_java_version(version): """Determine if java meets the specified version""" cmd = [getoption("java"), "-version"] with open(os.devnull, "w") as devnull: stderr = subprocess.Popen(cmd, stdout=devnull, stderr=subprocess.PIPE).communicate()[1] search = re.search("[\.0-9]+", stderr) return search and search.group() >= version
def _check_java_version(version): """Determine if java meets the specified version""" cmd = [getoption('java'), '-version'] with open(os.devnull, 'w') as devnull: stderr = subprocess.Popen(cmd, stdout=devnull, stderr=subprocess.PIPE).communicate()[1] search = re.search('[\.0-9]+', stderr) return search and search.group() >= version
def __init__(self, tmpdir, translator, entrypoint): """ 'tmpdir' --- where the generated files will go. In fact, we will put our binaries into the directory pypy/jvm 'translator' --- a TranslationContext object 'entrypoint' --- if supplied, an object with a render method """ GenOO.__init__(self, tmpdir, translator, entrypoint) self.jvmsrc = JvmGeneratedSource(tmpdir, getoption("package"))
def __init__(self, class_name): # We put all of our classes into some package like 'pypy': # strip the initial 'pypy.' that results from the class name, # and we append a number to make the class name unique. Strip # those. pkg = getoption('package')+'.' assert class_name.startswith(pkg) uniqidx = class_name.rindex('_') self.class_name = class_name[len(pkg):uniqidx]
def __init__(self, tmpdir, translator, entrypoint): """ 'tmpdir' --- where the generated files will go. In fact, we will put our binaries into the directory pypy/jvm 'translator' --- a TranslationContext object 'entrypoint' --- if supplied, an object with a render method """ GenOO.__init__(self, tmpdir, translator, entrypoint) self.jvmsrc = JvmGeneratedSource(tmpdir, getoption('package'))
def __init__(self, class_name): # We put all of our classes into some package like 'pypy': # strip the initial 'pypy.' that results from the class name, # and we append a number to make the class name unique. Strip # those. pkg = getoption('package') + '.' assert class_name.startswith(pkg) uniqidx = class_name.rindex('_') self.class_name = class_name[len(pkg):uniqidx]
def generate_source_for_function(func, annotation, backendopt=False): """ Given a Python function and some hints about its argument types, generates JVM sources that call it and print the result. Returns the JvmGeneratedSource object. """ if hasattr(func, 'im_func'): func = func.im_func t = TranslationContext() ann = t.buildannotator() ann.build_types(func, annotation) t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir jvm = GenJvm(tmpdir, t, EntryPoint(main_graph, True, True)) return jvm.generate_source()
def compile(self, fn, args, ann=None, backendopt=False): if ann is None: ann = [lltype_to_annotation(typeOf(x)) for x in args] if self._func is fn and self._ann == ann: return JvmGeneratedSourceWrapper(self._jvm_src) else: self._func = fn self._ann = ann olddefs = patch_os() self._jvm_src = generate_source_for_function(fn, ann, backendopt) unpatch_os(olddefs) if not getoption('noasm'): self._jvm_src.compile() return JvmGeneratedSourceWrapper(self._jvm_src)
def _compile_helper(self): # HACK: compile the Java helper classes. Should eventually # use rte.py if JvmGeneratedSource._cached == self.classdir: return log.red("Compiling java classes") javafiles = self.srcdir.listdir("*.java") javasrcs = [str(jf) for jf in javafiles] self._invoke( [getoption("javac"), "-nowarn", "-d", str(self.classdir), "-classpath", str(self.jnajar)] + javasrcs, True ) # NOTE if you are trying to add more caching: some .java files # compile to several .class files of various names. JvmGeneratedSource._cached = self.classdir
def run(self,*args): if not self.gensrc.compiled: py.test.skip("Assembly disabled") if getoption('norun'): py.test.skip("Execution disabled") # if self._exe is None: # py.test.skip("Compilation disabled") # if getoption('norun'): # py.test.skip("Execution disabled") stdout, stderr, retval = self.gensrc.execute(args) return stdout, stderr, retval
def __call__(self, *args): if not self.gensrc.compiled: py.test.skip("Assembly disabled") if getoption('norun'): py.test.skip("Execution disabled") stdout, stderr, retval = self.gensrc.execute(args) res = eval(stdout.strip()) if isinstance(res, tuple): res = StructTuple(res) # so tests can access tuple elements with .item0, .item1, etc. elif isinstance(res, list): res = OOList(res) elif isinstance(res, ExceptionWrapper): raise res return res
def run(self, *args): if not self.gensrc.compiled: py.test.skip("Assembly disabled") if getoption('norun'): py.test.skip("Execution disabled") # if self._exe is None: # py.test.skip("Compilation disabled") # if getoption('norun'): # py.test.skip("Execution disabled") stdout, stderr, retval = self.gensrc.execute(args) return stdout, stderr, retval
def detect_missing_support_programs(): global _missing_support_programs if _missing_support_programs is not None: if _missing_support_programs: py.test.skip(_missing_support_programs) return def missing(msg): global _missing_support_programs _missing_support_programs = msg py.test.skip(msg) for cmd in 'javac', 'java': if py.path.local.sysfind(getoption(cmd)) is None: missing("%s is not on your path" % cmd) if not _check_java_version(MIN_JAVA_VERSION): missing('Minimum of Java %s required' % MIN_JAVA_VERSION) _missing_support_programs = False
def detect_missing_support_programs(): global _missing_support_programs if _missing_support_programs is not None: if _missing_support_programs: py.test.skip(_missing_support_programs) return def missing(msg): global _missing_support_programs _missing_support_programs = msg py.test.skip(msg) for cmd in "javac", "java": if py.path.local.sysfind(getoption(cmd)) is None: missing("%s is not on your path" % cmd) if not _check_java_version(MIN_JAVA_VERSION): missing("Minimum of Java %s required" % MIN_JAVA_VERSION) _missing_support_programs = False
def __call__(self, *args): if not self.gensrc.compiled: py.test.skip("Assembly disabled") if getoption('norun'): py.test.skip("Execution disabled") stdout, stderr, retval = self.gensrc.execute(args) res = eval(stdout.strip()) if isinstance(res, tuple): res = StructTuple( res ) # so tests can access tuple elements with .item0, .item1, etc. elif isinstance(res, list): res = OOList(res) elif isinstance(res, ExceptionWrapper): raise res return res
def _compile_helper(self): # HACK: compile the Java helper classes. Should eventually # use rte.py if JvmGeneratedSource._cached == self.classdir: return log.red('Compiling java classes') javafiles = self.srcdir.listdir('*.java') javasrcs = [str(jf) for jf in javafiles] self._invoke([ getoption('javac'), '-nowarn', '-d', str(self.classdir), '-classpath', str(self.jnajar), ] + javasrcs, True) # NOTE if you are trying to add more caching: some .java files # compile to several .class files of various names. JvmGeneratedSource._cached = self.classdir
def compile(self): """ Compiles the .java sources into .class files, ready for execution. """ jascmd = [ getoption("java"), "-Djava.awt.headless=true", "-jar", str(self.jasminjar), "-g", "-d", str(self.javadir), ] def split_list(files): "Split the files list into manageable pieces" # - On Windows 2000, commands in .bat are limited to 2047 chars. # - But the 'jasmin' script contains a line like # path_to_jre/java -jar path_to_jasmin/jasmin.jar $* # So we limit the length of arguments files to: MAXLINE = 1500 chunk = [] chunklen = 0 for f in files: # Account for the space between items chunklen += len(f) + 1 if chunklen > MAXLINE: yield chunk chunk = [] chunklen = len(f) chunk.append(f) if chunk: yield chunk for files in split_list(self.jasmin_files): # print "Invoking jasmin on %s" % files self._invoke(jascmd + files, False) # print "... completed!" self.compiled = True self._compile_helper()
def execute(self, args): """ Executes the compiled sources in a separate process. Returns the output as a string. The 'args' are provided as arguments, and will be converted to strings. """ assert self.compiled strargs = [self._make_str(a) for a in args] cmd = [ getoption('java'), '-Xmx256M', # increase the heapsize so the microbenchmarks run '-Djava.awt.headless=true', '-cp', str(self.javadir) + os.pathsep + str(self.jnajar), self.package + ".Main" ] + strargs print "Invoking java to run the code" stdout, stderr, retval = self._invoke(cmd, True) print "...done!" sys.stderr.write(stderr) return stdout, stderr, retval
def execute(self, args): """ Executes the compiled sources in a separate process. Returns the output as a string. The 'args' are provided as arguments, and will be converted to strings. """ assert self.compiled strargs = [self._make_str(a) for a in args] cmd = [ getoption("java"), "-Xmx256M", # increase the heapsize so the microbenchmarks run "-Djava.awt.headless=true", "-cp", str(self.javadir) + os.pathsep + str(self.jnajar), self.package + ".Main", ] + strargs print "Invoking java to run the code" stdout, stderr, retval = self._invoke(cmd, True) print "...done!" sys.stderr.write(stderr) return stdout, stderr, retval
def compile(self): """ Compiles the .java sources into .class files, ready for execution. """ jascmd = [ getoption('java'), '-Djava.awt.headless=true', '-jar', str(self.jasminjar), '-g', '-d', str(self.javadir) ] def split_list(files): "Split the files list into manageable pieces" # - On Windows 2000, commands in .bat are limited to 2047 chars. # - But the 'jasmin' script contains a line like # path_to_jre/java -jar path_to_jasmin/jasmin.jar $* # So we limit the length of arguments files to: MAXLINE = 1500 chunk = [] chunklen = 0 for f in files: # Account for the space between items chunklen += len(f) + 1 if chunklen > MAXLINE: yield chunk chunk = [] chunklen = len(f) chunk.append(f) if chunk: yield chunk for files in split_list(self.jasmin_files): #print "Invoking jasmin on %s" % files self._invoke(jascmd + files, False) #print "... completed!" self.compiled = True self._compile_helper()
def _pkg(self, nm): return "%s.%s" % (getoption('package'), nm)
def _trace_enabled(self): return getoption('trace')