aleaxit / gmpy

General Multi-Precision arithmetic for Python 2.6+/3+ (GMP, MPIR, MPFR, MPC)
https://gmpy2.readthedocs.io/en/latest/
GNU Lesser General Public License v3.0
516 stars 86 forks source link

Incompatible (wrt the MPC library) parsing in the master #414

Closed skirpichev closed 1 year ago

skirpichev commented 1 year ago

After 43e8de6, the GMPy_RemoveIgnoredASCII() strips white spaces as well as underscores. This breaks MPC-compatible string input parsing, e.g.:

>>> gmpy2.mpc('(1 2)')
mpc('12.0+0.0j')

Not sure if it worth to support this feature forever (i.e. maybe we could deprecate this to simplify parsing). But this behaviour was documented in the sphinx docs before, the mpc.rst of the last stable release (2.1.5) says "In addition to the standard Python string representation of a complex number: "1+2j", the string representation used by the MPC library: "(1 2)" is also supported".

casevh commented 1 year ago

I think we should support the MPC format. Something like the following should work:

  1. Convert input string to ASCII.
  2. If the first character is '(' and the last character is ')' then remove underscores only and call mpc_strtoc.
  3. Otherwise, convert the input string using GMPy_RemoveIgnoredASCII() and proceed with the current code.