How to copy an instance without its cStringIO.StringO item ?

Barak, Ron Ron.Barak at lsi.com
Sun Feb 8 08:36:17 EST 2009


Hi,

I need to copy an instance of a class, where one of the items in the original is a cStringIO.StringO object.
I do not need the cStringIO.StringO in the target object.
The target instance is to be pickled.

Googling made me devise the following plan: do a deepcopy from the original to the target, and then delete the cStringIO.StringO object from the target.

However, trying to use copy.deepcopy produced: TypeError: object.__new__(cStringIO.StringO) is not safe, use cStringIO.StringO.__new__()

Is there a way to create a copy of the instance, sans the cStringIO.StringO ?

If I find no elegant way to do that, I thought of creating a "blank" target instance; then iterating over the __dict__ of the original, and manually copy the items to the target (while not copying the cStringIO.StringO to the target).

Can you suggest a better way ?

Thanks,
Ron.

Traceback (most recent call last):
  File "c:\Documents and Settings\rbarak\rbarak_devel\LogManager\./failover_pickle_demo02.py", line 388, in <module>
    for_pickle_log_stream = copy.deepcopy(log_stream)
  File "c:\Python25\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "c:\Python25\lib\copy.py", line 291, in _deepcopy_inst
    state = deepcopy(state, memo)
  File "c:\Python25\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "c:\Python25\lib\copy.py", line 254, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "c:\Python25\lib\copy.py", line 189, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "c:\Python25\lib\copy.py", line 322, in _reconstruct
    y = callable(*args)
  File "c:\Python25\lib\copy_reg.py", line 92, in __newobj__
    return cls.__new__(cls, *args)
TypeError: object.__new__(cStringIO.StringO) is not safe, use cStringIO.StringO.__new__()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090208/f84b2173/attachment.html>


More information about the Python-list mailing list