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

John Machin sjmachin at lexicon.net
Sun Feb 8 16:03:52 EST 2009


On Feb 9, 1:01 am, Christian Heimes <li... at cheimes.de> wrote:
> Barak, Ron schrieb:
>
> > 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 ?
>
> You can overwrite some functions in order to omit attributes from a
> pickle. It's all documented athttp://docs.python.org/library/pickle.html#pickling-and-unpickling-no...

If there is no chance that some other thread could be accessing the
source object, can't the OP just do:
    temp = source.stringio
    del source.stringio
    # make a pickle from source
    source.stringio = temp
?






More information about the Python-list mailing list