@@ -1566,17 +1566,27 @@ init_extended_context(PyObject *v) |
1566 | 1566 | } |
1567 | 1567 |
|
1568 | 1568 | /* Factory function for creating IEEE interchange format contexts */ |
| 1569 | + |
| 1570 | +/*[clinic input] |
| 1571 | +_decimal.Decimal.IEEEContext |
| 1572 | +
|
| 1573 | + self as module: self |
| 1574 | + bits: Py_ssize_t |
| 1575 | + / |
| 1576 | +
|
| 1577 | +Return a context object initialized as one of the IEEE interchange formats. |
| 1578 | +
|
| 1579 | +The argument must be a multiple of 32 and less than |
| 1580 | +IEEE_CONTEXT_MAX_BITS. |
| 1581 | +[clinic start generated code]*/ |
| 1582 | + |
1569 | 1583 | static PyObject * |
1570 | | -ieee_context(PyObject *module, PyObject *v) |
| 1584 | +_decimal_Decimal_IEEEContext_impl(PyObject *module, Py_ssize_t bits) |
| 1585 | +/*[clinic end generated code: output=042b8664fd2830f7 input=dc5578f331d0aaa9]*/ |
1571 | 1586 | { |
1572 | 1587 | PyObject *context; |
1573 | | - mpd_ssize_t bits; |
1574 | 1588 | mpd_context_t ctx; |
1575 | 1589 |
|
1576 | | - bits = PyLong_AsSsize_t(v); |
1577 | | - if (bits == -1 && PyErr_Occurred()) { |
1578 | | - return NULL; |
1579 | | - } |
1580 | 1590 | if (bits <= 0 || bits > INT_MAX) { |
1581 | 1591 | goto error; |
1582 | 1592 | } |
@@ -1774,8 +1784,9 @@ current_context(decimal_state *modstate) |
1774 | 1784 | } while (0) |
1775 | 1785 |
|
1776 | 1786 | /* Return a new reference to the current context */ |
| 1787 | + |
1777 | 1788 | static PyObject * |
1778 | | -PyDec_GetCurrentContext(PyObject *self, PyObject *Py_UNUSED(dummy)) |
| 1789 | +PyDec_GetCurrentContext(PyObject *self) |
1779 | 1790 | { |
1780 | 1791 | PyObject *context; |
1781 | 1792 | decimal_state *state = get_module_state(self); |
@@ -1871,7 +1882,7 @@ current_context(decimal_state *state) |
1871 | 1882 |
|
1872 | 1883 | /* Return a new reference to the current context */ |
1873 | 1884 | static PyObject * |
1874 | | -PyDec_GetCurrentContext(PyObject *self, PyObject *Py_UNUSED(dummy)) |
| 1885 | +PyDec_GetCurrentContext(PyObject *self) |
1875 | 1886 | { |
1876 | 1887 | decimal_state *state = get_module_state(self); |
1877 | 1888 | return current_context(state); |
@@ -1910,36 +1921,74 @@ PyDec_SetCurrentContext(PyObject *self, PyObject *v) |
1910 | 1921 | } |
1911 | 1922 | #endif |
1912 | 1923 |
|
| 1924 | +/*[clinic input] |
| 1925 | +_decimal.Decimal.getcontext |
| 1926 | +
|
| 1927 | +Get the current default context. |
| 1928 | +[clinic start generated code]*/ |
| 1929 | + |
| 1930 | +static PyObject * |
| 1931 | +_decimal_Decimal_getcontext_impl(PyObject *self) |
| 1932 | +/*[clinic end generated code: output=7efa232c0136dbba input=2d641118d62b25d4]*/ |
| 1933 | +{ |
| 1934 | + return PyDec_GetCurrentContext(self); |
| 1935 | +} |
| 1936 | + |
| 1937 | +/*[clinic input] |
| 1938 | +_decimal.Decimal.setcontext |
| 1939 | +
|
| 1940 | + context: object |
| 1941 | +
|
| 1942 | +Set a new default context. |
| 1943 | +[clinic start generated code]*/ |
| 1944 | + |
| 1945 | +static PyObject * |
| 1946 | +_decimal_Decimal_setcontext_impl(PyObject *self, PyObject *context) |
| 1947 | +/*[clinic end generated code: output=28637d8482b37a71 input=d008c3c978217ccd]*/ |
| 1948 | +{ |
| 1949 | + return PyDec_SetCurrentContext(self, context); |
| 1950 | +} |
| 1951 | + |
1913 | 1952 | /* Context manager object for the 'with' statement. The manager |
1914 | 1953 | * owns one reference to the global (outer) context and one |
1915 | 1954 | * to the local (inner) context. */ |
| 1955 | + |
| 1956 | +/*[clinic input] |
| 1957 | +@text_signature "($module, /, ctx=None, **kwargs)" |
| 1958 | +_decimal.Decimal.localcontext |
| 1959 | +
|
| 1960 | + self as m: self |
| 1961 | + ctx as local: object = None |
| 1962 | + * |
| 1963 | + prec: object = None |
| 1964 | + rounding: object = None |
| 1965 | + Emin: object = None |
| 1966 | + Emax: object = None |
| 1967 | + capitals: object = None |
| 1968 | + clamp: object = None |
| 1969 | + flags: object = None |
| 1970 | + traps: object = None |
| 1971 | +
|
| 1972 | +Return a context manager for a copy of the supplied context. |
| 1973 | +
|
| 1974 | +That will set the default context to a copy of ctx on entry to the |
| 1975 | +with-statement and restore the previous default context when exiting |
| 1976 | +the with-statement. If no context is specified, a copy of the current |
| 1977 | +default context is used. |
| 1978 | +[clinic start generated code]*/ |
| 1979 | + |
1916 | 1980 | static PyObject * |
1917 | | -ctxmanager_new(PyObject *m, PyObject *args, PyObject *kwds) |
| 1981 | +_decimal_Decimal_localcontext_impl(PyObject *m, PyObject *local, |
| 1982 | + PyObject *prec, PyObject *rounding, |
| 1983 | + PyObject *Emin, PyObject *Emax, |
| 1984 | + PyObject *capitals, PyObject *clamp, |
| 1985 | + PyObject *flags, PyObject *traps) |
| 1986 | +/*[clinic end generated code: output=21cc25fbed642f2f input=77906e599937a9b5]*/ |
1918 | 1987 | { |
1919 | | - static char *kwlist[] = { |
1920 | | - "ctx", "prec", "rounding", |
1921 | | - "Emin", "Emax", "capitals", |
1922 | | - "clamp", "flags", "traps", |
1923 | | - NULL |
1924 | | - }; |
1925 | | - PyObject *local = Py_None; |
1926 | 1988 | PyObject *global; |
1927 | 1989 |
|
1928 | | - PyObject *prec = Py_None; |
1929 | | - PyObject *rounding = Py_None; |
1930 | | - PyObject *Emin = Py_None; |
1931 | | - PyObject *Emax = Py_None; |
1932 | | - PyObject *capitals = Py_None; |
1933 | | - PyObject *clamp = Py_None; |
1934 | | - PyObject *flags = Py_None; |
1935 | | - PyObject *traps = Py_None; |
1936 | | - |
1937 | 1990 | decimal_state *state = get_module_state(m); |
1938 | 1991 | CURRENT_CONTEXT(state, global); |
1939 | | - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOOOO", kwlist, &local, |
1940 | | - &prec, &rounding, &Emin, &Emax, &capitals, &clamp, &flags, &traps)) { |
1941 | | - return NULL; |
1942 | | - } |
1943 | 1992 | if (local == Py_None) { |
1944 | 1993 | local = global; |
1945 | 1994 | } |
@@ -6806,10 +6855,10 @@ static PyType_Spec context_spec = { |
6806 | 6855 |
|
6807 | 6856 | static PyMethodDef _decimal_methods [] = |
6808 | 6857 | { |
6809 | | - { "getcontext", PyDec_GetCurrentContext, METH_NOARGS, doc_getcontext}, |
6810 | | - { "setcontext", PyDec_SetCurrentContext, METH_O, doc_setcontext}, |
6811 | | - { "localcontext", _PyCFunction_CAST(ctxmanager_new), METH_VARARGS|METH_KEYWORDS, doc_localcontext}, |
6812 | | - { "IEEEContext", ieee_context, METH_O, doc_ieee_context}, |
| 6858 | + _DECIMAL_DECIMAL_GETCONTEXT_METHODDEF |
| 6859 | + _DECIMAL_DECIMAL_SETCONTEXT_METHODDEF |
| 6860 | + _DECIMAL_DECIMAL_LOCALCONTEXT_METHODDEF |
| 6861 | + _DECIMAL_DECIMAL_IEEECONTEXT_METHODDEF |
6813 | 6862 | { NULL, NULL, 1, NULL } |
6814 | 6863 | }; |
6815 | 6864 |
|
|