473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create 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(file('/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'{'required': 7, 'optional': ()}
loaded <type 'instance'{'required': 7, 'optional': ()}

Base = <type 'exceptions.Exception'>

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 917

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

Similar topics

1
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...
8
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
0
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...
9
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...
3
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
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...
3
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
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: ...
7
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...
10
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...
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...
1
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.