8000 Incompatible (with int) parsing of the sign in the mpz constructor · Issue #381 · aleaxit/gmpy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Incompatible (with int) parsing of the sign in the mpz constructor #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skirpichev opened this issue Feb 15, 2023 · 3 comments · Fixed by #382
Closed

Incompatible (with int) parsing of the sign in the mpz constructor #381

skirpichev opened this issue Feb 15, 2023 · 3 comments · Fixed by #382

Comments

@skirpichev
Copy link
Contributor
>>> int('+1')
1
>>> mpz('+1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid digits

while

>>>>> int('-1')
-1
>>> mpz('-1')
mpz(-1)

"Optionally, the string can be preceded by + or - (with no space in between), have leading zeros, be surrounded by whitespace, and have single underscores interspersed between digits." (c) CPython docs

@skirpichev
Copy link
Contributor Author

Another example:

>>> int('  0xA',base=0)
10
>>> mpz('  0xA',base=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid digits

BTW, base=0 is incompatible with the int's base=10 default.

@casevh
Copy link
Collaborator
casevh commented Feb 19, 2023

Python and GMP handle embedded white space differently. They both ignore leading and trailing white space. But Python considers embedded white space an error while GMP ignores embedded white space.

The general rule is that gmpy2 should always return the same result as Python if no exception is raised by Python. Raising an exception when Python doesn't is a bug. Returning a "reasonable" value when Python raises an exception is acceptable (at least it is to me).

I think striping all white space is fine and matches the current behavior. But we won't match the Python exceptions for
embedded white space and I don't think it is worth changing GMP's behavior.

I also prefer the default of base=0. It is easier for converting strings that include the leading base indicators.

@skirpichev
Copy link
Contributor Author

The general rule is that gmpy2 should always return the same result as
Python if no exception is raised by Python. Raising an exception when
Python doesn't is a bug. Returning a "reasonable" value when Python raises
an exception is acceptable (at least it is to me).

Sure, that was the rule I kept in mind.

I think striping all white space is fine and matches the current behavior.

Probably, this does make sense for all constructors.

I also prefer the default of base=0. It is easier for converting strings
that include the leading base indicators.

My fault. That difference does matter only where Python raises exceptions (e.g. `int('0x10')).

skirpichev added a commit to skirpichev/gmpy that referenced this issue Feb 20, 2023
skirpichev added a commit to skirpichev/mpmath that referenced this issue Oct 16, 2023
gmpy2 has fix for aleaxit/gmpy#381 only in the development
version, so I have added simple workaround for this.
skirpichev added a commit to skirpichev/mpmath that referenced this issue Oct 17, 2023
gmpy2 has fix for aleaxit/gmpy#381 only in the development
version, so I have added simple workaround for this.
skirpichev added a commit to skirpichev/mpmath that referenced this issue Oct 18, 2023
gmpy2 has fix for aleaxit/gmpy#381 only in the development
version, so I have added simple workaround for this.
5029
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0