473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Mixing protocols in pickles?

Is there any prohibition against mixing different protocols within the
same pickle? I don't see anything about this in the Python Library
Reference and, after all, the pickle.dump function takes a protocol
argument for each time it's called. (This is in Python 2.3.3.)

I have a pickle containing two objects: a tag string and a (large)
object containing many children. The identifying string is there so
that you can unpickle it and decide whether you really want to unpickle
the whole data object.

So I thought it would be clever to write the tag string with protocol 0
so it would show up in a file viewer as plain text and then write the
rest of the data with protocol 1 (or 2; it doesn't use new-style
classes, though). I open the file in binary mode and then dump the tag
string in protocol 0, then the (big) instance data in protocol 1. When
loading time comes around, they're just both loaded in the same order:

def load(filename=DEFAULT_FILENAME):
try:
inputFile = gzip.GzipFile(filename + COMPRESSED_EXTENSION, 'rb')
except IOError:
inputFile = file(filename, 'rb')
tag = pickle.load(inputFile)
if DEBUG:
print >> sys.stderr, "Tag: %s" % tag
system = pickle.load(inputFile)
inputFile.close()
return system

def save(system, tag, filename=DEFAULT_FILENAME, protocol=1,
compressed=False):
if compressed:
outputFile = gzip.GzipFile(filename + COMPRESSED_EXTENSION,
'wb')
else:
outputFile = file(filename, 'wb')
pickle.dump(tag, outputFile, 0) # write the tag in text
pickle.dump(system, outputFile, protocol)
outputFile.close()

This works fine on Unix, but on Windows it generates the (utterly
puzzling) error:

C:\My Documents\botec-0.1x1>python -i ./botex.py
Tag: default.botec:20040110:11175:ebec37a7632cc7176ff35 9a3754750ec:0.1x1
Traceback (most recent call last):
File "./botex.py", line 70, in ?
SYSTEM = init()
File "./botex.py", line 15, in init
return load()
File "C:\My Documents\botec-0.1x1\botec.py", line 1666, in load
system = pickle.load(inputFile)
ImportError: No Module named copy_reg


.... made extra puzzling because you can type `import copy_reg' on that
prompt and it will import fine. Googling for this error comes up with a
few scattered complaints but nothing coherent about the cause. When I
modify the dumping routine to use the same protocol (1) for both dumps,
the problem goes away and everything works fine.

So I guess there are a few questions: Why is the error being generated
so obscure and seemingly incorrect? Is it really the case that mixing
multiple protocols within the same pickle isn't allowed, or is this
truly a bug (or, say, a Windows-specific problem because protocol 0
requires that the pickle file be in text mode?)?

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Love is the most subtle form of self-interest.
-- Holbrook Jackson
Jul 18 '05 #1
0 1785

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alexander Schumann | last post by:
Hello everyone, We are building a new web based e-mail client "Fiezel" that will support IMAP, POP3 and SMTP protocols. Will be written in php4. I am trying to collect all the infomation on SNMP...
7
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
2
by: Mark Olbert | last post by:
I'm looking to create a blogging component (or find one) that would basically be a "drop in" on an ASP.NET page (i.e., I don't want full blown blog theme management and page configuration...
0
by: John | last post by:
Hi, All, I have a web method like this: public string test(int32 i) { return i.ToString(); } and If I pass an empty string to test it by using HTTP POST I got the
1
by: Mae Lim | last post by:
Dear all, I'm having problem with C# Web Services, in the Web Services it suppose to call another Web Services to return XmlNode to current Web Services. Below is the sample code :- ...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
28
by: ziman137 | last post by:
Hello all, I have a question and am seeking for some advice. I am currently working to implement an algorithmic library. Because the performance is the most important factor in later...
1
by: jayhunters14 | last post by:
Hi everyone, i am an undergrad in physics.my thesis is about GPRS.i have a wireless module that sets up a connection to the internet. when i connect to the web server it can communicate using http...
0
by: John H | last post by:
Hi, have read quite a few articles http://www.devx.com/codemag/Article/33655/0/page/6 http://msdn.microsoft.com/en-us/library/bb332338.aspx and still slightly confused as to what protcols can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.