views:

364

answers:

2

Is there an existing implementation of an iterative version of deepcopy for Python 2.5.2? The deepcopy method available from the copy module is recursive and fails on large trees. I am not in a position where we can safely increase the stack limit at runtime.

EDIT

I did track this down:

http://code.activestate.com/recipes/302535/

I haven't tested it, but it looks like it has potential.

+1  A: 

I'm not sure this would make sense. Isn't the whole point of deepcopy that each object will ask its children to copy themselves?

Presumably you know exactly what kind of object you're copying and what its children look like as well, so if I were you, I'd just write my own function to copy it. Shouldn't be too hard.

Ben Alpert
No, shouldn't be hard. But, we hit recursion limit frequently. I'd prefer a well abused and robust prior implementation that can handle arbitrary objects.
daniel
by frequently, I mean any code we have that relies on recursion tends to break eventually. We've put an informal block on committing recursive code into our software package.
daniel
To Ben > I think daniel wants an version of deepcopy that is doing the same as the current one, but does not fail for trees that are too deep.The process of writing a logically recursive algorithm in iterative is called something like derecursivation.
poulejapon
A: 

Maybe it would work as such with Stackless Python

poulejapon
needs to function without issue on an existing software platform that is >50,000 lines of Python/NumPy/matplotlib/C/pyrex across a variety of architectures. I'd shit myself if the transition were seemless. Though, I agree there are benefits to stackless.
daniel