简体   繁体   中英

Random AttibuteError on python3

I am facing a strange problem.

I changed a piece of python2 I had to python3 using 2to3.

After this, my code is randomly running/failing... The Error I get when it fails is related to an Attribute Error... But in python2 and in random python3 trials it works, and find the attribute...

I tested this behavior on 2 machines, running both python3.4.

Would you have any clue of what is going wrong (I am puzzled by this "random" failure) ?

Here is the trace I get :

Traceback (most recent call last):  
File "tools/ciregs.py", line 1596, in <module>  
module = Module(json_data)  
File "tools/ciregs.py", line 1147, in \__init__  
self.registers = [Register(json_reg, parent_module=self) for json_reg in  json_module[key]]  
File "tools/ciregs.py", line 1147, in <listcomp>
self.registers = [Register(json_reg, parent_module=self) for json_reg in json_module[key]]  
File "tools/ciregs.py", line 1260, in \__init__
self.fields = [Field(json_field, self) for json_field in json_reg[key]]  
File "tools/ciregs.py", line 1260, in <listcomp>
self.fields = [Field(json_field, self) for json_field in json_reg[key]]  
File "tools/ciregs.py", line 1443, in \__init__
self.check()         
File "tools/ciregs.py", line 1453, in check
if(self.bitWidth <= 0 or self.bitWidth > self.parent_reg.size()):  
File "tools/ciregs.py", line 1326, in size return self.parent_module_.width
AttributeError: 'Module' object has no attribute 'width'

I have a dictionary that contains a "width" key. In a sub-element, I want to evaluate the size of the said sub-element against the width of the top to see if it fits.

That's why I try to access to self.parent_reg.size() that access to the width and that randomly issues that attribute width doesn't exist in the top level dictionary.

Thanks for your help!

Ok guys, thanks for your help, @Bryan Oakley, your help helped me.

In the end the problem is that I get my keys from a json I load, and the load was done on a dictionary and not a sorted dictionary.

One of my key, last in json, then calls functions that expect other keys (width in the Error reported) to be set.

Having the dictionary not sorted makes the width key to be set in some cases before the setting of the last key, and not set in some other cases.

I am still puzzled why it worked for python2....

Thanks all for your help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM