Commit Graph

98 Commits

Author SHA1 Message Date
Tim Peters 8fda7bc48d save_int(): Fixed two new off-by-1 glitches. 2003-01-28 03:40:52 +00:00
Guido van Rossum e0b904232f Add a comment explaining that struct.pack() beats marshal.dumps(), but
marshal.loads() beats struct.unpack()!  Possibly because the latter
creates a one-tuple. :-(
2003-01-28 03:17:21 +00:00
Guido van Rossum 5c938d00a1 Got rid of mdumps; I timed it, and struct.pack("<i", x) is more than
40% faster than marshal.dumps(x)[1:]!  (That's not counting the
module attribute lookups, which can be avoided in either case.)
2003-01-28 03:03:08 +00:00
Tim Peters f558da0f90 save_tuple(): Minor rewriting, and added a comment about the subtlety
created by recursive tuples.
2003-01-28 02:09:55 +00:00
Tim Peters 209ad95b00 load_appends(): replaced .append() loop with an .extend(). 2003-01-28 01:44:45 +00:00
Tim Peters c23d18a955 Comments. 2003-01-28 01:41:51 +00:00
Tim Peters 064567e41a save_dict(): Untangled most of the bin-vs-not-bin logic. Also used
iteritems() instead of materializing a (possibly giant) list of the
items.
2003-01-28 01:34:43 +00:00
Tim Peters 21c18f0bf5 save_list(): Rewrote, to untangle the proto 0 from the proto 1 cases.
The code is much easier to follow now, and I bet it's faster too.
2003-01-28 01:15:46 +00:00
Tim Peters 22dc6f4f7a save_list(): removed unused local "d". 2003-01-28 01:07:48 +00:00
Tim Peters 58d7846e68 save_list(): removed unused local "memo". 2003-01-28 01:07:10 +00:00
Tim Peters bd1cdb9227 save_pers(): Switched the order of cases, to get rid of a "not", and to
make the bin-vs-not-bin order consistent with what other routines try to
do (they almost all handle the bin case first).
2003-01-28 01:03:10 +00:00
Tim Peters 518df0dae4 Several routines appeared to inline the guts of memoize(), possibly for
some notion of low-level efficiency.  Undid that, but left one routine
alone:  save_inst() claims it has a reason for not using memoize().
I don't understand that comment, so added an XXX comment there.
2003-01-28 01:00:38 +00:00
Tim Peters b32a8317d7 save(): Fiddled the control flow to put the normal case where it
belongs.  This is a much smaller change than it may appear:  the bulk
of the function merely got unindented by one level.
2003-01-28 00:48:09 +00:00
Tim Peters c9d7c4a656 Added XXX about save()'s special-casing of tuples -- I don't get it. 2003-01-28 00:43:26 +00:00
Tim Peters 22987e3cbd save_bool(): simplified. 2003-01-28 00:26:14 +00:00
Tim Peters cbd0a32d09 Repaired grammar in new comment. 2003-01-28 00:24:43 +00:00
Tim Peters b377f8ac3e clear_memo(): Repaired grammar in docstring. 2003-01-28 00:23:36 +00:00
Tim Peters e105478a0c Added one-line comments to the proto 2 opcodes. 2003-01-28 00:22:12 +00:00
Tim Peters fdb8cfab08 Removed the new LONG2 opcode: it's extravagant. If LONG1 isn't enough,
then the embedded argument consumes at least 256 bytes.  The difference
between a 3-byte prefix (LONG2 + 2 bytes) and a 5-byte prefix (LONG4 +
4 bytes) is at worst less than 1%.  Note that binary strings and binary
Unicode strings also have only "size is 1 byte, or size is 4 bytes?"
flavors, and I expect for the same reason.  The only place a 2-byte
thingie was used was in BININT2, where the 2 bytes make up the *entire*
embedded argument (and now EXT2 also does this); that's a large savings
over 4 bytes, because the total opcode+argument size is so small in
the BININT2/EXT2 case.

Removed the TAKEN_FROM_ARGUMENT "number of bytes" code, and bifurcated it
into TAKEN_FROM_ARGUMENT1 and TAKEN_FROM_ARGUMENT4.  Now there's enough
info in ArgumentDescriptor objects to deduce the # of bytes consumed by
each opcode.

Rearranged the order in which proto2 opcodes are listed in pickle.py.
2003-01-28 00:13:19 +00:00
Guido van Rossum f29d3d6011 Begin the change from 'binary vs. text mode' to 'protocol 0, 1, 2'.
The protocol now defaults to 1.  Protocol 2 is still unimplemented.
2003-01-27 22:47:53 +00:00
Guido van Rossum 5a2d8f5e9a Begin documenting protocol 2. 2003-01-27 21:44:25 +00:00
Tim Peters bb38e306ae Added some comments. 2003-01-27 21:25:41 +00:00
Tim Peters e46b73f5b3 memoize(): Reworded the docs to try to disentangle the Pickler's memo
dict from the Unpickler's memo (which is a different beast!).
2003-01-27 21:22:10 +00:00
Tim Peters bbf63cd05c Using marshal functions to pack & unpack 1-byte ints is an obscure &
expensive way to spell chr() and ord().
2003-01-27 21:15:36 +00:00
Tim Peters 22a449aacc Added a brief comment to each pickle opcode declaration. 2003-01-27 20:16:36 +00:00
Jeremy Hylton 3422c99de1 Raise PicklingError when __reduce__() fails, and
add memoize() helper function to update the memo.

The first element of the tuple returned by __reduce__() must be a
callable.  If it isn't the Unpickler will raise an error.  Catch this
error in the pickler and raise the error there.

The memoize() helper also has a comment explaining how the memo
works.  So methods can't use memoize() because the write funny codes.
2003-01-24 19:29:52 +00:00
Jeremy Hylton 5e0f4e73a9 Remove inst_persistent_id() WANNI (we ain't never needed it).
Add some simple tests of the persistence hooks.
2002-11-13 22:01:27 +00:00
Jeremy Hylton f0cfdf7314 Fiddle comments and variable names in whichmodule(). 2002-09-19 23:00:12 +00:00
Jeremy Hylton 065a5ab8fb whichmodule() should skip dummy package entries in sys.modules.
This fixes the charming, but unhelpful error message for
>>> pickle.dumps(type.__new__)
Can't pickle <built-in method __new__ of type object at 0x812a440>: it's not the same object as datetime.math.__new__

Bugfix candidate.
2002-09-19 22:57:26 +00:00
Martin v. Löwis 8a8da798a5 Patch #505705: Remove eval in pickle and cPickle. 2002-08-14 07:46:28 +00:00
Jeremy Hylton 39c6116483 Given the persistent id code a shot at a class before calling save_global().
Some persistent picklers (well, probably, the *only* persistent
pickler) would like to pickle some classes in a special way.
2002-07-16 19:47:43 +00:00
Raymond Hettinger 46ac8eb3c8 Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i) 2002-06-30 03:39:14 +00:00
Raymond Hettinger 54f0222547 SF 563203. Replaced 'has_key()' with 'in'. 2002-06-01 14:18:47 +00:00
Neal Norwitz efbb67b1a7 Remove comment about inheritance, look one line up 2002-05-30 12:12:04 +00:00
Raymond Hettinger aef22fb9cd Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2). 2002-05-29 16:18:42 +00:00
Tim Peters 8ac1495a6a Whitespace normalization. 2002-05-23 15:15:30 +00:00
Raymond Hettinger 97394bc795 Patch 533291. Deprecate None return form of __reduce__. 2002-05-21 17:22:02 +00:00
Fred Drake 7f781c9aab Add Pickler.clear_memo() so the pickle and cPickle modules are more similar. 2002-05-01 20:33:53 +00:00
Guido van Rossum e276339cea Implement an idea by Paul Rubin:
Change pickling format for bools to use a backwards compatible
encoding.  This means you can pickle True or False on Python 2.3
and Python 2.2 or before will read it back as 1 or 0.  The code
used for pickling bools before would create pickles that could
not be read in previous Python versions.
2002-04-05 19:30:08 +00:00
Guido van Rossum 77f6a65eb0 Add the 'bool' type and its values 'False' and 'True', as described in
PEP 285.  Everything described in the PEP is here, and there is even
some documentation.  I had to fix 12 unit tests; all but one of these
were printing Boolean outcomes that changed from 0/1 to False/True.
(The exception is test_unicode.py, which did a type(x) == type(y)
style comparison.  I could've fixed that with a single line using
issubtype(x, type(y)), but instead chose to be explicit about those
places where a bool is expected.

Still to do: perhaps more documentation; change standard library
modules to return False/True from predicates.
2002-04-03 22:41:51 +00:00
Guido van Rossum 85ee491b3a Fix for SF 502085.
Don't die when issubclass(t, TypeType) fails.

Bugfix candidate (but I think it's too late for 2.2.1).
2002-03-26 00:51:56 +00:00
Neal Norwitz d5ba4aeab0 SF #515018, delete global variable that was apparently used only
in a list comprehension.
2002-02-11 18:12:06 +00:00
Guido van Rossum f048a8f6d7 Pickler.save(): Fix for SF bug #494904: Cannot pickle a class with a
metaclass, reported by Dan Parisien.

Objects that are instances of custom metaclasses, i.e. whose class is
a subclass of 'type', should be pickled the same as new-style classes
(objects whose class is 'type').  This can't be done through a
dispatch table entry, and the __reduce__ trick doesn't work for these,
since it finds the unbound __reduce__ for instances of the class
(inherited from 'object').  So check explicitly using issubclass().
2001-12-19 16:55:02 +00:00
Barry Warsaw bf4d959d28 Two changes:
load_inst(): Implement the security hook that cPickle already had.
When unpickling callables which are not classes, we look to see if the
object has an attribute __safe_for_unpickling__.  If this exists and
has a true value, then we can call it to create the unpickled object.
Otherwise we raise an UnpicklingError.

find_class(): We no longer mask ImportError, KeyError, and
AttributeError by transforming them into SystemError.  The latter is
definitely not the right thing to do, so we let the former three
exceptions simply propagate up if they occur, i.e. we remove the
try/except!
2001-11-15 23:42:58 +00:00
Jeremy Hylton 20747fa167 A better new, unique object 2001-11-09 16:15:04 +00:00
Jeremy Hylton abe2c62bdb Use cStringIO when available.
Remove test code.  It's available in Lib/test/picklertester.py.
2001-10-15 21:29:28 +00:00
Guido van Rossum dbb718fa87 Make these modules work when Python is compiled without Unicode support. 2001-09-21 19:22:34 +00:00
Tim Peters 19ef62d5a9 pickle.py, load_int(): Match cPickle's just-repaired ability to unpickle
64-bit INTs on 32-bit boxes (where they become longs).  Also exploit that
int(str) and long(str) will ignore a trailing newline (saves creating a
new string at the Python level).

pickletester.py:  Simulate reading a pickle produced by a 64-bit box.
2001-08-28 22:21:18 +00:00
Guido van Rossum b0a98e9c94 Address SF #451547. The approach is a bit draconian: any object that
is pickled as a global must now exist by the name under which it is
pickled, otherwise the pickling fails.  Previously, such things would
fail on unpickling, or unpickle as the wrong global object.  I'm
hoping that this won't break existing code that is playing tricks with
this.

I need a volunteer to do this for cPickle too.
2001-08-17 18:49:52 +00:00
Tim Peters 6d6c1a35e0 Merge of descr-branch back into trunk. 2001-08-02 04:15:00 +00:00