473,594 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unpickling Exception-type objects

Hi everyone,

I'm stumped over what is probably (hopefully?) a very simple problem.
Suppose I have a class that multiply inherits from two classes, call
them A and B. A inherits from a class that takes any number of
arguments in __init__. B does not inherit from anything and requires
one argument. For reasons I don't understand, if A inherits from
Exception, unpickling fails due to a TypeError:

__init__() takes exactly 2 arguments (1 given)

If A in inherits from a class that also takes any number of arguments,
the TypeError vanishes. To illustrate:

# ------------------------------------
import pickle

class Parent:
def __init__(self, *optional):
self.optional = optional

for Base in (Parent, Exception):
print "Base =", Base
print

class A(Base):
pass

class B:
def __init__(self, required):
self.required = required

class Child(A, B):
def __init__(self, *args):
A.__init__(self )
B.__init__(self , *args)

def test_dump(obj):
print 'obj:', type(obj), obj.__dict__
pickle.dump(obj , file('/tmp/pickle.p', 'w'), -1)
try:
loaded = pickle.load(fil e('/tmp/pickle.p'))
print 'loaded', type(loaded), loaded.__dict__
except TypeError, e:
print 'failed to load', e
print

test_dump(Child (7))
# ------------------------------------

I get the following output in python 2.5.2:

# ------------------------------------
Base = __main__.Parent

obj: <type 'instance'{'req uired': 7, 'optional': ()}
loaded <type 'instance'{'req uired': 7, 'optional': ()}

Base = <type 'exceptions.Exc eption'>

obj: <class '__main__.Child '{'required': 7}
failed to load __init__() takes exactly 2 arguments (1 given)
# ------------------------------------

Any help on this would be greatly appreciated.

Thanks,
David
Oct 2 '08 #1
0 927

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

Similar topics

1
1733
by: Bob | last post by:
I've read over section 3.14.5.2 of the doc about a zillion times, and it still makes absolutely no sense to me. Can someone please explain it? What I'd like to do is, basically, have the object be pickled as None, since it represents an object in the C world, and it doesn't make sense to pickle it at all. How can I do that, if at all? In particular, what does this
8
2712
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
0
1924
by: Viktor Lundström | last post by:
Hi! I recently decided to write a streambuf which handles streamed IO to a device (ie. a socket). Now, in an effort to move away from C-style IO error handling (ie. if(read(..) == -1) ...), I decided to make my streambuf raise exceptions upon device error states. The code is supposed to run on several platforms, but there seems to be some inconsistency between the different iostream implementations. This led me to believe that I'm...
9
3583
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b def __getstate__(self): return self.A, self.B def __setstate__(self, tup):
3
3604
by: sushant.sirsikar | last post by:
Hi, I am new in Python World.I want to know what is mean by ``pickling'' and ``unpickling'' ? And how can we used it?Please Give Me some links of Picking Examples. Thanks Sushant
3
2502
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let me explain how my question is different. Second, let me say that I have two questions. One is an ASP.Net related question. The other is a general System.Exception question. They are related but also individual. Okay here is my problem...
3
1073
by: Amir Michail | last post by:
Hi, Is there an easy way to unpickle the older Set as the newer set behind the scenes when using shelve? Amir
2
6973
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: parseMetadata.obj : error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(void)" (??0exception@std@@QAE@XZ) 2>libcpmt.lib(string.obj) : error LNK2001: unresolved external symbol "public: __thiscall...
7
1911
by: Steve Bergman | last post by:
I'm involved in a discussion thread in which it has been stated that: """ Anything written in a language that is 20x slower (Perl, Python, PHP) than C/C++ should be instantly rejected by users on those grounds alone. """ I've challenged someone to beat the snippet of code below in C, C++, or assembler, for reading in one million pairs of random floats and
10
5022
by: dinopc | last post by:
Hello, i am italian, I apologize for my English. I would like to extend the class std::exception: #include <iostream.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <sys/types.h>
0
8374
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
8009
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
8240
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6661
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
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
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
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.