shelve implementation

David Fisher python at rose164.wuh.wustl.edu
Tue Feb 1 11:46:47 CET 2000


>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 1/31/00, 10:34:36 AM, Robin Becker <robin at jessikat.demon.co.uk> wrote 
regarding shelve implementation:


> can someone explain why shelve uses StringIO as an intermediate rather
> than doing the pickles direct

> ie shelve uses
>         def __getitem__(self, key):
>                 f = StringIO(self.dict[key])
>                 return Unpickler(f).load()

>         def __setitem__(self, key, value):
>                 f = StringIO()
>                 p = Pickler(f)
>                 p.dump(value)
>                 self.dict[key] = f.getvalue()


> whereas I would have done
>         def __getitem__(self, key):
>                 return loads(self.dict[key])

>         def __setitem__(self, key, value):
>                 self.dict[key] = dumps(value)

Well, I wasn't there but...  After glancing through shelve.py, 
pickle.py, cPickle.c, and marshal.c, and consulting with my 
numerologist, I have constructed the following plausible (i.e. Wrong) 
explaination.

The code in shelve would be faster than your code if the module 
cStringIO was available and the module cPickle wasn't.  Loads() and 
dumps() don't get rid of the StringIO() call, just move it into the 
pickle module, and pickle imports StringIO() from module StringIO, not 
cStringIO.  After consulting a chicken's innards, I suspect (i.e. 
Don't know) that cStringIO wasn't available at the time, and was 
actually inspired by the existance of pickle (now i'm really 
reaching).

Anyway, to make a long story short, i'm sure it made sense at the 
time.
If you wait a while, i'm sure that someone who was actually there, 
constructing mnemonic circuits with stone knives and bear skins, will 
tell you what they were thinking <wink>.

On another archeologial note, both cStringIO and cPickle are copyright 
Digital Creations and are the only modules that i have found:

#define UNLESS(E) if(!(E))

which lets them write code like:

	UNLESS(PyArg_ParseTuple(...))
		return NULL;

which i think is unbearably cute.







More information about the Python-list mailing list