473,385 Members | 1,890 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,385 software developers and data experts.

pickling extension class

Hi all,

I have a problem pickling an extension class. As written in the
Extending/Embedding Manual, I
provided a function __reduce__ that returns the appropreate tuple. This
seams to work fine,
but I still cannot pickle because of the following error:
from model import hyper
g = hyper.PeriodicGrid(4,4,1)
g.__reduce__() (<type 'hyper.PeriodicGrid'>,(4.,4.,1.)) import pickle
pickle.dump(g,file("test","w")) Traceback (most recent call last):
File "pickle_test.py", line 5, in ?
pickle.dump(g,file("test","w"))
File "/sw/lib/python2.4/pickle.py", line 1382, in dump
Pickler(file, protocol, bin).dump(obj)
File "/sw/lib/python2.4/pickle.py", line 231, in dump
self.save(obj)
File "/sw/lib/python2.4/pickle.py", line 338, in save
self.save_reduce(obj=obj, *rv)
File "/sw/lib/python2.4/pickle.py", line 414, in save_reduce
save(func)
File "/sw/lib/python2.4/pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "/sw/lib/python2.4/pickle.py", line 760, in save_global
raise PicklingError(
pickle.PicklingError: Can't pickle <type 'hyper.PeriodicGrid'>: it's
not found as hyper.PeriodicGrid dir(hyper) ['Dir', 'Neighbors', 'PeriodicGrid', 'PeriodicPos', '__doc__',
'__file__', '__name__', 'refcount'] hyper.PeriodicGrid

<type 'hyper.PeriodicGrid'>

So pickle complains about the class PeriodicGrid not being found in the
module hyper, but a dir()
proves that python can find it. Has anyone an idea what's going wrong
here?

Any help appreceated,

- harold -

--
What is mind? -- Doesn't matter.
What is matter? -- Never mind!
--

Jul 18 '05 #1
4 1769
harold fellermann <ha***************@upf.edu> wrote:
File "/sw/lib/python2.4/pickle.py", line 760, in save_global
raise PicklingError(
pickle.PicklingError: Can't pickle <type 'hyper.PeriodicGrid'>: it's
not found as hyper.PeriodicGrid
>>> dir(hyper) ['Dir', 'Neighbors', 'PeriodicGrid', 'PeriodicPos', '__doc__',
'__file__', '__name__', 'refcount'] >>> hyper.PeriodicGrid

<type 'hyper.PeriodicGrid'>

So pickle complains about the class PeriodicGrid not being found in the
module hyper, but a dir()
proves that python can find it. Has anyone an idea what's going wrong
here?


These symptomps are pretty weird -- let's try to pin things down a bit
more. The relevant few lines of pickle.py are:

try:
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
except (ImportError, KeyError, AttributeError):
raise PicklingError(

so, could you please edit your pickle.py to provide VASTLY more info,
say:

try:
print 'Here it goes...:'
_xx = __import__(module)
print ' __import__ says: %r' % (_xx,)
mod = sys.modules[module]
print ' in sys.modules: %r' % (mod,)
klass = getattr(mod, name)
print ' klass is: %r' % (klass,)
except (ImportError, KeyError, AttributeError), _xx:
print ' OOPS, error (%s): %s' % (_xx.__class__, _xx)
raise PicklingError(

and let us know exactly what his modified pickle.py outputs...?
Thanks,

Alex
Jul 18 '05 #2

On 18.01.2005, at 20:31, Alex Martelli wrote:
harold fellermann <ha***************@upf.edu> wrote:
File "/sw/lib/python2.4/pickle.py", line 760, in save_global
raise PicklingError(
pickle.PicklingError: Can't pickle <type 'hyper.PeriodicGrid'>: it's
not found as hyper.PeriodicGrid
> dir(hyper) ['Dir', 'Neighbors', 'PeriodicGrid', 'PeriodicPos', '__doc__',
'__file__', '__name__', 'refcount']
> hyper.PeriodicGrid

<type 'hyper.PeriodicGrid'>

So pickle complains about the class PeriodicGrid not being found in
the
module hyper, but a dir()
proves that python can find it. Has anyone an idea what's going wrong
here?


These symptomps are pretty weird -- let's try to pin things down a bit
more. The relevant few lines of pickle.py are:

try:
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
except (ImportError, KeyError, AttributeError):
raise PicklingError(

so, could you please edit your pickle.py to provide VASTLY more info,


[...]
and let us know exactly what his modified pickle.py outputs...?

Here it goes...:
OOPS, error (exceptions.ImportError): No module named hyper
Traceback (most recent call last):
File "pickle_test.py", line 5, in ?
pickle.dump(g,file("test","w"))
File "/Volumes/space/Users/harold/uni/pace/ono/pickle.py", line 1387,
in dump
Pickler(file, protocol, bin).dump(obj)
File "/Volumes/space/Users/harold/uni/pace/ono/pickle.py", line 231,
in dump
self.save(obj)
File "/Volumes/space/Users/harold/uni/pace/ono/pickle.py", line 338,
in save
self.save_reduce(obj=obj, *rv)
File "/Volumes/space/Users/harold/uni/pace/ono/pickle.py", line 414,
in save_reduce
save(func)
File "/Volumes/space/Users/harold/uni/pace/ono/pickle.py", line 293,
in save
f(self, obj) # Call unbound method with explicit self
File "/Volumes/space/Users/harold/uni/pace/ono/pickle.py", line 765,
in save_global
raise PicklingError(
pickle.PicklingError: Can't pickle <type 'hyper.PeriodicGrid'>: it's
not found as hyper.PeriodicGrid
I have noticed that the error does not occur, when the imported module
('hyper') is in the same directory as the script that pickles. When it
is imported from a subpackage (like in the code
I sent you) it goes wrong.

- harold -

--
Reality is for people who lack imagination.

Jul 18 '05 #3
harold fellermann <ha***************@upf.edu> wrote:
...
Here it goes...:
OOPS, error (exceptions.ImportError): No module named hyper
So, the __import__ in pickle fails -- indeed, __import__('foo') when
'foo' ``is imported from a subpackage'' is _supposed_ to fail, as
'hyper' is not the name you SHOULD be importing. For example,

__import__('email.Encoders')

is fine, but just

__import__('Encoders')

fails with ImportError -- there IS no toplevel module by that name!

I have noticed that the error does not occur, when the imported module
('hyper') is in the same directory as the script that pickles. When it
is imported from a subpackage (like in the code
I sent you) it goes wrong.


If you can't fix the modulename given by your type, you can perhaps
kludge things up by (e.g.)

import the.real.hyper
sys.modules['hyper']=the.real.hyper

before you pickle; but I suspect UNpickling would fail in that case.

Using the standard library copy_reg module to register your way to
pickle and recover instances of your type might work better.
Alex
Jul 18 '05 #4
harold fellermann <ha***************@upf.edu> writes:
Hi all,

I have a problem pickling an extension class. As written in the
Extending/Embedding Manual, I
provided a function __reduce__ that returns the appropreate tuple.
This seams to work fine,
but I still cannot pickle because of the following error:
>>> from model import hyper
>>> g = hyper.PeriodicGrid(4,4,1)
>>> g.__reduce__() (<type 'hyper.PeriodicGrid'>,(4.,4.,1.)) >>> import pickle
>>> pickle.dump(g,file("test","w")) Traceback (most recent call last):
File "pickle_test.py", line 5, in ?
pickle.dump(g,file("test","w"))
File "/sw/lib/python2.4/pickle.py", line 1382, in dump
Pickler(file, protocol, bin).dump(obj)
File "/sw/lib/python2.4/pickle.py", line 231, in dump
self.save(obj)
File "/sw/lib/python2.4/pickle.py", line 338, in save
self.save_reduce(obj=obj, *rv)
File "/sw/lib/python2.4/pickle.py", line 414, in save_reduce
save(func)
File "/sw/lib/python2.4/pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "/sw/lib/python2.4/pickle.py", line 760, in save_global
raise PicklingError(
pickle.PicklingError: Can't pickle <type 'hyper.PeriodicGrid'>: it's
not found as hyper.PeriodicGrid >>> dir(hyper) ['Dir', 'Neighbors', 'PeriodicGrid', 'PeriodicPos', '__doc__',
'__file__', '__name__', 'refcount'] >>> hyper.PeriodicGrid

<type 'hyper.PeriodicGrid'>

^^^^^

I think that's your error. The extension type is declared to be
hyper.PeriodicGrid, where it actually is model.hyper.PeriodicGrid
(because hyper is in the model package).

Pickle stores g.__class__.__module__ (which is "hyper") and
g.__class__.__name__ (="PeriodicGrid") to find the class object for
reimporting, and on unpickling, tries to do __import__("hyper"), which
fails.

The tp_name slot of your extension type should be "model.hyper.PeriodicGrid".

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #5

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

Similar topics

4
by: gong | last post by:
hi i would like to pickle a lambda; according to the library docs in 2.3, i believe this shouldnt be possible, since a lambda is not a function defined at the top level of a module (?) ...
0
by: Skip Montanaro | last post by:
I have a class that inherits from PyGTK's gobject.GObject. I thought to pickle it I'd be able to just define __etstate__ methods. When I attempt to dump an instance of this class I get a...
0
by: benevilent | last post by:
Hey, I am creating an extension type in C for Python, which I want it such that instances can be pickled. I basically want the functionality of the 'object' type such that subclasses of the type...
8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
2
by: Kirk Strauser | last post by:
I have a module that defines a Search class and a SearchResult class. I use these classes by writing other modules that subclass both of them as needed to interface with particular search engines....
0
by: jeanphilippe.aumasson | last post by:
Hi, I have some problems when pickling an instance of a class, i don't retrieve all its attributes instances after loading. I'm quite a beginner in Python, so it may be a stupid error... Here...
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...
0
by: Irmen de Jong | last post by:
I'm having troubles pickling classes that extend Exception. Given the following source: class Foo(object): def __init__(self, m): self.m=m class Bar(Exception): def __init__(self, m):
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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?
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
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...

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.