You can subscribe to this list here.
2000 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
(1) |
Nov
(107) |
Dec
(67) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2001 |
Jan
(76) |
Feb
(125) |
Mar
(72) |
Apr
(13) |
May
(18) |
Jun
(12) |
Jul
(129) |
Aug
(47) |
Sep
(1) |
Oct
(36) |
Nov
(128) |
Dec
(124) |
2002 |
Jan
(59) |
Feb
|
Mar
(14) |
Apr
(14) |
May
(72) |
Jun
(9) |
Jul
(3) |
Aug
(5) |
Sep
(18) |
Oct
(65) |
Nov
(28) |
Dec
(12) |
2003 |
Jan
(10) |
Feb
(2) |
Mar
(4) |
Apr
(33) |
May
(21) |
Jun
(9) |
Jul
(29) |
Aug
(34) |
Sep
(4) |
Oct
(8) |
Nov
(15) |
Dec
(4) |
2004 |
Jan
(26) |
Feb
(12) |
Mar
(11) |
Apr
(9) |
May
(7) |
Jun
|
Jul
(5) |
Aug
|
Sep
(3) |
Oct
(7) |
Nov
(1) |
Dec
(10) |
2005 |
Jan
(2) |
Feb
(72) |
Mar
(16) |
Apr
(39) |
May
(48) |
Jun
(97) |
Jul
(57) |
Aug
(13) |
Sep
(16) |
Oct
(24) |
Nov
(100) |
Dec
(24) |
2006 |
Jan
(15) |
Feb
(34) |
Mar
(33) |
Apr
(31) |
May
(79) |
Jun
(64) |
Jul
(41) |
Aug
(64) |
Sep
(31) |
Oct
(46) |
Nov
(55) |
Dec
(37) |
2007 |
Jan
(32) |
Feb
(61) |
Mar
(11) |
Apr
(58) |
May
(46) |
Jun
(30) |
Jul
(94) |
Aug
(93) |
Sep
(86) |
Oct
(69) |
Nov
(125) |
Dec
(177) |
2008 |
Jan
(169) |
Feb
(97) |
Mar
(74) |
Apr
(113) |
May
(120) |
Jun
(334) |
Jul
(215) |
Aug
(237) |
Sep
(72) |
Oct
(189) |
Nov
(126) |
Dec
(160) |
2009 |
Jan
(180) |
Feb
(45) |
Mar
(98) |
Apr
(140) |
May
(151) |
Jun
(71) |
Jul
(107) |
Aug
(119) |
Sep
(73) |
Oct
(121) |
Nov
(14) |
Dec
(6) |
2010 |
Jan
(13) |
Feb
(9) |
Mar
(10) |
Apr
(64) |
May
(3) |
Jun
(16) |
Jul
(7) |
Aug
(23) |
Sep
(17) |
Oct
(37) |
Nov
(5) |
Dec
(8) |
2011 |
Jan
(10) |
Feb
(11) |
Mar
(77) |
Apr
(11) |
May
(2) |
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: <zy...@us...> - 2009-02-24 05:45:43
|
Revision: 6046 http://jython.svn.sourceforge.net/jython/?rev=6046&view=rev Author: zyasoft Date: 2009-02-24 05:45:38 +0000 (Tue, 24 Feb 2009) Log Message: ----------- Merged revisions 5949,5952,5962,5964,5967-5970,5980,5984,5987-5989,5998-5999,6004-6005,6015-6022,6024-6030,6038,6041-6042 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/branches/pbcvm ........ r5949 | zyasoft | 2009-01-20 06:56:46 -0700 (Tue, 20 Jan 2009) | 2 lines Initial commit. ........ r5952 | zyasoft | 2009-01-20 22:02:29 -0700 (Tue, 20 Jan 2009) | 3 lines Fixed binary and inplace bytecodes to use the proper methods. Added block support to support iteration. ........ r5962 | zyasoft | 2009-01-22 21:40:26 -0700 (Thu, 22 Jan 2009) | 3 lines Added support for try/except and try/finally blocks (and their combination). ........ r5964 | zyasoft | 2009-01-23 08:30:09 -0700 (Fri, 23 Jan 2009) | 2 lines Basic generators are almost supported. ........ r5967 | zyasoft | 2009-01-23 19:49:52 -0700 (Fri, 23 Jan 2009) | 2 lines Now supports generators, via the standard PyGenerator protocol. ........ r5968 | zyasoft | 2009-01-23 21:36:06 -0700 (Fri, 23 Jan 2009) | 2 lines Added support for keyword args, *arg, and **kwarg calling conventions. ........ r5969 | zyasoft | 2009-01-24 09:47:59 -0700 (Sat, 24 Jan 2009) | 3 lines Added support for basic coroutines (no support for throw) and generator expressions. ........ r5970 | zyasoft | 2009-01-24 11:02:37 -0700 (Sat, 24 Jan 2009) | 2 lines Supports throwing exceptions into generators. ........ r5980 | zyasoft | 2009-01-25 18:19:12 -0700 (Sun, 25 Jan 2009) | 4 lines Properly handles *args, **kwargs for the receiver function. func_code can now be set to a PyBytecode (or other extender of PyBaseCode). Turned off debugging by default for PyBytecode objects. ........ r5984 | zyasoft | 2009-01-25 22:02:21 -0700 (Sun, 25 Jan 2009) | 2 lines Added extract tool (to be run by CPython) for creating test modules. ........ r5987 | zyasoft | 2009-01-26 18:45:48 -0700 (Mon, 26 Jan 2009) | 2 lines Fixed raising of exceptions and PyStack.rot ........ r5988 | zyasoft | 2009-01-26 19:16:49 -0700 (Mon, 26 Jan 2009) | 2 lines Fixed opcode support for LOAD/STORE/DELETE_NAME and BUILD_CLASS. ........ r5989 | zyasoft | 2009-01-26 19:26:20 -0700 (Mon, 26 Jan 2009) | 2 lines Fixed RAISE_VARARGS opcode to raise exceptions properly. ........ r5998 | zyasoft | 2009-01-28 20:44:58 -0700 (Wed, 28 Jan 2009) | 3 lines extract.py now creates modules where codeobjects respect their dependency relationships (via a tsort). ........ r5999 | zyasoft | 2009-01-29 07:24:33 -0700 (Thu, 29 Jan 2009) | 6 lines Extract script now processes most/all of the test.test_* modules with a test_main and importable by CPython. May want to consider generalizing support for instance methods and closure setup with respect to new.function (= PyFunction), but this seems purely a testing nicety for top-level defs. ........ r6004 | zyasoft | 2009-02-01 01:12:13 -0700 (Sun, 01 Feb 2009) | 3 lines From http://svn.python.org/projects/python/branches/release25-maint/Lib/test/test_marshal@27825 ........ r6005 | zyasoft | 2009-02-01 01:24:06 -0700 (Sun, 01 Feb 2009) | 13 lines Added _marshal, a Java implementation of the Python module, and made current. test_marshal now passes (with minor modifications around GC and floating point accuracy on to/from string representation), except for support of code objects. That's next. Pulled IOFile (now PyIOFile) from cPickle to share its duck typing with _marshal A historical note: from what I can discern in the log, the original marshal.py was written by Guido van Rossum specifically for "JPython" in 1997, one of the few bits he apparently did. Now it survives only as a shell, to import in _marshal. ........ r6015 | zyasoft | 2009-02-05 20:50:10 -0700 (Thu, 05 Feb 2009) | 5 lines Changed read/write binary_float so that it doesn't go through struct but instead j.l.Double's support for converting to/from bits. This also resolves endian ordering issues so that it follows the native endian ordering. ........ r6016 | zyasoft | 2009-02-06 00:07:45 -0700 (Fri, 06 Feb 2009) | 7 lines 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. ........ r6017 | zyasoft | 2009-02-06 00:42:15 -0700 (Fri, 06 Feb 2009) | 2 lines Fixed slice, exec support. ........ r6018 | zyasoft | 2009-02-06 21:04:54 -0700 (Fri, 06 Feb 2009) | 2 lines Fixed stack ops (DUP_TOPX, ROT_THREE, ROT_FOUR), INPLACE_POWER. ........ r6019 | zyasoft | 2009-02-06 23:29:37 -0700 (Fri, 06 Feb 2009) | 5 lines Fixed IMPORT_STAR for nested modules. sys.exc_info now returns Py.None for the traceback (this fixes an issue in PBC where null is being used as both a sentinel value by __getitem__ AND an actual value for exception handling). ........ r6020 | zyasoft | 2009-02-07 10:47:19 -0700 (Sat, 07 Feb 2009) | 2 lines Fixes nested scope treatment of free variables. ........ r6021 | zyasoft | 2009-02-07 11:14:33 -0700 (Sat, 07 Feb 2009) | 2 lines Further refinement of nested scopes. ........ r6022 | zyasoft | 2009-02-07 14:26:31 -0700 (Sat, 07 Feb 2009) | 3 lines Fixed control flow by pushing the wrapped Why onto the stack (instead of its string representation from the earliest version of this code). ........ r6024 | zyasoft | 2009-02-10 20:11:10 -0700 (Tue, 10 Feb 2009) | 7 lines Support tostring/fromstring of unsigned types so that they can be stored in the same size as their signed representation. This fixes an issues with peak.util.assembler where it expects that unsigned bytes (typecode 'B') take one byte to store and therefore are directly interoperable with co_code. ........ r6025 | zyasoft | 2009-02-11 21:38:04 -0700 (Wed, 11 Feb 2009) | 2 lines Beginning of PBC compilation support. Pretty much a shell for now. ........ r6026 | zyasoft | 2009-02-14 02:23:58 -0700 (Sat, 14 Feb 2009) | 7 lines Now interns names in co_names, etc., when unmarshalling, which fixes equality tests on special case like __class__, __dict__, etc. Fix instanceof test in eval and exec so that it looks at PyBaseCode, not PyTableCode. Also, exec cannot be called with Py.None instead of null. Fixes test_scope. ........ r6027 | zyasoft | 2009-02-14 03:44:17 -0700 (Sat, 14 Feb 2009) | 3 lines Set the traceback when intercepting an exception. Set __doc__ strings for functions from the constant pool (if available). ........ r6028 | zyasoft | 2009-02-14 04:17:17 -0700 (Sat, 14 Feb 2009) | 3 lines LOAD_DEREF needs to do the same lookup as LOAD_CLOSURE. Populate doc strings for MAKE_CLOSURE. ........ r6029 | zyasoft | 2009-02-14 04:58:48 -0700 (Sat, 14 Feb 2009) | 3 lines Fixed WITH_CLEANUP so that it properly calls __exit__ with the exception components off the stack. ........ r6030 | zyasoft | 2009-02-14 12:17:04 -0700 (Sat, 14 Feb 2009) | 3 lines Fixed UNPACK_SEQUENCE so that ValueError is raised if too few/too many values to unpack. ........ r6038 | zyasoft | 2009-02-17 17:06:55 -0700 (Tue, 17 Feb 2009) | 8 lines Added support for code.getlineno (using co_lnotab), co_code, co_consts, co_lnotab. co_code and co_lnotab now use byte[] for a more compact representation and then manage accessing as if it were an unsigned representation. This enables a good chunk of the peak.util.assembler unit tests to pass; the remainder seem to have a dictionary traversal dependency. ........ r6041 | zyasoft | 2009-02-22 07:40:49 -0700 (Sun, 22 Feb 2009) | 4 lines Inlined PyTableCode#interpret into #call(PyFrame,PyClosure) (which restores this method to what it was before the PBC-VM work). This change restores the performance as tested by test/pystone.py. ........ r6042 | zyasoft | 2009-02-23 21:25:13 -0700 (Mon, 23 Feb 2009) | 8 lines LineCache implementation to support efficient setline edge detection, as necessary for tracing. No longer looks up the ThreadState twice, in the caller of PyBytecode#interpret and interpret itself, so this should provide for some optimization. ........ Modified Paths: -------------- trunk/jython/Lib/marshal.py trunk/jython/Lib/new.py trunk/jython/Lib/test/test_array.py trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyArray.java trunk/jython/src/org/python/core/PyFrame.java trunk/jython/src/org/python/core/PyFunction.java trunk/jython/src/org/python/core/PySystemState.java trunk/jython/src/org/python/core/PyTableCode.java trunk/jython/src/org/python/core/__builtin__.java trunk/jython/src/org/python/core/imp.java trunk/jython/src/org/python/modules/Setup.java trunk/jython/src/org/python/modules/cPickle.java trunk/jython/src/org/python/modules/struct.java Added Paths: ----------- trunk/jython/Lib/pycimport.py trunk/jython/Lib/test/test_marshal.py trunk/jython/Tools/pbcvm/ trunk/jython/Tools/pbcvm/extract.py trunk/jython/src/org/python/compiler/pbc/ trunk/jython/src/org/python/compiler/pbc/Bytecode.java trunk/jython/src/org/python/compiler/pbc/BytecodeCompiler.java trunk/jython/src/org/python/core/Opcode.java trunk/jython/src/org/python/core/PyBaseCode.java trunk/jython/src/org/python/core/PyBytecode.java trunk/jython/src/org/python/modules/PyIOFile.java trunk/jython/src/org/python/modules/PyIOFileFactory.java trunk/jython/src/org/python/modules/_marshal.java Removed Paths: ------------- trunk/jython/Tools/pbcvm/extract.py trunk/jython/src/org/python/compiler/pbc/Bytecode.java trunk/jython/src/org/python/compiler/pbc/BytecodeCompiler.java Property Changed: ---------------- trunk/jython/ Property changes on: trunk/jython ___________________________________________________________________ Modified: svnmerge-integrated - /branches/pbcvm:1-5948 + /branches/pbcvm:1-6045 Modified: trunk/jython/Lib/marshal.py =================================================================== --- trunk/jython/Lib/marshal.py 2009-02-24 05:04:05 UTC (rev 6045) +++ trunk/jython/Lib/marshal.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -5,332 +5,24 @@ """ -import StringIO -import string -from types import * -try: - import new -except ImportError: - new = None +import cStringIO +from _marshal import Marshaller, Unmarshaller -TYPE_NULL = '0' -TYPE_NONE = 'N' -TYPE_FALSE = 'F' -TYPE_TRUE = 'T' -TYPE_ELLIPSIS = '.' -TYPE_INT = 'i' -TYPE_INT64 = 'I' -TYPE_FLOAT = 'f' -TYPE_COMPLEX = 'x' -TYPE_LONG = 'l' -TYPE_STRING = 's' -TYPE_TUPLE = '(' -TYPE_LIST = '[' -TYPE_DICT = '{' -TYPE_CODE = 'c' -TYPE_UNKNOWN = '?' +def dump(x, f, version=2): + Marshaller(f, version).dump(x) +# XXX - added just for debugging. remove! +def load(f, debug=False): + u = Unmarshaller(f) + if debug: + u._debug() + return u.load() -class Marshaller: - - dispatch = {} - - def __init__(self, f): - self.f = f - - def dump(self, x): - self.dispatch[type(x)](self, x) - - def w_long64(self, x): - self.w_long(x) - self.w_long(x>>32) - - def w_long(self, x): - write = self.f.write - write(chr((x) & 0xff)) - write(chr((x>> 8) & 0xff)) - write(chr((x>>16) & 0xff)) - write(chr((x>>24) & 0xff)) - - def w_short(self, x): - write = self.f.write - write(chr((x) & 0xff)) - write(chr((x>> 8) & 0xff)) - - def dump_none(self, x): - self.f.write(TYPE_NONE) - dispatch[NoneType] = dump_none - - def dump_bool(self, x): - if x: - self.f.write(TYPE_TRUE) - else: - self.f.write(TYPE_FALSE) - dispatch[BooleanType] = dump_bool - - def dump_ellipsis(self, x): - self.f.write(TYPE_ELLIPSIS) - try: - dispatch[EllipsisType] = dump_ellipsis - except NameError: - pass - - def dump_int(self, x): - y = x>>31 - if y and y != -1: - self.f.write(TYPE_INT64) - self.w_long64(x) - else: - self.f.write(TYPE_INT) - self.w_long(x) - dispatch[IntType] = dump_int - - def dump_long(self, x): - self.f.write(TYPE_LONG) - sign = 1 - if x < 0: - sign = -1 - x = -x - digits = [] - while x: - digits.append(x & 0x7FFF) - x = x>>15 - self.w_long(len(digits) * sign) - for d in digits: - self.w_short(d) - dispatch[LongType] = dump_long - - def dump_float(self, x): - write = self.f.write - write(TYPE_FLOAT) - s = `x` - write(chr(len(s))) - write(s) - dispatch[FloatType] = dump_float - - def dump_complex(self, x): - write = self.f.write - write(TYPE_COMPLEX) - s = `x.real` - write(chr(len(s))) - write(s) - s = `x.imag` - write(chr(len(s))) - write(s) - try: - dispatch[ComplexType] = dump_complex - except NameError: - pass - - def dump_string(self, x): - self.f.write(TYPE_STRING) - self.w_long(len(x)) - self.f.write(x) - dispatch[StringType] = dump_string - - def dump_tuple(self, x): - self.f.write(TYPE_TUPLE) - self.w_long(len(x)) - for item in x: - self.dump(item) - dispatch[TupleType] = dump_tuple - - def dump_list(self, x): - self.f.write(TYPE_LIST) - self.w_long(len(x)) - for item in x: - self.dump(item) - dispatch[ListType] = dump_list - - def dump_dict(self, x): - self.f.write(TYPE_DICT) - for key, value in x.items(): - self.dump(key) - self.dump(value) - self.f.write(TYPE_NULL) - dispatch[DictionaryType] = dump_dict - - def dump_code(self, x): - self.f.write(TYPE_CODE) - self.w_short(x.co_argcount) - self.w_short(x.co_nlocals) - self.w_short(x.co_stacksize) - self.w_short(x.co_flags) - self.dump(x.co_code) - self.dump(x.co_consts) - self.dump(x.co_names) - self.dump(x.co_varnames) - self.dump(x.co_filename) - self.dump(x.co_name) - self.w_short(x.co_firstlineno) - self.dump(x.co_lnotab) - try: - dispatch[CodeType] = dump_code - except NameError: - pass - - -class NULL: - pass - -class Unmarshaller: - - dispatch = {} - - def __init__(self, f): - self.f = f - - def load(self): - c = self.f.read(1) - if not c: - raise EOFError - return self.dispatch[c](self) - - def r_short(self): - read = self.f.read - lo = ord(read(1)) - hi = ord(read(1)) - x = lo | (hi<<8) - if x & 0x8000: - x = x - 0x10000 - return x - - def r_long(self): - read = self.f.read - a = ord(read(1)) - b = ord(read(1)) - c = ord(read(1)) - d = ord(read(1)) - # convert unsigned d to signed to avoid d << 24 possibly - # overflowing into a long value - if d > 127: - d -= 256 - x = a | (b<<8) | (c<<16) | (d<<24) - if x & 0x80000000 and x > 0: - x = string.atoi(x - 0x100000000L) - return x - - def r_long64(self): - a = self.r_long() - b = self.r_long() - return a | (b<<32) - - def load_null(self): - return NULL - dispatch[TYPE_NULL] = load_null - - def load_none(self): - return None - dispatch[TYPE_NONE] = load_none - - def load_False(self): - return False - dispatch[TYPE_FALSE] = load_False - - def load_True(self): - return True - dispatch[TYPE_TRUE] = load_True - - def load_ellipsis(self): - return EllipsisType - dispatch[TYPE_ELLIPSIS] = load_ellipsis - - def load_int(self): - return self.r_long() - dispatch[TYPE_INT] = load_int - - def load_int64(self): - return self.r_long64() - dispatch[TYPE_INT64] = load_int64 - - def load_long(self): - size = self.r_long() - sign = 1 - if size < 0: - sign = -1 - size = -size - x = 0L - for i in range(size): - d = self.r_short() - x = x | (d<<(i*15L)) - return x * sign - dispatch[TYPE_LONG] = load_long - - def load_float(self): - n = ord(self.f.read(1)) - s = self.f.read(n) - return string.atof(s) - dispatch[TYPE_FLOAT] = load_float - - def load_complex(self): - n = ord(self.f.read(1)) - s = self.f.read(n) - real = float(s) - n = ord(self.f.read(1)) - s = self.f.read(n) - imag = float(s) - return complex(real, imag) - dispatch[TYPE_COMPLEX] = load_complex - - def load_string(self): - n = self.r_long() - return self.f.read(n) - dispatch[TYPE_STRING] = load_string - - def load_tuple(self): - return tuple(self.load_list()) - dispatch[TYPE_TUPLE] = load_tuple - - def load_list(self): - n = self.r_long() - list = [] - for i in range(n): - list.append(self.load()) - return list - dispatch[TYPE_LIST] = load_list - - def load_dict(self): - d = {} - while 1: - key = self.load() - if key is NULL: - break - value = self.load() - d[key] = value - return d - dispatch[TYPE_DICT] = load_dict - - def load_code(self): - argcount = self.r_short() - nlocals = self.r_short() - stacksize = self.r_short() - flags = self.r_short() - code = self.load() - consts = self.load() - names = self.load() - varnames = self.load() - filename = self.load() - name = self.load() - firstlineno = self.r_short() - lnotab = self.load() - if not new: - raise RuntimeError, "can't unmarshal code objects; no 'new' module" - return new.code(argcount, nlocals, stacksize, flags, code, consts, - names, varnames, filename, name, firstlineno, lnotab) - dispatch[TYPE_CODE] = load_code - - -def dump(x, f): - Marshaller(f).dump(x) - -def load(f): - return Unmarshaller(f).load() - -def dumps(x): - f = StringIO.StringIO() - dump(x, f) +def dumps(x, version=2): + f = cStringIO.StringIO() + dump(x, f, version) return f.getvalue() def loads(s): - f = StringIO.StringIO(s) + f = cStringIO.StringIO(s) return load(f) Modified: trunk/jython/Lib/new.py =================================================================== --- trunk/jython/Lib/new.py 2009-02-24 05:04:05 UTC (rev 6045) +++ trunk/jython/Lib/new.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -20,3 +20,5 @@ # from types import CodeType as code #except ImportError: # pass + +from org.python.core import PyBytecode as code Copied: trunk/jython/Lib/pycimport.py (from rev 6042, branches/pbcvm/Lib/pycimport.py) =================================================================== --- trunk/jython/Lib/pycimport.py (rev 0) +++ trunk/jython/Lib/pycimport.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -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: trunk/jython/Lib/test/test_array.py =================================================================== --- trunk/jython/Lib/test/test_array.py 2009-02-24 05:04:05 UTC (rev 6045) +++ trunk/jython/Lib/test/test_array.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -213,7 +213,7 @@ self.assertRaises(TypeError, b.fromstring, 42) b.fromstring(a.tostring()) self.assertEqual(a, b) - if a.itemsize>1: + if a.itemsize>1 and self.typecode not in ('b', 'B'): self.assertRaises(ValueError, b.fromstring, "x") def test_repr(self): Copied: trunk/jython/Lib/test/test_marshal.py (from rev 6042, branches/pbcvm/Lib/test/test_marshal.py) =================================================================== --- trunk/jython/Lib/test/test_marshal.py (rev 0) +++ trunk/jython/Lib/test/test_marshal.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -0,0 +1,264 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-1 -*- + +from __future__ import with_statement +from test import test_support +import marshal +import sys +import unittest +import os + +# the original had incorrect semantics for non-refcounting GCs: +# marshal.dump(expected, file(test_support.TESTFN, "wb")) +# got = marshal.load(file(test_support.TESTFN, "rb")) + +def roundtrip(item): + with open(test_support.TESTFN, "wb") as test_file: + marshal.dump(item, test_file) + with open(test_support.TESTFN, "rb") as test_file: + got = marshal.load(file(test_support.TESTFN, "rb")) #, debug=True) + return got + + +class IntTestCase(unittest.TestCase): + def test_ints(self): + # Test the full range of Python ints. + n = sys.maxint + while n: + for expected in (-n, n): + s = marshal.dumps(expected) + got = marshal.loads(s) + self.assertEqual(expected, got) + self.assertEqual(expected, roundtrip(expected)) + n = n >> 1 + os.unlink(test_support.TESTFN) + + def test_int64(self): + # Simulate int marshaling on a 64-bit box. This is most interesting if + # we're running the test on a 32-bit box, of course. + + def to_little_endian_string(value, nbytes): + bytes = [] + for i in range(nbytes): + bytes.append(chr(value & 0xff)) + value >>= 8 + return ''.join(bytes) + + maxint64 = (1L << 63) - 1 + minint64 = -maxint64-1 + + for base in maxint64, minint64, -maxint64, -(minint64 >> 1): + while base: + s = 'I' + to_little_endian_string(base, 8) + got = marshal.loads(s) + self.assertEqual(base, got) + if base == -1: # a fixed-point for shifting right 1 + base = 0 + else: + base >>= 1 + + def test_bool(self): + for b in (True, False): + new = marshal.loads(marshal.dumps(b)) + self.assertEqual(b, new) + self.assertEqual(type(b), type(new)) + new = roundtrip(b) + self.assertEqual(b, new) + self.assertEqual(type(b), type(new)) + +class FloatTestCase(unittest.TestCase): + def test_floats(self): + # Test a few floats + small = 1e-25 + n = sys.maxint * 3.7e250 + while n > small: + for expected in (-n, n): + f = float(expected) + s = marshal.dumps(f) + got = marshal.loads(s) + self.assertEqual(f, got) + got = roundtrip(f) + self.assertEqual(f, got) + n /= 123.4567 + + f = 0.0 + s = marshal.dumps(f, 2) + got = marshal.loads(s) + self.assertEqual(f, got) + # and with version <= 1 (floats marshalled differently then) + s = marshal.dumps(f, 1) + got = marshal.loads(s) + self.assertEqual(f, got) + + n = sys.maxint * 3.7e-250 + while n < small: + for expected in (-n, n): + f = float(expected) + + s = marshal.dumps(f) + got = marshal.loads(s) + self.assertEqual(f, got) + + s = marshal.dumps(f, 1) + got = marshal.loads(s) + if test_support.is_jython: + self.assertAlmostEqual(f, got) + else: + self.assertEqual(f, got) + + got = roundtrip(f) + self.assertEqual(f, got) + + # XXX - not certain what this extra arg to dump is! + #marshal.dump(f, file(test_support.TESTFN, "wb"), 1) + #got = marshal.load(file(test_support.TESTFN, "rb")) + #self.assertEqual(f, got) + n *= 123.4567 + os.unlink(test_support.TESTFN) + +class StringTestCase(unittest.TestCase): + def test_unicode(self): + for s in [u"", u"Andr\xE8 Previn", u"abc", u" "*10000]: + new = marshal.loads(marshal.dumps(s)) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + new = roundtrip(s) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + os.unlink(test_support.TESTFN) + + def test_string(self): + for s in ["", "Andr\xE8 Previn", "abc", " "*10000]: + new = marshal.loads(marshal.dumps(s)) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + new = roundtrip(s) + self.assertEqual(s, new) + self.assertEqual(type(s), type(new)) + os.unlink(test_support.TESTFN) + + if not test_support.is_jython: + def test_buffer(self): + for s in ["", "Andr\xE8 Previn", "abc", " "*10000]: + b = buffer(s) + new = marshal.loads(marshal.dumps(b)) + self.assertEqual(s, new) + new = roundtrip(b) + self.assertEqual(s, new) + os.unlink(test_support.TESTFN) + +class ExceptionTestCase(unittest.TestCase): + def test_exceptions(self): + new = marshal.loads(marshal.dumps(StopIteration)) + self.assertEqual(StopIteration, new) + +class CodeTestCase(unittest.TestCase): + if not test_support.is_jython: # XXX - need to use the PBC compilation backend, which doesn't exist yet + def test_code(self): + co = ExceptionTestCase.test_exceptions.func_code + new = marshal.loads(marshal.dumps(co)) + self.assertEqual(co, new) + +class ContainerTestCase(unittest.TestCase): + d = {'astring': 'fo...@ba...z.spam', + 'afloat': 7283.43, + 'anint': 2**20, + 'ashortlong': 2L, + 'alist': ['.zyx.41'], + 'atuple': ('.zyx.41',)*10, + 'aboolean': False, + 'aunicode': u"Andr\xE8 Previn" + } + def test_dict(self): + new = marshal.loads(marshal.dumps(self.d)) + self.assertEqual(self.d, new) + new = roundtrip(self.d) + self.assertEqual(self.d, new) + os.unlink(test_support.TESTFN) + + def test_list(self): + lst = self.d.items() + new = marshal.loads(marshal.dumps(lst)) + self.assertEqual(lst, new) + new = roundtrip(lst) + self.assertEqual(lst, new) + os.unlink(test_support.TESTFN) + + def test_tuple(self): + t = tuple(self.d.keys()) + new = marshal.loads(marshal.dumps(t)) + self.assertEqual(t, new) + new = roundtrip(t) + self.assertEqual(t, new) + os.unlink(test_support.TESTFN) + + def test_sets(self): + for constructor in (set, frozenset): + t = constructor(self.d.keys()) + new = marshal.loads(marshal.dumps(t)) + self.assertEqual(t, new) + self.assert_(isinstance(new, constructor)) + self.assertNotEqual(id(t), id(new)) + new = roundtrip(t) + self.assertEqual(t, new) + os.unlink(test_support.TESTFN) + +class BugsTestCase(unittest.TestCase): + def test_bug_5888452(self): + # Simple-minded check for SF 588452: Debug build crashes + marshal.dumps([128] * 1000) + + def test_patch_873224(self): + self.assertRaises(Exception, marshal.loads, '0') + self.assertRaises(Exception, marshal.loads, 'f') + self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1]) + + def test_version_argument(self): + # Python 2.4.0 crashes for any call to marshal.dumps(x, y) + self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5) + self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5) + + def test_fuzz(self): + # simple test that it's at least not *totally* trivial to + # crash from bad marshal data + for c in [chr(i) for i in range(256)]: + try: + marshal.loads(c) + except Exception: + pass + + def test_loads_recursion(self): + s = 'c' + ('X' * 4*4) + '{' * 2**20 + self.assertRaises(ValueError, marshal.loads, s) + + def test_recursion_limit(self): + # Create a deeply nested structure. + head = last = [] + # The max stack depth should match the value in Python/marshal.c. + MAX_MARSHAL_STACK_DEPTH = 2000 + for i in range(MAX_MARSHAL_STACK_DEPTH - 2): + last.append([0]) + last = last[-1] + + # Verify we don't blow out the stack with dumps/load. + data = marshal.dumps(head) + new_head = marshal.loads(data) + # Don't use == to compare objects, it can exceed the recursion limit. + self.assertEqual(len(new_head), len(head)) + self.assertEqual(len(new_head[0]), len(head[0])) + self.assertEqual(len(new_head[-1]), len(head[-1])) + + last.append([0]) + self.assertRaises(ValueError, marshal.dumps, head) + +def test_main(): + test_support.run_unittest(IntTestCase, + FloatTestCase, + StringTestCase, + CodeTestCase, + ContainerTestCase, + ExceptionTestCase, + BugsTestCase) + +if __name__ == "__main__": + test_main() Deleted: trunk/jython/Tools/pbcvm/extract.py =================================================================== --- branches/pbcvm/Tools/pbcvm/extract.py 2009-02-24 04:25:13 UTC (rev 6042) +++ trunk/jython/Tools/pbcvm/extract.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -1,141 +0,0 @@ -"""Given a module, generates a new module where all functions that are - not builtin (including within classes) have their func_code pointed - to a PyBytecode constructor. This enables CPython to generate for - Jython the desired PBC. -""" - -from __future__ import with_statement -import inspect -import networkx # add dependency to a setup script? probably overkill -import sys -from collections import defaultdict - -attrs = ["co_" + x for x in """ - argcount nlocals stacksize flags - code consts names varnames - filename name firstlineno lnotab - freevars cellvars -""".split()] # add - -ROOT = object() - -class Extract(object): - - def __init__(self, mod, writer): - self.mod = mod - self.writer = writer - self.codeobjs = {} - self.codeobjs_names = {} - self.counters = defaultdict(int) - self.depend = networkx.DiGraph() - - def extract(self): - mod = self.mod - writer = self.writer - functionobjs = self.candidate_functions() - for name, f in functionobjs: - self.extract_code_obj(f) - - print >> writer, "from %s import *" % mod.__name__ - print >> writer, "from org.python.core import PyBytecode" - print >> writer - print >> writer, "_codeobjs = {}" - print >> writer - - objs = networkx.topological_sort(self.depend) - for obj in objs: - if not inspect.iscode(obj): - continue - name = self.codeobjs_names[obj] - print >> writer, "_codeobjs[%r] = %s" % (name, self.codeobjs[obj]) - print >> writer - for name, f in functionobjs: - # this may be a Jython diff, need to determine further; need to check if im_func or not on the object - print >> writer, "try: %s.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code]) - print >> writer, "except (AttributeError, ValueError): pass" # ignore setting cells, im_func, etc... %s.im_func.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code]) - - print >> writer - print >> writer, 'if __name__ == "__main__":' - print >> writer, ' test_main()' - - def candidate_functions(self): - """functions and methods we will retrieve code objects""" - - mod = self.mod - functions = inspect.getmembers(mod, inspect.isfunction) - functions = [(name, f) for (name, f) in functions if not inspect.isbuiltin(f) and name != "test_main"] - - classes = inspect.getmembers(mod, inspect.isclass) - for classname, cls in classes: - for methodname, method in inspect.getmembers(cls, inspect.ismethod): - if inspect.getmodule(method) == mod: - functions.append(("%s.%s" % (classname, methodname), method)) - return functions - - def extract_code_obj(self, f_or_code): - if inspect.iscode(f_or_code): - code = f_or_code - else: - code = f_or_code.func_code - self.extract_def(code) - - def extract_def(self, code): - if code in self.codeobjs_names: - #print >> sys.stderr, "Already seen", code - return "_codeobjs[%r]" % (self.codeobjs_names[code],) - - co_name = code.co_name - #print >> sys.stderr, "Processing", code - name = co_name + "." + str(self.counters[co_name]) - self.counters[co_name] += 1 - self.codeobjs_names[code] = name - values = [] - self.depend.add_edge(code, ROOT) - for attr in attrs: - # treat co_consts specially - maybe also need to use pickling in case repr is not suitable - if attr == 'co_consts': - co_consts = [] - for const in getattr(code, attr): - if inspect.iscode(const): - #print >> sys.stderr, "Extracting code const " + str(const) - co_consts.append(self.extract_def(const)) - self.depend.add_edge(const, code) - else: - co_consts.append(repr(const)) - values.append((attr, "["+', '.join(co_consts)+"]")) - else: - values.append((attr, repr(getattr(code, attr)))) - self.codeobjs[code] = "PyBytecode(\n" + '\n'.join([' '* 4 + v + ', # ' + attr for (attr, v) in values])+"\n )" - return "_codeobjs[%r]" % (name,) - - -# for now, just use the cwd -def import_modules(path): - import glob - import os.path - - sys.path.insert(0, path) - for name in sorted(glob.iglob("test/test_*.py")): - modname = os.path.splitext(os.path.basename(name))[0] - qualified_name = "test." + modname - print "Trying", qualified_name - try: - topmod = __import__(qualified_name) - except Exception, e: - print "Could not import", qualified_name, ":", repr(e) - continue - mod = getattr(topmod, modname) - try: - mod.test_main - except AttributeError, e: - print "No test_main in", qualified_name - continue - - output_name = "test/" + modname + "_pbc.py" - with open(output_name, "w") as f: - print "Extracting", name, "to", output_name - Extract(mod, f).extract() - -if __name__ == '__main__': - path = modname = sys.argv[1] - import_modules(path) Copied: trunk/jython/Tools/pbcvm/extract.py (from rev 6042, branches/pbcvm/Tools/pbcvm/extract.py) =================================================================== --- trunk/jython/Tools/pbcvm/extract.py (rev 0) +++ trunk/jython/Tools/pbcvm/extract.py 2009-02-24 05:45:38 UTC (rev 6046) @@ -0,0 +1,141 @@ +"""Given a module, generates a new module where all functions that are + not builtin (including within classes) have their func_code pointed + to a PyBytecode constructor. This enables CPython to generate for + Jython the desired PBC. +""" + +from __future__ import with_statement +import inspect +import networkx # add dependency to a setup script? probably overkill +import sys +from collections import defaultdict + +attrs = ["co_" + x for x in """ + argcount nlocals stacksize flags + code consts names varnames + filename name firstlineno lnotab + freevars cellvars +""".split()] # add + +ROOT = object() + +class Extract(object): + + def __init__(self, mod, writer): + self.mod = mod + self.writer = writer + self.codeobjs = {} + self.codeobjs_names = {} + self.counters = defaultdict(int) + self.depend = networkx.DiGraph() + + def extract(self): + mod = self.mod + writer = self.writer + functionobjs = self.candidate_functions() + for name, f in functionobjs: + self.extract_code_obj(f) + + print >> writer, "from %s import *" % mod.__name__ + print >> writer, "from org.python.core import PyBytecode" + print >> writer + print >> writer, "_codeobjs = {}" + print >> writer + + objs = networkx.topological_sort(self.depend) + for obj in objs: + if not inspect.iscode(obj): + continue + name = self.codeobjs_names[obj] + print >> writer, "_codeobjs[%r] = %s" % (name, self.codeobjs[obj]) + print >> writer + for name, f in functionobjs: + # this may be a Jython diff, need to determine further; need to check if im_func or not on the object + print >> writer, "try: %s.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code]) + print >> writer, "except (AttributeError, ValueError): pass" # ignore setting cells, im_func, etc... %s.im_func.func_code = _codeobjs[%r]" % (name, self.codeobjs_names[f.func_code]) + + print >> writer + print >> writer, 'if __name__ == "__main__":' + print >> writer, ' test_main()' + + def candidate_functions(self): + """functions and methods we will retrieve code objects""" + + mod = self.mod + functions = inspect.getmembers(mod, inspect.isfunction) + functions = [(name, f) for (name, f) in functions if not inspect.isbuiltin(f) and name != "test_main"] + + classes = inspect.getmembers(mod, inspect.isclass) + for classname, cls in classes: + for methodname, method in inspect.getmembers(cls, inspect.ismethod): + if inspect.getmodule(method) == mod: + functions.append(("%s.%s" % (classname, methodname), method)) + return functions + + def extract_code_obj(self, f_or_code): + if inspect.iscode(f_or_code): + code = f_or_code + else: + code = f_or_code.func_code + self.extract_def(code) + + def extract_def(self, code): + if code in self.codeobjs_names: + #print >> sys.stderr, "Already seen", code + return "_codeobjs[%r]" % (self.codeobjs_names[code],) + + co_name = code.co_name + #print >> sys.stderr, "Processing", code + name = co_name + "." + str(self.counters[co_name]) + self.counters[co_name] += 1 + self.codeobjs_names[code] = name + values = [] + self.depend.add_edge(code, ROOT) + for attr in attrs: + # treat co_consts specially - maybe also need to use pickling in case repr is not suitable + if attr == 'co_consts': + co_consts = [] + for const in getattr(code, attr): + if inspect.iscode(const): + #print >> sys.stderr, "Extracting code const " + str(const) + co_consts.append(self.extract_def(const)) + self.depend.add_edge(const, code) + else: + co_consts.append(repr(const)) + values.append((attr, "["+', '.join(co_consts)+"]")) + else: + values.append((attr, repr(getattr(code, attr)))) + self.codeobjs[code] = "PyBytecode(\n" + '\n'.join([' '* 4 + v + ', # ' + attr for (attr, v) in values])+"\n )" + return "_codeobjs[%r]" % (name,) + + +# for now, just use the cwd +def import_modules(path): + import glob + import os.path + + sys.path.insert(0, path) + for name in sorted(glob.iglob("test/test_*.py")): + modname = os.path.splitext(os.path.basename(name))[0] + qualified_name = "test." + modname + print "Trying", qualified_name + try: + topmod = __import__(qualified_name) + except Exception, e: + print "Could not import", qualified_name, ":", repr(e) + continue + mod = getattr(topmod, modname) + try: + mod.test_main + except AttributeError, e: + print "No test_main in", qualified_name + continue + + output_name = "test/" + modname + "_pbc.py" + with open(output_name, "w") as f: + print "Extracting", name, "to", output_name + Extract(mod, f).extract() + +if __name__ == '__main__': + path = modname = sys.argv[1] + import_modules(path) Deleted: trunk/jython/src/org/python/compiler/pbc/Bytecode.java =================================================================== --- branches/pbcvm/src/org/python/compiler/pbc/Bytecode.java 2009-02-24 04:25:13 UTC (rev 6042) +++ trunk/jython/src/org/python/compiler/pbc/Bytecode.java 2009-02-24 05:45:38 UTC (rev 6046) @@ -1,555 +0,0 @@ -package org.python.compiler.pbc; - -// Copyright (c) 2009 Jython Developers -// -// ported from peak.util.assembler (BytecodeAssembler): -// Copyright (C) 1996-2004 by Phillip J. Eby and Tyler C. Sarna. -// All rights reserved. This software may be used under the same terms -// as Zope or Python. (http://cvs.eby-sarna.com/*checkout*/PEAK/README.txt) -// -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.python.core.Py; -import org.python.core.Opcode; -import org.python.core.PyBaseCode; -import org.python.core.PyBytecode; -import org.python.core.PyObject; - -// must be thread confined, as might be expected -class Bytecode { - - private int co_argcount = 0; - private int co_stacksize = 0; - private int co_flags = PyBaseCode.CO_OPTIMIZED | PyBaseCode.CO_NEWLOCALS; // typical usage -// co_filename = '<generated code>' -// co_name = '<lambda>' -// co_firstlineno = 0 -// co_freevars = () -// co_cellvars = () -// _last_lineofs = 0 -// _ss = 0 -// _tmp_level = 0 -// - - public Bytecode(int co_flags) { - this.co_flags = co_flags; - co_const.put(Py.None, 0); - } -// def __init__(self): -// self.co_code = array('B') -// self.co_names = [] -// self.co_varnames = [] -// self.blocks = [] - private final List<Integer> stack_history = new ArrayList<Integer>(); - private final List<Integer> co_code = new ArrayList<Integer>(); - private final List<Integer> co_lnotab = new ArrayList<Integer>(); - private final Map<PyObject, Integer> co_const = new HashMap<PyObject, Integer>(); - - private void emit(int opcode) { - assert (opcode >= 0 && opcode <= 0xFF); // at this point we should verify we are only emitting unsigned bytes - co_code.add(opcode); - } - - private void emit(int opcode, int oparg) { - if (oparg > 0xFFFF) { - emit(Opcode.EXTENDED_ARG); - emit((oparg >> 16) & 0xFF); - emit((oparg >> 24) & 0xFF); - } - emit(opcode); - emit(oparg & 0xFF); - emit((oparg >> 8) & 0xFF); - } - - private void LOAD_CONST(PyObject constant) { - int arg; - if (co_const.containsKey(constant)) { - arg = co_const.get(constant); - } else { - arg = co_const.size() + 1; - co_const.put(constant, arg); - } - stackchange(0, 1); - emit(Opcode.LOAD_CONST, arg); - } - - private void RETURN_VALUE() { - stackchange(1, 0); - emit(Opcode.RETURN_VALUE); - stack_unknown(); - } - - public void code() { -// return new PyBytecode(); - } - - -// - -// -// def locals_written(self): -// vn = self.co_varnames -// hl = dict.fromkeys([STORE_FAST, DELETE_FAST]) -// return dict.fromkeys([vn[arg] for ofs, op, arg in self if op in hl]) -// -// -// -// - public void set_lineno(int lno) { -// if not self.co_firstlineno: -// self.co_firstlineno = self._last_line = lno -// return -// -// append = self.co_lnotab.append -// incr_line = lno - self._last_line -// incr_addr = len(self.co_code) - self._last_lineofs -// if not incr_line: -// return -// -// assert incr_addr>=0 and incr_line>=0 -// -// while incr_addr>255: -// append(255) -// append(0) -// incr_addr -= 255 -// -// while incr_line>255: -// append(incr_addr) -// append(255) -// incr_line -= 255 -// incr_addr = 0 -// -// if incr_addr or incr_line: -// append(incr_addr) -// append(incr_line) -// -// self._last_line = lno -// self._last_lineofs = len(self.co_code) - } - // - - public void YIELD_VALUE() { - stackchange(1, 1); - co_flags |= PyBaseCode.CO_GENERATOR; - emit(Opcode.YIELD_VALUE); - } - - // defaults? def CALL_FUNCTION(self, argc=0, kwargc=0, op=CALL_FUNCTION, extra=0): - public void CALL_FUNCTION(int argc, int kwargc, int op, int extra) { - stackchange(1 + argc + 2 * kwargc + extra, 1); - emit(op); - emit(argc); - emit(kwargc); - } -// -// def CALL_FUNCTION_VAR(self, argc=0, kwargc=0): -// self.CALL_FUNCTION(argc,kwargc,CALL_FUNCTION_VAR, 1) # 1 for *args -// -// def CALL_FUNCTION_KW(self, argc=0, kwargc=0): -// self.CALL_FUNCTION(argc,kwargc,CALL_FUNCTION_KW, 1) # 1 for **kw -// -// def CALL_FUNCTION_VAR_KW(self, argc=0, kwargc=0): -// self.CALL_FUNCTION(argc,kwargc,CALL_FUNCTION_VAR_KW, 2) # 2 *args,**kw -// - - public void BUILD_TUPLE(int count) { - stackchange(count, 1); - emit(Opcode.BUILD_TUPLE, count); - } - - public void BUILD_LIST(int count) { - stackchange(count, 1); - emit(Opcode.BUILD_LIST, count); - } - - public void UNPACK_SEQUENCE(int count) { - stackchange(1, count); - emit(Opcode.UNPACK_SEQUENCE, count); - } - - public void BUILD_SLICE(int count) { - if (count != 2 && count != 3) { - throw Py.AssertionError("Invalid number of arguments for BUILD_SLICE"); - } - stackchange(count, 1); - emit(Opcode.BUILD_SLICE, count); - } - - public void DUP_TOPX(int count) { - stackchange(count, count * 2); - emit(Opcode.DUP_TOPX, count); - } - - public void RAISE_VARARGS(int argc) { - if (0 <= argc && argc <= 3) { - throw Py.AssertionError("Invalid number of arguments for RAISE_VARARGS"); - } - stackchange(argc, 0); - emit(Opcode.RAISE_VARARGS, argc); - } - - public void MAKE_FUNCTION(int ndefaults) { - stackchange(1 + ndefaults, 1); - emit(Opcode.MAKE_FUNCTION, ndefaults); - } - - public void MAKE_CLOSURE(int ndefaults, int freevars) { - freevars = 1; - stackchange(1 + freevars + ndefaults, 1); - emit(Opcode.MAKE_CLOSURE, ndefaults); - } - - public int here() { - return co_code.size(); - } - private int _ss = -1; - - public void set_stack_size(int size) { - if (size < 0) { - throw Py.AssertionError("Stack underflow"); - } - if (size > co_stacksize) { - co_stacksize = size; - } - int bytes = co_code.size() - stack_history.size() + 1; - if (bytes > 0) { - for (int i = 0; i < bytes; i++) { - stack_history.add(_ss); - } - } - _ss = size; - } - - public int get_stack_size() { - return _ss; - } - - public void stackchange(int inputs, int outputs) { - if (_ss == -1) { - throw Py.AssertionError("Unknown stack size at this location"); - } - set_stack_size(get_stack_size() - inputs); // check underflow - set_stack_size(get_stack_size() + outputs); // update maximum height - - } - - public void stack_unknown() { - _ss = -1; - } - - public void branch_stack(int location, int expected) { - if (location >= stack_history.size()) { - if (location > co_code.size()) { - throw Py.AssertionError(String.format( - "Forward-looking stack prediction! %d, %d", location, co_code.size())); - } - int actual = get_stack_size(); - if (actual == -1) { - actual = expected; - set_stack_size(actual); - stack_history.set(location, actual); - } else { - actual = stack_history.get(location); - if (actual == -1) { - actual = expected; - stack_history.set(location, actual); - } - if (actual != expected) { - throw Py.AssertionError(String.format( - "Stack level mismatch: actual=%d expected=%d", actual, expected)); - } - } - } - } - -// def jump(self, op, arg=None): -// def jump_target(offset): -// target = offset -// if op not in hasjabs: -// target = target - (posn+3) -// assert target>=0, "Relative jumps can't go backwards" -// if target>0xFFFF: -// target = offset - (posn+6) -// return target -// -// def backpatch(offset): -// target = jump_target(offset) -// if target>0xFFFF: -// raise AssertionError("Forward jump span must be <64K bytes") -// self.patch_arg(posn, 0, target) -// self.branch_stack(offset, old_level) -// -// if op==FOR_ITER: -// old_level = self.stack_size = self.stack_size - 1 -// self.stack_size += 2 -// else: -// old_level = self.stack_size -// posn = self.here() -// -// if arg is not None: -// self.emit_arg(op, jump_target(arg)) -// self.branch_stack(arg, old_level) -// lbl = None -// else: -// self.emit_arg(op, 0) -// def lbl(code=None): -// backpatch(self.here()) -// if op in (JUMP_FORWARD, JUMP_ABSOLUTE, CONTINUE_LOOP): -// self.stack_unknown() -// return lbl -// -// def COMPARE_OP(self, op): -// self.stackchange((2,1)) -// self.emit_arg(COMPARE_OP, compares[op]) -// -// -// def setup_block(self, op): -// jmp = self.jump(op) -// self.blocks.append((op,self.stack_size,jmp)) -// return jmp -// -// def SETUP_EXCEPT(self): -// ss = self.stack_size -// self.stack_size = ss+3 # simulate the level at "except:" time -// self.setup_block(SETUP_EXCEPT) -// self.stack_size = ss # restore the current level -// -// def SETUP_FINALLY(self): -// ss = self.stack_size -// self.stack_size = ss+3 # allow for exceptions -// self.stack_size = ss+1 # simulate the level after the None is pushed -// self.setup_block(SETUP_FINALLY) -// self.stack_size = ss # restore original level -// -// def SETUP_LOOP(self): -// self.setup_block(SETUP_LOOP) -// -// def POP_BLOCK(self): -// if not self.blocks: -// raise AssertionError("Not currently in a block") -// -// why, level, fwd = self.blocks.pop() -// self.emit(POP_BLOCK) -// -// if why!=SETUP_LOOP: -// if why==SETUP_FINALLY: -// self.LOAD_CONST(None) -// fwd() -// else: -// self.stack_size = level-3 # stack level resets here -// else_ = self.JUMP_FORWARD() -// fwd() -// return else_ -// else: -// return fwd -// -// -// def assert_loop(self): -// for why,level,fwd in self.blocks: -// if why==SETUP_LOOP: -// return -// raise AssertionError("Not inside a loop") -// -// def BREAK_LOOP(self): -// self.assert_loop(); self.emit(BREAK_LOOP) -// self.stack_unknown() -// -// def CONTINUE_LOOP(self, label): -// self.assert_loop() -// if self.blocks[-1][0]==SETUP_LOOP: -// op = JUMP_ABSOLUTE # more efficient if not in a nested block -// else: -// op = CONTINUE_LOOP -// return self.jump(op, label) -// -// def __call__(self, *args): -// last = None -// for ob in args: -// if callable(ob): -// last = ob(self) -// else: -// try: -// f = generate_types[type(ob)] -// except KeyError: -// raise TypeError("Can't generate", ob) -// else: -// last = f(self, ob) -// return last -// -// def return_(self, ob=None): -// return self(ob, Code.RETURN_VALUE) -// -// decorate(classmethod) -// def from_function(cls, function, copy_lineno=False): -// code = cls.from_code(function.func_code, copy_lineno) -// return code -// -// -// decorate(classmethod) -// def from_code(cls, code, copy_lineno=False): -// import inspect -// self = cls.from_spec(code.co_name, *inspect.getargs(code)) -// if copy_lineno: -// self.set_lineno(code.co_firstlineno) -// self.co_filename = code.co_filename -// self.co_freevars = code.co_freevars # XXX untested! -// return self -// -// decorate(classmethod) -// def from_spec(cls, name='<lambda>', args=(), var=None, kw=None): -// self = cls() -// self.co_name = name -// self.co_argcount = len(args) -// self.co_varnames.extend(args) -// if var: -// self.co_varnames.append(var) -// self.co_flags |= CO_VARARGS -// if kw: -// self.co_varnames.append(kw) -// self.co_flags |= CO_VARKEYWORDS -// -// def tuple_arg(args): -// self.UNPACK_SEQUENCE(len(args)) -// for arg in args: -// if not isinstance(arg, basestring): -// tuple_arg(arg) -// else: -// self.STORE_FAST(arg) -// -// for narg, arg in enumerate(args): -// if not isinstance(arg, basestring): -// dummy_name = '.'+str(narg) -// self.co_varnames[narg] = dummy_name -// self.LOAD_FAST(dummy_name) -// tuple_arg(arg) -// -// return self -// -// -// def patch_arg(self, offset, oldarg, newarg): -// code = self.co_code -// if (oldarg>0xFFFF) != (newarg>0xFFFF): -// raise AssertionError("Can't change argument size", oldarg, newarg) -// code[offset+1] = newarg & 255 -// code[offset+2] = (newarg>>8) & 255 -// if newarg>0xFFFF: -// newarg >>=16 -// code[offset-2] = newarg & 255 -// code[offset-1] = (newarg>>8) & 255 -// -// def nested(self, name='<lambda>', args=(), var=None, kw=None, cls=None): -// if cls is None: -// cls = Code -// code = cls.from_spec(name, args, var, kw) -// code.co_filename=self.co_filename -// return code -// -// def __iter__(self): -// i = 0 -// extended_arg = 0 -// code = self.co_code -// n = len(code) -// while i < n: -// op = code[i] -// if op >= HAVE_ARGUMENT: -// oparg = code[i+1] + code[i+2]*256 + extended_arg -// extended_arg = 0 -// if op == EXTENDED_ARG: -// extended_arg = oparg*65536 -// i+=3 -// continue -// yield i, op, oparg -// i += 3 -// else: -// yield i, op, None -// i += 1 -// -// -// -// -// def makefree(self, names): -// nowfree = dict.fromkeys(self.co_freevars) -// newfree = [n for n in names if n not in nowfree] -// if newfree: -// self.co_freevars += tuple(newfree) -// self._locals_to_cells() -// -// def makecells(self, names): -// nowcells = dict.fromkeys(self.co_cellvars+self.co_freevars) -// newcells = [n for n in names if n not in nowcells] -// if new... [truncated message content] |
From: <zy...@us...> - 2009-02-24 05:04:08
|
Revision: 6045 http://jython.svn.sourceforge.net/jython/?rev=6045&view=rev Author: zyasoft Date: 2009-02-24 05:04:05 +0000 (Tue, 24 Feb 2009) Log Message: ----------- Initialized merge tracking via "svnmerge" with revisions "1-5948" from https://jython.svn.sourceforge.net/svnroot/jython/branches/pbcvm Property Changed: ---------------- trunk/jython/ Property changes on: trunk/jython ___________________________________________________________________ Added: svnmerge-integrated + /branches/pbcvm:1-5948 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-24 04:58:03
|
Revision: 6044 http://jython.svn.sourceforge.net/jython/?rev=6044&view=rev Author: zyasoft Date: 2009-02-24 04:57:56 +0000 (Tue, 24 Feb 2009) Log Message: ----------- Merged revisions 5950-5951,5953-5957,5959,5961,5965-5966,5971-5976,5978-5979,5981-5983,5992,6001,6003,6006-6014,6023,6031-6037,6039-6040 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython ........ r5950 | zyasoft | 2009-01-20 11:28:03 -0700 (Tue, 20 Jan 2009) | 4 lines Added os.getcwdu. This fixes #1219 (necessary for Django 1.0.2 support), but not the larger question of better Unicode handling in the os module, specifically around os.path. ........ r5951 | leosoto | 2009-01-20 12:29:17 -0700 (Tue, 20 Jan 2009) | 1 line Use the new getcwdu on POSIXHandler, to avoid unnecesary conversions from unicode to bytestring. Also added getcwdu to os.__all__ ........ r5953 | cgroves | 2009-01-21 13:47:19 -0700 (Wed, 21 Jan 2009) | 1 line Break out the compile method for use in subclasses ........ r5954 | cgroves | 2009-01-21 14:26:50 -0700 (Wed, 21 Jan 2009) | 1 line Turn includeJavaStackInExceptions on by default as it was always done in previous versions. ........ r5955 | cgroves | 2009-01-21 16:32:55 -0700 (Wed, 21 Jan 2009) | 8 lines Add a jython initializer service. If META-INF/services/org.python.core.JythonInitializer is on the classpath, the class named in that file will be instantiated and used in Jython's initialization. This is useful when Jython is initialized by a library outside of your control, but some customization still needs to be done to Jython's environment. I promise not to add a JythonFactory or a JythonFactoryFactory. ........ r5956 | fwierzbicki | 2009-01-21 19:21:08 -0700 (Wed, 21 Jan 2009) | 4 lines Fix for http://bugs.jython.org/issue1239. When jars where added to sys.path, they did not always trigger package caching, leading to unexpected import failures. This is Charlie Grove's fix, which was much better than mine. ........ r5957 | fwierzbicki | 2009-01-21 21:16:47 -0700 (Wed, 21 Jan 2009) | 7 lines Thanks to Marc Downie for this patch that fixes http://bugs.jython.org/issue1230 importing with '*' from java packages does not work with 2.5b1. Also added a test to test_java_integration.py. ........ r5959 | cgroves | 2009-01-22 12:50:18 -0700 (Thu, 22 Jan 2009) | 1 line Expose rich comparision methods on Java classes implementing Comparable ........ r5961 | fwierzbicki | 2009-01-22 18:03:39 -0700 (Thu, 22 Jan 2009) | 3 lines Over-ride pyset in PyTuple so our tuples go back to being immutable. Fixes http://bugs.jython.org/issue1242. ........ r5965 | pjenvey | 2009-01-23 16:40:46 -0700 (Fri, 23 Jan 2009) | 2 lines more java.lang.Strings to regular str instead of unicode ........ r5966 | pjenvey | 2009-01-23 17:17:40 -0700 (Fri, 23 Jan 2009) | 3 lines merge from: http://svn.python.org/projects/python/branches/release25-maint/Lib/platform.py -c 66214 ........ r5971 | cgroves | 2009-01-24 13:10:59 -0700 (Sat, 24 Jan 2009) | 1 line Run the java import * test from a sub-interpreter to allow regrtest to be run with python.cachedir.skip=true ........ r5972 | cgroves | 2009-01-24 13:11:54 -0700 (Sat, 24 Jan 2009) | 1 line Try the current classloader for places where the context classloader isn't set ........ r5973 | cgroves | 2009-01-24 13:17:39 -0700 (Sat, 24 Jan 2009) | 1 line Exclude the xerces service declaration as our jarjar'd version doesn't match the names in the declaration ........ r5974 | cgroves | 2009-01-25 05:15:25 -0700 (Sun, 25 Jan 2009) | 1 line Bring up to the current state of affairs ........ r5975 | cgroves | 2009-01-25 05:19:27 -0700 (Sun, 25 Jan 2009) | 3 lines Use Python's mro to determine an mro for Java classes and interfaces. If the Java class has an mro conflict, resolve it by arbitrarily choosing an interface or class. If an interface or class involved in an mro conflict has had its dict modified, or has its dict modified, raise a TypeError. ........ r5976 | cgroves | 2009-01-25 05:19:58 -0700 (Sun, 25 Jan 2009) | 1 line Test support classes for the last commit ........ r5978 | nriley | 2009-01-25 14:06:04 -0700 (Sun, 25 Jan 2009) | 1 line Typo fix. ........ r5979 | cgroves | 2009-01-25 17:33:21 -0700 (Sun, 25 Jan 2009) | 4 lines Include Python Java subclasses as bases as well as the Java proxy type, as it may have additional methods in its Python. Return the proxyClass directly from getProxyType as we're setting that on Python subclasses now. ........ r5981 | cgroves | 2009-01-25 19:21:05 -0700 (Sun, 25 Jan 2009) | 1 line Defer resolving inner classes till the containing class is fully resolved. Fixes issue #1234. ........ r5982 | cgroves | 2009-01-25 20:10:36 -0700 (Sun, 25 Jan 2009) | 1 line Try to assign to a static field for a given name in __setattr__ on a Java type before filling something in on the dict. Fixes bug #1241 ........ r5983 | cgroves | 2009-01-25 20:36:25 -0700 (Sun, 25 Jan 2009) | 1 line Fix javatests ........ r5992 | amak | 2009-01-28 09:05:59 -0700 (Wed, 28 Jan 2009) | 3 lines Fix and unit test for bug 1244. Problem letting system choose the port for binding UDP socket http://bugs.jython.org/issue1244 ........ r6001 | amak | 2009-01-29 10:59:04 -0700 (Thu, 29 Jan 2009) | 2 lines Re-arranging the socket shutdown methods. TCP client sockets can shutdown their input streams, but this is not appropriate for either TCP server sockets or UDP sockets, which don't have input and output streams. For these latter two types, the shutdown method should have the same effect as the close method, and thus the shutdown method is a no-op for these types. I have documented this difference between cpython and jython on the wiki. This should finally resolve bug 1121: listening socket shutdown expects the wrong kind of socket http://bugs.jython.org/issue1121 ........ r6003 | pjenvey | 2009-01-30 19:20:48 -0700 (Fri, 30 Jan 2009) | 1 line make --version resemble CPython ........ r6006 | amak | 2009-02-02 08:50:25 -0700 (Mon, 02 Feb 2009) | 4 lines Fix for bug 1258. select() semantics differ from CPython, causing pydoc HTTPd to fail http://bugs.jython.org/issue1258 Thanks to Ryan Blair for reporting the bug and providing a patch. ........ r6007 | amak | 2009-02-03 05:01:46 -0700 (Tue, 03 Feb 2009) | 2 lines Committing the latest version of modjy. See the release notes for details. ........ r6008 | pjenvey | 2009-02-03 13:19:57 -0700 (Tue, 03 Feb 2009) | 1 line fix test_version per r6003 ........ r6009 | pjenvey | 2009-02-03 14:25:23 -0700 (Tue, 03 Feb 2009) | 2 lines include PATH for platforms lacking bash in /bin ........ r6010 | leosoto | 2009-02-03 17:51:47 -0700 (Tue, 03 Feb 2009) | 1 line PyObject#reduce_2: Use invoke("iteritems") instead of this.iteritems() to avoid problems with dict subclasses which override iteritems on python code. Fixes #1257 ........ r6011 | amak | 2009-02-04 08:20:56 -0700 (Wed, 04 Feb 2009) | 4 lines Our asyncore is completely broken; see bug 1237. Asnycore does not seem to work in Jython 2.5 (b0 or b1) http://bugs.jython.org/issue1237 Starting again from scratch; Step 1: remove existing module. ........ r6012 | amak | 2009-02-04 08:22:21 -0700 (Wed, 04 Feb 2009) | 1 line Fresh start with asyncore; step 2: copy over asyncore from cpython 2.5.2. ........ r6013 | amak | 2009-02-04 08:27:59 -0700 (Wed, 04 Feb 2009) | 3 lines Fresh start with asycnore; step 3: make necessary changes to cpython 2.5 module to work on jython 2.5. The code in this bug report now works identically to cpython. http://bugs.jython.org/issue1237 ........ r6014 | otmarhumbel | 2009-02-05 11:08:10 -0700 (Thu, 05 Feb 2009) | 1 line added dummy_thread.py and dummy_threading.py ........ r6023 | fwierzbicki | 2009-02-08 18:40:53 -0700 (Sun, 08 Feb 2009) | 2 lines Fix for http://bugs.jython.org/issue1243. Thanks RJ Ryan for suggesting the fix. ........ r6031 | fwierzbicki | 2009-02-15 07:23:28 -0700 (Sun, 15 Feb 2009) | 3 lines Fix potential infinite loop (and wrong slice behavior) -- found with FindBugs ........ r6032 | fwierzbicki | 2009-02-15 11:22:18 -0700 (Sun, 15 Feb 2009) | 2 lines Fixed two infinite recursions found with FindBugs. ........ r6033 | fwierzbicki | 2009-02-15 11:31:27 -0700 (Sun, 15 Feb 2009) | 2 lines Fixed NPE found with FindBugs. ........ r6034 | amak | 2009-02-15 11:35:41 -0700 (Sun, 15 Feb 2009) | 2 lines A new version of modjy, containing some bug fixes kindly contributed by Philip Jenvey; see contained release notes for more information. http://github.com/pjenvey/modjy-pjenvey ........ r6035 | pjenvey | 2009-02-15 16:27:44 -0700 (Sun, 15 Feb 2009) | 3 lines double antlr's conversiontimeout for slower environments, in particular the hudson buildot ........ r6036 | fwierzbicki | 2009-02-16 08:41:50 -0700 (Mon, 16 Feb 2009) | 2 lines Useless double assignment. ........ r6037 | fwierzbicki | 2009-02-16 19:38:09 -0700 (Mon, 16 Feb 2009) | 2 lines Make the public adapters in AstAdapters final. ........ r6039 | amak | 2009-02-18 06:21:13 -0700 (Wed, 18 Feb 2009) | 1 line Updating the zip file for modjy 0.25.3; previously checked in version had the wrong version of modjy.jar in WEB-INF/lib. ........ r6040 | fwierzbicki | 2009-02-19 13:16:18 -0700 (Thu, 19 Feb 2009) | 4 lines Our handling of writes after a seek beyond the end of cStringIO was not consistent with CPython (or even our own use of StringIO). Found while trying to get Mercurial to respond to "hg log". ........ Modified Paths: -------------- branches/pbcvm/CPythonLib.includes branches/pbcvm/Lib/os.py branches/pbcvm/Lib/platform.py branches/pbcvm/Lib/pydoc.py branches/pbcvm/Lib/socket.py branches/pbcvm/Lib/test/test_StringIO_jy.py branches/pbcvm/Lib/test/test_classpathimporter.py branches/pbcvm/Lib/test/test_cmd_line.py branches/pbcvm/Lib/test/test_import_jy.py branches/pbcvm/Lib/test/test_java_integration.py branches/pbcvm/Lib/test/test_java_subclasses.py branches/pbcvm/Lib/test/test_java_visibility.py branches/pbcvm/Lib/test/test_socket.py branches/pbcvm/Lib/test/test_tuple.py branches/pbcvm/build.xml branches/pbcvm/registry branches/pbcvm/src/org/python/antlr/adapter/AstAdapters.java branches/pbcvm/src/org/python/compiler/JavaMaker.java branches/pbcvm/src/org/python/core/AstList.java branches/pbcvm/src/org/python/core/ClasspathPyImporter.java branches/pbcvm/src/org/python/core/Options.java branches/pbcvm/src/org/python/core/Py.java branches/pbcvm/src/org/python/core/PyJavaType.java branches/pbcvm/src/org/python/core/PyNewWrapper.java branches/pbcvm/src/org/python/core/PyObject.java branches/pbcvm/src/org/python/core/PySequence.java branches/pbcvm/src/org/python/core/PySystemState.java branches/pbcvm/src/org/python/core/PyTuple.java branches/pbcvm/src/org/python/core/PyType.java branches/pbcvm/src/org/python/core/SyspathJavaLoader.java branches/pbcvm/src/org/python/core/ThreadStateMapping.java branches/pbcvm/src/org/python/core/imp.java branches/pbcvm/src/org/python/core/packagecache/PackageManager.java branches/pbcvm/src/org/python/core/packagecache/PathPackageManager.java branches/pbcvm/src/org/python/modules/_csv/PyWriter.java branches/pbcvm/src/org/python/modules/cStringIO.java branches/pbcvm/src/org/python/modules/thread/PyLocal.java branches/pbcvm/src/org/python/util/JycompileAntTask.java branches/pbcvm/src/org/python/util/jython.java branches/pbcvm/tests/java/org/python/expose/generate/TypeExposerTest.java branches/pbcvm/tests/java/org/python/tests/Coercions.java branches/pbcvm/tests/java/org/python/tests/Visible.java Added Paths: ----------- branches/pbcvm/Lib/asyncore.py branches/pbcvm/Lib/test/bug1239.jar branches/pbcvm/Lib/test/check_for_initializer_in_syspath.py branches/pbcvm/Lib/test/import_star_from_java.py branches/pbcvm/Lib/test/test_jython_initializer.py branches/pbcvm/extlibs/modjy_0_25_3.zip branches/pbcvm/src/org/python/core/JythonInitializer.java branches/pbcvm/tests/data/ branches/pbcvm/tests/data/initializer/ branches/pbcvm/tests/data/initializer/META-INF/ branches/pbcvm/tests/data/initializer/META-INF/services/ branches/pbcvm/tests/data/initializer/META-INF/services/org.python.core.JythonInitializer branches/pbcvm/tests/data/initializer/SyspathAppendingInitializer.java branches/pbcvm/tests/java/org/python/tests/inbred/ branches/pbcvm/tests/java/org/python/tests/inbred/Metis.java branches/pbcvm/tests/java/org/python/tests/inbred/Zeus.java branches/pbcvm/tests/java/org/python/tests/mro/ branches/pbcvm/tests/java/org/python/tests/mro/ConfusedOnGetitemAdd.java branches/pbcvm/tests/java/org/python/tests/mro/ConfusedOnImport.java branches/pbcvm/tests/java/org/python/tests/mro/FirstAndPost.java branches/pbcvm/tests/java/org/python/tests/mro/FirstAndSecond.java branches/pbcvm/tests/java/org/python/tests/mro/FirstPredefinedGetitem.java branches/pbcvm/tests/java/org/python/tests/mro/GetitemAdder.java branches/pbcvm/tests/java/org/python/tests/mro/PostAndFirst.java branches/pbcvm/tests/java/org/python/tests/mro/PostdefinedGetitem.java branches/pbcvm/tests/java/org/python/tests/mro/SecondAndFirst.java branches/pbcvm/tests/java/org/python/tests/mro/SecondPredefinedGetitem.java Removed Paths: ------------- branches/pbcvm/Lib/asyncore.py branches/pbcvm/extlibs/modjy_0_25_1.zip branches/pbcvm/tests/data/initializer/ branches/pbcvm/tests/data/initializer/META-INF/ branches/pbcvm/tests/data/initializer/META-INF/services/ branches/pbcvm/tests/data/initializer/META-INF/services/org.python.core.JythonInitializer branches/pbcvm/tests/data/initializer/SyspathAppendingInitializer.java branches/pbcvm/tests/java/org/python/tests/inbred/Metis.java branches/pbcvm/tests/java/org/python/tests/inbred/Zeus.java branches/pbcvm/tests/java/org/python/tests/mro/ConfusedOnGetitemAdd.java branches/pbcvm/tests/java/org/python/tests/mro/ConfusedOnImport.java branches/pbcvm/tests/java/org/python/tests/mro/FirstAndPost.java branches/pbcvm/tests/java/org/python/tests/mro/FirstAndSecond.java branches/pbcvm/tests/java/org/python/tests/mro/FirstPredefinedGetitem.java branches/pbcvm/tests/java/org/python/tests/mro/GetitemAdder.java branches/pbcvm/tests/java/org/python/tests/mro/PostAndFirst.java branches/pbcvm/tests/java/org/python/tests/mro/PostdefinedGetitem.java branches/pbcvm/tests/java/org/python/tests/mro/SecondAndFirst.java branches/pbcvm/tests/java/org/python/tests/mro/SecondPredefinedGetitem.java Property Changed: ---------------- branches/pbcvm/ Property changes on: branches/pbcvm ___________________________________________________________________ Modified: svnmerge-integrated - /trunk/jython:1-5947 + /trunk/jython:1-6043 Modified: branches/pbcvm/CPythonLib.includes =================================================================== --- branches/pbcvm/CPythonLib.includes 2009-02-24 04:37:39 UTC (rev 6043) +++ branches/pbcvm/CPythonLib.includes 2009-02-24 04:57:56 UTC (rev 6044) @@ -52,6 +52,8 @@ DocXMLRPCServer.py dospath.py dumbdbm.py +dummy_thread.py +dummy_threading.py exceptions.py fileinput.py fnmatch.py Deleted: branches/pbcvm/Lib/asyncore.py =================================================================== --- branches/pbcvm/Lib/asyncore.py 2009-02-24 04:37:39 UTC (rev 6043) +++ branches/pbcvm/Lib/asyncore.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -1,561 +0,0 @@ -# -*- Mode: Python -*- -# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp -# Author: Sam Rushing <ru...@ni...> - -# ====================================================================== -# Copyright 1996 by Sam Rushing -# -# All Rights Reserved -# -# Permission to use, copy, modify, and distribute this software and -# its documentation for any purpose and without fee is hereby -# granted, provided that the above copyright notice appear in all -# copies and that both that copyright notice and this permission -# notice appear in supporting documentation, and that the name of Sam -# Rushing not be used in advertising or publicity pertaining to -# distribution of the software without specific, written prior -# permission. -# -# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN -# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR -# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS -# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, -# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN -# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# ====================================================================== - -"""Basic infrastructure for asynchronous socket service clients and servers. - -There are only two ways to have a program on a single processor do "more -than one thing at a time". Multi-threaded programming is the simplest and -most popular way to do it, but there is another very different technique, -that lets you have nearly all the advantages of multi-threading, without -actually using multiple threads. it's really only practical if your program -is largely I/O bound. If your program is CPU bound, then pre-emptive -scheduled threads are probably what you really need. Network servers are -rarely CPU-bound, however. - -If your operating system supports the select() system call in its I/O -library (and nearly all do), then you can use it to juggle multiple -communication channels at once; doing other work while your I/O is taking -place in the "background." Although this strategy can seem strange and -complex, especially at first, it is in many ways easier to understand and -control than multi-threaded programming. The module documented here solves -many of the difficult problems for you, making the task of building -sophisticated high-performance network servers and clients a snap. -""" - -import exceptions -import select -import socket -import sys -import time - -import os -from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ - ENOTCONN, ESHUTDOWN, EINTR, EISCONN - -try: - socket_map -except NameError: - socket_map = {} - -class ExitNow(exceptions.Exception): - pass - -def read(obj): - try: - obj.handle_read_event() - except ExitNow: - raise - except: - obj.handle_error() - -def write(obj): - try: - obj.handle_write_event() - except ExitNow: - raise - except: - obj.handle_error() - -def readwrite(obj, flags): - try: - if flags & select.POLLIN: - obj.handle_read_event() - if flags & select.POLLOUT: - obj.handle_write_event() - except ExitNow: - raise - except: - obj.handle_error() - -def poll(timeout=0.0, map=None): - if map is None: - map = socket_map - if map: - r = []; w = []; e = [] - for fd, obj in map.items(): - if obj.readable(): - r.append(fd) - if obj.writable(): - w.append(fd) - if [] == r == w == e: - time.sleep(timeout) - else: - try: - r, w, e = select.select(r, w, e, timeout) - except select.error, err: - if err[0] != EINTR: - raise - else: - return - - for fd in r: - obj = map.get(fd) - if obj is None: - continue - read(obj) - - for fd in w: - obj = map.get(fd) - if obj is None: - continue - write(obj) - -def poll2(timeout=0.0, map=None): - import poll - if map is None: - map = socket_map - if timeout is not None: - # timeout is in milliseconds - timeout = int(timeout*1000) - if map: - l = [] - for fd, obj in map.items(): - flags = 0 - if obj.readable(): - flags = poll.POLLIN - if obj.writable(): - flags = flags | poll.POLLOUT - if flags: - l.append((fd, flags)) - r = poll.poll(l, timeout) - for fd, flags in r: - obj = map.get(fd) - if obj is None: - continue - readwrite(obj, flags) - -def poll3(timeout=0.0, map=None): - # Use the poll() support added to the select module in Python 2.0 - if map is None: - map = socket_map - if timeout is not None: - # timeout is in milliseconds - timeout = int(timeout*1000) - pollster = select.poll() - if map: - for fd, obj in map.items(): - flags = 0 - if obj.readable(): - flags = select.POLLIN - if obj.writable(): - flags = flags | select.POLLOUT - if flags: - pollster.register(fd, flags) - try: - r = pollster.poll(timeout) - except select.error, err: - if err[0] != EINTR: - raise - r = [] - for fd, flags in r: - obj = map.get(fd) - if obj is None: - continue - readwrite(obj, flags) - -def loop(timeout=30.0, use_poll=0, map=None): - if map is None: - map = socket_map - - if use_poll: - if hasattr(select, 'poll'): - poll_fun = poll3 - else: - poll_fun = poll2 - else: - poll_fun = poll - - while map: - poll_fun(timeout, map) - -class dispatcher: - - debug = 0 - connected = 0 - accepting = 0 - closing = 0 - addr = None - - def __init__(self, sock=None, map=None): - if sock: - self.set_socket(sock, map) - # I think it should inherit this anyway - self.socket.setblocking(0) - self.connected = 1 - # XXX Does the constructor require that the socket passed - # be connected? - try: - self.addr = sock.getpeername() - except socket.error: - # The addr isn't crucial - pass - else: - self.socket = None - - def __repr__(self): - status = [self.__class__.__module__+"."+self.__class__.__name__] - if self.accepting and self.addr: - status.append('listening') - elif self.connected: - status.append('connected') - if self.addr is not None: - try: - status.append('%s:%d' % self.addr) - except TypeError: - status.append(repr(self.addr)) - # On some systems (RH10) id() can be a negative number. - # work around this. - MAX = 2L*sys.maxint+1 - return '<%s at %#x>' % (' '.join(status), id(self)&MAX) - - def add_channel(self, map=None): - #self.log_info('adding channel %s' % self) - if map is None: - if hasattr(self, '_map'): - map = self._map - del self._map - else: - map = socket_map - if not hasattr(self, '_fileno'): - self._fileno = self.socket.fileno() - map[self._fileno] = self - - def del_channel(self, map=None): - fd = self._fileno - if map is None: - map = socket_map - if map.has_key(fd): - #self.log_info('closing channel %d:%s' % (fd, self)) - del map[fd] - - def create_socket(self, family, type): - self.family_and_type = family, type - self.socket = socket.socket(family, type) - self.socket.setblocking(0) - - def set_socket(self, sock, map=None): - self.socket = sock -## self.__dict__['socket'] = sock - if sock.fileno(): - self.add_channel(map) - else: - self._map = map - - def set_reuse_addr(self): - # try to re-use a server port if possible - try: - self.socket.setsockopt( - socket.SOL_SOCKET, socket.SO_REUSEADDR, - self.socket.getsockopt(socket.SOL_SOCKET, - socket.SO_REUSEADDR) | 1 - ) - except socket.error: - pass - - # ================================================== - # predicates for select() - # these are used as filters for the lists of sockets - # to pass to select(). - # ================================================== - - def readable(self): - return True - - if os.name == 'mac': - # The macintosh will select a listening socket for - # write if you let it. What might this mean? - def writable(self): - return not self.accepting - else: - def writable(self): - return True - - # ================================================== - # socket object methods. - # ================================================== - - def listen(self, num): - self.accepting = 1 - if os.name == 'nt' and num > 5: - num = 1 - ret = self.socket.listen(num) - self.add_channel() - return ret - - def bind(self, addr): - self.addr = addr - return self.socket.bind(addr) - - def connect(self, address): - self.connected = 0 - err = self.socket.connect_ex(address) - # XXX Should interpret Winsock return values - if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): - return - if err in (0, EISCONN): - self.add_channel() - self.addr = address - self.connected = 1 - self.handle_connect() - else: - raise socket.error, err - - def accept(self): - # XXX can return either an address pair or None - try: - conn, addr = self.socket.accept() - self.add_channel() - return conn, addr - except socket.error, why: - if why[0] == EWOULDBLOCK: - pass - else: - raise socket.error, why - - def send(self, data): - try: - result = self.socket.send(data) - return result - except socket.error, why: - if why[0] == EWOULDBLOCK: - return 0 - else: - raise socket.error, why - return 0 - - def recv(self, buffer_size): - try: - data = self.socket.recv(buffer_size) - if not data: - # a closed connection is indicated by signaling - # a read condition, and having recv() return 0. - self.handle_close() - return '' - else: - return data - except socket.error, why: - # winsock sometimes throws ENOTCONN - if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: - self.handle_close() - return '' - else: - raise socket.error, why - - def close(self): - self.del_channel() - self.socket.close() - - # cheap inheritance, used to pass all other attribute - # references to the underlying socket object. - def __getattr__(self, attr): - return getattr(self.socket, attr) - - # log and log_info may be overridden to provide more sophisticated - # logging and warning methods. In general, log is for 'hit' logging - # and 'log_info' is for informational, warning and error logging. - - def log(self, message): - sys.stderr.write('log: %s\n' % str(message)) - - def log_info(self, message, type='info'): - if __debug__ or type != 'info': - print '%s: %s' % (type, message) - - def handle_read_event(self): - if self.accepting: - # for an accepting socket, getting a read implies - # that we are connected - if not self.connected: - self.connected = 1 - self.handle_accept() - elif not self.connected: - self.handle_connect() - self.connected = 1 - self.handle_read() - else: - self.handle_read() - - def handle_write_event(self): - # getting a write implies that we are connected - if not self.connected: - self.handle_connect() - self.connected = 1 - self.handle_write() - - def handle_expt_event(self): - self.handle_expt() - - def handle_error(self): - nil, t, v, tbinfo = compact_traceback() - - # sometimes a user repr method will crash. - try: - self_repr = repr(self) - except: - self_repr = '<__repr__(self) failed for object at %0x>' % id(self) - - self.log_info( - 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( - self_repr, - t, - v, - tbinfo - ), - 'error' - ) - self.close() - - def handle_expt(self): - self.log_info('unhandled exception', 'warning') - - def handle_read(self): - self.log_info('unhandled read event', 'warning') - - def handle_write(self): - self.log_info('unhandled write event', 'warning') - - def handle_connect(self): - self.log_info('unhandled connect event', 'warning') - - def handle_accept(self): - self.log_info('unhandled accept event', 'warning') - - def handle_close(self): - self.log_info('unhandled close event', 'warning') - self.close() - -# --------------------------------------------------------------------------- -# adds simple buffered output capability, useful for simple clients. -# [for more sophisticated usage use asynchat.async_chat] -# --------------------------------------------------------------------------- - -class dispatcher_with_send(dispatcher): - - def __init__(self, sock=None): - dispatcher.__init__(self, sock) - self.out_buffer = '' - - def initiate_send(self): - num_sent = 0 - num_sent = dispatcher.send(self, self.out_buffer[:512]) - self.out_buffer = self.out_buffer[num_sent:] - - def handle_write(self): - self.initiate_send() - - def writable(self): - return (not self.connected) or len(self.out_buffer) - - def send(self, data): - if self.debug: - self.log_info('sending %s' % repr(data)) - self.out_buffer = self.out_buffer + data - self.initiate_send() - -# --------------------------------------------------------------------------- -# used for debugging. -# --------------------------------------------------------------------------- - -def compact_traceback(): - t, v, tb = sys.exc_info() - tbinfo = [] - assert tb # Must have a traceback - while tb: - tbinfo.append(( - tb.tb_frame.f_code.co_filename, - tb.tb_frame.f_code.co_name, - str(tb.tb_lineno) - )) - tb = tb.tb_next - - # just to be safe - del tb - - file, function, line = tbinfo[-1] - info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) - return (file, function, line), t, v, info - -def close_all(map=None): - if map is None: - map = socket_map - for x in map.values(): - x.socket.close() - map.clear() - -# Asynchronous File I/O: -# -# After a little research (reading man pages on various unixen, and -# digging through the linux kernel), I've determined that select() -# isn't meant for doing asynchronous file i/o. -# Heartening, though - reading linux/mm/filemap.c shows that linux -# supports asynchronous read-ahead. So _MOST_ of the time, the data -# will be sitting in memory for us already when we go to read it. -# -# What other OS's (besides NT) support async file i/o? [VMS?] -# -# Regardless, this is useful for pipes, and stdin/stdout... - -if os.name == 'posix': - import fcntl - - class file_wrapper: - # here we override just enough to make a file - # look like a socket for the purposes of asyncore. - - def __init__(self, fd): - self.fd = fd - - def recv(self, *args): - return os.read(self.fd, *args) - - def send(self, *args): - return os.write(self.fd, *args) - - read = recv - write = send - - def close(self): - return os.close(self.fd) - - def fileno(self): - return self.fd - - class file_dispatcher(dispatcher): - - def __init__(self, fd): - dispatcher.__init__(self) - self.connected = 1 - # set it to non-blocking mode - flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) - flags = flags | os.O_NONBLOCK - fcntl.fcntl(fd, fcntl.F_SETFL, flags) - self.set_file(fd) - - def set_file(self, fd): - self._fileno = fd - self.socket = file_wrapper(fd) - self.add_channel() Copied: branches/pbcvm/Lib/asyncore.py (from rev 6040, trunk/jython/Lib/asyncore.py) =================================================================== --- branches/pbcvm/Lib/asyncore.py (rev 0) +++ branches/pbcvm/Lib/asyncore.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -0,0 +1,551 @@ +# -*- Mode: Python -*- +# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp +# Author: Sam Rushing <ru...@ni...> + +# ====================================================================== +# Copyright 1996 by Sam Rushing +# +# All Rights Reserved +# +# Permission to use, copy, modify, and distribute this software and +# its documentation for any purpose and without fee is hereby +# granted, provided that the above copyright notice appear in all +# copies and that both that copyright notice and this permission +# notice appear in supporting documentation, and that the name of Sam +# Rushing not be used in advertising or publicity pertaining to +# distribution of the software without specific, written prior +# permission. +# +# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN +# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR +# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# ====================================================================== + +"""Basic infrastructure for asynchronous socket service clients and servers. + +There are only two ways to have a program on a single processor do "more +than one thing at a time". Multi-threaded programming is the simplest and +most popular way to do it, but there is another very different technique, +that lets you have nearly all the advantages of multi-threading, without +actually using multiple threads. it's really only practical if your program +is largely I/O bound. If your program is CPU bound, then pre-emptive +scheduled threads are probably what you really need. Network servers are +rarely CPU-bound, however. + +If your operating system supports the select() system call in its I/O +library (and nearly all do), then you can use it to juggle multiple +communication channels at once; doing other work while your I/O is taking +place in the "background." Although this strategy can seem strange and +complex, especially at first, it is in many ways easier to understand and +control than multi-threaded programming. The module documented here solves +many of the difficult problems for you, making the task of building +sophisticated high-performance network servers and clients a snap. +""" + +import select +import socket +import sys +import time + +import os +from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ + ENOTCONN, ESHUTDOWN, EINTR, EISCONN, errorcode + +try: + socket_map +except NameError: + socket_map = {} + +class ExitNow(Exception): + pass + +def read(obj): + try: + obj.handle_read_event() + except ExitNow: + raise + except: + obj.handle_error() + +def write(obj): + try: + obj.handle_write_event() + except ExitNow: + raise + except: + obj.handle_error() + +def _exception (obj): + try: + obj.handle_expt_event() + except ExitNow: + raise + except: + obj.handle_error() + +def readwrite(obj, flags): + try: + if flags & (select.POLLIN | select.POLLPRI): + obj.handle_read_event() + if flags & select.POLLOUT: + obj.handle_write_event() + if flags & (select.POLLERR | select.POLLHUP | select.POLLNVAL): + obj.handle_expt_event() + except ExitNow: + raise + except: + obj.handle_error() + +def poll(timeout=0.0, map=None): + if map is None: + map = socket_map + if map: + r = []; w = []; e = [] + for fd, obj in map.items(): + is_r = obj.readable() + is_w = obj.writable() + if is_r: + r.append(fd) + if is_w: + w.append(fd) + if is_r or is_w: + e.append(fd) + if [] == r == w == e: + time.sleep(timeout) + else: + try: + r, w, e = select.select(r, w, e, timeout) + except select.error, err: + if err[0] != EINTR: + raise + else: + return + + for fd in r: + obj = map.get(fd) + if obj is None: + continue + read(obj) + + for fd in w: + obj = map.get(fd) + if obj is None: + continue + write(obj) + + for fd in e: + obj = map.get(fd) + if obj is None: + continue + _exception(obj) + +def poll2(timeout=0.0, map=None): + # Use the poll() support added to the select module in Python 2.0 + if map is None: + map = socket_map + if timeout is not None: + # timeout is in milliseconds + timeout = int(timeout*1000) + pollster = select.poll() + if map: + for fd, obj in map.items(): + flags = 0 + if obj.readable(): + flags |= select.POLLIN | select.POLLPRI + if obj.writable(): + flags |= select.POLLOUT + if flags: + # Only check for exceptions if object was either readable + # or writable. + flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL + pollster.register(fd, flags) + try: + r = pollster.poll(timeout) + except select.error, err: + if err[0] != EINTR: + raise + r = [] + for fd, flags in r: + obj = map.get(fd) + if obj is None: + continue + readwrite(obj, flags) + +poll3 = poll2 # Alias for backward compatibility + +def loop(timeout=30.0, use_poll=True, map=None, count=None): + if map is None: + map = socket_map + + if use_poll and hasattr(select, 'poll'): + poll_fun = poll2 + else: + poll_fun = poll + + if count is None: + while map: + poll_fun(timeout, map) + + else: + while map and count > 0: + poll_fun(timeout, map) + count = count - 1 + +class dispatcher: + + debug = False + connected = False + accepting = False + closing = False + addr = None + + def __init__(self, sock=None, map=None): + if map is None: + self._map = socket_map + else: + self._map = map + + if sock: + self.set_socket(sock, map) + # I think it should inherit this anyway + self.socket.setblocking(0) + self.connected = True + # XXX Does the constructor require that the socket passed + # be connected? + try: + self.addr = sock.getpeername() + except socket.error: + # The addr isn't crucial + pass + else: + self.socket = None + + def __repr__(self): + status = [self.__class__.__module__+"."+self.__class__.__name__] + if self.accepting and self.addr: + status.append('listening') + elif self.connected: + status.append('connected') + if self.addr is not None: + try: + status.append('%s:%d' % self.addr) + except TypeError: + status.append(repr(self.addr)) + return '<%s at %#x>' % (' '.join(status), id(self)) + + def add_channel(self, map=None): + #self.log_info('adding channel %s' % self) + if map is None: + map = self._map + map[self._fileno] = self + + def del_channel(self, map=None): + fd = self._fileno + if map is None: + map = self._map + if map.has_key(fd): + #self.log_info('closing channel %d:%s' % (fd, self)) + del map[fd] + self._fileno = None + + def create_socket(self, family, type): + self.family_and_type = family, type + self.socket = socket.socket(family, type) + self.socket.setblocking(0) + self._fileno = self.socket + self.add_channel() + + def set_socket(self, sock, map=None): + self.socket = sock +## self.__dict__['socket'] = sock + self._fileno = sock + self.add_channel(map) + + def set_reuse_addr(self): + # try to re-use a server port if possible + try: + self.socket.setsockopt( + socket.SOL_SOCKET, socket.SO_REUSEADDR, + self.socket.getsockopt(socket.SOL_SOCKET, + socket.SO_REUSEADDR) | 1 + ) + except socket.error: + pass + + # ================================================== + # predicates for select() + # these are used as filters for the lists of sockets + # to pass to select(). + # ================================================== + + def readable(self): + return True + + def writable(self): + return True + + # ================================================== + # socket object methods. + # ================================================== + + def listen(self, num): + self.accepting = True + if os.name == 'nt' and num > 5: + num = 1 + return self.socket.listen(num) + + def bind(self, addr): + self.addr = addr + return self.socket.bind(addr) + + def connect(self, address): + self.connected = False + err = self.socket.connect_ex(address) + # XXX Should interpret Winsock return values + if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): + return + if err in (0, EISCONN): + self.addr = address + self.connected = True + self.handle_connect() + else: + raise socket.error, (err, errorcode[err]) + + def accept(self): + # XXX can return either an address pair or None + try: + conn, addr = self.socket.accept() + return conn, addr + except socket.error, why: + if why[0] == EWOULDBLOCK: + pass + else: + raise + + def send(self, data): + try: + result = self.socket.send(data) + return result + except socket.error, why: + if why[0] == EWOULDBLOCK: + return 0 + else: + raise + return 0 + + def recv(self, buffer_size): + try: + data = self.socket.recv(buffer_size) + if not data: + # a closed connection is indicated by signaling + # a read condition, and having recv() return 0. + self.handle_close() + return '' + else: + return data + except socket.error, why: + # winsock sometimes throws ENOTCONN + if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: + self.handle_close() + return '' + else: + raise + + def close(self): + self.del_channel() + self.socket.close() + + # cheap inheritance, used to pass all other attribute + # references to the underlying socket object. + def __getattr__(self, attr): + return getattr(self.socket, attr) + + # log and log_info may be overridden to provide more sophisticated + # logging and warning methods. In general, log is for 'hit' logging + # and 'log_info' is for informational, warning and error logging. + + def log(self, message): + sys.stderr.write('log: %s\n' % str(message)) + + def log_info(self, message, type='info'): + if __debug__ or type != 'info': + print '%s: %s' % (type, message) + + def handle_read_event(self): + if self.accepting: + # for an accepting socket, getting a read implies + # that we are connected + if not self.connected: + self.connected = True + self.handle_accept() + elif not self.connected: + self.handle_connect() + self.connected = True + self.handle_read() + else: + self.handle_read() + + def handle_write_event(self): + # getting a write implies that we are connected + if not self.connected: + self.handle_connect() + self.connected = True + self.handle_write() + + def handle_expt_event(self): + self.handle_expt() + + def handle_error(self): + nil, t, v, tbinfo = compact_traceback() + + # sometimes a user repr method will crash. + try: + self_repr = repr(self) + except: + self_repr = '<__repr__(self) failed for object at %0x>' % id(self) + + self.log_info( + 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( + self_repr, + t, + v, + tbinfo + ), + 'error' + ) + self.close() + + def handle_expt(self): + self.log_info('unhandled exception', 'warning') + + def handle_read(self): + self.log_info('unhandled read event', 'warning') + + def handle_write(self): + self.log_info('unhandled write event', 'warning') + + def handle_connect(self): + self.log_info('unhandled connect event', 'warning') + + def handle_accept(self): + self.log_info('unhandled accept event', 'warning') + + def handle_close(self): + self.log_info('unhandled close event', 'warning') + self.close() + +# --------------------------------------------------------------------------- +# adds simple buffered output capability, useful for simple clients. +# [for more sophisticated usage use asynchat.async_chat] +# --------------------------------------------------------------------------- + +class dispatcher_with_send(dispatcher): + + def __init__(self, sock=None, map=None): + dispatcher.__init__(self, sock, map) + self.out_buffer = '' + + def initiate_send(self): + num_sent = 0 + num_sent = dispatcher.send(self, self.out_buffer[:512]) + self.out_buffer = self.out_buffer[num_sent:] + + def handle_write(self): + self.initiate_send() + + def writable(self): + return (not self.connected) or len(self.out_buffer) + + def send(self, data): + if self.debug: + self.log_info('sending %s' % repr(data)) + self.out_buffer = self.out_buffer + data + self.initiate_send() + +# --------------------------------------------------------------------------- +# used for debugging. +# --------------------------------------------------------------------------- + +def compact_traceback(): + t, v, tb = sys.exc_info() + tbinfo = [] + assert tb # Must have a traceback + while tb: + tbinfo.append(( + tb.tb_frame.f_code.co_filename, + tb.tb_frame.f_code.co_name, + str(tb.tb_lineno) + )) + tb = tb.tb_next + + # just to be safe + del tb + + file, function, line = tbinfo[-1] + info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) + return (file, function, line), t, v, info + +def close_all(map=None): + if map is None: + map = socket_map + for x in map.values(): + x.socket.close() + map.clear() + +# Asynchronous File I/O: +# +# After a little research (reading man pages on various unixen, and +# digging through the linux kernel), I've determined that select() +# isn't meant for doing asynchronous file i/o. +# Heartening, though - reading linux/mm/filemap.c shows that linux +# supports asynchronous read-ahead. So _MOST_ of the time, the data +# will be sitting in memory for us already when we go to read it. +# +# What other OS's (besides NT) support async file i/o? [VMS?] +# +# Regardless, this is useful for pipes, and stdin/stdout... + +if os.name == 'posix': + import fcntl + + class file_wrapper: + # here we override just enough to make a file + # look like a socket for the purposes of asyncore. + + def __init__(self, fd): + self.fd = fd + + def recv(self, *args): + return os.read(self.fd, *args) + + def send(self, *args): + return os.write(self.fd, *args) + + read = recv + write = send + + def close(self): + os.close(self.fd) + + def fileno(self): + return self.fd + + class file_dispatcher(dispatcher): + + def __init__(self, fd, map=None): + dispatcher.__init__(self, None, map) + self.connected = True + self.set_file(fd) + # set it to non-blocking mode + flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) + flags = flags | os.O_NONBLOCK + fcntl.fcntl(fd, fcntl.F_SETFL, flags) + + def set_file(self, fd): + self._fileno = fd + self.socket = file_wrapper(fd) + self.add_channel() Modified: branches/pbcvm/Lib/os.py =================================================================== --- branches/pbcvm/Lib/os.py 2009-02-24 04:37:39 UTC (rev 6043) +++ branches/pbcvm/Lib/os.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -30,8 +30,8 @@ 'O_RDWR', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'W_OK', 'X_OK', '_exit', 'access', 'altsep', 'chdir', 'chmod', 'close', 'curdir', 'defpath', - 'environ', 'error', 'fdopen', 'getcwd', 'getegid', 'getenv', - 'geteuid', 'getgid', 'getlogin', 'getlogin', 'getpgrp', + 'environ', 'error', 'fdopen', 'getcwd', 'getcwdu', 'getegid', + 'getenv','geteuid', 'getgid', 'getlogin', 'getlogin', 'getpgrp', 'getpid', 'getppid', 'getuid', 'isatty', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'popen', 'popen2', 'popen3', 'popen4', @@ -83,9 +83,9 @@ """ os_name = sys.registry.getProperty('python.os') if os_name: - return str(os_name) + return asPyString(os_name) - os_name = str(java.lang.System.getProperty('os.name')) + os_name = asPyString(java.lang.System.getProperty('os.name')) os_type = None for type, (patterns, shell_commands) in _os_map.iteritems(): for pattern in patterns: @@ -118,7 +118,7 @@ def isVerbose(self): return False def getCurrentWorkingDirectory(self): - return File(getcwd()) + return File(getcwdu()) def getEnv(self): return ['%s=%s' % (key, val) for key, val in environ.iteritems()] def getInputStream(self): @@ -251,6 +251,13 @@ """ return asPyString(sys.getCurrentWorkingDir()) +def getcwdu(): + """getcwd() -> path + + Return a unicode string representing the current working directory. + """ + return sys.getCurrentWorkingDir() + def chdir(path): """chdir(path) Modified: branches/pbcvm/Lib/platform.py =================================================================== --- branches/pbcvm/Lib/platform.py 2009-02-24 04:37:39 UTC (rev 6043) +++ branches/pbcvm/Lib/platform.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -624,8 +624,9 @@ def _java_getprop(name,default): from java.lang import System + from org.python.core.Py import newString try: - return System.getProperty(name) + return newString(System.getProperty(name)) except: return default @@ -793,7 +794,7 @@ filepath = _abspath(filepath) while os.path.islink(filepath): filepath = os.path.normpath( - os.path.join(filepath,os.readlink(filepath))) + os.path.join(os.path.dirname(filepath),os.readlink(filepath))) return filepath def _syscmd_uname(option,default=''): Modified: branches/pbcvm/Lib/pydoc.py =================================================================== --- branches/pbcvm/Lib/pydoc.py 2009-02-24 04:37:39 UTC (rev 6043) +++ branches/pbcvm/Lib/pydoc.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -1959,13 +1959,17 @@ self.callback = callback self.base.__init__(self, self.address, self.handler) - def serve_until_quit(self): - import select - self.quit = False - while not self.quit: - rd, wr, ex = select.select([self.socket.fileno()], [], [], 1) - if rd: self.handle_request() - + def serve_until_quit(self): + import sys + if sys.platform.startswith('java'): + from select import cpython_compatible_select as select + else: + from select import select + self.quit = False + while not self.quit: + rd, wr, ex = select([self.socket], [], [], 1) + if rd: self.handle_request() + def server_activate(self): self.base.server_activate(self) if self.callback: self.callback(self) @@ -1976,7 +1980,7 @@ try: try: DocServer(port, callback).serve_until_quit() - except (KeyboardInterrupt, select.error): + except (KeyboardInterrupt, select.error): pass finally: if completer: completer() Modified: branches/pbcvm/Lib/socket.py =================================================================== --- branches/pbcvm/Lib/socket.py 2009-02-24 04:37:39 UTC (rev 6043) +++ branches/pbcvm/Lib/socket.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -248,22 +248,6 @@ def close(self): self.jsocket.close() - def shutdownInput(self): - try: - self.jsocket.shutdownInput() - except AttributeError, ax: - pass # Fail silently server sockets - except java.lang.Exception, jlx: - raise _map_exception(jlx) - - def shutdownOutput(self): - try: - self.jsocket.shutdownOutput() - except AttributeError, ax: - pass # Fail silently server sockets - except java.lang.Exception, jlx: - raise _map_exception(jlx) - def getchannel(self): return self.jchannel @@ -340,6 +324,12 @@ else: return self._do_write_nio(buf) + def shutdown(self, how): + if how in (SHUT_RD, SHUT_RDWR): + self.jsocket.shutdownInput() + if how in (SHUT_WR, SHUT_RDWR): + self.jsocket.shutdownOutput() + class _server_socket_impl(_nio_impl): options = { @@ -371,6 +361,13 @@ new_cli_sock = self.jsocket.accept() return _client_socket_impl(new_cli_sock) + def shutdown(self, how): + # This is no-op on java, for server sockets. + # What the user wants to achieve is achieved by calling close() on + # java/jython. But we can't call that here because that would then + # later cause the user explicit close() call to fail + pass + class _datagram_socket_impl(_nio_impl): options = { @@ -384,7 +381,7 @@ def __init__(self, port=None, address=None, reuse_addr=0): self.jchannel = java.nio.channels.DatagramChannel.open() self.jsocket = self.jchannel.socket() - if port: + if port is not None: if address is not None: local_address = java.net.InetSocketAddress(address, port) else: @@ -406,6 +403,13 @@ """ self.jchannel.disconnect() + def shutdown(self, how): + # This is no-op on java, for datagram sockets. + # What the user wants to achieve is achieved by calling close() on + # java/jython. But we can't call that here because that would then + # later cause the user explicit close() call to fail + pass + def _do_send_net(self, byte_array, socket_address, flags): # Need two separate implementations because the java.nio APIs do not support timeouts num_bytes = len(byte_array) @@ -689,6 +693,22 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) + def shutdown(self, how): + assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR) + if not self.sock_impl: + raise error(errno.ENOTCONN, "Transport endpoint is not connected") + try: + self.sock_impl.shutdown(how) + except java.lang.Exception, jlx: + raise _map_exception(jlx) + + def close(self): + try: + if self.sock_impl: + self.sock_impl.close() + except java.lang.Exception, jlx: + raise _map_exception(jlx) + def _config(self): assert self.mode in _permitted_modes if self.sock_impl: @@ -878,15 +898,6 @@ except java.lang.Exception, jlx: raise _map_exception(jlx) - def shutdown(self, how): - if not self.sock_impl: - raise error(errno.ENOTCONN, "Transport endpoint is not connected") - assert how in (SHUT_RD, SHUT_WR, SHUT_RDWR) - if how in (SHUT_RD, SHUT_RDWR): - self.sock_impl.shutdownInput() - if how in (SHUT_WR, SHUT_RDWR): - self.sock_impl.shutdownOutput() - def close(self): try: if self.istream: @@ -1015,13 +1026,6 @@ def __del__(self): self.close() - def close(self): - try: - if self.sock_impl: - self.sock_impl.close() - except java.lang.Exception, jlx: - raise _map_exception(jlx) - _socketmethods = ( 'bind', 'connect', 'connect_ex', 'fileno', 'listen', 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', Copied: branches/pbcvm/Lib/test/bug1239.jar (from rev 6040, trunk/jython/Lib/test/bug1239.jar) =================================================================== (Binary files differ) Copied: branches/pbcvm/Lib/test/check_for_initializer_in_syspath.py (from rev 6040, trunk/jython/Lib/test/check_for_initializer_in_syspath.py) =================================================================== --- branches/pbcvm/Lib/test/check_for_initializer_in_syspath.py (rev 0) +++ branches/pbcvm/Lib/test/check_for_initializer_in_syspath.py 2009-02-24 04:57:56 UTC (rev 6044) @@ -0,0 +1,2 @@ +import sys +assert "/from_SyspathAppendingInitializer_with_love" in sys.path Copied: branches/pbcvm/Lib/test/import_star_from_java.py (from rev 6040, trunk/jython/Lib/test/import_star_from_java.py) =================================================================== --- branches/pbcvm/Lib/te... [truncated message content] |
From: <zy...@us...> - 2009-02-24 04:37:45
|
Revision: 6043 http://jython.svn.sourceforge.net/jython/?rev=6043&view=rev Author: zyasoft Date: 2009-02-24 04:37:39 +0000 (Tue, 24 Feb 2009) Log Message: ----------- Initialized merge tracking via "svnmerge" with revisions "1-5947" from https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython Property Changed: ---------------- branches/pbcvm/ Property changes on: branches/pbcvm ___________________________________________________________________ Added: svnmerge-integrated + /trunk/jython:1-5947 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-24 04:25:17
|
Revision: 6042 http://jython.svn.sourceforge.net/jython/?rev=6042&view=rev Author: zyasoft Date: 2009-02-24 04:25:13 +0000 (Tue, 24 Feb 2009) Log Message: ----------- LineCache implementation to support efficient setline edge detection, as necessary for tracing. No longer looks up the ThreadState twice, in the caller of PyBytecode#interpret and interpret itself, so this should provide for some optimization. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBaseCode.java branches/pbcvm/src/org/python/core/PyBytecode.java branches/pbcvm/src/org/python/core/PyTableCode.java Modified: branches/pbcvm/src/org/python/core/PyBaseCode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-22 14:40:49 UTC (rev 6041) +++ branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-24 04:25:13 UTC (rev 6042) @@ -86,7 +86,7 @@ PyObject ret; try { - ret = interpret(frame); + ret = interpret(frame, ts); } catch (Throwable t) { // Convert exceptions that occured in Java code to PyExceptions PyException pye = Py.JavaError(t); @@ -308,7 +308,7 @@ co_name, Py.idstr(this), co_filename, co_firstlineno); } - protected abstract PyObject interpret(PyFrame f); + protected abstract PyObject interpret(PyFrame f, ThreadState ts); protected int getline(PyFrame f) { return f.f_lineno; Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-22 14:40:49 UTC (rev 6041) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-24 04:25:13 UTC (rev 6042) @@ -84,7 +84,6 @@ co_code = getBytes(codestring); co_lnotab = getBytes(lnotab); } - private static final String[] __members__ = { "co_name", "co_argcount", "co_varnames", "co_filename", "co_firstlineno", @@ -217,9 +216,9 @@ return buf.toString(); } - private void print_debug(int count, int next_instr, int opcode, int oparg, PyStack stack, PyFrame f) { + private void print_debug(int count, int next_instr, int line, int opcode, int oparg, PyStack stack, PyFrame f) { if (debug) { - System.err.println(co_name + ":" + + System.err.println(co_name + " " + line + ":" + count + "," + f.f_lasti + "> " + get_opname().__getitem__(Py.newInteger(opcode)) + (opcode >= Opcode.HAVE_ARGUMENT ? " " + oparg : "") + @@ -249,14 +248,16 @@ } @Override - protected PyObject interpret(PyFrame f) { + protected PyObject interpret(PyFrame f, ThreadState ts) { final PyStack stack = new PyStack(); int next_instr = -1; int opcode; /* Current opcode */ int oparg = 0; /* Current opcode argument, if any */ Why why = Why.NOT; PyObject retval = null; - ThreadState ts = Py.getThreadState(); // XXX - change interpret to pass through from PyFrame since our ts will never change + LineCache lineCache = null; + int last_line = -1; + int line = 0; // XXX - optimization opportunities // 1. consider detaching the setting/getting of frame fields to improve performance, instead do this @@ -294,6 +295,19 @@ while (!debug || (maxCount == -1 || count < maxCount)) { // XXX - replace with while(true) + if (f.tracefunc != null || debug) { + if (lineCache == null) { + lineCache = new LineCache(); + if (debug) { + System.err.println("LineCache: " + lineCache.toString()); + } + } + line = lineCache.getline(next_instr); // XXX - should also return the range this is valid to avoid an unnecessary bisect + if (line != last_line) { + f.setline(line); + } + } + try { if (checkGeneratorInput) { @@ -310,11 +324,11 @@ next_instr += 2; oparg = (getUnsigned(co_code, next_instr) << 8) + getUnsigned(co_code, next_instr - 1); } - print_debug(count, next_instr, opcode, oparg, stack, f); + print_debug(count, next_instr, line, opcode, oparg, stack, f); count += 1; next_instr += 1; - f.f_lasti = next_instr; // should have no worries about needing co_lnotab, just keep this current + f.f_lasti = next_instr; switch (opcode) { case Opcode.NOP: @@ -1498,15 +1512,78 @@ return line; } + private class LineCache { + + private class Pair { + + private final int addr; + private final int line; + + private Pair(int a, int b) { + this.addr = a; + this.line = b; + } + + public String toString() { + return "(" + addr + "," + line + ")"; + } + } + List<Integer> addr_breakpoints = new ArrayList<Integer>(); + List<Integer> lines = new ArrayList<Integer>(); // length should be one more than addr_breakpoints + + private LineCache() { // based on dis.findlinestarts + + int size = co_lnotab.length / 2; + int p = 0; + int lastline = -1; + int line = co_firstlineno; + int addr = 0; + while (--size >= 0) { + int byte_incr = getUnsigned(co_lnotab, p++); + int line_incr = getUnsigned(co_lnotab, p++); + if (byte_incr > 0) { + if (line != lastline) { + addr_breakpoints.add(addr); + lines.add(line); + lastline = line; + } + addr += byte_incr; + } + line += line_incr; + } + if (line != lastline) { + lines.add(line); + } + } + + private int getline(int addrq) { // bisect_right to the lineno + + int lo = 0; + int hi = addr_breakpoints.size(); + while (lo < hi) { + int mid = (lo + hi) / 2; + if (addrq < addr_breakpoints.get(mid)) { + hi = mid; + } else { + lo = mid + 1; + } + } + return lines.get(lo); + } + + public String toString() { + return addr_breakpoints.toString() + ";" + lines.toString(); + } + } + // Utility functions to enable storage of unsigned bytes in co_code, co_lnotab byte[] arrays private static char getUnsigned(byte[] x, int i) { byte b = x[i]; if (b < 0) { - return (char)(b + 256); + return (char) (b + 256); + } else { + return (char) b; } - else { - return (char)b; - } } private static String getString(byte[] x) { @@ -1521,11 +1598,10 @@ int len = s.length(); byte[] x = new byte[len]; for (int i = 0; i < len; i++) { - x[i] = (byte)(s.charAt(i) & 0xFF); + x[i] = (byte) (s.charAt(i) & 0xFF); } return x; } - } Modified: branches/pbcvm/src/org/python/core/PyTableCode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyTableCode.java 2009-02-22 14:40:49 UTC (rev 6041) +++ branches/pbcvm/src/org/python/core/PyTableCode.java 2009-02-24 04:25:13 UTC (rev 6042) @@ -203,7 +203,7 @@ } @Override - protected PyObject interpret(PyFrame f) { + protected PyObject interpret(PyFrame f, ThreadState ts) { throw new UnsupportedOperationException("Inlined interpret to improve call performance (may want to reconsider in the future)."); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-22 14:40:53
|
Revision: 6041 http://jython.svn.sourceforge.net/jython/?rev=6041&view=rev Author: zyasoft Date: 2009-02-22 14:40:49 +0000 (Sun, 22 Feb 2009) Log Message: ----------- Inlined PyTableCode#interpret into #call(PyFrame,PyClosure) (which restores this method to what it was before the PBC-VM work). This change restores the performance as tested by test/pystone.py. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBaseCode.java branches/pbcvm/src/org/python/core/PyTableCode.java Modified: branches/pbcvm/src/org/python/core/PyBaseCode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-19 20:16:18 UTC (rev 6040) +++ branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-22 14:40:49 UTC (rev 6041) @@ -308,9 +308,7 @@ co_name, Py.idstr(this), co_filename, co_firstlineno); } - protected PyObject interpret(PyFrame f) { - throw new UnsupportedOperationException("interpret not supported"); - } + protected abstract PyObject interpret(PyFrame f); protected int getline(PyFrame f) { return f.f_lineno; Modified: branches/pbcvm/src/org/python/core/PyTableCode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyTableCode.java 2009-02-19 20:16:18 UTC (rev 6040) +++ branches/pbcvm/src/org/python/core/PyTableCode.java 2009-02-22 14:40:49 UTC (rev 6041) @@ -6,6 +6,8 @@ * is stored as a PyFunctionTable instance and an integer index. */ +import org.python.modules._systemrestart; + public class PyTableCode extends PyBaseCode { @@ -116,7 +118,92 @@ return super.__findattr_ex__(name); } - protected PyObject interpret(PyFrame frame) { - return funcs.call_function(func_id, frame); + @Override + public PyObject call(PyFrame frame, PyObject closure) { +// System.err.println("tablecode call: "+co_name); + ThreadState ts = Py.getThreadState(); + if (ts.systemState == null) { + ts.systemState = Py.defaultSystemState; + } + //System.err.println("got ts: "+ts+", "+ts.systemState); + + // Cache previously defined exception + PyException previous_exception = ts.exception; + + // Push frame + frame.f_back = ts.frame; + if (frame.f_builtins == null) { + if (frame.f_back != null) { + frame.f_builtins = frame.f_back.f_builtins; + } else { + //System.err.println("ts: "+ts); + //System.err.println("ss: "+ts.systemState); + frame.f_builtins = PySystemState.builtins; + } + } + // nested scopes: setup env with closure + // this should only be done once, so let the frame take care of it + frame.setupEnv((PyTuple)closure); + + ts.frame = frame; + + // Handle trace function for debugging + if (ts.tracefunc != null) { + frame.f_lineno = co_firstlineno; + frame.tracefunc = ts.tracefunc.traceCall(frame); + } + + // Handle trace function for profiling + if (ts.profilefunc != null) { + ts.profilefunc.traceCall(frame); + } + + PyObject ret; + try { + ret = funcs.call_function(func_id, frame); + } catch (Throwable t) { + // Convert exceptions that occured in Java code to PyExceptions + PyException pye = Py.JavaError(t); + pye.tracebackHere(frame); + + frame.f_lasti = -1; + + if (frame.tracefunc != null) { + frame.tracefunc.traceException(frame, pye); + } + if (ts.profilefunc != null) { + ts.profilefunc.traceException(frame, pye); + } + + // Rethrow the exception to the next stack frame + ts.exception = previous_exception; + ts.frame = ts.frame.f_back; + throw pye; + } + + if (frame.tracefunc != null) { + frame.tracefunc.traceReturn(frame, ret); + } + // Handle trace function for profiling + if (ts.profilefunc != null) { + ts.profilefunc.traceReturn(frame, ret); + } + + // Restore previously defined exception + ts.exception = previous_exception; + + ts.frame = ts.frame.f_back; + + // Check for interruption, which is used for restarting the interpreter + // on Jython + if (Thread.currentThread().isInterrupted()) { + throw new PyException(_systemrestart.SystemRestart); + } + return ret; } + + @Override + protected PyObject interpret(PyFrame f) { + throw new UnsupportedOperationException("Inlined interpret to improve call performance (may want to reconsider in the future)."); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-19 20:16:25
|
Revision: 6040 http://jython.svn.sourceforge.net/jython/?rev=6040&view=rev Author: fwierzbicki Date: 2009-02-19 20:16:18 +0000 (Thu, 19 Feb 2009) Log Message: ----------- Our handling of writes after a seek beyond the end of cStringIO was not consistent with CPython (or even our own use of StringIO). Found while trying to get Mercurial to respond to "hg log". Modified Paths: -------------- trunk/jython/Lib/test/test_StringIO_jy.py trunk/jython/src/org/python/modules/cStringIO.java Modified: trunk/jython/Lib/test/test_StringIO_jy.py =================================================================== --- trunk/jython/Lib/test/test_StringIO_jy.py 2009-02-18 13:21:13 UTC (rev 6039) +++ trunk/jython/Lib/test/test_StringIO_jy.py 2009-02-19 20:16:18 UTC (rev 6040) @@ -20,6 +20,14 @@ f.write('hi') self.assertEquals(f.getvalue(), 'hehio') + #XXX: this should get pushed to CPython's test_StringIO + def test_write_past_end(self): + f = cStringIO.StringIO() + f.write("abcdef") + f.seek(10) + f.write("uvwxyz") + self.assertEqual(f.getvalue(), 'abcdef\x00\x00\x00\x00uvwxyz') + def test_main(): test_support.run_unittest(TestUnicodeInput) test_support.run_unittest(TestWrite) Modified: trunk/jython/src/org/python/modules/cStringIO.java =================================================================== --- trunk/jython/src/org/python/modules/cStringIO.java 2009-02-18 13:21:13 UTC (rev 6039) +++ trunk/jython/src/org/python/modules/cStringIO.java 2009-02-19 20:16:18 UTC (rev 6040) @@ -350,12 +350,17 @@ int newpos = spos + s.length(); - if (newpos > slen) { - buf.replace(spos, slen - spos, s); - buf.append(s.substring(slen)); + if (spos < slen) { + if (newpos > slen) { + buf.replace(spos, slen - spos, s); + buf.append(s.substring(slen)); + slen = newpos; + } else { + buf.replace(spos, spos + s.length(), s); + } + } else { + buf.append(s); slen = newpos; - } else { - buf.replace(spos, spos + s.length(), s); } buf.setLength(slen); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-18 13:21:16
|
Revision: 6039 http://jython.svn.sourceforge.net/jython/?rev=6039&view=rev Author: amak Date: 2009-02-18 13:21:13 +0000 (Wed, 18 Feb 2009) Log Message: ----------- Updating the zip file for modjy 0.25.3; previously checked in version had the wrong version of modjy.jar in WEB-INF/lib. Modified Paths: -------------- trunk/jython/extlibs/modjy_0_25_3.zip Modified: trunk/jython/extlibs/modjy_0_25_3.zip =================================================================== (Binary files differ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-18 00:06:59
|
Revision: 6038 http://jython.svn.sourceforge.net/jython/?rev=6038&view=rev Author: zyasoft Date: 2009-02-18 00:06:55 +0000 (Wed, 18 Feb 2009) Log Message: ----------- Added support for code.getlineno (using co_lnotab), co_code, co_consts, co_lnotab. co_code and co_lnotab now use byte[] for a more compact representation and then manage accessing as if it were an unsigned representation. This enables a good chunk of the peak.util.assembler unit tests to pass; the remainder seem to have a dictionary traversal dependency. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBaseCode.java branches/pbcvm/src/org/python/core/PyBytecode.java branches/pbcvm/src/org/python/core/PyFrame.java Modified: branches/pbcvm/src/org/python/core/PyBaseCode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-17 02:38:09 UTC (rev 6037) +++ branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-18 00:06:55 UTC (rev 6038) @@ -311,4 +311,8 @@ protected PyObject interpret(PyFrame f) { throw new UnsupportedOperationException("interpret not supported"); } + + protected int getline(PyFrame f) { + return f.f_lineno; + } } Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-17 02:38:09 UTC (rev 6037) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-18 00:06:55 UTC (rev 6038) @@ -40,11 +40,11 @@ // end debugging public final static int CO_MAXBLOCKS = 20; // same as in CPython - public final char[] co_code; // widened to char to avoid signed byte issues + public final byte[] co_code; // widened to char to avoid signed byte issues public final PyObject[] co_consts; public final String[] co_names; public final int co_stacksize; // XXX - use to convert PyStack to use PyObject[] instead of ArrayList<PyObject> - public final String co_lnotab; + public final byte[] co_lnotab; private final static int CALL_FLAG_VAR = 1; private final static int CALL_FLAG_KW = 2; @@ -57,6 +57,8 @@ null, null); } + + // XXX - intern names HERE instead of in marshal public PyBytecode(int argcount, int nlocals, int stacksize, int flags, String codestring, PyObject[] constants, String[] names, String varnames[], String filename, String name, int firstlineno, String lnotab, @@ -79,9 +81,10 @@ co_stacksize = stacksize; co_consts = constants; co_names = names; - co_code = codestring.toCharArray(); - co_lnotab = lnotab; // ignore + co_code = getBytes(codestring); + co_lnotab = getBytes(lnotab); } + private static final String[] __members__ = { "co_name", "co_argcount", "co_varnames", "co_filename", "co_firstlineno", @@ -89,6 +92,7 @@ "co_code", "co_consts", "co_names", "co_lnotab", "co_stacksize" }; + @Override public PyObject __dir__() { PyString members[] = new PyString[__members__.length]; for (int i = 0; i < __members__.length; i++) { @@ -144,6 +148,15 @@ if (name == "co_name") { return new PyString(co_name); } + if (name == "co_code") { + return new PyString(getString(co_code)); + } + if (name == "co_lnotab") { + return new PyString(getString(co_lnotab)); + } + if (name == "co_consts") { + return new PyTuple(co_consts); + } return super.__findattr_ex__(name); } @@ -184,7 +197,7 @@ @Override public String toString() { - return exception.toString(); + return String.format("PyStackException<%s,%s,%.100s>", exception.type, exception.value, exception.traceback); } } @@ -207,9 +220,9 @@ private void print_debug(int count, int next_instr, int opcode, int oparg, PyStack stack, PyFrame f) { if (debug) { System.err.println(co_name + ":" + - count + "," + f.f_lasti + "> opcode: " + + count + "," + f.f_lasti + "> " + get_opname().__getitem__(Py.newInteger(opcode)) + - (opcode >= Opcode.HAVE_ARGUMENT ? ", oparg: " + oparg : "") + + (opcode >= Opcode.HAVE_ARGUMENT ? " " + oparg : "") + ", stack: " + stack.toString() + ", blocks: " + stringify_blocks(f)); } @@ -292,10 +305,10 @@ stack.push((PyObject) generatorInput); } - opcode = co_code[next_instr]; + opcode = getUnsigned(co_code, next_instr); if (opcode >= Opcode.HAVE_ARGUMENT) { next_instr += 2; - oparg = (co_code[next_instr] << 8) + co_code[next_instr - 1]; + oparg = (getUnsigned(co_code, next_instr) << 8) + getUnsigned(co_code, next_instr - 1); } print_debug(count, next_instr, opcode, oparg, stack, f); @@ -734,10 +747,10 @@ f.dellocal(co_names[oparg]); break; - case Opcode.UNPACK_SEQUENCE: + case Opcode.UNPACK_SEQUENCE: unpack_iterable(oparg, stack); break; - + case Opcode.STORE_ATTR: { PyObject obj = stack.pop(); PyObject v = stack.pop(); @@ -1112,9 +1125,9 @@ } case Opcode.EXTENDED_ARG: - opcode = co_code[next_instr++]; + opcode = getUnsigned(co_code, next_instr++); next_instr += 2; - oparg = oparg << 16 | ((co_code[next_instr] << 8) + co_code[next_instr - 1]); + oparg = oparg << 16 | ((getUnsigned(co_code, next_instr) << 8) + getUnsigned(co_code, next_instr - 1)); break; default: @@ -1172,19 +1185,18 @@ } stack.push(exc.traceback); stack.push(exc.value); - stack.push(new PyStackException(exc)); // instead of stack.push(exc.type);, like CPython + stack.push(new PyStackException(exc)); // instead of stack.push(exc.type), like CPython } else { if (why == Why.RETURN || why == Why.CONTINUE) { stack.push(retval); } - // FIGURE OUT THE FLOW HERE, in test_optparse2 stack.push(new PyStackWhy(why)); } why = Why.NOT; next_instr = b.b_handler; break; } - } // unwindstack + } // unwind block stack if (why != Why.NOT) { break; @@ -1413,8 +1425,38 @@ @Override public String toString() { - return stack.toString(); + StringBuilder buffer = new StringBuilder(); + int size = stack.size(); + int N = size > 4 ? 4 : size; + buffer.append("["); + for (int i = 0; i < N; i++) { + if (i > 0) { + buffer.append(", "); + } + PyObject item = stack.get(size - (i + 1)); + buffer.append(upto(item.__repr__().toString())); + } + if (N < size) { + buffer.append(String.format(", %d more...", size - N)); + } + buffer.append("]"); + return buffer.toString(); } + + private String upto(String x) { + return upto(x, 100); + } + + private String upto(String x, int n) { + x = x.replace('\n', '|'); + if (x.length() > n) { + StringBuilder item = new StringBuilder(x.substring(0, n)); + item.append("..."); + return item.toString(); + } else { + return x; + } + } } private class PyTryBlock extends PyObject { // purely to sit on top of the existing PyFrame in f_exits!!! @@ -1438,6 +1480,52 @@ b_handler + "," + b_level + ">"; } } + + @Override + protected int getline(PyFrame f) { + int addrq = f.f_lasti; + int size = co_lnotab.length / 2; + int p = 0; + int line = co_firstlineno; + int addr = 0; + while (--size >= 0) { + addr += getUnsigned(co_lnotab, p++); + if (addr > addrq) { + break; + } + line += getUnsigned(co_lnotab, p++); + } + return line; + } + + // Utility functions to enable storage of unsigned bytes in co_code, co_lnotab byte[] arrays + private static char getUnsigned(byte[] x, int i) { + byte b = x[i]; + if (b < 0) { + return (char)(b + 256); + } + else { + return (char)b; + } + } + + private static String getString(byte[] x) { + StringBuilder buffer = new StringBuilder(x.length); + for (int i = 0; i < x.length; i++) { + buffer.append(getUnsigned(x, i)); + } + return buffer.toString(); + } + + private static byte[] getBytes(String s) { + int len = s.length(); + byte[] x = new byte[len]; + for (int i = 0; i < len; i++) { + x[i] = (byte)(s.charAt(i) & 0xFF); + } + return x; + } + } Modified: branches/pbcvm/src/org/python/core/PyFrame.java =================================================================== --- branches/pbcvm/src/org/python/core/PyFrame.java 2009-02-17 02:38:09 UTC (rev 6037) +++ branches/pbcvm/src/org/python/core/PyFrame.java 2009-02-18 00:06:55 UTC (rev 6038) @@ -244,7 +244,7 @@ } public int getline() { - return f_lineno; + return f_code.getline(this); } public PyObject getlocal(int index) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-17 02:38:12
|
Revision: 6037 http://jython.svn.sourceforge.net/jython/?rev=6037&view=rev Author: fwierzbicki Date: 2009-02-17 02:38:09 +0000 (Tue, 17 Feb 2009) Log Message: ----------- Make the public adapters in AstAdapters final. Modified Paths: -------------- trunk/jython/src/org/python/antlr/adapter/AstAdapters.java Modified: trunk/jython/src/org/python/antlr/adapter/AstAdapters.java =================================================================== --- trunk/jython/src/org/python/antlr/adapter/AstAdapters.java 2009-02-16 15:41:50 UTC (rev 6036) +++ trunk/jython/src/org/python/antlr/adapter/AstAdapters.java 2009-02-17 02:38:09 UTC (rev 6037) @@ -10,15 +10,15 @@ * AstAdapter turns Python and Java objects into ast nodes. */ public class AstAdapters { - public static AliasAdapter aliasAdapter = new AliasAdapter(); - public static CmpopAdapter cmpopAdapter = new CmpopAdapter(); - public static ComprehensionAdapter comprehensionAdapter = new ComprehensionAdapter(); - public static ExcepthandlerAdapter excepthandlerAdapter = new ExcepthandlerAdapter(); - public static ExprAdapter exprAdapter = new ExprAdapter(); - public static IdentifierAdapter identifierAdapter = new IdentifierAdapter(); - public static KeywordAdapter keywordAdapter = new KeywordAdapter(); - public static SliceAdapter sliceAdapter = new SliceAdapter(); - public static StmtAdapter stmtAdapter = new StmtAdapter(); + public final static AliasAdapter aliasAdapter = new AliasAdapter(); + public final static CmpopAdapter cmpopAdapter = new CmpopAdapter(); + public final static ComprehensionAdapter comprehensionAdapter = new ComprehensionAdapter(); + public final static ExcepthandlerAdapter excepthandlerAdapter = new ExcepthandlerAdapter(); + public final static ExprAdapter exprAdapter = new ExprAdapter(); + public final static IdentifierAdapter identifierAdapter = new IdentifierAdapter(); + public final static KeywordAdapter keywordAdapter = new KeywordAdapter(); + public final static SliceAdapter sliceAdapter = new SliceAdapter(); + public final static StmtAdapter stmtAdapter = new StmtAdapter(); public static java.util.List<alias> py2aliasList(PyObject o) { return (java.util.List<alias>)aliasAdapter.iter2ast(o); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-16 15:41:52
|
Revision: 6036 http://jython.svn.sourceforge.net/jython/?rev=6036&view=rev Author: fwierzbicki Date: 2009-02-16 15:41:50 +0000 (Mon, 16 Feb 2009) Log Message: ----------- Useless double assignment. Modified Paths: -------------- trunk/jython/src/org/python/core/PySequence.java Modified: trunk/jython/src/org/python/core/PySequence.java =================================================================== --- trunk/jython/src/org/python/core/PySequence.java 2009-02-15 23:27:44 UTC (rev 6035) +++ trunk/jython/src/org/python/core/PySequence.java 2009-02-16 15:41:50 UTC (rev 6036) @@ -249,7 +249,7 @@ protected int boundToSequence(int index) { int length = __len__(); if(index < 0) { - index = index += length; + index += length; if(index < 0) { index = 0; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-02-15 23:27:48
|
Revision: 6035 http://jython.svn.sourceforge.net/jython/?rev=6035&view=rev Author: pjenvey Date: 2009-02-15 23:27:44 +0000 (Sun, 15 Feb 2009) Log Message: ----------- double antlr's conversiontimeout for slower environments, in particular the hudson buildot Modified Paths: -------------- trunk/jython/build.xml Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-02-15 18:35:41 UTC (rev 6034) +++ trunk/jython/build.xml 2009-02-15 23:27:44 UTC (rev 6035) @@ -426,6 +426,8 @@ <target name="antlr_gen" depends="prepare-output" unless="antlr.notneeded"> <java classname="org.antlr.Tool" failonerror="true" fork="true" dir="${jython.base.dir}"> + <arg value="-Xconversiontimeout"/> + <arg value="2000"/> <arg value="-fo"/> <arg path="${work.dir}/build/gensrc/org/python/antlr"/> <arg value="-lib"/> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <am...@us...> - 2009-02-15 18:35:44
|
Revision: 6034 http://jython.svn.sourceforge.net/jython/?rev=6034&view=rev Author: amak Date: 2009-02-15 18:35:41 +0000 (Sun, 15 Feb 2009) Log Message: ----------- A new version of modjy, containing some bug fixes kindly contributed by Philip Jenvey; see contained release notes for more information. http://github.com/pjenvey/modjy-pjenvey Added Paths: ----------- trunk/jython/extlibs/modjy_0_25_3.zip Removed Paths: ------------- trunk/jython/extlibs/modjy_0_25_2.zip Deleted: trunk/jython/extlibs/modjy_0_25_2.zip =================================================================== (Binary files differ) Added: trunk/jython/extlibs/modjy_0_25_3.zip =================================================================== (Binary files differ) Property changes on: trunk/jython/extlibs/modjy_0_25_3.zip ___________________________________________________________________ Added: svn:mime-type + application/octet-stream This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-15 18:31:31
|
Revision: 6033 http://jython.svn.sourceforge.net/jython/?rev=6033&view=rev Author: fwierzbicki Date: 2009-02-15 18:31:27 +0000 (Sun, 15 Feb 2009) Log Message: ----------- Fixed NPE found with FindBugs. Modified Paths: -------------- trunk/jython/src/org/python/core/AstList.java Modified: trunk/jython/src/org/python/core/AstList.java =================================================================== --- trunk/jython/src/org/python/core/AstList.java 2009-02-15 18:22:18 UTC (rev 6032) +++ trunk/jython/src/org/python/core/AstList.java 2009-02-15 18:31:27 UTC (rev 6033) @@ -586,9 +586,10 @@ public void pyset(int index, PyObject element) { if (adapter == null) { data.set(index, element); + } else { + Object o = adapter.py2ast(element); + data.set(index, o); } - Object o = adapter.py2ast(element); - data.set(index, o); } public Object remove(int index) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-15 18:22:22
|
Revision: 6032 http://jython.svn.sourceforge.net/jython/?rev=6032&view=rev Author: fwierzbicki Date: 2009-02-15 18:22:18 +0000 (Sun, 15 Feb 2009) Log Message: ----------- Fixed two infinite recursions found with FindBugs. Modified Paths: -------------- trunk/jython/src/org/python/modules/_csv/PyWriter.java trunk/jython/src/org/python/modules/thread/PyLocal.java Modified: trunk/jython/src/org/python/modules/_csv/PyWriter.java =================================================================== --- trunk/jython/src/org/python/modules/_csv/PyWriter.java 2009-02-15 14:23:28 UTC (rev 6031) +++ trunk/jython/src/org/python/modules/_csv/PyWriter.java 2009-02-15 18:22:18 UTC (rev 6032) @@ -51,7 +51,7 @@ "elements will be converted to string."); public void writerows(PyObject seqseq) { - writerows(seqseq); + writer_writerows(seqseq); } @ExposedMethod Modified: trunk/jython/src/org/python/modules/thread/PyLocal.java =================================================================== --- trunk/jython/src/org/python/modules/thread/PyLocal.java 2009-02-15 14:23:28 UTC (rev 6031) +++ trunk/jython/src/org/python/modules/thread/PyLocal.java 2009-02-15 18:22:18 UTC (rev 6032) @@ -72,7 +72,7 @@ @Override @ExposedSet(name = "__dict__") public void setDict(PyObject dict) { - setDict(dict); + super.setDict(dict); } @Override This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-15 14:23:31
|
Revision: 6031 http://jython.svn.sourceforge.net/jython/?rev=6031&view=rev Author: fwierzbicki Date: 2009-02-15 14:23:28 +0000 (Sun, 15 Feb 2009) Log Message: ----------- Fix potential infinite loop (and wrong slice behavior) -- found with FindBugs Modified Paths: -------------- trunk/jython/src/org/python/core/PyJavaType.java Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2009-02-14 19:17:04 UTC (rev 6030) +++ trunk/jython/src/org/python/core/PyJavaType.java 2009-02-15 14:23:28 UTC (rev 6031) @@ -845,6 +845,7 @@ int j = 0; for (int i = start; j < n; i += step) { newList.add(list.get(i)); + j++; } return Py.java2py(newList); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-14 19:17:07
|
Revision: 6030 http://jython.svn.sourceforge.net/jython/?rev=6030&view=rev Author: zyasoft Date: 2009-02-14 19:17:04 +0000 (Sat, 14 Feb 2009) Log Message: ----------- Fixed UNPACK_SEQUENCE so that ValueError is raised if too few/too many values to unpack. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 11:58:48 UTC (rev 6029) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 19:17:04 UTC (rev 6030) @@ -734,22 +734,10 @@ f.dellocal(co_names[oparg]); break; - case Opcode.UNPACK_SEQUENCE: { - PyObject v = stack.pop(); - int i = 0; - PyObject items[] = new PyObject[oparg]; - for (PyObject item : v.asIterable()) { - items[i++] = item; - if (i > oparg) { - break; - } - } - for (i = i - 1; i >= 0; i--) { - stack.push(items[i]); - } + case Opcode.UNPACK_SEQUENCE: + unpack_iterable(oparg, stack); break; - } - + case Opcode.STORE_ATTR: { PyObject obj = stack.pop(); PyObject v = stack.pop(); @@ -1319,6 +1307,26 @@ stack.push(callable._callextra(args, keywords, starargs, kwargs)); } + private static void unpack_iterable(int oparg, PyStack stack) { + PyObject v = stack.pop(); + int i = oparg; + PyObject items[] = new PyObject[oparg]; + for (PyObject item : v.asIterable()) { + if (i <= 0) { + throw Py.ValueError("too many values to unpack"); + } + i--; + items[i] = item; + } + if (i > 0) { + throw Py.ValueError(String.format("need more than %d value%s to unpack", + i, i == 1 ? "" : "s")); + } + for (i = 0; i < oparg; i++) { + stack.push(items[i]); + } + } + // XXX - perhaps add support for max stack size (presumably from co_stacksize) // and capacity hints private class PyStack { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-14 13:22:20
|
Revision: 6029 http://jython.svn.sourceforge.net/jython/?rev=6029&view=rev Author: zyasoft Date: 2009-02-14 11:58:48 +0000 (Sat, 14 Feb 2009) Log Message: ----------- Fixed WITH_CLEANUP so that it properly calls __exit__ with the exception components off the stack. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 11:17:17 UTC (rev 6028) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 11:58:48 UTC (rev 6029) @@ -686,7 +686,7 @@ PyObject locals = stack.pop(); PyObject globals = stack.pop(); PyObject code = stack.pop(); - Py.exec(code, globals==Py.None ? null : globals, locals==Py.None ? null : locals); + Py.exec(code, globals == Py.None ? null : globals, locals == Py.None ? null : locals); break; } @@ -1048,12 +1048,19 @@ v = stack.top(3); w = stack.top(4); } - PyObject x = exit.__call__(u, v, w); - if (u != Py.None && x.__nonzero__()) { - stack.popN(4); + PyObject x = null; + if (u instanceof PyStackException) { + PyException exc = ((PyStackException) u).exception; + x = exit.__call__(exc.type, exc.value, exc.traceback); + } else { + x = exit.__call__(u, v, w); + } + + if (u != Py.None && x != null && x.__nonzero__()) { + stack.popN(4); // XXX - consider stack.stackadj op stack.push(Py.None); } else { - stack.pop(); + stack.pop(); // this should be popping off a block } break; } @@ -1087,8 +1094,8 @@ PyCode code = (PyCode) stack.pop(); PyObject[] defaults = stack.popN(oparg); PyObject doc = null; - if (code instanceof PyBytecode && ((PyBytecode)code).co_consts.length > 0) { - doc = ((PyBytecode)code).co_consts[0]; + if (code instanceof PyBytecode && ((PyBytecode) code).co_consts.length > 0) { + doc = ((PyBytecode) code).co_consts[0]; } PyFunction func = new PyFunction(f.f_globals, defaults, code, doc); stack.push(func); @@ -1100,8 +1107,8 @@ PyObject[] closure_cells = ((PySequenceList) (stack.pop())).getArray(); PyObject[] defaults = stack.popN(oparg); PyObject doc = null; - if (code instanceof PyBytecode && ((PyBytecode)code).co_consts.length > 0) { - doc = ((PyBytecode)code).co_consts[0]; + if (code instanceof PyBytecode && ((PyBytecode) code).co_consts.length > 0) { + doc = ((PyBytecode) code).co_consts[0]; } PyFunction func = new PyFunction(f.f_globals, defaults, code, doc, closure_cells); stack.push(func); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-14 11:17:22
|
Revision: 6028 http://jython.svn.sourceforge.net/jython/?rev=6028&view=rev Author: zyasoft Date: 2009-02-14 11:17:17 +0000 (Sat, 14 Feb 2009) Log Message: ----------- LOAD_DEREF needs to do the same lookup as LOAD_CLOSURE. Populate doc strings for MAKE_CLOSURE. Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 10:44:17 UTC (rev 6027) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 11:17:17 UTC (rev 6028) @@ -815,7 +815,36 @@ } case Opcode.LOAD_DEREF: { - stack.push(f.getderef(oparg)); + // common code from LOAD_CLOSURE + PyCell cell = (PyCell) (f.getclosure(oparg)); + if (cell.ob_ref == null) { + String name; + if (oparg >= co_cellvars.length) { + name = co_freevars[oparg - co_cellvars.length]; + } else { + name = co_cellvars[oparg]; + } + // XXX - consider some efficient lookup mechanism, like a hash :), + // at least if co_varnames is much greater than say a certain + // size (but i would think, it's not going to happen in real code. profile?) + if (f.f_fastlocals != null) { + int i = 0; + boolean matched = false; + for (String match : co_varnames) { + if (match.equals(name)) { + matched = true; + break; + } + i++; + } + if (matched) { + cell.ob_ref = f.f_fastlocals[i]; + } + } else { + cell.ob_ref = f.f_locals.__finditem__(name); + } + } + stack.push(cell.ob_ref); break; } @@ -1070,7 +1099,11 @@ PyCode code = (PyCode) stack.pop(); PyObject[] closure_cells = ((PySequenceList) (stack.pop())).getArray(); PyObject[] defaults = stack.popN(oparg); - PyFunction func = new PyFunction(f.f_globals, defaults, code, closure_cells); + PyObject doc = null; + if (code instanceof PyBytecode && ((PyBytecode)code).co_consts.length > 0) { + doc = ((PyBytecode)code).co_consts[0]; + } + PyFunction func = new PyFunction(f.f_globals, defaults, code, doc, closure_cells); stack.push(func); break; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-14 10:44:19
|
Revision: 6027 http://jython.svn.sourceforge.net/jython/?rev=6027&view=rev Author: zyasoft Date: 2009-02-14 10:44:17 +0000 (Sat, 14 Feb 2009) Log Message: ----------- Set the traceback when intercepting an exception. Set __doc__ strings for functions from the constant pool (if available). Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 09:23:58 UTC (rev 6026) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 10:44:17 UTC (rev 6027) @@ -835,9 +835,11 @@ stack.push(new PyDictionary()); break; - case Opcode.LOAD_ATTR: - stack.push(stack.pop().__getattr__(co_names[oparg])); + case Opcode.LOAD_ATTR: { + String name = co_names[oparg]; + stack.push(stack.pop().__getattr__(name)); break; + } case Opcode.COMPARE_OP: { PyObject b = stack.pop(); @@ -1055,7 +1057,11 @@ case Opcode.MAKE_FUNCTION: { PyCode code = (PyCode) stack.pop(); PyObject[] defaults = stack.popN(oparg); - PyFunction func = new PyFunction(f.f_globals, defaults, code); + PyObject doc = null; + if (code instanceof PyBytecode && ((PyBytecode)code).co_consts.length > 0) { + doc = ((PyBytecode)code).co_consts[0]; + } + PyFunction func = new PyFunction(f.f_globals, defaults, code, doc); stack.push(func); break; } @@ -1093,7 +1099,7 @@ } // end switch } // end try catch (Throwable t) { - PyException pye = Py.JavaError(t); + PyException pye = Py.setException(t, f); why = Why.EXCEPTION; ts.exception = pye; if (debug) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-14 09:24:01
|
Revision: 6026 http://jython.svn.sourceforge.net/jython/?rev=6026&view=rev Author: zyasoft Date: 2009-02-14 09:23:58 +0000 (Sat, 14 Feb 2009) Log Message: ----------- Now interns names in co_names, etc., when unmarshalling, which fixes equality tests on special case like __class__, __dict__, etc. Fix instanceof test in eval and exec so that it looks at PyBaseCode, not PyTableCode. Also, exec cannot be called with Py.None instead of null. Fixes test_scope. Modified Paths: -------------- branches/pbcvm/src/org/python/core/Py.java branches/pbcvm/src/org/python/core/PyBaseCode.java branches/pbcvm/src/org/python/core/PyBytecode.java branches/pbcvm/src/org/python/core/__builtin__.java branches/pbcvm/src/org/python/modules/_marshal.java Modified: branches/pbcvm/src/org/python/core/Py.java =================================================================== --- branches/pbcvm/src/org/python/core/Py.java 2009-02-12 04:38:04 UTC (rev 6025) +++ branches/pbcvm/src/org/python/core/Py.java 2009-02-14 09:23:58 UTC (rev 6026) @@ -1211,14 +1211,14 @@ int flags = 0; if (o instanceof PyCode) { code = (PyCode) o; - if (locals == null && o instanceof PyTableCode && ((PyTableCode) o).hasFreevars()) { + if (locals == null && o instanceof PyBaseCode && ((PyBaseCode) o).hasFreevars()) { throw Py.TypeError("code object passed to exec may not contain free variables"); } } else { String contents = null; if (o instanceof PyString) { if (o instanceof PyUnicode) { - flags |= PyTableCode.PyCF_SOURCE_IS_UTF8; + flags |= PyBaseCode.PyCF_SOURCE_IS_UTF8; } contents = o.toString(); } else if (o instanceof PyFile) { Modified: branches/pbcvm/src/org/python/core/PyBaseCode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-12 04:38:04 UTC (rev 6025) +++ branches/pbcvm/src/org/python/core/PyBaseCode.java 2009-02-14 09:23:58 UTC (rev 6026) @@ -18,7 +18,7 @@ public boolean varargs, varkwargs; final public static int CO_OPTIMIZED = 0x0001; - //final public static int CO_NEWLOCALS = 0x0002 + final public static int CO_NEWLOCALS = 0x0002; final public static int CO_VARARGS = 0x0004; final public static int CO_VARKEYWORDS = 0x0008; final public static int CO_GENERATOR = 0x0020; Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-12 04:38:04 UTC (rev 6025) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-14 09:23:58 UTC (rev 6026) @@ -44,7 +44,7 @@ public final PyObject[] co_consts; public final String[] co_names; public final int co_stacksize; // XXX - use to convert PyStack to use PyObject[] instead of ArrayList<PyObject> - public final String co_lnotab; // ignored for now + public final String co_lnotab; private final static int CALL_FLAG_VAR = 1; private final static int CALL_FLAG_KW = 2; @@ -686,7 +686,7 @@ PyObject locals = stack.pop(); PyObject globals = stack.pop(); PyObject code = stack.pop(); - Py.exec(code, globals, locals); + Py.exec(code, globals==Py.None ? null : globals, locals==Py.None ? null : locals); break; } @@ -711,10 +711,6 @@ why = Why.RERAISE; } else if (v instanceof PyString) { - if (debug) { - System.err.println("ts.exception=" + ts.exception + ", v=" + v); -// ts.exception = new PyException(v); // XXX - what do I do about string exceptions??!!! - } why = Why.RERAISE; } else if (v != Py.None) { throw Py.SystemError("'finally' pops bad exception"); @@ -794,8 +790,9 @@ } else { name = co_cellvars[oparg]; } -// System.err.println("Loading closure: " + name); - // XXX - consider some efficient lookup mechanism, like a hash + // XXX - consider some efficient lookup mechanism, like a hash :), + // at least if co_varnames is much greater than say a certain + // size (but i would think, it's not going to happen in real code. profile?) if (f.f_fastlocals != null) { int i = 0; boolean matched = false; Modified: branches/pbcvm/src/org/python/core/__builtin__.java =================================================================== --- branches/pbcvm/src/org/python/core/__builtin__.java 2009-02-12 04:38:04 UTC (rev 6025) +++ branches/pbcvm/src/org/python/core/__builtin__.java 2009-02-14 09:23:58 UTC (rev 6026) @@ -506,7 +506,7 @@ } public static PyObject eval(PyObject o) { - if (o instanceof PyTableCode && ((PyTableCode) o).hasFreevars()) { + if (o instanceof PyBaseCode && ((PyBaseCode) o).hasFreevars()) { throw Py.TypeError("code object passed to eval() may not contain free variables"); } return eval(o, null, null); Modified: branches/pbcvm/src/org/python/modules/_marshal.java =================================================================== --- branches/pbcvm/src/org/python/modules/_marshal.java 2009-02-12 04:38:04 UTC (rev 6025) +++ branches/pbcvm/src/org/python/modules/_marshal.java 2009-02-14 09:23:58 UTC (rev 6026) @@ -235,7 +235,7 @@ write_strings(code.co_cellvars, depth + 1); write_object(Py.newString(code.co_name), depth + 1); write_int(code.co_firstlineno); - write_object(new PyTuple(code.co_lnotab), depth + 1); + write_object(Py.newString(new String(code.co_lnotab)), depth + 1); } else { write_byte(TYPE_UNKNOWN); } @@ -353,7 +353,7 @@ String some_strings[] = new String[t.__len__()]; int i = 0; for (PyObject item : t.asIterable()) { - some_strings[i++] = item.toString(); + some_strings[i++] = item.toString().intern(); } return some_strings; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-12 04:38:08
|
Revision: 6025 http://jython.svn.sourceforge.net/jython/?rev=6025&view=rev Author: zyasoft Date: 2009-02-12 04:38:04 +0000 (Thu, 12 Feb 2009) Log Message: ----------- Beginning of PBC compilation support. Pretty much a shell for now. Added Paths: ----------- branches/pbcvm/src/org/python/compiler/pbc/ branches/pbcvm/src/org/python/compiler/pbc/Bytecode.java branches/pbcvm/src/org/python/compiler/pbc/BytecodeCompiler.java Added: branches/pbcvm/src/org/python/compiler/pbc/Bytecode.java =================================================================== --- branches/pbcvm/src/org/python/compiler/pbc/Bytecode.java (rev 0) +++ branches/pbcvm/src/org/python/compiler/pbc/Bytecode.java 2009-02-12 04:38:04 UTC (rev 6025) @@ -0,0 +1,555 @@ +package org.python.compiler.pbc; + +// Copyright (c) 2009 Jython Developers +// +// ported from peak.util.assembler (BytecodeAssembler): +// Copyright (C) 1996-2004 by Phillip J. Eby and Tyler C. Sarna. +// All rights reserved. This software may be used under the same terms +// as Zope or Python. (http://cvs.eby-sarna.com/*checkout*/PEAK/README.txt) +// +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.python.core.Py; +import org.python.core.Opcode; +import org.python.core.PyBaseCode; +import org.python.core.PyBytecode; +import org.python.core.PyObject; + +// must be thread confined, as might be expected +class Bytecode { + + private int co_argcount = 0; + private int co_stacksize = 0; + private int co_flags = PyBaseCode.CO_OPTIMIZED | PyBaseCode.CO_NEWLOCALS; // typical usage +// co_filename = '<generated code>' +// co_name = '<lambda>' +// co_firstlineno = 0 +// co_freevars = () +// co_cellvars = () +// _last_lineofs = 0 +// _ss = 0 +// _tmp_level = 0 +// + + public Bytecode(int co_flags) { + this.co_flags = co_flags; + co_const.put(Py.None, 0); + } +// def __init__(self): +// self.co_code = array('B') +// self.co_names = [] +// self.co_varnames = [] +// self.blocks = [] + private final List<Integer> stack_history = new ArrayList<Integer>(); + private final List<Integer> co_code = new ArrayList<Integer>(); + private final List<Integer> co_lnotab = new ArrayList<Integer>(); + private final Map<PyObject, Integer> co_const = new HashMap<PyObject, Integer>(); + + private void emit(int opcode) { + assert (opcode >= 0 && opcode <= 0xFF); // at this point we should verify we are only emitting unsigned bytes + co_code.add(opcode); + } + + private void emit(int opcode, int oparg) { + if (oparg > 0xFFFF) { + emit(Opcode.EXTENDED_ARG); + emit((oparg >> 16) & 0xFF); + emit((oparg >> 24) & 0xFF); + } + emit(opcode); + emit(oparg & 0xFF); + emit((oparg >> 8) & 0xFF); + } + + private void LOAD_CONST(PyObject constant) { + int arg; + if (co_const.containsKey(constant)) { + arg = co_const.get(constant); + } else { + arg = co_const.size() + 1; + co_const.put(constant, arg); + } + stackchange(0, 1); + emit(Opcode.LOAD_CONST, arg); + } + + private void RETURN_VALUE() { + stackchange(1, 0); + emit(Opcode.RETURN_VALUE); + stack_unknown(); + } + + public void code() { +// return new PyBytecode(); + } + + +// + +// +// def locals_written(self): +// vn = self.co_varnames +// hl = dict.fromkeys([STORE_FAST, DELETE_FAST]) +// return dict.fromkeys([vn[arg] for ofs, op, arg in self if op in hl]) +// +// +// +// + public void set_lineno(int lno) { +// if not self.co_firstlineno: +// self.co_firstlineno = self._last_line = lno +// return +// +// append = self.co_lnotab.append +// incr_line = lno - self._last_line +// incr_addr = len(self.co_code) - self._last_lineofs +// if not incr_line: +// return +// +// assert incr_addr>=0 and incr_line>=0 +// +// while incr_addr>255: +// append(255) +// append(0) +// incr_addr -= 255 +// +// while incr_line>255: +// append(incr_addr) +// append(255) +// incr_line -= 255 +// incr_addr = 0 +// +// if incr_addr or incr_line: +// append(incr_addr) +// append(incr_line) +// +// self._last_line = lno +// self._last_lineofs = len(self.co_code) + } + // + + public void YIELD_VALUE() { + stackchange(1, 1); + co_flags |= PyBaseCode.CO_GENERATOR; + emit(Opcode.YIELD_VALUE); + } + + // defaults? def CALL_FUNCTION(self, argc=0, kwargc=0, op=CALL_FUNCTION, extra=0): + public void CALL_FUNCTION(int argc, int kwargc, int op, int extra) { + stackchange(1 + argc + 2 * kwargc + extra, 1); + emit(op); + emit(argc); + emit(kwargc); + } +// +// def CALL_FUNCTION_VAR(self, argc=0, kwargc=0): +// self.CALL_FUNCTION(argc,kwargc,CALL_FUNCTION_VAR, 1) # 1 for *args +// +// def CALL_FUNCTION_KW(self, argc=0, kwargc=0): +// self.CALL_FUNCTION(argc,kwargc,CALL_FUNCTION_KW, 1) # 1 for **kw +// +// def CALL_FUNCTION_VAR_KW(self, argc=0, kwargc=0): +// self.CALL_FUNCTION(argc,kwargc,CALL_FUNCTION_VAR_KW, 2) # 2 *args,**kw +// + + public void BUILD_TUPLE(int count) { + stackchange(count, 1); + emit(Opcode.BUILD_TUPLE, count); + } + + public void BUILD_LIST(int count) { + stackchange(count, 1); + emit(Opcode.BUILD_LIST, count); + } + + public void UNPACK_SEQUENCE(int count) { + stackchange(1, count); + emit(Opcode.UNPACK_SEQUENCE, count); + } + + public void BUILD_SLICE(int count) { + if (count != 2 && count != 3) { + throw Py.AssertionError("Invalid number of arguments for BUILD_SLICE"); + } + stackchange(count, 1); + emit(Opcode.BUILD_SLICE, count); + } + + public void DUP_TOPX(int count) { + stackchange(count, count * 2); + emit(Opcode.DUP_TOPX, count); + } + + public void RAISE_VARARGS(int argc) { + if (0 <= argc && argc <= 3) { + throw Py.AssertionError("Invalid number of arguments for RAISE_VARARGS"); + } + stackchange(argc, 0); + emit(Opcode.RAISE_VARARGS, argc); + } + + public void MAKE_FUNCTION(int ndefaults) { + stackchange(1 + ndefaults, 1); + emit(Opcode.MAKE_FUNCTION, ndefaults); + } + + public void MAKE_CLOSURE(int ndefaults, int freevars) { + freevars = 1; + stackchange(1 + freevars + ndefaults, 1); + emit(Opcode.MAKE_CLOSURE, ndefaults); + } + + public int here() { + return co_code.size(); + } + private int _ss = -1; + + public void set_stack_size(int size) { + if (size < 0) { + throw Py.AssertionError("Stack underflow"); + } + if (size > co_stacksize) { + co_stacksize = size; + } + int bytes = co_code.size() - stack_history.size() + 1; + if (bytes > 0) { + for (int i = 0; i < bytes; i++) { + stack_history.add(_ss); + } + } + _ss = size; + } + + public int get_stack_size() { + return _ss; + } + + public void stackchange(int inputs, int outputs) { + if (_ss == -1) { + throw Py.AssertionError("Unknown stack size at this location"); + } + set_stack_size(get_stack_size() - inputs); // check underflow + set_stack_size(get_stack_size() + outputs); // update maximum height + + } + + public void stack_unknown() { + _ss = -1; + } + + public void branch_stack(int location, int expected) { + if (location >= stack_history.size()) { + if (location > co_code.size()) { + throw Py.AssertionError(String.format( + "Forward-looking stack prediction! %d, %d", location, co_code.size())); + } + int actual = get_stack_size(); + if (actual == -1) { + actual = expected; + set_stack_size(actual); + stack_history.set(location, actual); + } else { + actual = stack_history.get(location); + if (actual == -1) { + actual = expected; + stack_history.set(location, actual); + } + if (actual != expected) { + throw Py.AssertionError(String.format( + "Stack level mismatch: actual=%d expected=%d", actual, expected)); + } + } + } + } + +// def jump(self, op, arg=None): +// def jump_target(offset): +// target = offset +// if op not in hasjabs: +// target = target - (posn+3) +// assert target>=0, "Relative jumps can't go backwards" +// if target>0xFFFF: +// target = offset - (posn+6) +// return target +// +// def backpatch(offset): +// target = jump_target(offset) +// if target>0xFFFF: +// raise AssertionError("Forward jump span must be <64K bytes") +// self.patch_arg(posn, 0, target) +// self.branch_stack(offset, old_level) +// +// if op==FOR_ITER: +// old_level = self.stack_size = self.stack_size - 1 +// self.stack_size += 2 +// else: +// old_level = self.stack_size +// posn = self.here() +// +// if arg is not None: +// self.emit_arg(op, jump_target(arg)) +// self.branch_stack(arg, old_level) +// lbl = None +// else: +// self.emit_arg(op, 0) +// def lbl(code=None): +// backpatch(self.here()) +// if op in (JUMP_FORWARD, JUMP_ABSOLUTE, CONTINUE_LOOP): +// self.stack_unknown() +// return lbl +// +// def COMPARE_OP(self, op): +// self.stackchange((2,1)) +// self.emit_arg(COMPARE_OP, compares[op]) +// +// +// def setup_block(self, op): +// jmp = self.jump(op) +// self.blocks.append((op,self.stack_size,jmp)) +// return jmp +// +// def SETUP_EXCEPT(self): +// ss = self.stack_size +// self.stack_size = ss+3 # simulate the level at "except:" time +// self.setup_block(SETUP_EXCEPT) +// self.stack_size = ss # restore the current level +// +// def SETUP_FINALLY(self): +// ss = self.stack_size +// self.stack_size = ss+3 # allow for exceptions +// self.stack_size = ss+1 # simulate the level after the None is pushed +// self.setup_block(SETUP_FINALLY) +// self.stack_size = ss # restore original level +// +// def SETUP_LOOP(self): +// self.setup_block(SETUP_LOOP) +// +// def POP_BLOCK(self): +// if not self.blocks: +// raise AssertionError("Not currently in a block") +// +// why, level, fwd = self.blocks.pop() +// self.emit(POP_BLOCK) +// +// if why!=SETUP_LOOP: +// if why==SETUP_FINALLY: +// self.LOAD_CONST(None) +// fwd() +// else: +// self.stack_size = level-3 # stack level resets here +// else_ = self.JUMP_FORWARD() +// fwd() +// return else_ +// else: +// return fwd +// +// +// def assert_loop(self): +// for why,level,fwd in self.blocks: +// if why==SETUP_LOOP: +// return +// raise AssertionError("Not inside a loop") +// +// def BREAK_LOOP(self): +// self.assert_loop(); self.emit(BREAK_LOOP) +// self.stack_unknown() +// +// def CONTINUE_LOOP(self, label): +// self.assert_loop() +// if self.blocks[-1][0]==SETUP_LOOP: +// op = JUMP_ABSOLUTE # more efficient if not in a nested block +// else: +// op = CONTINUE_LOOP +// return self.jump(op, label) +// +// def __call__(self, *args): +// last = None +// for ob in args: +// if callable(ob): +// last = ob(self) +// else: +// try: +// f = generate_types[type(ob)] +// except KeyError: +// raise TypeError("Can't generate", ob) +// else: +// last = f(self, ob) +// return last +// +// def return_(self, ob=None): +// return self(ob, Code.RETURN_VALUE) +// +// decorate(classmethod) +// def from_function(cls, function, copy_lineno=False): +// code = cls.from_code(function.func_code, copy_lineno) +// return code +// +// +// decorate(classmethod) +// def from_code(cls, code, copy_lineno=False): +// import inspect +// self = cls.from_spec(code.co_name, *inspect.getargs(code)) +// if copy_lineno: +// self.set_lineno(code.co_firstlineno) +// self.co_filename = code.co_filename +// self.co_freevars = code.co_freevars # XXX untested! +// return self +// +// decorate(classmethod) +// def from_spec(cls, name='<lambda>', args=(), var=None, kw=None): +// self = cls() +// self.co_name = name +// self.co_argcount = len(args) +// self.co_varnames.extend(args) +// if var: +// self.co_varnames.append(var) +// self.co_flags |= CO_VARARGS +// if kw: +// self.co_varnames.append(kw) +// self.co_flags |= CO_VARKEYWORDS +// +// def tuple_arg(args): +// self.UNPACK_SEQUENCE(len(args)) +// for arg in args: +// if not isinstance(arg, basestring): +// tuple_arg(arg) +// else: +// self.STORE_FAST(arg) +// +// for narg, arg in enumerate(args): +// if not isinstance(arg, basestring): +// dummy_name = '.'+str(narg) +// self.co_varnames[narg] = dummy_name +// self.LOAD_FAST(dummy_name) +// tuple_arg(arg) +// +// return self +// +// +// def patch_arg(self, offset, oldarg, newarg): +// code = self.co_code +// if (oldarg>0xFFFF) != (newarg>0xFFFF): +// raise AssertionError("Can't change argument size", oldarg, newarg) +// code[offset+1] = newarg & 255 +// code[offset+2] = (newarg>>8) & 255 +// if newarg>0xFFFF: +// newarg >>=16 +// code[offset-2] = newarg & 255 +// code[offset-1] = (newarg>>8) & 255 +// +// def nested(self, name='<lambda>', args=(), var=None, kw=None, cls=None): +// if cls is None: +// cls = Code +// code = cls.from_spec(name, args, var, kw) +// code.co_filename=self.co_filename +// return code +// +// def __iter__(self): +// i = 0 +// extended_arg = 0 +// code = self.co_code +// n = len(code) +// while i < n: +// op = code[i] +// if op >= HAVE_ARGUMENT: +// oparg = code[i+1] + code[i+2]*256 + extended_arg +// extended_arg = 0 +// if op == EXTENDED_ARG: +// extended_arg = oparg*65536 +// i+=3 +// continue +// yield i, op, oparg +// i += 3 +// else: +// yield i, op, None +// i += 1 +// +// +// +// +// def makefree(self, names): +// nowfree = dict.fromkeys(self.co_freevars) +// newfree = [n for n in names if n not in nowfree] +// if newfree: +// self.co_freevars += tuple(newfree) +// self._locals_to_cells() +// +// def makecells(self, names): +// nowcells = dict.fromkeys(self.co_cellvars+self.co_freevars) +// newcells = [n for n in names if n not in nowcells] +// if newcells: +// if not (self.co_flags & CO_OPTIMIZED): +// raise AssertionError("Can't use cellvars in unoptimized scope") +// cc = len(self.co_cellvars) +// nc = len(newcells) +// self.co_cellvars += tuple(newcells) +// if self.co_freevars: +// self._patch( +// deref_to_deref, +// dict([(n+cc,n+cc+nc)for n in range(len(self.co_freevars))]) +// ) +// self._locals_to_cells() +// +// def _locals_to_cells(self): +// freemap = dict( +// [(n,p) for p,n in enumerate(self.co_cellvars+self.co_freevars)] +// ) +// argmap = dict( +// [(p,freemap[n]) for p,n in enumerate(self.co_varnames) +// if n in freemap] +// ) +// if argmap: +// for ofs, op, arg in self: +// if op==DELETE_FAST and arg in argmap: +// raise AssertionError( +// "Can't delete local %r used in nested scope" +// % self.co_varnames[arg] +// ) +// self._patch(fast_to_deref, argmap) +// +// +// def _patch(self, opmap, argmap={}): +// code = self.co_code +// for ofs, op, arg in self: +// if op in opmap: +// if arg in argmap: +// self.patch_arg(ofs, arg, argmap[arg]) +// elif arg is not None: +// continue +// code[ofs] = opmap[op] +// +// def code(self, parent=None): +// if self.blocks: +// raise AssertionError("%d unclosed block(s)" % len(self.blocks)) +// +// flags = self.co_flags & ~CO_NOFREE +// if parent is not None: +// locals_written = self.locals_written() +// self.makefree([ +// n for n in self.co_varnames[ +// self.co_argcount +// + ((self.co_flags & CO_VARARGS)==CO_VARARGS) +// + ((self.co_flags & CO_VARKEYWORDS)==CO_VARKEYWORDS) +// : +// ] if n not in locals_written +// ]) +// +// if not self.co_freevars and not self.co_cellvars: +// flags |= CO_NOFREE +// elif parent is not None and self.co_freevars: +// parent.makecells(self.co_freevars) +// +// return code( +// self.co_argcount, len(self.co_varnames), +// self.co_stacksize, flags, self.co_code.tostring(), +// tuple(self.co_consts), tuple(self.co_names), +// tuple(self.co_varnames), +// self.co_filename, self.co_name, self.co_firstlineno, +// self.co_lnotab.tostring(), self.co_freevars, self.co_cellvars +// ) +} Added: branches/pbcvm/src/org/python/compiler/pbc/BytecodeCompiler.java =================================================================== --- branches/pbcvm/src/org/python/compiler/pbc/BytecodeCompiler.java (rev 0) +++ branches/pbcvm/src/org/python/compiler/pbc/BytecodeCompiler.java 2009-02-12 04:38:04 UTC (rev 6025) @@ -0,0 +1,2419 @@ +// Copyright (c) Corporation for National Research Initiatives + +// should be able to use ScopesCompiler pretty much *as is* + +// needs to construct a PyBytecode object, include +// co_code, co_const, co_cellvars, co_freevars, co_stacksize (use something like compile.opcode_stack_effect) +// etc. + +package org.python.compiler.pbc; + +import org.python.compiler.*; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +//import java.util.Stack; +//import java.util.Vector; +// +import org.python.antlr.ParseException; +import org.python.antlr.PythonTree; +import org.python.antlr.Visitor; +import org.python.antlr.ast.Assert; +import org.python.antlr.ast.Assign; +import org.python.antlr.ast.Attribute; +import org.python.antlr.ast.AugAssign; +import org.python.antlr.ast.BinOp; +import org.python.antlr.ast.BoolOp; +import org.python.antlr.ast.Break; +import org.python.antlr.ast.Call; +import org.python.antlr.ast.ClassDef; +import org.python.antlr.ast.Compare; +import org.python.antlr.ast.Continue; +import org.python.antlr.ast.Delete; +import org.python.antlr.ast.Dict; +import org.python.antlr.ast.Ellipsis; +import org.python.antlr.ast.ExceptHandler; +import org.python.antlr.ast.Exec; +import org.python.antlr.ast.Expr; +import org.python.antlr.ast.Expression; +import org.python.antlr.ast.ExtSlice; +import org.python.antlr.ast.For; +import org.python.antlr.ast.FunctionDef; +import org.python.antlr.ast.GeneratorExp; +import org.python.antlr.ast.Global; +import org.python.antlr.ast.If; +import org.python.antlr.ast.IfExp; +import org.python.antlr.ast.Import; +import org.python.antlr.ast.ImportFrom; +import org.python.antlr.ast.Index; +import org.python.antlr.ast.Interactive; +import org.python.antlr.ast.Lambda; +import org.python.antlr.ast.List; +import org.python.antlr.ast.ListComp; +import org.python.antlr.ast.Name; +import org.python.antlr.ast.Num; +import org.python.antlr.ast.Pass; +import org.python.antlr.ast.Print; +import org.python.antlr.ast.Raise; +import org.python.antlr.ast.Repr; +import org.python.antlr.ast.Return; +import org.python.antlr.ast.Slice; +import org.python.antlr.ast.Str; +import org.python.antlr.ast.Subscript; +import org.python.antlr.ast.Suite; +import org.python.antlr.ast.TryExcept; +import org.python.antlr.ast.TryFinally; +import org.python.antlr.ast.Tuple; +import org.python.antlr.ast.UnaryOp; +import org.python.antlr.ast.While; +import org.python.antlr.ast.With; +import org.python.antlr.ast.Yield; +import org.python.antlr.ast.alias; +import org.python.antlr.ast.cmpopType; +import org.python.antlr.ast.comprehension; +import org.python.antlr.ast.expr_contextType; +import org.python.antlr.ast.keyword; +import org.python.antlr.ast.operatorType; +import org.python.antlr.base.expr; +import org.python.antlr.base.mod; +import org.python.antlr.base.stmt; +import org.python.core.CompilerFlags; +import org.python.core.PyComplex; +import org.python.core.PyFloat; +import org.python.core.PyInteger; +import org.python.core.PyLong; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyUnicode; + +public class BytecodeCompiler extends Visitor +{ +// + public static final Object Exit=new Integer(1); + public static final Object NoExit=null; + + public static final int GET=0; + public static final int SET=1; + public static final int DEL=2; + public static final int AUGGET=3; + public static final int AUGSET=4; +// +// public Module module; +// public ClassWriter cw; +// public Code code; +// public CodeCompiler mrefs; +// public CompilerFlags cflags; +// +// int temporary; +// expr_contextType augmode; +// int augtmp1; +// int augtmp2; +// int augtmp3; +// int augtmp4; +// +// public boolean fast_locals, print_results; +// +// public Map<String, SymInfo> tbl; +// public ScopeInfo my_scope; +// +// boolean optimizeGlobals = true; +// public Vector names; + public String className; +// +// public Stack continueLabels, breakLabels; +// public Stack exceptionHandlers; +// public Vector yields = new Vector(); +// +// /* break/continue finally's level. +// * This is the lowest level in the exceptionHandlers which should +// * be executed at break or continue. +// * It is saved/updated/restored when compiling loops. +// * A similar level for returns is not needed because a new CodeCompiler +// * is used for each PyCode, ie. each 'function'. +// * When returning through finally's all the exceptionHandlers are executed. +// */ + public int bcfLevel = 0; +// + int yield_count = 0; + + private int stackDepth = 0; +// + public BytecodeCompiler(Module module, boolean print_results) { +// this.module = module; +// this.print_results = print_results; +// +// mrefs = this; +// cw = module.classfile.cw; +// +// continueLabels = new Stack(); +// breakLabels = new Stack(); +// exceptionHandlers = new Stack(); + } +// +//// public void getNone() throws IOException { +//// code.getstatic("org/python/core/Py", "None", $pyObj); +//// } +//// +//// public void loadFrame() throws Exception { +//// code.aload(1); +//// } +// +//// public void setLastI(int idx) throws Exception { +//// loadFrame(); +//// code.iconst(idx); +//// code.putfield("org/python/core/PyFrame", "f_lasti", "I"); +//// } +//// +//// private void loadf_back() throws Exception { +//// code.getfield("org/python/core/PyFrame", "f_back", $pyFrame); +//// } +//// +// public int storeTop() throws Exception { +////// int tmp = code.getLocal("org/python/core/PyObject"); +////// code.astore(tmp); +////// return tmp; +// } +//// + public void setline(int line) throws Exception { +//// if (module.linenumbers) { +//// code.setline(line); +//// loadFrame(); +//// code.iconst(line); +//// code.invokevirtual("org/python/core/PyFrame", "setline", "(I)V"); +//// } + } +//// + public void setline(PythonTree node) throws Exception { +//// setline(node.getLine()); + } +//// + public void set(PythonTree node) throws Exception { +//// int tmp = storeTop(); +//// set(node, tmp); +//// code.aconst_null(); +//// code.astore(tmp); +//// code.freeLocal(tmp); + } +// +// + public void set(PythonTree node, int tmp) throws Exception { +//// temporary = tmp; +//// visit(node); + } +//// +//// +//// private void saveAugTmps(PythonTree node, int count) throws Exception { +//// if (count >= 4) { +//// augtmp4 = code.getLocal("Lorg/python/core/PyObject;"); +//// code.astore(augtmp4); +//// } +//// if (count >= 3) { +//// augtmp3 = code.getLocal("Lorg/python/core/PyObject;"); +//// code.astore(augtmp3); +//// } +//// if (count >= 2) { +//// augtmp2 = code.getLocal("Lorg/python/core/PyObject;"); +//// code.astore(augtmp2); +//// } +//// augtmp1 = code.getLocal("Lorg/python/core/PyObject;"); +//// code.astore(augtmp1); +//// +//// code.aload(augtmp1); +//// if (count >= 2) +//// code.aload(augtmp2); +//// if (count >= 3) +//// code.aload(augtmp3); +//// if (count >= 4) +//// code.aload(augtmp4); +//// } +//// +//// +//// private void restoreAugTmps(PythonTree node, int count) throws Exception { +//// code.aload(augtmp1); +//// code.freeLocal(augtmp1); +//// if (count == 1) +//// return; +//// code.aload(augtmp2); +//// code.freeLocal(augtmp2); +//// if (count == 2) +//// return; +//// code.aload(augtmp3); +//// code.freeLocal(augtmp3); +//// if (count == 3) +//// return; +//// code.aload(augtmp4); +//// code.freeLocal(augtmp4); +//// } +// +// +// public void parse(mod node, Code code, +// boolean fast_locals, String className, +// boolean classBody, ScopeInfo scope, CompilerFlags cflags) +// throws Exception +// { +// this.fast_locals = fast_locals; +// this.className = className; +// this.code = code; +// this.cflags = cflags; +// +// my_scope = scope; +// names = scope.names; +// +// tbl = scope.tbl; +// optimizeGlobals = fast_locals&&!scope.exec&&!scope.from_import_star; +// +// if (scope.max_with_count > 0) { +// // allocate for all the with-exits we will have in the frame; +// // this allows yield and with to happily co-exist +// loadFrame(); +// code.iconst(scope.max_with_count); +// code.anewarray("org/python/core/PyObject"); +// code.putfield("org/python/core/PyFrame", "f_exits", $pyObjArr); +// } +// +// Object exit = visit(node); +// +// if (classBody) { +// loadFrame(); +// code.invokevirtual("org/python/core/PyFrame", "getf_locals", "()" + $pyObj); +// code.areturn(); +// } else { +// if (exit == null) { +// setLastI(-1); +// +// getNone(); +// code.areturn(); +// } +// } +// } +// +// @Override +// public Object visitInteractive(Interactive node) throws Exception { +// traverse(node); +// return null; +// } +// +// @Override +// public Object visitModule(org.python.antlr.ast.Module suite) +// throws Exception +// { +// if (suite.getInternalBody().size() > 0 && +// suite.getInternalBody().get(0) instanceof Expr && +// ((Expr) suite.getInternalBody().get(0)).getInternalValue() instanceof Str) +// { +// loadFrame(); +// code.ldc("__doc__"); +// visit(((Expr) suite.getInternalBody().get(0)).getInternalValue()); +// code.invokevirtual("org/python/core/PyFrame", "setglobal", "(" +$str + $pyObj + ")V"); +// } +// traverse(suite); +// return null; +// } +// +// @Override +// public Object visitExpression(Expression node) throws Exception { +// if (my_scope.generator && node.getInternalBody() != null) { +// module.error("'return' with argument inside generator", +// true, node); +// } +// return visitReturn(new Return(node,node.getInternalBody()), true); +// } +// +// public int makeArray(java.util.List<? extends PythonTree> nodes) throws Exception { +// int n; +// +// if (nodes == null) +// n = 0; +// else +// n = nodes.size(); +// +// int array = code.getLocal("[Lorg/python/core/PyObject;"); +// if (n == 0) { +// code.getstatic("org/python/core/Py", "EmptyObjects", $pyObjArr); +// code.astore(array); +// } else { +// code.iconst(n); +// code.anewarray("org/python/core/PyObject"); +// code.astore(array); +// +// for(int i=0; i<n; i++) { +// visit(nodes.get(i)); +// code.aload(array); +// code.swap(); +// code.iconst(i); +// code.swap(); +// code.aastore(); +// } +// } +// return array; +// } +// + public void getDocString(java.util.List<stmt> suite) throws Exception { +// if (suite.size() > 0 && suite.get(0) instanceof Expr && +// ((Expr) suite.get(0)).getInternalValue() instanceof Str) +// { +// visit(((Expr) suite.get(0)).getInternalValue()); +// } else { +// code.aconst_null(); +// } + } +// + public boolean makeClosure(ScopeInfo scope) throws Exception { +// if (scope == null || scope.freevars == null) return false; +// int n = scope.freevars.size(); +// if (n == 0) return false; +// +// int tmp = code.getLocal("[Lorg/python/core/PyObject;"); +// code.iconst(n); +// code.anewarray("org/python/core/PyObject"); +// code.astore(tmp); +// Map<String, SymInfo> upTbl = scope.up.tbl; +// for(int i=0; i<n; i++) { +// code.aload(tmp); +// code.iconst(i); +// loadFrame(); +// for(int j = 1; j < scope.distance; j++) { +// loadf_back(); +// } +// SymInfo symInfo = upTbl.get(scope.freevars.elementAt(i)); +// code.iconst(symInfo.env_index); +// code.invokevirtual("org/python/core/PyFrame", "getclosure", "(I)" + $pyObj); +// code.aastore(); +// } +// +// code.aload(tmp); +// code.freeLocal(tmp); +// + return true; + } +// + @Override + public Object visitFunctionDef(FunctionDef node) throws Exception { + String name = getName(node.getInternalName()); +// +// setline(node); +// +// ScopeInfo scope = module.getScopeInfo(node); +// +// int defaults = makeArray(scope.ac.getDefaults()); +// +// code.new_("org/python/core/PyFunction"); +// code.dup(); +// loadFrame(); +// code.getfield("org/python/core/PyFrame", "f_globals", $pyObj); +// code.aload(defaults); +// code.freeLocal(defaults); +// +// scope.setup_closure(); +// scope.dump(); +// module.PyCode(new Suite(node,node.getInternalBody()), name, true, +// className, false, false, +// node.getLine(), scope, cflags).get(code); +// +// getDocString(node.getInternalBody()); +// +// if (!makeClosure(scope)) { +// code.invokespecial("org/python/core/PyFunction", "<init>", "(" + $pyObj + $pyObjArr + $pyCode + $pyObj + ")V"); +// } else { +// code.invokespecial( "org/python/core/PyFunction", "<init>", "(" + $pyObj + $pyObjArr + $pyCode + $pyObj + $pyObjArr + ")V"); +// } +// + set(new Name(node,node.getInternalName(), expr_contextType.Store)); + doDecorators(node,node.getInternalDecorator_list(), node.getInternalName()); + return null; + } +// + private void doDecorators(stmt node, java.util.List<expr> decs, String name) throws Exception { + if (decs.size() > 0) { + expr currentExpr = new Name(node, name, expr_contextType.Load); + for (int i=decs.size() - 1;i > -1;i--) { + java.util.List args = new ArrayList(); + args.add(currentExpr); + currentExpr = new Call(node, decs.get(i), args, new ArrayList<keyword>(), null, null); + } + visit(currentExpr); + set(new Name(node, name, expr_contextType.Store)); + } + } +// +// @Override +// public Object visitExpr(Expr node) throws Exception { +// setline(node); +// visit(node.getInternalValue()); +// +// if (print_results) { +// code.invokestatic("org/python/core/Py", "printResult", "(" + $pyObj + ")V"); +// } else { +// code.pop(); +// } +// return null; +// } +// +// @Override +// public Object visitAssign(Assign node) throws Exception { +// setline(node); +// visit(node.getInternalValue()); +// if (node.getInternalTargets().size() == 1) { +// set(node.getInternalTargets().get(0)); +// } else { +// int tmp = storeTop(); +// for (expr target : node.getInternalTargets()) { +// set(target, tmp); +// } +// code.freeLocal(tmp); +// } +// return null; +// } +// +// @Override +// public Object visitPrint(Print node) throws Exception { +// setline(node); +// int tmp = -1; +// +// if (node.getInternalDest() != null) { +// visit(node.getInternalDest()); +// tmp = storeTop(); +// } +// if (node.getInternalValues() == null || node.getInternalValues().size() == 0) { +// if (node.getInternalDest() != null) { +// code.aload(tmp); +// code.invokestatic("org/python/core/Py", "printlnv", "(" + $pyObj + ")V"); +// } else { +// code.invokestatic("org/python/core/Py", "println", "()V"); +// } +// } else { +// for (int i = 0; i < node.getInternalValues().size(); i++) { +// if (node.getInternalDest() != null) { +// code.aload(tmp); +// visit(node.getInternalValues().get(i)); +// if (node.getInternalNl() && i == node.getInternalValues().size() - 1) { +// code.invokestatic("org/python/core/Py", "println", "(" + $pyObj + $pyObj + ")V"); +// } else { +// code.invokestatic("org/python/core/Py", "printComma", "(" + $pyObj + $pyObj + ")V"); +// } +// } else { +// visit(node.getInternalValues().get(i)); +// if (node.getInternalNl() && i == node.getInternalValues().size() - 1) { +// code.invokestatic("org/python/core/Py", "println", "(" + $pyObj + ")V"); +// } else { +// code.invokestatic("org/python/core/Py", "printComma", "(" + $pyObj + ")V"); +// } +// +// } +// } +// } +// if (node.getInternalDest() != null) { +// code.freeLocal(tmp); +// } +// return null; +// } +// +// @Override +// public Object visitDelete(Delete node) throws Exception { +// setline(node); +// traverse(node); +// return null; +// } +// +// @Override +// public Object visitPass(Pass node) throws Exception { +// setline(node); +// return null; +// } +// +// @Override +// public Object visitBreak(Break node) throws Exception { +// //setline(node); Not needed here... +// if (breakLabels.empty()) { +// throw new ParseException("'break' outside loop", node); +// } +// +// doFinallysDownTo(bcfLevel); +// +// code.goto_((Label)breakLabels.peek()); +// return null; +// } +// +// @Override +// public Object visitContinue(Continue node) throws Exception { +// //setline(node); Not needed here... +// if (continueLabels.empty()) { +// throw new ParseException("'continue' not properly in loop", node); +// } +// +// doFinallysDownTo(bcfLevel); +// +// code.goto_((Label)continueLabels.peek()); +// return Exit; +// } +// +// @Override +// public Object visitYield(Yield node) throws Exception { +// setline(node); +// if (!fast_locals) { +// throw new ParseException("'yield' outside function", node); +// } +// +// int stackState = saveStack(); +// +// if (node.getInternalValue() != null) { +// visit(node.getInternalValue()); +// } else { +// getNone(); +// } +// +// setLastI(++yield_count); +// +// saveLocals(); +// code.areturn(); +// +// Label restart = new Label(); +// yields.addElement(restart); +// code.label(restart); +// restoreLocals(); +// restoreStack(stackState); +// +// loadFrame(); +// code.invokevirtual("org/python/core/PyFrame", "getGeneratorInput", "()" + $obj); +// code.dup(); +// code.instanceof_("org/python/core/PyException"); +// Label done2 = new Label(); +// code.ifeq(done2); +// code.checkcast("java/lang/Throwable"); +// code.athrow(); +// code.label(done2); +// code.checkcast("org/python/core/PyObject"); +// +// return null; +// } +// +// private void stackProduce() { +// stackDepth++; +// } +// +// private void stackConsume() { +// stackDepth--; +// } +// +// private void stackConsume(int numItems) { +// stackDepth -= numItems; +// } +// +// private int saveStack() throws Exception { +// if (stackDepth > 0) { +// int array = code.getLocal("[Lorg/python/core/PyObject;"); +// code.iconst(stackDepth); +// code.anewarray("org/python/core/PyObject"); +// code.astore(array); +// for (int i = 0; i < stackDepth; i++) { +// code.aload(array); +// // Stack: |- ... value array +// code.swap(); +// code.iconst(i); +// code.swap(); +// // Stack: |- ... array index value +// code.aastore(); +// // Stack: |- ... +// } +// return array; +// } else { +// return -1; +// } +// } +// +// private void restoreStack(int array) throws Exception { +// if (stackDepth > 0) { +// for (int i = stackDepth - 1; i >= 0; i--) { +// code.aload(array); +// // Stack: |- ... array +// code.iconst(i); +// code.aaload(); +// // Stack: |- ... value +// } +// code.freeLocal(array); +// } +// } +// +// private void restoreLocals() throws Exception { +// endExceptionHandlers(); +// +// Vector v = code.getActiveLocals(); +// +// loadFrame(); +// code.getfield("org/python/core/PyFrame", "f_savedlocals", "[Ljava/lang/Object;"); +// +// int locals = code.getLocal("[Ljava/lang/Object;"); +// code.astore(locals); +// +// for (int i = 0; i < v.size(); i++) { +// String type = (String) v.elementAt(i); +// if (type == null) +// continue; +// code.aload(locals); +// code.iconst(i); +// code.aaload(); +// code.checkcast(type); +// code.astore(i); +// } +// code.freeLocal(locals); +// +// restartExceptionHandlers(); +// } +// +// /** +// * Close all the open exception handler ranges. This should be paired +// * with restartExceptionHandlers to delimit internal code that +// * shouldn't be handled by user handlers. This allows us to set +// * variables without the verifier thinking we might jump out of our +// * handling with an exception. +// */ +// private void endExceptionHandlers() +// { +// Label end = new Label(); +// code.label(end); +// for (int i = 0; i < exceptionHandlers.size(); ++i) { +// ExceptionHandler handler = +// (ExceptionHandler)exceptionHandlers.elementAt(i); +// handler.exceptionEnds.addElement(end); +// } +// } +// +// private void restartExceptionHandlers() +// { +// Label start = new Label(); +// code.label(start); +// for (int i = 0; i < exceptionHandlers.size(); ++i) { +// ExceptionHandler handler = +// (ExceptionHandler)exceptionHandlers.elementAt(i); +// handler.exceptionStarts.addElement(start); +// } +// } +// +// private void saveLocals() throws Exception { +// Vector v = code.getActiveLocals(); +// code.iconst(v.size()); +// code.anewarray("java/lang/Object"); +// int locals = code.getLocal("[Ljava/lang/Object;"); +// code.astore(locals); +// +// for (int i = 0; i < v.size(); i++) { +// String type = (String) v.elementAt(i); +// if (type == null) +// continue; +// code.aload(locals); +// code.iconst(i); +// //code.checkcast(code.pool.Class("java/lang/Object")); +// if (i == 2222) { +// code.aconst_null(); +// } else +// code.aload(i); +// code.aastore(); +// } +// +// loadFrame(); +// code.aload(locals); +// code.putfield("org/python/core/PyFrame", "f_savedlocals", "[Ljava/lang/Object;"); +// code.freeLocal(locals); +// } +// +// +// @Override +// public Object visitReturn(Return node) throws Exception { +// return visitReturn(node, false); +// } +// +// public Object visitReturn(Return node, boolean inEval) throws Exception { +// setline(node); +// if (!inEval && !fast_locals) { +// throw new ParseException("'return' outside function", node); +// } +// int tmp = 0; +// if (node.getInternalValue() != null) { +// if (my_scope.generator) +// throw new ParseException("'return' with argument " + +// "inside generator", node); +// visit(node.getInternalValue()); +// tmp = code.getReturnLocal(); +// code.astore(tmp); +// } +// doFinallysDownTo(0); +// +// setLastI(-1); +// +// if (node.getInternalValue() != null) { +// code.aload(tmp); +// } else { +// getNone(); +// } +// code.areturn(); +// return Exit; +// } +// +// @Override +// public Object visitRaise(Raise node) throws Exception { +// setline(node); +// if (node.getInternalType() != null) { visit(node.getInternalType()); stackProduce(); } +// if (node.getInternalInst() != null) { visit(node.getInternalInst()); stackProduce(); } +// if (node.getInternalTback() != null) { visit(node.getInternalTback()); stackProduce(); } +// if (node.getInternalType() == null) { +// code.invokestatic("org/python/core/Py", "makeException", "()" + $pyExc); +// } else if (node.getInternalInst() == null) { +// stackConsume(); +// code.invokestatic("org/python/core/Py", "makeException", "(" + $pyObj + ")" + $pyExc); +// } else if (node.getInternalTback() == null) { +// stackConsume(2); +// code.invokestatic("org/python/core/Py", "makeException", "(" + $pyObj + $pyObj + ")" + $pyExc); +// } else { +// stackConsume(3); +// code.invokestatic("org/python/core/Py", "makeException", "(" + $pyObj + $pyObj + $pyObj + ")" + $pyExc); +// } +// code.athrow(); +// return Exit; +// } +// +// @Override +// public Object visitImport(Import node) throws Exception { +// setline(node); +// for (alias a : node.getInternalNames()) { +// String asname = null; +// if (a.getInternalAsname() != null) { +// String name = a.getInternalName(); +// asname = a.getInternalAsname(); +// code.ldc(name); +// loadFrame(); +// code.invokestatic("org/python/core/imp", "importOneAs", "(" + $str + $pyFrame + ")" + $pyObj); +// } else { +// String name = a.getInternalName(); +// asname = name; +// if (asname.indexOf('.') > 0) +// asname = asname.substring(0, asname.indexOf('.')); +// code.ldc(name); +// loadFrame(); +// code.invokestatic("org/python/core/imp", "importOne", "(" + $str + $pyFrame + ")" + $pyObj); +// } +// set(new Name(a, asname, expr_contextType.Store)); +// } +// return null; +// } +// +// +// @Override +// public Object visitImportFrom(ImportFrom node) throws Exception { +// Future.checkFromFuture(node); // future stmt support +// setline(node); +// code.ldc(node.getInternalModule()); +// java.util.List<alias> names = node.getInternalNames(); +// if (names == null || names.size() == 0) { +// throw new ParseException("Internel parser error", node); +// } else if (names.size() == 1 && names.get(0).getInternalName().equals("*")) { +// if (node.getInternalLevel() > 0) { +// throw new ParseException("'import *' not allowed with 'from .'", node); +// } +// if (my_scope.func_level > 0) { +// module.error("import * only allowed at module level", false, node); +// +// if (my_scope.contains_ns_free_vars) { +// module.error("import * is not allowed in function '" + +// my_scope.scope_name + +// "' because it contains a nested function with free variables", +// true, node); +// } +// } +// if (my_scope.func_level > 1) { +// module.error("import * is not allowed in function '" + +// my_scope.scope_name + +// "' because it is a nested function", +// true, node); +// } +// +// loadFrame(); +// code.invokestatic("org/python/core/imp", "importAll", "(" + $str + $pyFrame + ")V"); +// } else { +// java.util.List<String> fromNames = new ArrayList<String>();//[names.size()]; +// java.util.List<String> asnames = new ArrayList<String>();//[names.size()]; +// for (int i = 0; i < names.size(); i++) { +// fromNames.add(names.get(i).getInternalName()); +// asnames.add(names.get(i).getInternalAsname()); +// if (asnames.get(i) == null) +// asnames.set(i, fromNames.get(i)); +// } +// int strArray = makeStrings(code, fromNames); +// code.aload(strArray); +// code.freeLocal(strArray); +// +// loadFrame(); +// +// if (node.getInternalLevel() == 0) { +// if (module.getFutures().isAbsoluteImportOn()) { +// code.iconst_0(); +// } else { +// code.iconst_m1(); +// } +// } else { +// code.iconst(node.getInternalLevel()); +// } +// code.invokestatic("org/python/core/imp", "importFrom", "(" + $str + $strArr + $pyFrame + "I" + ")" + $pyObjArr); +// int tmp = storeTop(); +// for (int i = 0; i < names.size(); i++) { +// code.aload(tmp); +// code.iconst(i); +// code.aaload(); +// set(new Name(names.get(i), asnames.get(i), expr_contextType.Store)); +// } +// code.freeLocal(tmp); +// } +// return null; +// } +// +// @Override +// public Object visitGlobal(Global node) throws Exception { +// return null; +// } +// +// @Override +// public Object visitExec(Exec node) throws Exception { +// setline(node); +// visit(node.getInternalBody()); +// stackProduce(); +// +// if (node.getInternalGlobals() != null) { +// visit(node.getInternalGlobals()); +// } else { +// code.aconst_null(); +// } +// stackProduce(); +// +// if (node.getInternalLocals() != null) { +// visit(node.getInternalLocals()); +// } else { +// code.aconst_null(); +// } +// stackProduce(); +// +// //do the real work here +// stackConsume(3); +// code.invokestatic("org/python/core/Py", "exec", "(" + $pyObj + $pyObj + $pyObj + ")V"); +// return null; +// } +// +// @Override +// public Object visitAssert(Assert node) throws Exception { +// setline(node); +// Label end_of_assert = new Label(); +// +// /* First do an if __debug__: */ +// loadFrame(); +// emitGetGlobal("__debug__"); +// +// code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); +// +// code.ifeq(end_of_assert); +// +// /* Now do the body of the assert. If PyObject.__nonzero__ is true, +// then the assertion succeeded, the message portion should not be +// processed. Otherwise, the message will be processed. */ +// visit(node.getInternalTest()); +// code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); +// +// /* If evaluation is false, then branch to end of method */ +// code.ifne(end_of_assert); +// +// /* Visit the message part of the assertion, or pass Py.None */ +// if( node.getInternalMsg() != null ) { +// visit(node.getInternalMsg()); +// } else { +// getNone(); +// } +// +// /* Push exception type onto stack(Py.AssertionError) */ +// code.getstatic("org/python/core/Py", "AssertionError", "Lorg/python/core/PyObject;"); +// +// code.swap(); // The type is the first argument, but the message could be a yield +// +// code.invokestatic("org/python/core/Py", "makeException", "(" + $pyObj + $pyObj + ")" + $pyExc); +// +// /* Raise assertion error. Only executes this logic if assertion +// failed */ +// code.athrow(); +// +// /* And finally set the label for the end of it all */ +// code.label(end_of_assert); +// +// return null; +// } +// +// public Object doTest(Label end_of_if, If node, int index) +// throws Exception +// { +// Label end_of_suite = new Label(); +// +// setline(node.getInternalTest()); +// visit(node.getInternalTest()); +// code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); +// +// code.ifeq(end_of_suite); +// +// Object exit = suite(node.getInternalBody()); +// +// if (end_of_if != null && exit == null) +// code.goto_(end_of_if); +// +// code.label(end_of_suite); +// +// if (node.getInternalOrelse() != null) { +// return suite(node.getInternalOrelse()) != null ? exit : null; +// } else { +// return null; +// } +// } +// +// @Override +// public Object visitIf(If node) throws Exception { +// Label end_of_if = null; +// if (node.getInternalOrelse() != null) +// end_of_if = new Label(); +// +// Object exit = doTest(end_of_if, node, 0); +// if (end_of_if != null) +// code.label(end_of_if); +// return exit; +// } +// +// @Override +// public Object visitIfExp(IfExp node) throws Exception { +// setline(node.getInternalTest()); +// Label end = new Label(); +// Label end_of_else = new Label(); +// +// visit(node.getInternalTest()); +// code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); +// +// code.ifeq(end_of_else); +// visit(node.getInternalBody()); +// code.goto_(end); +// +// code.label(end_of_else); +// visit(node.getInternalOrelse()); +// +// code.label(end); +// +// return null; +// } +// +// public int beginLoop() { +// continueLabels.push(new Label()); +// breakLabels.push(new Label()); +// int savebcf = bcfLevel; +// bcfLevel = exceptionHandlers.size(); +// return savebcf; +// } +// +// public void finishLoop(int savebcf) { +// continueLabels.pop(); +// breakLabels.pop(); +// bcfLevel = savebcf; +// } +// +// +// @Override +// public Object visitWhile(While node) throws Exception { +// int savebcf = beginLoop(); +// Label continue_loop = (Label)continueLabels.peek(); +// Label break_loop = (Label)breakLabels.peek(); +// +// Label start_loop = new Label(); +// +// code.goto_(continue_loop); +// code.label(start_loop); +// +// //Do suite +// suite(node.getInternalBody()); +// +// code.label(continue_loop); +// setline(node); +// +// //Do test +// visit(node.getInternalTest()); +// code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); +// code.ifne(start_loop); +// +// finishLoop(savebcf); +// +// if (node.getInternalOrelse() != null) { +// //Do else +// suite(node.getInternalOrelse()); +// } +// code.label(break_loop); +// +// // Probably need to detect "guaranteed exits" +// return null; +// } +// +// @Override +// public Object visitFor(For node) throws Exception { +// int savebcf = beginLoop(); +// Label continue_loop = (Label)continueLabels.peek(); +// Label break_loop = (Label)breakLabels.peek(); +// Label start_loop = new Label(); +// Label next_loop = new Label(); +// +// int iter_tmp = code.getLocal("org/python/core/PyObject"); +// int expr_tmp = code.getLocal("org/python/core/PyObject"); +// +// setline(node); +// +// //parse the list +// visit(node.getInternalIter()); +// +// //set up the loop iterator +// code.invokevirtual("org/python/core/PyObject", "__iter__", "()Lorg/python/core/PyObject;"); +// code.astore(iter_tmp); +// +// //do check at end of loop. Saves one opcode ;-) +// code.goto_(next_loop); +// +// code.label(start_loop); +// //set iter variable to current entry in list +// set(node.getInternalTarget(), expr_tmp); +// +// //evaluate for body +// suite(node.getInternalBody()); +// +// code.label(continue_loop); +// +// code.label(next_loop); +// setline(node); +// //get the next element from the list +// code.aload(iter_tmp); +// code.invokevirtual("org/python/core/PyObject", "__iternext__", "()" + $pyObj); +// +// code.astore(expr_tmp); +// code.aload(expr_tmp); +// //if no more elements then fall through +// code.ifnonnull(start_loop); +// +// finishLoop(savebcf); +// +// if (node.getInternalOrelse() != null) { +// //Do else clause if provided +// suite(node.getInternalOrelse()); +// } +// +// code.label(break_loop); +// +// code.freeLocal(iter_tmp); +// code.freeLocal(expr_tmp); +// +// // Probably need to detect "guaranteed exits" +// return null; +// } +// +// public void exceptionTest(int exc, Label end_of_exceptions, +// TryExcept node, int index) +// throws Exception +// { +// for (int i = 0; i < node.getInternalHandlers().size(); i++) { +// ExceptHandler handler = (ExceptHandler)node.getInternalHandlers().get(i); +// +// //setline(name); +// Label end_of_self = new Label(); +// +// if (handler.getInternalType() != null) { +// code.aload(exc); +// //get specific exception +// visit(handler.getInternalType()); +// code.invokestatic("org/python/core/Py", "matchException", "(" + $pyExc + $pyObj + ")Z"); +// code.ifeq(end_of_self); +// } else { +// if (i != node.getInternalHandlers().size()-1) { +// throw new ParseException( +// "default 'except:' must be last", handler); +// } +// } +// +// if (handler.getInternalName() != null) { +// code.aload(exc); +// code.getfield("org/python/core/PyException", "value", "Lorg/python/core/PyObject;"); +// set(handler.getInternalName()); +// } +// +// //do exception body +// suite(handler.getInternalBody()); +// code.goto_(end_of_exceptions); +// code.label(end_of_self); +// } +// code.aload(exc); +// code.athrow(); +// } +// +// +// @Override +// public Object visitTryFinally(TryFinally node) throws Exception +// { +// Label start = new Label(); +// Label end = new Label(); +// Label handlerStart = new Label(); +// Label finallyEnd = new Label(); +// +// Object ret; +// +// ExceptionHandler inFinally = new ExceptionHandler(node); +// +// // Do protected suite +// exceptionHandlers.push(inFinally); +// +// int excLocal = code.getLocal("java/lang/Throwable"); +// code.aconst_null(); +// code.astore(excLocal); +// +// code.label(start); +// inFinally.exceptionStarts.addElement(start); +// +// ret = suite(node.getInternalBody()); +// +// code.label(end); +// inFinally.exceptionEnds.addElement(end); +// inFinally.bodyDone = true; +// +// exceptionHandlers.pop(); +// +// if (ret == NoExit) { +// inlineFinally(inFinally); +// code.goto_(finallyEnd); +// } +// +// // Handle any exceptions that get thrown in suite +// code.label(handlerStart); +// code.astore(excLocal); +// +// code.aload(excLocal); +// loa... [truncated message content] |
From: <zy...@us...> - 2009-02-11 03:11:14
|
Revision: 6024 http://jython.svn.sourceforge.net/jython/?rev=6024&view=rev Author: zyasoft Date: 2009-02-11 03:11:10 +0000 (Wed, 11 Feb 2009) Log Message: ----------- Support tostring/fromstring of unsigned types so that they can be stored in the same size as their signed representation. This fixes an issues with peak.util.assembler where it expects that unsigned bytes (typecode 'B') take one byte to store and therefore are directly interoperable with co_code. Modified Paths: -------------- branches/pbcvm/Lib/test/test_array.py branches/pbcvm/src/org/python/core/PyArray.java Modified: branches/pbcvm/Lib/test/test_array.py =================================================================== --- branches/pbcvm/Lib/test/test_array.py 2009-02-09 01:40:53 UTC (rev 6023) +++ branches/pbcvm/Lib/test/test_array.py 2009-02-11 03:11:10 UTC (rev 6024) @@ -213,7 +213,7 @@ self.assertRaises(TypeError, b.fromstring, 42) b.fromstring(a.tostring()) self.assertEqual(a, b) - if a.itemsize>1: + if a.itemsize>1 and self.typecode not in ('b', 'B'): self.assertRaises(ValueError, b.fromstring, "x") def test_repr(self): Modified: branches/pbcvm/src/org/python/core/PyArray.java =================================================================== --- branches/pbcvm/src/org/python/core/PyArray.java 2009-02-09 01:40:53 UTC (rev 6023) +++ branches/pbcvm/src/org/python/core/PyArray.java 2009-02-11 03:11:10 UTC (rev 6024) @@ -491,7 +491,7 @@ * written on a machine with a different byte order. */ public void byteswap() { - if (getItemsize() == 0 || "u".equals(typecode)) { + if (getStorageSize() == 0 || "u".equals(typecode)) { throw Py.RuntimeError("don't know how to byteswap this array type"); } ByteSwapper.swap(data); @@ -591,6 +591,39 @@ } } + private static Class char2storageClass(char type) throws PyIgnoreMethodTag { + switch(type){ + case 'z': + return Boolean.TYPE; + case 'b': + return Byte.TYPE; + case 'B': + return Byte.TYPE; + case 'u': + return Integer.TYPE; + case 'c': + return Character.TYPE; + case 'h': + return Short.TYPE; + case 'H': + return Short.TYPE; + case 'i': + return Integer.TYPE; + case 'I': + return Integer.TYPE; + case 'l': + return Long.TYPE; + case 'L': + return Long.TYPE; + case 'f': + return Float.TYPE; + case 'd': + return Double.TYPE; + default: + throw Py.ValueError("bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); + } + } + private static String class2char(Class cls) { if(cls.equals(Boolean.TYPE)) return "z"; @@ -800,13 +833,13 @@ throw Py.TypeError("file needs to be in read mode"); } // read the data via the PyFile - int readbytes = count * getItemsize(); + int readbytes = count * getStorageSize(); String buffer = file.read(readbytes).toString(); // load whatever was collected into the array fromstring(buffer); // check for underflow if(buffer.length() < readbytes) { - int readcount = buffer.length() / getItemsize(); + int readcount = buffer.length() / getStorageSize(); throw Py.EOFError("not enough items in file. " + Integer.toString(count) + " requested, " + Integer.toString(readcount) + " actually read"); @@ -854,7 +887,7 @@ * @throws EOFException */ private int fromStream(InputStream is) throws IOException, EOFException { - return fromStream(is, is.available() / getItemsize()); + return fromStream(is, is.available() / getStorageSize()); } /** @@ -880,47 +913,87 @@ int index = origsize; // create capacity for 'count' items delegate.ensureCapacity(index + count); - if(type.isPrimitive()) { - if(type == Boolean.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setBoolean(data, index, dis.readBoolean()); - delegate.size++; - } - } else if(type == Byte.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setByte(data, index, dis.readByte()); - delegate.size++; - } - } else if(type == Character.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setChar(data, index, (char)(dis.readByte() & 0xff)); - delegate.size++; - } - } else if(type == Integer.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setInt(data, index, dis.readInt()); - delegate.size++; - } - } else if(type == Short.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setShort(data, index, dis.readShort()); - delegate.size++; - } - } else if(type == Long.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setLong(data, index, dis.readLong()); - delegate.size++; - } - } else if(type == Float.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setFloat(data, index, dis.readFloat()); - delegate.size++; - } - } else if(type == Double.TYPE) { - for(int i = 0; i < count; i++, index++) { - Array.setDouble(data, index, dis.readDouble()); - delegate.size++; - } + if (type.isPrimitive()) { + switch (typecode.charAt(0)) { + case 'z': + for (int i = 0; i < count; i++, index++) { + Array.setBoolean(data, index, dis.readBoolean()); + delegate.size++; + } + break; + case 'b': + for (int i = 0; i < count; i++, index++) { + Array.setByte(data, index, dis.readByte()); + delegate.size++; + } + break; + case 'B': + for (int i = 0; i < count; i++, index++) { + Array.setShort(data, index, unsignedByte(dis.readByte())); + delegate.size++; + } + break; + case 'u': + // use 32-bit integers since we want UCS-4 storage + for (int i = 0; i < count; i++, index++) { + Array.setInt(data, index, dis.readInt()); + delegate.size++; + } + break; + case 'c': + for (int i = 0; i < count; i++, index++) { + Array.setChar(data, index, (char) (dis.readByte() & 0xff)); + delegate.size++; + } + break; + case 'h': + for (int i = 0; i < count; i++, index++) { + Array.setShort(data, index, dis.readShort()); + delegate.size++; + } + break; + case 'H': + for (int i = 0; i < count; i++, index++) { + Array.setInt(data, index, unsignedShort(dis.readShort())); + delegate.size++; + } + break; + case 'i': + for (int i = 0; i < count; i++, index++) { + Array.setInt(data, index, dis.readInt()); + delegate.size++; + } + break; + case 'I': + for (int i = 0; i < count; i++, index++) { + Array.setLong(data, index, unsignedInt(dis.readInt())); + delegate.size++; + } + break; + case 'l': + for (int i = 0; i < count; i++, index++) { + Array.setLong(data, index, dis.readLong()); + delegate.size++; + } + break; + case 'L': // faking it + for (int i = 0; i < count; i++, index++) { + Array.setLong(data, index, dis.readLong()); + delegate.size++; + } + break; + case 'f': + for (int i = 0; i < count; i++, index++) { + Array.setFloat(data, index, dis.readFloat()); + delegate.size++; + } + break; + case 'd': + for (int i = 0; i < count; i++, index++) { + Array.setDouble(data, index, dis.readDouble()); + delegate.size++; + } + break; } } dis.close(); @@ -941,7 +1014,7 @@ */ @ExposedMethod final void array_fromstring(String input) { - int itemsize = getItemsize(); + int itemsize = getStorageSize(); int strlen = input.length(); if((strlen % itemsize) != 0) { throw Py.ValueError("string length not a multiple of item size"); @@ -1077,6 +1150,43 @@ return 0; } + public int getStorageSize() { + if (type.isPrimitive()) { + switch (typecode.charAt(0)) { + case 'z': + return 1; + case 'b': + return 1; + case 'B': + return 1; + case 'u': + return 4; + case 'c': + return 1; + case 'h': + return 2; + case 'H': + return 2; + case 'i': + return 4; + case 'I': + return 4; + case 'l': + return 8; + case 'L': + return 8; + case 'f': + return 4; + case 'd': + return 8; + default: + throw Py.ValueError("bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); + } + } + // return something here... could be a calculated size? + return 0; + } + /** * Retrieve a slice from the array specified by the <em>start</em>, * <em>stop</em> and <em>step</em>. @@ -1543,36 +1653,128 @@ */ private int toStream(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); - if(type.isPrimitive()) { - if(type == Boolean.TYPE) { + switch (typecode.charAt(0)) { + case 'z': for(int i = 0; i < delegate.getSize(); i++) dos.writeBoolean(Array.getBoolean(data, i)); - } else if(type == Byte.TYPE) { + break; + case 'b': for(int i = 0; i < delegate.getSize(); i++) dos.writeByte(Array.getByte(data, i)); - } else if(type == Character.TYPE) { + break; + case 'B': for(int i = 0; i < delegate.getSize(); i++) - dos.writeByte((byte)Array.getChar(data, i)); - } else if(type == Integer.TYPE) { + dos.writeByte(signedByte(Array.getShort(data, i))); + break; + case 'u': + // use 32-bit integers since we want UCS-4 storage for(int i = 0; i < delegate.getSize(); i++) dos.writeInt(Array.getInt(data, i)); - } else if(type == Short.TYPE) { + break; + case 'c': for(int i = 0; i < delegate.getSize(); i++) + dos.writeByte((byte)Array.getChar(data, i)); + break; + case 'h': + for(int i = 0; i < delegate.getSize(); i++) dos.writeShort(Array.getShort(data, i)); - } else if(type == Long.TYPE) { + break; + case 'H': for(int i = 0; i < delegate.getSize(); i++) + dos.writeShort(signedShort(Array.getInt(data, i))); + break; + case 'i': + for(int i = 0; i < delegate.getSize(); i++) + dos.writeInt(Array.getInt(data, i)); + break; + case 'I': + for(int i = 0; i < delegate.getSize(); i++) + dos.writeInt(signedInt(Array.getLong(data, i))); + break; + case 'l': + for(int i = 0; i < delegate.getSize(); i++) dos.writeLong(Array.getLong(data, i)); - } else if(type == Float.TYPE) { + break; + case 'L': // faking it for(int i = 0; i < delegate.getSize(); i++) + dos.writeLong(Array.getLong(data, i)); + break; + case 'f': + for(int i = 0; i < delegate.getSize(); i++) dos.writeFloat(Array.getFloat(data, i)); - } else if(type == Double.TYPE) { + break; + case 'd': for(int i = 0; i < delegate.getSize(); i++) dos.writeDouble(Array.getDouble(data, i)); - } + break; } return dos.size(); } + private static byte signedByte(short x) { + if (x >= 128 && x < 256) { + return (byte)(x - 256); + } + else if (x >= 0) { + return (byte)x; + } + else { + throw Py.ValueError("invalid storage"); + } + } + + private static short signedShort(int x) { + if (x >= 32768 && x < 65536) { + return (short)(x - 65536); + } + else if (x >= 0) { + return (short)x; + } + else { + throw Py.ValueError("invalid storage"); + } + } + + private static int signedInt(long x) { + if (x >= 2147483648L && x < 4294967296L) { + return (int)(x - 4294967296L); + } + else if (x >= 0) { + return (int)x; + } + else { + throw Py.ValueError("invalid storage"); + } + } + + private static short unsignedByte(byte x) { + if (x < 0) { + return (short)(x + 256); + } + else { + return x; + } + } + + private static int unsignedShort(short x) { + if (x < 0) { + return x + 65536; + } + else { + return x; + } + } + + private static long unsignedInt(int x) { + if (x < 0) { + return x + 4294967296L; + } + else { + return x; + } + + } + @ExposedMethod public final PyObject array_tostring(){ return new PyString(tostring()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fwi...@us...> - 2009-02-09 02:22:13
|
Revision: 6023 http://jython.svn.sourceforge.net/jython/?rev=6023&view=rev Author: fwierzbicki Date: 2009-02-09 01:40:53 +0000 (Mon, 09 Feb 2009) Log Message: ----------- Fix for http://bugs.jython.org/issue1243. Thanks RJ Ryan for suggesting the fix. Modified Paths: -------------- trunk/jython/src/org/python/core/Py.java Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-02-07 21:26:31 UTC (rev 6022) +++ trunk/jython/src/org/python/core/Py.java 2009-02-09 01:40:53 UTC (rev 6023) @@ -1623,7 +1623,7 @@ // w/o compiler-flags public static PyObject compile(InputStream istream, String filename, String kind) { - return compile_flags(istream, filename, kind, null); + return compile_flags(istream, filename, kind, new CompilerFlags()); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <zy...@us...> - 2009-02-07 21:26:34
|
Revision: 6022 http://jython.svn.sourceforge.net/jython/?rev=6022&view=rev Author: zyasoft Date: 2009-02-07 21:26:31 +0000 (Sat, 07 Feb 2009) Log Message: ----------- Fixed control flow by pushing the wrapped Why onto the stack (instead of its string representation from the earliest version of this code). Modified Paths: -------------- branches/pbcvm/src/org/python/core/PyBytecode.java Modified: branches/pbcvm/src/org/python/core/PyBytecode.java =================================================================== --- branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 18:14:33 UTC (rev 6021) +++ branches/pbcvm/src/org/python/core/PyBytecode.java 2009-02-07 21:26:31 UTC (rev 6022) @@ -706,9 +706,16 @@ if (why == Why.RETURN || why == Why.CONTINUE) { retval = stack.pop(); } - } else if ((v instanceof PyStackException) || (v instanceof PyString)) { + } else if (v instanceof PyStackException) { ts.exception = ((PyStackException) v).exception; why = Why.RERAISE; + + } else if (v instanceof PyString) { + if (debug) { + System.err.println("ts.exception=" + ts.exception + ", v=" + v); +// ts.exception = new PyException(v); // XXX - what do I do about string exceptions??!!! + } + why = Why.RERAISE; } else if (v != Py.None) { throw Py.SystemError("'finally' pops bad exception"); } @@ -1059,15 +1066,6 @@ case Opcode.MAKE_CLOSURE: { PyCode code = (PyCode) stack.pop(); 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); @@ -1144,13 +1142,12 @@ stack.push(exc.traceback); stack.push(exc.value); stack.push(new PyStackException(exc)); // instead of stack.push(exc.type);, like CPython -// stack.dup(); // x3 to conform with CPython's calling convention, which -// stack.dup(); // stores the type, val, tb separately on the stack } else { if (why == Why.RETURN || why == Why.CONTINUE) { stack.push(retval); } - stack.push(Py.newString(why.name())); // XXX - hack! + // FIGURE OUT THE FLOW HERE, in test_optparse2 + stack.push(new PyStackWhy(why)); } why = Why.NOT; next_instr = b.b_handler; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |