Commit Graph
16 Commits
Author SHA1 Message Date
1f2f7ff012 Fix int unicode decimal digits (#8521)
* Accept Unicode decimal digits in int(), Decimal() and complex()

CPython runs a string argument through
_PyUnicode_TransformDecimalAndSpaceToASCII before parsing it, so decimal
digits from any script are accepted:

    int('١٢٣')          # 123
    int('0x١f', 16)     # 31
    Decimal('١٢٣')      # Decimal('123')
    complex('1+2j')    # (1+2j)

RustPython only did this for float(), which had the transform inlined.
int() handed the raw UTF-8 bytes to bytes_to_int(), whose digit check is
is_ascii_alphanumeric(), so every non-ASCII digit was rejected — even
though float() accepted the same string.

Lift the inlined transform out of float_from_string() into
common::str::transform_decimal_and_space_to_ascii() and apply it to the
str paths of int() and complex() too. The result is always ASCII: as in
CPython, a character that is neither ASCII, whitespace nor a decimal
digit becomes '?' and truncates the string, which no parser accepts at
any base, leaving the caller to raise the error from the original string.

Bytes-like input keeps going straight to the parser, matching CPython's
split between PyLong_FromUnicodeObject and PyLong_FromString.

This unmarks two expectedFailure tests: test_int.test_unicode and
test_decimal.test_unicode_digits.

* Share one PyStr-to-numeric-literal step across int, float and complex

All three constructors need the same thing from a str argument: trim it,
fold Unicode decimal digits and whitespace to ASCII, and give up on a
string holding surrogates. Each expressed that last part differently —
float matched PyKindStr and returned b"", complex leaned on to_str()
returning None, int returned an empty Cow — so the rule lived in three
places at once.

Move it into protocol::numeric_literal_from_str() and have all three call
it. CPython repeats this per type because its wrapper is three lines over
a single PyUnicode representation; ours has to match over Ascii/Utf8/Wtf8,
which is worth writing once.

Only the shared step moves. int keeps its base handling, int and float
keep accepting bytes-like input, complex keeps rejecting it, and each
keeps raising its own error, because none of that is shared.

No behavior change: the CPython differential suite is byte-identical
before and after.

* Drop the now-empty test_unicode_digits override in test_decimal

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
2026-08-18 22:24:02 +09:00
Shahar Naveh a2fabfa064 Update some of changed libs and tests from 3.14.5 -> 3.14.6 (part 1) (#8084) 2026-06-12 23:49:15 +09:00
ShaharNaveh 6af0af2b3c Unmark passing test 2026-01-30 16:39:33 +02:00
ShaharNaveh a1b41ad2ad Update decimal from 3.14.2 2026-01-30 16:38:36 +02:00
Jeong, YunWon 1b17587585 __slots__ xor __dict__ , name mangling (#6392)
* __slots__ xor __dict__

* mangle_name for `__` prefixed members
2025-12-10 23:30:10 +09:00
Shahar Naveh ae03bacb39 Update {_py,}decimal.py from 3.13.5 (#6034) 2025-07-25 19:04:49 +09:00
Ashwin Naren 4336b9e787 fix test_decimal 2025-04-15 15:55:09 -07:00
Ankit Goel 5f75728ede Fix inconsistent behavior of Decimal.from_float 2024-09-22 01:42:28 +01:00
Ankit Goel 8066f36a4e Fix copysign behavior for NaNs (#5396) 2024-09-11 15:19:21 +09:00
Jeong, YunWonandCPython developers af884cb284 First step for Python 3.12 support (#5078)
* Mark 3.12

* Update importlib from Python 3.12.0

* Update test_importlib from Python3.12

* Mark failings tests from importlib

* Update test.support from Python3.12

* Fix unsupported parser feature

* mark failing test

* Update functools from Python 3.12

* manual type annotation

* slice behavior changed in 3.12

* empty unittest.main returns non-zero

* test_decimal from CPython 3.12

* Mark failing tests

* Update test_unicode from CPython 3.12

* Update test_functools from Python 3.12

* Update enum from Python 3.12

* enum

* Doc format changed

* Update test_module from CPython

---------

Co-authored-by: CPython developers <>
2023-10-22 19:19:05 -07:00
Jeong Yunwon 3d27f163e4 adjust failing markers for test_{builtin,decimal,float,range} 2022-07-18 04:38:26 +09:00
CPython developers 33690be35b Update tests from CPython 3.10.5 2022-07-18 04:07:35 +09:00
Jeong Yunwon 612ceecdc4 Fix test_decimal for current test support 2022-03-22 04:36:16 +09:00
Dean Li 6f98288e84 test: use import_helper 2021-11-29 21:03:02 +08:00
snowapril ff98ea33a0 add annotations for making test success
Signed-off-by: snowapril <sinjihng@gmail.com>
2021-09-06 18:55:23 +09:00
snowapril 6c30d02a72 add test_decimal.py from cpython 3.8
Signed-off-by: snowapril <sinjihng@gmail.com>
2021-09-06 18:55:23 +09:00