From: <pj...@us...> - 2009-04-04 01:17:09
|
Revision: 6156 http://jython.svn.sourceforge.net/jython/?rev=6156&view=rev Author: pjenvey Date: 2009-04-04 01:16:56 +0000 (Sat, 04 Apr 2009) Log Message: ----------- more robust handling of pickle loading errors Modified Paths: -------------- trunk/jython/src/org/python/modules/cPickle.java Modified: trunk/jython/src/org/python/modules/cPickle.java =================================================================== --- trunk/jython/src/org/python/modules/cPickle.java 2009-04-03 16:43:24 UTC (rev 6155) +++ trunk/jython/src/org/python/modules/cPickle.java 2009-04-04 01:16:56 UTC (rev 6156) @@ -1670,6 +1670,9 @@ case LONG4: load_bin_long(4); break; case STOP: return load_stop(); + default: + throw new PyException(UnpicklingError, + String.format("invalid load key, '%s'.", key)); } } } @@ -2150,40 +2153,43 @@ } private void load_build() { - PyObject value = pop(); + PyObject state = pop(); PyObject inst = peek(); PyObject setstate = inst.__findattr__("__setstate__"); - if(setstate == null) { - PyObject slotstate = null; - // A default __setstate__. First see whether state - // embeds a slot state dict too (a proto 2 addition). - if (value instanceof PyTuple && value.__len__() == 2) { - PyObject temp = value; - value = temp.__getitem__(0); - slotstate = temp.__getitem__(1); - } + if (setstate != null) { + // The explicit __setstate__ is responsible for everything. + setstate.__call__(state); + return; + } - PyObject dict; - if(inst instanceof PyInstance) { - dict = ((PyInstance)inst).__dict__; - } else { - dict = inst.getDict(); + // A default __setstate__. First see whether state embeds a slot state dict + // too (a proto 2 addition). + PyObject slotstate = null; + if (state instanceof PyTuple && state.__len__() == 2) { + PyObject temp = state; + state = temp.__getitem__(0); + slotstate = temp.__getitem__(1); + } + + if (state != Py.None) { + if (!(state instanceof PyDictionary)) { + throw new PyException(UnpicklingError, "state is not a dictionary"); } - dict.__findattr__("update").__call__(value); + PyObject dict = inst.__getattr__("__dict__"); + for (PyObject item : ((PyDictionary)state).iteritems().asIterable()) { + dict.__setitem__(item.__getitem__(0), item.__getitem__(1)); + } + } - // Also set instance attributes from the slotstate - // dict (if any). - if (slotstate != null) { - if (!(slotstate instanceof PyDictionary)) { - throw new PyException(UnpicklingError, "slot state is not a dictionary"); - } - for (PyObject item : ((PyDictionary)slotstate).iteritems().asIterable()) { - inst.__setattr__(PyObject.asName(item.__getitem__(0)), - item.__getitem__(1)); - } + // Also set instance attributes from the slotstate dict (if any). + if (slotstate != null) { + if (!(slotstate instanceof PyDictionary)) { + throw new PyException(UnpicklingError, "slot state is not a dictionary"); } - } else { - setstate.__call__(value); + for (PyObject item : ((PyDictionary)slotstate).iteritems().asIterable()) { + inst.__setattr__(PyObject.asName(item.__getitem__(0)), + item.__getitem__(1)); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pj...@us...> - 2009-04-16 05:48:48
|
Revision: 6230 http://jython.svn.sourceforge.net/jython/?rev=6230&view=rev Author: pjenvey Date: 2009-04-16 05:48:35 +0000 (Thu, 16 Apr 2009) Log Message: ----------- make BadPickleGet an actual exception Modified Paths: -------------- trunk/jython/src/org/python/modules/cPickle.java Modified: trunk/jython/src/org/python/modules/cPickle.java =================================================================== --- trunk/jython/src/org/python/modules/cPickle.java 2009-04-16 03:11:53 UTC (rev 6229) +++ trunk/jython/src/org/python/modules/cPickle.java 2009-04-16 05:48:35 UTC (rev 6230) @@ -384,11 +384,8 @@ public static PyObject PicklingError; public static PyObject UnpickleableError; public static PyObject UnpicklingError; + public static PyObject BadPickleGet; - public static final PyString BadPickleGet = - new PyString("cPickle.BadPickleGet"); - - final static char MARK = '('; final static char STOP = '.'; final static char POP = '0'; @@ -510,6 +507,7 @@ PicklingError = Py.makeClass("PicklingError", PickleError, exceptionNamespace()); UnpickleableError = Py.makeClass("UnpickleableError", PicklingError, _UnpickleableError()); UnpicklingError = Py.makeClass("UnpicklingError", PickleError, exceptionNamespace()); + BadPickleGet = Py.makeClass("BadPickleGet", UnpicklingError, exceptionNamespace()); } public static PyObject exceptionNamespace() { @@ -2067,16 +2065,18 @@ final private void load_get() { String py_str = file.readlineNoNl(); PyObject value = memo.get(py_str); - if (value == null) + if (value == null) { throw new PyException(BadPickleGet, py_str); + } push(value); } final private void load_binget() { String py_key = String.valueOf((int)file.read(1).charAt(0)); PyObject value = memo.get(py_key); - if (value == null) + if (value == null) { throw new PyException(BadPickleGet, py_key); + } push(value); } @@ -2084,8 +2084,9 @@ int i = read_binint(); String py_key = String.valueOf(i); PyObject value = memo.get(py_key); - if (value == null) + if (value == null) { throw new PyException(BadPickleGet, py_key); + } push(value); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |