[2.2] Weird behaviour of file.write() (no data written)?
Mike C. Fletcher
mcfletch at rogers.com
Tue Apr 16 00:25:36 EDT 2002
I'm trying to figure out what's going wrong with my index-storing
routines. These are dirt-simple pickle-dumps to disk. I must be doing
something terribly obviously wrong, but darned if I can figure out what
it is.
Here's the relevant code:
fileHandle = open( self.filename,'wb',0)
values = {}
for key, value in self.tables.items():
if isinstance( key, Persistent ):
if not hasattr( key, '_p_oid'):
raise ValueError( """Attempting to store a collection index for a
collection not currently in the database %s"""%( key))
key = key._p_oid
values[key] = value
self.tables = values
print 'dumping indices to', self.filename
data = dumps( values)
fileHandle.write( data)
fileHandle.write( '')
fileHandle.flush()
fileHandle.close()
print 'fileHandle closed'
What happens with that code is that a 0-length file is created on disk,
despite the pickle being 7 or 8 kb in length. Now, this code works (no
clue why it's even different):
values = {}
for key, value in self.tables.items():
if isinstance( key, Persistent ):
if not hasattr( key, '_p_oid'):
raise ValueError( """Attempting to store a collection index for a
collection not currently in the database %s"""%( key))
key = key._p_oid
values[key] = value
self.tables = values
print 'dumping indices to', self.filename
data = dumps( values)
fileHandle = open( self.filename,'wb',0)
fileHandle.write( data)
fileHandle.write( '')
fileHandle.flush()
fileHandle.close()
print 'fileHandle closed'
(The only difference being _when_ the fileHandle object is created).
I've tried buffered and unbuffered files, using pickle.dump instead of
dumps, and just about everything else I could think of.
The code is getting called in the OnClose handler of a wxPython wxFrame
object. Zope 3's ZODB is being used (it's what's pulling in
Persistent), but I'm not using it for the indices (the only things in
the indices are readily pickle-able objects).
There's lots of alpha or beta-quality C software running around this
application (Zope3 and wxPython 2.3.3pre2, respectively), but I'd be
surprised if it could affect something as stable as file.write without
causing something a little more obvious to occur.
Ideas welcome,
Mike
_______________________________________
Mike C. Fletcher
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list