shelve keeping old (unused) data around?

A

AK

Hello,

I'm using shelve module in python 2.3 and I found that it does a very
odd thing, at least very unexpected to me. It seems that the data of a
replaced or deleted key stays in filename.dat file.

Example:
d = [1]*1000
from shelve import *
s = open('test')
s['test'] = d
s.close()

test.dat is 4100 bytes
d = 1
s = open('test')
s['test'] = d
s.close()

test.dat is still 4100 bytes

If I do del s['test'], it stays the same.
 
S

Skip Montanaro

nospam> I'm using shelve module in python 2.3 and I found that it does a
nospam> very odd thing, at least very unexpected to me. It seems that
nospam> the data of a replaced or deleted key stays in filename.dat
nospam> file.

The bits which actually get written to the disk depend on the underlying
library writing those bits. The shelve module has no say in the matter.
All it does to create a file is ask anydbm to open it, then pickle the
values so they can be written out as strings. Again, shelve doesn't do that
directly. It asks the object created by anydbm to do that. You didn't say
(perhaps you don't know) what the underlying library is on your system. It
seems likely that the library deletes the key, but leaves the data dangling
(perhaps also marked free so it can reuse it at a later time).

Skip
 
A

AK

nospam> I'm using shelve module in python 2.3 and I found that it does a
nospam> very odd thing, at least very unexpected to me. It seems that
nospam> the data of a replaced or deleted key stays in filename.dat
nospam> file.

The bits which actually get written to the disk depend on the underlying
library writing those bits. The shelve module has no say in the matter.
All it does to create a file is ask anydbm to open it, then pickle the
values so they can be written out as strings. Again, shelve doesn't do that
directly. It asks the object created by anydbm to do that. You didn't say
(perhaps you don't know) what the underlying library is on your system. It
seems likely that the library deletes the key, but leaves the data dangling
(perhaps also marked free so it can reuse it at a later time).

Skip

Okay, as long as it's not a bug.. For the record, it seems like it's
bsddb on this side. Thanks, Skip!

-AK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top