473,790 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pickled text file causing ValueError (dos/unix issue)

Hello everyone,

I started to use pickle to store the latest user settings for the tool
I wrote. It writes out a pickled text file when it terminates and it
restores the settings when it starts.

It worked very nicely.

However, I got a ValueError when I started the tool from Unix when I
previously used the tool from Windows.

File "/usr/local/lib/python2.3/pickle.py", line 980, in load_string
raise ValueError, "insecure string pickle"
ValueError: insecure string pickle

If I do 'dos2unix <my.cfg> <my.cfg>' to convert the file, then
everything
becomes fine.

I found in the Python release note saying ...
"pickle: Now raises ValueError when an invalid pickle that contains a
non-string repr where a string repr was expected. This behavior matches
cPickle."

I guess DOS text format is creating this problem.
My question is "Is there any elegant way to deal with this?".

I certainly can catch ValueError and run 'dos2unix' explicitly.
But I don't like such crude solution.
Any suggestions would be highly appreciated.

Best regards,
Aki Niimura

Jul 18 '05 #1
12 4274
Open the file on windows for writing with "wb" mode, the b is for binary.
Jul 18 '05 #2
[Aki Niimura]
I started to use pickle to store the latest user settings for the tool
I wrote. It writes out a pickled text file when it terminates and it
restores the settings when it starts. .... I guess DOS text format is creating this problem.
Yes.
My question is "Is there any elegant way to deal with this?".


Yes: regardless of platform, always open files used for pickles in
binary mode. That is, pass "rb" to open() when reading a pickle file,
and "wb" to open() when writing a pickle file. Then your pickle files
will work unchanged on all platforms. The same is true of files
containing binary data of any kind (and despite that pickle protocol 0
was called "text mode" for years, it's still binary data).
Jul 18 '05 #3
Tim Peters wrote:
Yes: regardless of platform, always open files used for pickles in
binary mode. That is, pass "rb" to open() when reading a pickle file,
and "wb" to open() when writing a pickle file. Then your pickle files
will work unchanged on all platforms. The same is true of files
containing binary data of any kind (and despite that pickle protocol 0
was called "text mode" for years, it's still binary data).


I've been wondering why there even is the choice between binary mode
and text mode. Why can't we just do away with the 'text mode' ?
What does it do, anyways? At least, if it does something, I'm sure
that it isn't something that can be done in Python itself if
really required to do so...

--Irmen

Jul 18 '05 #4
[Irmen de Jong]
I've been wondering why there even is the choice between binary mode
and text mode. Why can't we just do away with the 'text mode' ?
What does it do, anyways? At least, if it does something, I'm sure
that it isn't something that can be done in Python itself if
really required to do so...


It's not Python's decision, it's the operating system's. Whether
there's an actual difference between text mode and binary mode is up
to the operating system, and, if there is an actual difference, every
detail about what the difference(s) consists of is also up to the
operating system. That differences may exist is reflected in the C
standard, and the rules for text-mode files are more restrictive than
most people would believe.

On Unixish systems, there's no difference. On Windows boxes, there
are conceptually small differences with huge consequences, and the
distinction appears to be kept just for backward-compatibility
reasons. On some other systems, text and binary files are entirely
different kinds of beasts.

If Python didn't offer text mode then it would be clumsy at best to
use Python to write ordinary human-readable text files in the format
that native software on Windows, and Mac Classic, and VAX (and ...)
expects (and the native format for text mode differs across all of
them). If Python didn't offer binary mode then it wouldn't be
possible to use Python to process data in binary files on Windows and
Mac Classic and VAX (and ...). If Python used its own
platform-independent file format, then it would end up creating files
that other programs wouldn't be able to deal with.

Live with it <wink>.
Jul 18 '05 #5
Irmen de Jong wrote:
Tim Peters wrote:
Yes: regardless of platform, always open files used for pickles in
binary mode. That is, pass "rb" to open() when reading a pickle file, and "wb" to open() when writing a pickle file. Then your pickle files will work unchanged on all platforms. The same is true of files
containing binary data of any kind (and despite that pickle protocol 0 was called "text mode" for years, it's still binary data).


I've been wondering why there even is the choice between binary mode
and text mode. Why can't we just do away with the 'text mode' ?


We can't because characters and bytes are not the same things. But I
believe what you're really complaining about is that "t" mode sometimes
mysteriously corrupts data if processed by the code that expects binary
files. In Python 3.0 it will be fixed because file.read will have to
return different objects: bytes for "b" mode, str for "t" mode. It
would be great if file type was split into binfile and textfile,
removing need for cryptic "b" and "t" modes but I'm afraid that's too
much of a change even for Python 3.0

Serge.

Jul 18 '05 #6
Tim Peters wrote:
That differences may exist is reflected in the C
standard, and the rules for text-mode files are more restrictive than
most people would believe.


Apparently. Because I know only about the Unix <-> Windows difference
(windows converts \r\n <--> \n when using 'r' mode, right).
So it's in the line endings.

Is there more obscure stuff going on on the other systems you
mentioned (Mac OS, VAX) ?

(That means that the bug in Simplehttpserve r that my patch
839496 addressed, also occured on those systems? Or that
the patch may be incorrect after all??)

While your argument about why Python doesn't use its own platform-
independent file format is sound ofcourse, I find it often a nuisance
that platform specific things tricle trough into Python itself and
ultimately in the programs you write. I sometimes feel that some
parts of Python expose the underlying C/os implementation
a bit too much. Python never claimed write once run anywhere (as
that other language does) but it would have been nice nevertheless ;-)
In practice it's just not possible I guess.

Thanks,
--Irmen
Jul 18 '05 #7
On Fri, 14 Jan 2005 09:12:49 -0500, Tim Peters <ti********@gma il.com>
wrote:
[Aki Niimura]
I started to use pickle to store the latest user settings for the tool
I wrote. It writes out a pickled text file when it terminates and it
restores the settings when it starts.

...
I guess DOS text format is creating this problem.


Yes.
My question is "Is there any elegant way to deal with this?".


Yes: regardless of platform, always open files used for pickles in
binary mode. That is, pass "rb" to open() when reading a pickle file,
and "wb" to open() when writing a pickle file. Then your pickle files
will work unchanged on all platforms. The same is true of files
containing binary data of any kind (and despite that pickle protocol 0
was called "text mode" for years, it's still binary data).


Tim, the manual as of version 2.4 does _not_ mention the need to use
'b' on OSes where it makes a difference, not even in the examples at
the end of the chapter. Further, it still refers to protocol 0 as
'text' in several places. There is also a reference to protocol 0
files being viewable in a text editor.

In other words, enough to lead even the most careful Reader of TFM up
the garden path :-)

Cheers,
John
Jul 18 '05 #8
[Tim Peters]
Yes: regardless of platform, always open files used for pickles
in binary mode. ...

[John Machin] Tim, the manual as of version 2.4 does _not_ mention the need
to use 'b' on OSes where it makes a difference, not even in the
examples at the end of the chapter. Further, it still refers to
protocol 0 as 'text' in several places. There is also a reference to
protocol 0 files being viewable in a text editor.

In other words, enough to lead even the most careful Reader of
TFM up the garden path :-)


Take the next step: submit a patch with corrected text. I'm not paid
to work on the Python docs either <0.5 wink>. (BTW, protocol 0 files
are viewable in a text editor regardless, although the line ends may
"look funny")
Jul 18 '05 #9
[Tim Peters]
That differences may exist is reflected in the C
standard, and the rules for text-mode files are more restrictive
than most people would believe.

[Irmen de Jong] Apparently. Because I know only about the Unix <-> Windows
difference (windows converts \r\n <--> \n when using 'r' mode,
right). So it's in the line endings.
That's one difference. The worse difference is that, in text mode on
Windows, the first instance of chr(26) in a file is taken as meaning
"that's the end of the file", no matter how many bytes may follow it.
That's fine by the C standard, because everything about a text-mode
file containing a chr(26) character is undefined.
Is there more obscure stuff going on on the other systems you
mentioned (Mac OS, VAX) ?
I think on Mac Classic it was *just* line end differences. Native VAX
has many file formats. "Record-based" file formats used to be very
popular. There the OS saves meta-information in the file, such as
each record contains an offset to the start of the next record, and
may even contain an index structure to support random access to
records quickly (for example, "a line" may be a record, and "read the
last line" may go quickly). Read that in binary mode, and you'll be
reading up the bits in the index and offsets too, etc. IIRC, Unix was
actually quite novel at the time in insisting that all files were just
raw byte streams to the OS.
(That means that the bug in Simplehttpserve r that my patch
839496 addressed, also occured on those systems? Or that
the patch may be incorrect after all??)
Don't know, and (sorry) no time to dig.
While your argument about why Python doesn't use its own
platform- independent file format is sound of course, I find it often
a nuisance that platform specific things tricle trough into Python
itself and ultimately in the programs you write. I sometimes feel
that some parts of Python expose the underlying C/os
implementation a bit too much. Python never claimed write once
run anywhere (as that other language does) but it would have
been nice nevertheless ;-)
In practice it's just not possible I guess.


It would be difficult at best. Python hides a lot of platform crap,
but generally where it's reasonably easy to hide. It's not easy to
hide native file conventions, partly because Python wouldn't play well
with *other* platform software if it did.

Remember that Guido worked on ABC before Python, and Python is in
(small) part a reaction against the extremes of ABC. ABC was 100%
platform-independent. You could read and write files from ABC.
However, the only files you could read from ABC were files that were
written by ABC -- and files written by ABC were essentially unusable
by other software. Socket semantics were also 100% portable in ABC:
it didn't have sockets, nor any way to extend the language to add
them. Etc -- ABC was a self-contained universe. "Plays well with
others" was a strong motivator for Python's design, and that often
means playing by others' rules.
Jul 18 '05 #10

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

Similar topics

1
2758
by: Newgene | last post by:
Hi, group, I have python2.3 installed on win2k. I noticed that when I open a dos format text file (eol is '\r\n'), readline() always returns a line ending with '\n' only, not '\r\n'. While I read the same file on unix, it returns a line ending with '\r\n' correctly. This makes me difficult to determine the format of a text file, dos or unix. Is this a bug or intended behavior? If not a bug, then how to determine the format of a text...
27
5041
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there any solid reasons to prefer text files over binary files files?
11
3655
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own rules when it comes to file paths. A lot of Microsoft installers for example, and also installers of other companies, do not work because they handle paths in the following manner:
8
3446
by: Dave Moore | last post by:
I realize this is a somewhat platform specific question, but I think it is still of general enough interest to ask it here ... if I am wrong I guess I will find out 8*). As we all know, DOS uses two characters (carriage-return and line-feed), to signal the end of a line, while UNIX uses only one (line-feed). When using getline in C++, one can only specify a single character as the terminator (default is '\n'), so if you read a line of...
2
1825
by: Larry Rekow | last post by:
A friend's unix application produces various reports daily as flat text files. I link these files as tables in an Access (2000) database. After linking, all of these reports begin with a blank row; no problem. But the latest report I've asked, once linked, creates not only a blank row at the beginning, but a blank row after every row with data.
8
9764
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
10
5982
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows, where text files use CF+LF (0x0d, 0x0a) to mark end-of-line. The file in question, however, was in Unix format, with only LF (0x0a) at the end of each line. First, does the above situation already invoke "implementation defined" or "undefined"...
2
1183
by: krishnakant Mane | last post by:
hello all, I am trying a very complex kind of a task in a project. I have a knowledge management system where I need to store a lot of objects (pickled). I have to store mostly lists and dictionaries into a rdbms. mostly I will be using mysql. I want to know if there is any module that can help me store a pickled object inside a blob field instead of a file. I know that pickle.dump() can store an object into a file but I can't find a...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.