git-afsantos / bonsai

Simplified interface for syntax trees and program models.
MIT License
16 stars 8 forks source link

Fix interpreting integers with leading 0 #26

Closed hellantos closed 3 years ago

hellantos commented 3 years ago

When parsing CPP-Integers with a leading zero, the current code throws a ValueError. It seams Python cannot derive the base from such a literal.

Python proof: int("0666", 0) results in:

Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 0: '0666'

CPP example where this error occurs: const int get_flags = 0666;

The pull request will catch the error and assume that its a base 10 integer. Then interpreting works.

git-afsantos commented 3 years ago

Sounds good, but I have some comments:

hellantos commented 3 years ago
  • It seems this is only a problem in Python 3, not in Python 2.7.

I did run into this problem with Python 3. I did not check Python 2.7. With Python 2.7 being EOL, it is probably good to fix this, also it has backwards compatibility.

  • Your commit adds exactly the same line in the except part, which would result in an error again. You should replace the 0 base with something else.
  • In C++, int literals with leading zeroes are octal literals, not decimal. So maybe the correct base should be 8, and not 10.

You're absolutely correct, changed it in the commit.

git-afsantos commented 3 years ago

Looks good now! Thanks for the contribution :+1: