From: <zy...@us...> - 2009-02-06 07:07:47
|
Revision: 6016 http://jython.svn.sourceforge.net/jython/?rev=6016&view=rev Author: zyasoft Date: 2009-02-06 07:07:45 +0000 (Fri, 06 Feb 2009) Log Message: ----------- Fixed some cellvar/freevar nested scope issues, with work to continue. Added a slightly modified version of pycimport.py from Tobias' pyasm implementation in the sandbox. This, in conjunction with compileall (run from CPython; compileall.compile_dir against the unit tests) is allowing a significant fraction of the regrtest to run against PBC-VM. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Added Paths: ----------- branches/pbcvm/Lib/pycimport.py Added: branches/pbcvm/Lib/pycimport.py =================================================================== --- branches/pbcvm/Lib/pycimport.py (rev 0) +++ branches/pbcvm/Lib/pycimport.py 2009-02-06 07:07:45 UTC (rev 6016) @@ -0,0 +1,84 @@ +import sys +import os + +from org.python.core import imp as _imp, PyFrame as _Frame, Py as _Py +from marshal import Unmarshaller + +__debugging__ = False + +def __readPycHeader(file): + def read(): + return ord(file.read(1)) + magic = read() | (read()<<8) + if not ( file.read(1) == '\r' and file.read(1) == '\n' ): + raise TypeError("not valid pyc-file") + mtime = read() | (read()<<8) | (read()<<16) | (read()<<24) + return magic, mtime + +def __makeModule(name, code, path): + module = _imp.addModule(name) + builtins = _Py.getSystemState().builtins + frame = _Frame(code, module.__dict__, module.__dict__, builtins) + module.__file__ = path + code.call(frame) # execute module code + return module + +class __Importer(object): + def __init__(self, path): + if __debugging__: print "Importer invoked" + self.__path = path + def find_module(self, fullname, path=None): + if __debugging__: + print "Importer.find_module(fullname=%s, path=%s)" % ( + repr(fullname), repr(path)) + path = fullname.split('.') + filename = path[-1] + path = path[:-1] + pycfile = os.path.join(self.__path, *(path + [filename + '.pyc'])) + pyfile = os.path.join(self.__path, *(path + [filename + '.py'])) + if os.path.exists(pycfile): + f = open(pycfile, 'rb') + try: + magic, mtime = __readPycHeader(f) + except: + return None # abort! not a valid pyc-file + f.close() + if os.path.exists(pyfile): + pytime = os.stat(pyfile).st_mtime + if pytime > mtime: + return None # abort! py-file was newer + return self + else: + return None # abort! pyc-file does not exist + def load_module(self, fullname): + path = fullname.split('.') + path[-1] += '.pyc' + filename = os.path.join(self.__path, *path) + f = open(filename, 'rb') + magic, mtime = __readPycHeader(f) + #code = Unmarshaller(f, magic=magic).load() + code = Unmarshaller(f).load() + if __debugging__: print "Successfully loaded:", fullname + return __makeModule( fullname, code, filename ) + +class __MetaImporter(object): + def __init__(self): + self.__importers = {} + def find_module(self, fullname, path): + if __debugging__: print "MetaImporter.find_module(%s, %s)" % ( + repr(fullname), repr(path)) + for _path in sys.path: + if _path not in self.__importers: + try: + self.__importers[_path] = __Importer(_path) + except: + self.__importers[_path] = None + importer = self.__importers[_path] + if importer is not None: + loader = importer.find_module(fullname, path) + if loader is not None: + return loader + else: + return None + +sys.meta_path.insert(0, __MetaImporter()) Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 03:50:10 UTC (rev 6015) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-06 07:07:45 UTC (rev 6016) @@ -1,6 +1,7 @@ package org.python.core; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -234,7 +235,8 @@ // in a shadow version of the frame that we copy back to on entry/exit and downcalls if (debug) { - System.err.println("Entry with " + f.f_lasti + " into " + co_code.length); + System.err.println(co_name + ":" + f.f_lasti + "/" + co_code.length + + ", cells:" + Arrays.toString(co_cellvars) + ", free:" + Arrays.toString(co_freevars)); } if (f.f_lasti >= co_code.length) { throw Py.SystemError(""); @@ -700,7 +702,7 @@ case Opcode.BUILD_CLASS: { PyObject methods = stack.pop(); - PyObject bases[] = ((PyTuple)(stack.pop())).getArray(); // new PyTuple(stack.pop())).getArray(); + PyObject bases[] = ((PySequenceList)(stack.pop())).getArray(); String name = stack.pop().toString(); stack.push(Py.makeClass(name, bases, methods)); break; @@ -761,13 +763,26 @@ f.dellocal(oparg); break; - case Opcode.LOAD_CLOSURE: - stack.push(f.getclosure(oparg)); + case Opcode.LOAD_CLOSURE: { + if (debug) { + System.err.println("LOAD_CLOSURE: " + Arrays.toString(f.f_env)); + } + PyCell cell = (PyCell)(f.getclosure(oparg)); + if (cell.ob_ref == null) { + cell.ob_ref = f.getlocal(oparg); + } +// cell.ob_ref = f.getname(co_freevars[oparg]); + stack.push(cell); break; + } - case Opcode.LOAD_DEREF: + case Opcode.LOAD_DEREF: { + if (debug) { + System.err.println("LOAD_DEREF: " + Arrays.toString(f.f_env)); + } stack.push(f.getderef(oparg)); break; + } case Opcode.STORE_DEREF: f.setderef(oparg, stack.pop()); @@ -864,7 +879,7 @@ case Opcode.IMPORT_FROM: String name = co_names[oparg]; try { - stack.push(stack.pop().__getattr__(name)); + stack.push(stack.top().__getattr__(name)); } catch (PyException pye) { if (Py.matchException(pye, Py.AttributeError)) { @@ -1012,7 +1027,16 @@ case Opcode.MAKE_CLOSURE: { PyCode code = (PyCode) stack.pop(); - PyObject[] closure_cells = new PyTuple(stack.pop()).getArray(); + PyObject[] closure_cells = ((PySequenceList)(stack.pop())).getArray(); +// PyObject[] closure_cells = new PyCell[src_closure_cells.length]; +// for (int i = 0; i < src_closure_cells.length; i++) { +// PyCell cell = new PyCell(); +// cell.ob_ref = src_closure_cells[i]; +// closure_cells[i] = cell; +// } +//// for (int i = 0; i < src_closure_cells.length; i++) { +//// closure_cells[i] = f.getclosure(i); +//// } PyObject[] defaults = stack.popN(oparg); PyFunction func = new PyFunction(f.f_globals, defaults, code, closure_cells); stack.push(func); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |