Skip to content

gh-73487: Convert _decimal to use Argument Clinic (part 2) #137637

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

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
+ convert functions
  • Loading branch information
skirpichev committed Aug 12, 2025
commit d0b03ffd4f7d17a5571fbf79f37afa675663ed2e
7 changes: 7 additions & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct _Py_global_strings {

struct {
STRUCT_FOR_ID(CANCELLED)
STRUCT_FOR_ID(Emax)
STRUCT_FOR_ID(Emin)
STRUCT_FOR_ID(FINISHED)
STRUCT_FOR_ID(False)
STRUCT_FOR_ID(JSONDecodeError)
Expand Down Expand Up @@ -336,10 +338,12 @@ struct _Py_global_strings {
STRUCT_FOR_ID(callback)
STRUCT_FOR_ID(cancel)
STRUCT_FOR_ID(capath)
STRUCT_FOR_ID(capitals)
STRUCT_FOR_ID(category)
STRUCT_FOR_ID(cb_type)
STRUCT_FOR_ID(certfile)
STRUCT_FOR_ID(check_same_thread)
STRUCT_FOR_ID(clamp)
STRUCT_FOR_ID(clear)
STRUCT_FOR_ID(close)
STRUCT_FOR_ID(closed)
Expand Down Expand Up @@ -378,6 +382,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(coro)
STRUCT_FOR_ID(count)
STRUCT_FOR_ID(covariant)
STRUCT_FOR_ID(ctx)
STRUCT_FOR_ID(cwd)
STRUCT_FOR_ID(d_parameter_type)
STRUCT_FOR_ID(data)
Expand Down Expand Up @@ -666,6 +671,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(pos1)
STRUCT_FOR_ID(pos2)
STRUCT_FOR_ID(posix)
STRUCT_FOR_ID(prec)
STRUCT_FOR_ID(print_file_and_line)
STRUCT_FOR_ID(priority)
STRUCT_FOR_ID(progress)
Expand Down Expand Up @@ -782,6 +788,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(traceback)
STRUCT_FOR_ID(trailers)
STRUCT_FOR_ID(translate)
STRUCT_FOR_ID(traps)
STRUCT_FOR_ID(true)
STRUCT_FOR_ID(truncate)
STRUCT_FOR_ID(twice)
Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 82 additions & 33 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1566,17 +1566,27 @@ init_extended_context(PyObject *v)
}

/* Factory function for creating IEEE interchange format contexts */

/*[clinic input]
_decimal.Decimal.IEEEContext

self as module: self
bits: Py_ssize_t
/

Return a context object initialized as one of the IEEE interchange formats.

The argument must be a multiple of 32 and less than
IEEE_CONTEXT_MAX_BITS.
[clinic start generated code]*/

static PyObject *
ieee_context(PyObject *module, PyObject *v)
_decimal_Decimal_IEEEContext_impl(PyObject *module, Py_ssize_t bits)
/*[clinic end generated code: output=042b8664fd2830f7 input=dc5578f331d0aaa9]*/
{
PyObject *context;
mpd_ssize_t bits;
mpd_context_t ctx;

bits = PyLong_AsSsize_t(v);
if (bits == -1 && PyErr_Occurred()) {
return NULL;
}
if (bits <= 0 || bits > INT_MAX) {
goto error;
}
Expand Down Expand Up @@ -1774,8 +1784,9 @@ current_context(decimal_state *modstate)
} while (0)

/* Return a new reference to the current context */

static PyObject *
PyDec_GetCurrentContext(PyObject *self, PyObject *Py_UNUSED(dummy))
PyDec_GetCurrentContext(PyObject *self)
{
PyObject *context;
decimal_state *state = get_module_state(self);
Expand Down Expand Up @@ -1871,7 +1882,7 @@ current_context(decimal_state *state)

/* Return a new reference to the current context */
static PyObject *
PyDec_GetCurrentContext(PyObject *self, PyObject *Py_UNUSED(dummy))
PyDec_GetCurrentContext(PyObject *self)
{
decimal_state *state = get_module_state(self);
return current_context(state);
Expand Down Expand Up @@ -1910,36 +1921,74 @@ PyDec_SetCurrentContext(PyObject *self, PyObject *v)
}
#endif

/*[clinic input]
_decimal.Decimal.getcontext

Get the current default context.
[clinic start generated code]*/

static PyObject *
_decimal_Decimal_getcontext_impl(PyObject *self)
/*[clinic end generated code: output=7efa232c0136dbba input=2d641118d62b25d4]*/
{
return PyDec_GetCurrentContext(self);
}

/*[clinic input]
_decimal.Decimal.setcontext

context: object

Set a new default context.
[clinic start generated code]*/

static PyObject *
_decimal_Decimal_setcontext_impl(PyObject *self, PyObject *context)
/*[clinic end generated code: output=28637d8482b37a71 input=d008c3c978217ccd]*/
{
return PyDec_SetCurrentContext(self, context);
}

/* Context manager object for the 'with' statement. The manager
* owns one reference to the global (outer) context and one
* to the local (inner) context. */

/*[clinic input]
@text_signature "($module, /, ctx=None, **kwargs)"
_decimal.Decimal.localcontext

self as m: self
ctx as local: object = None
*
prec: object = None
rounding: object = None
Emin: object = None
Emax: object = None
capitals: object = None
clamp: object = None
flags: object = None
traps: object = None

Return a context manager for a copy of the supplied context.

That will set the default context to a copy of ctx on entry to the
with-statement and restore the previous default context when exiting
the with-statement. If no context is specified, a copy of the current
default context is used.
[clinic start generated code]*/

static PyObject *
ctxmanager_new(PyObject *m, PyObject *args, PyObject *kwds)
_decimal_Decimal_localcontext_impl(PyObject *m, PyObject *local,
PyObject *prec, PyObject *rounding,
PyObject *Emin, PyObject *Emax,
PyObject *capitals, PyObject *clamp,
PyObject *flags, PyObject *traps)
/*[clinic end generated code: output=21cc25fbed642f2f input=77906e599937a9b5]*/
{
static char *kwlist[] = {
"ctx", "prec", "rounding",
"Emin", "Emax", "capitals",
"clamp", "flags", "traps",
NULL
};
PyObject *local = Py_None;
PyObject *global;

PyObject *prec = Py_None;
PyObject *rounding = Py_None;
PyObject *Emin = Py_None;
PyObject *Emax = Py_None;
PyObject *capitals = Py_None;
PyObject *clamp = Py_None;
PyObject *flags = Py_None;
PyObject *traps = Py_None;

decimal_state *state = get_module_state(m);
CURRENT_CONTEXT(state, global);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOOOO", kwlist, &local,
&prec, &rounding, &Emin, &Emax, &capitals, &clamp, &flags, &traps)) {
return NULL;
}
if (local == Py_None) {
local = global;
}
Expand Down Expand Up @@ -6806,10 +6855,10 @@ static PyType_Spec context_spec = {

static PyMethodDef _decimal_methods [] =
{
{ "getcontext", PyDec_GetCurrentContext, METH_NOARGS, doc_getcontext},
{ "setcontext", PyDec_SetCurrentContext, METH_O, doc_setcontext},
{ "localcontext", _PyCFunction_CAST(ctxmanager_new), METH_VARARGS|METH_KEYWORDS, doc_localcontext},
{ "IEEEContext", ieee_context, METH_O, doc_ieee_context},
_DECIMAL_DECIMAL_GETCONTEXT_METHODDEF
_DECIMAL_DECIMAL_SETCONTEXT_METHODDEF
_DECIMAL_DECIMAL_LOCALCONTEXT_METHODDEF
_DECIMAL_DECIMAL_IEEECONTEXT_METHODDEF
{ NULL, NULL, 1, NULL }
};

Expand Down
Loading
Loading