10000 gh-114563: C decimal falls back to pydecimal for unsupported format strings by belm0 · Pull Request #114879 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-114563: C decimal falls back to pydecimal for unsupported format strings #114879

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

Merged
merged 12 commits into from
Feb 12, 2024
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
belm0 and serhiy-storchaka authored Feb 10, 2024
commit d37b5b81c582a8964fe83683e88660e9c4d4017b
1 change: 1 addition & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ def test_formatting(self):
('z>z6.1f', '-0.', 'zzz0.0'),
('x>z6.1f', '-0.', 'xxx0.0'),
('🖤>z6.1f', '-0.', '🖤🖤🖤0.0'), # multi-byte fill char
('\0>z6.1f', '-0.', '\0\0\00.0'), # null fill char

# issue 114563 ('z' format on F type in cdecimal)
('z3,.10F', '-6.24E-323', '0.0000000000'),
Expand Down
13 changes: 3 additions & 10 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3377,14 +3377,7 @@ pydec_format(PyObject *dec, PyObject *fmt, decimal_state *state)
PyObject *u;

if (state->PyDecimal == NULL) {
PyObject *obj = PyImport_ImportModule("_pydecimal");
if (obj == NULL) {
return NULL;
}

state->PyDecimal = PyObject_GetAttrString(obj, "Decimal");
Py_DECREF(obj);

state->PyDecimal = _PyImport_GetModuleAttrString("_pydecimal", "Decimal");
if (state->PyDecimal == NULL) {
return NULL;
}
Expand All @@ -3395,13 +3388,13 @@ pydec_format(PyObject *dec, PyObject *fmt, decimal_state *state)
return NULL;
}

pydec = PyObject_CallFunctionObjArgs(state->PyDecimal, u, NULL);
pydec = PyObject_CallOneArg(state->PyDecimal, u);
Py_DECREF(u);
if (pydec == NULL) {
return NULL;
}

result = PyObject_CallMethod(pydec, "__format__", "(O)", fmt);
result = PyObject_Format(pydec, fmt);
Py_DECREF(pydec);

if (result == NULL) {
Expand Down
0