473,397 Members | 2,099 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,397 software developers and data experts.

Type-checking unpickled objects

I have this class, and I've been pickling it's objects as a file format
for my program, which works great. The problems are a.) how to handle
things when the user tries to load a non-pickled file, and b.) when they
load a pickled file of the wrong class.

a. I can handle with a general exception I guess. I just tried to
pickle.load() a .pyc and it failed with a 'KeyError' exception - however
that works. It might do that every time, it might not.

Regarding b., if I type check I simply get the 'instance' type, which
doesn't get me all of the way there because I might have an instance of
the wrong class. I can't find any sort of __type__ attribute to set in
the objects. Perhaps I should use __str__?
Nov 22 '05 #1
6 1229
Gordon Airporte wrote:
I have this class, and I've been pickling it's objects as a file format
for my program, which works great. The problems are a.) how to handle
things when the user tries to load a non-pickled file, and b.) when they
load a pickled file of the wrong class.

a. I can handle with a general exception I guess. I just tried to
pickle.load() a .pyc and it failed with a 'KeyError' exception - however
that works. It might do that every time, it might not.
I hope you're aware of the fact that pickle is not suitable for applications
where you cannot trust the source; see e.g.

http://www.livejournal.com/users/jcalderone/15864.html

(one of the comments show how to place restrictions on what can be loaded
from a given pickle)
Regarding b., if I type check I simply get the 'instance' type, which
doesn't get me all of the way there because I might have an instance of
the wrong class. I can't find any sort of __type__ attribute to set in
the objects. Perhaps I should use __str__?


how about a plain

obj = cPickle.load(...)
if not isinstance(obj, Class):
print "expected a Class instance"

</F>

Nov 22 '05 #2
Gordon Airporte wrote:
I have this class, and I've been pickling it's objects as a file format
for my program, which works great. The problems are a.) how to handle
things when the user tries to load a non-pickled file, and b.) when they
load a pickled file of the wrong class.

a. I can handle with a general exception I guess. I just tried to
pickle.load() a .pyc and it failed with a 'KeyError' exception - however
that works. It might do that every time, it might not.
I hope you're aware of the fact that pickle is not suitable for applications
where you cannot trust the source; see e.g.

http://www.livejournal.com/users/jcalderone/15864.html

(one of the comments show how to place restrictions on what can be loaded
from a given pickle)
Regarding b., if I type check I simply get the 'instance' type, which
doesn't get me all of the way there because I might have an instance of
the wrong class. I can't find any sort of __type__ attribute to set in
the objects. Perhaps I should use __str__?


how about a plain

obj = cPickle.load(...)
if not isinstance(obj, Class):
print "expected a Class instance"

</F>

Nov 22 '05 #3
isinstance! Now why didn't I know about that? Thanks you. I guess I'll
need a throwaway instance of the class to run type() on to get a usable
type object for comparison, but I'll work something out.
As to the security considerations...this is a small enough program with
a limited enough release that I'm not going to sweat it, although I will
keep this in mind.
Nov 22 '05 #4
isinstance! Now why didn't I know about that? Thanks you. I guess I'll
need a throwaway instance of the class to run type() on to get a usable
type object for comparison, but I'll work something out.
As to the security considerations...this is a small enough program with
a limited enough release that I'm not going to sweat it, although I will
keep this in mind.
Nov 22 '05 #5
> I guess I'll
need a throwaway instance of the class to run type() on to get a usable
type object for comparison, but I'll work something out.


Never mind - I can just pass the name of the class, as it should be.
Nov 22 '05 #6
> I guess I'll
need a throwaway instance of the class to run type() on to get a usable
type object for comparison, but I'll work something out.


Never mind - I can just pass the name of the class, as it should be.
Nov 22 '05 #7

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

Similar topics

3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
2
by: Jason Cartwright | last post by:
I have an abstract base class and two derived classes that I want to serialize and deserialize with schema validation. When I serialize instances of the derived classes the XmlSerializer adds the...
4
by: Ced | last post by:
Hi, i'm not an expert in C but i try to compile BTNG software under linux kernel 2.4.2-2. I get these errors at the very first stage. Does someone could have a rapid look on this and tell me...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
3
by: amanjsingh | last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.