I have been using this copy method for quite a while, in lots of classes that needed it.
class population (list):
def __init__ (self):
pass
def copy(self):
return copy.deepcopy(self)
It has suddenly started producing this error:
File "C:\Python26\lib\copy.py", line 338, in _reconstruct
state = deepcopy(state, memo)
File "C:\Python26\lib\copy.py", line 162, in deepcopy
y = copier(x, memo)
File "C:\Python26\lib\copy.py", line 255, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Python26\lib\copy.py", line 189, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "C:\Python26\lib\copy.py", line 323, in _reconstruct
y = callable(*args)
File "C:\Python26\lib\copy_reg.py", line 93, in __newobj__
return cls.__new__(cls, *args)
TypeError: object.__new__(generator) is not safe, use generator.__new__()
>>>
the lines which include the references to lines 338, 162, 255, 189 were repeated quite a few times before the 'line 338' that I copied here.
This occurs in many instances when one accidentally tries to clone the iterator to a class. For instance, in PIL, trying to clone the
PixelAccess
of anImage
will throw this error.Take an example where
pixels = image.load()
. Instead of trying to do something likepixels_copy = copy.copy(pixels)
, you have to copy the base object and then produce an iterator. So, replace that piece of code withpixels_copy = image.copy().load()
.Are you cloning a generator? Generators can't be cloned.
Copying answer by Gabriel Genellina here:
There is no way of "cloning" a generator:
You can do that with a generator function because it acts as a "generator
factory", building a new generator when called. Even using the Python C
API, to create a generator one needs a frame object -- and there is no way
to create a frame object "on the fly" that I know of :(
g and g2 share the same execution frame, so they're not independent. There
is no easy way to create a new frame in Python:
One could try using PyFrame_New -- but that's way too magic for my taste...