tags:

views:

205

answers:

2

Hello,

it gives me this error:

Exception in thread Thread-163:
Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 532, in __bootstrap_inner
    self.run()
  File "C:\Python26\lib\threading.py", line 736, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Users\Public\SoundLog\Code\Código Python\SoundLog\SoundLog.py", line 337, in getInfo
    self.data1 = copy.deepcopy(Auxiliar.DataCollection.getInfo(1))
  File "C:\Python26\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "C:\Python26\lib\copy.py", line 254, in _deepcopy_dict
    for key, value in x.iteritems():
RuntimeError: dictionary changed size during iteration

while executing my python program.

How can I avoid this to happen?

Thanks in advance ;)

A: 

Sounds like your are adding or removing something from the dictionary you are trying to iterate over. This is not allowed in most languages.

Falle1234
when I do the copy.deepcopy doesn't create a new dictionary?
aF
+4  A: 

The normal advice, as per the other answers, would be to avoid using iteritems (use items instead). That of course is not an option in your case, since the iteritems call is being done on your behalf deep in the bowels of a system call.

Therefore, what I would suggest, assuming Auxiliar.DataCollection.getInfo(1) returns a dictionary (which is the one that's changing during the copy) is that you change your deepcopy call to:

self.data1 = copy.deepcopy(dict(Auxiliar.DataCollection.getInfo(1)))

This takes a "snapshot" of the dict in question, and the snapshot won't change, so you'll be fine.

If Auxiliar.DataCollection.getInfo(1) does not return a dict, but some more complicated object which includes dicts as items and/or attributes, it will be a bit more complicated, since those dicts are what you'll need to snapshot. However, it's impossible to be any more specific in this case, since you give us absolutely no clue as to the code that composes that crucial Auxiliar.DataCollection.getInfo(1) call!-)

Alex Martelli
Yes, it is a dictionary and what you said really worked!Thanks for the quick and good answer :)
aF