You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Where the references are cyclic(this is for a research project into
graph algorithms), and using deepcopy, even on a tiny graph of five
vertexes and 25 edges causes deepcopy to run into the recursion limit.
At the very least, it should warn it can't copy the indicated object, as
the references are cyclic. At the very most, it should be able to handle
complex cyclic references like this properly.
Whoops, sorry, correction, when there are 100 vertexes and 500 edges.
gpolo commented on Jun 5, 2008
gpolomannequin
Mannequin
More actions
This should have been fixed at 2.2, as long as you change your classes
to new-style classes. If it still happens, post a sample code using your
new code that uses new-style classes.
Zeroth commented on Jun 5, 2008
Zerothmannequin
MannequinAuthor
More actions
Same problem, even with new-style classes. Here, I'll show the function
I use to generate the graph as well.
classVertex(object):
def__init__(self, type):
self.type=typeself.color=-1self.edges=[]
classEdge(object):
def__init__(self, V1, V2):
self.vertexes=[V1, V2]
V1.edges.append(self)
V2.edges.append(self)
defgenerate(graph={'V':[], 'E':[]}, seed=777, vertexes=5, edges=25):
#generates a graph similar to the KEGG pathway database format.# Purpose is for testing algorithms on a smaller scale "predicatible" graph# For that reason, the "random" selections are seeded with a knownnumber. Thisistobeable# to measure efficacy, in that on the same graph, if algorithm Aperformsinhalfthetime, its# not a characteristic of the graph, but the algorithm.r=random.Random(seed)
p=[0, 0, 0, 0]
c=0#generate vertices, with a regularly incremented set of numbers, toappearliketheKEGGpathwaydatabasedoes.
whilec!=vertexes:
#This is ugly, could be done easier in a loop. Fix later.p[3]+=1ifp[3]>9:
p[3]=0p[2]+=1ifp[2]>9:
p[2]=0p[1]+=1ifp[1]>9:
p[1]=0p[0]+=1#we copy the set of numbers, due to the way python passes lists byreference, insteadofbycopy.
v=Vertex(p[:])
graph['V'].append(v)
c+=1v=graph['V']
iflen(v)!=vertexes:
print"Not enough vertices, aborting."returngraphc=0#randomly choose two vertices. Could even, randomly, be the same vertex.# Then, connect an edge between them. Just creating the edge byimplication,
# with the vertices passed, connects them all together.whilec!=edges:
v1=r.choice(v)
v2=r.choice(v)
graph['E'].append(Edge(v1, v2))
c+=1iflen(graph['E']) !=edges:
print"Not enough edges, aborting."returngraph
Thanks for the prompt response, this isn't critical in nature, just
playing around with the graph, and wanted to alter a copy of it. Ran
into this bug, thought I should report it. :)
Your graph is simply too deep for Python to handle it. The copy module
(and also the pickle) pushes a new stack frame every time a new
container object is encountered. Therefore, there is a recursion limit
to prevent the interpreter from crashing.
If you want, you can carefully increase the limit by using the function sys.setrecursionlimit. Using your example:
>>> sys.setrecursionlimit(2000)
>>> g2 = copy.deepcopy(g)
>>> # no error
Activity
Zeroth commentedon Jun 5, 2008
With the following code:
class Vertex:
def __init__(self, type):
self.type = type
self.color=-1
self.edges=[]
Where the references are cyclic(this is for a research project into
graph algorithms), and using deepcopy, even on a tiny graph of five
vertexes and 25 edges causes deepcopy to run into the recursion limit.
At the very least, it should warn it can't copy the indicated object, as
the references are cyclic. At the very most, it should be able to handle
complex cyclic references like this properly.
Zeroth commentedon Jun 5, 2008
Whoops, sorry, correction, when there are 100 vertexes and 500 edges.
gpolo commentedon Jun 5, 2008
This should have been fixed at 2.2, as long as you change your classes
to new-style classes. If it still happens, post a sample code using your
new code that uses new-style classes.
Zeroth commentedon Jun 5, 2008
Same problem, even with new-style classes. Here, I'll show the function
I use to generate the graph as well.
And here is how I use it:
Thanks for the prompt response, this isn't critical in nature, just
playing around with the graph, and wanted to alter a copy of it. Ran
into this bug, thought I should report it. :)
-Zeroth
avassalotti commentedon Jun 5, 2008
Your graph is simply too deep for Python to handle it. The copy module
(and also the pickle) pushes a new stack frame every time a new
container object is encountered. Therefore, there is a recursion limit
to prevent the interpreter from crashing.
If you want, you can carefully increase the limit by using the function
sys.setrecursionlimit. Using your example:>>> sys.setrecursionlimit(2000)
>>> g2 = copy.deepcopy(g)
>>> # no error
facundobatista commentedon Jun 21, 2008
Two weeks passed, closing it manually (see
http://mail.python.org/pipermail/python-dev/2008-February/076992.html).