Skip to content

Commit d9f233d

Browse files
committed
Redo local_context() at C level, correct context() docs
1 parent 84f53db commit d9f233d

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

gmpy2/__init__.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from .gmpy2 import *
22
from .gmpy2 import __version__
3-
from .gmpy2 import _local_context
43
# Internal variables/functions are not imported by * above.
54
# These are used by some python level functions and are needed
65
# at the top level.
@@ -9,11 +8,3 @@
98
from .gmpy2 import _C_API, _mpmath_normalize, _mpmath_create
109
except ImportError:
1110
from .gmpy2 import _mpmath_normalize, _mpmath_create
12-
13-
14-
def local_context(*args, **kwargs):
15-
"""Alias of `context()`, for compatibility."""
16-
import warnings
17-
warnings.warn("local_context() is deprecated, use context() instead",
18-
DeprecationWarning)
19-
return context(*args, **kwargs)

src/gmpy2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ static PyMethodDef Pygmpy_methods [] =
888888
{ "lgamma", GMPy_Context_Lgamma, METH_O, GMPy_doc_function_lgamma },
889889
{ "li2", GMPy_Context_Li2, METH_O, GMPy_doc_function_li2 },
890890
{ "lngamma", GMPy_Context_Lngamma, METH_O, GMPy_doc_function_lngamma },
891-
{ "_local_context", (PyCFunction)GMPy_CTXT_Local_Context2, METH_VARARGS | METH_KEYWORDS, GMPy_doc_local_context2 },
891+
{ "local_context", (PyCFunction)GMPy_CTXT_Local, METH_VARARGS | METH_KEYWORDS, GMPy_doc_local_context },
892892
{ "log", GMPy_Context_Log, METH_O, GMPy_doc_function_log },
893893
{ "log1p", GMPy_Context_Log1p, METH_O, GMPy_doc_function_log1p },
894894
{ "log10", GMPy_Context_Log10, METH_O, GMPy_doc_function_log10 },

src/gmpy2_context.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,26 +528,43 @@ _parse_context_args(CTXT_Object *ctxt, PyObject *kwargs)
528528
return 1;
529529
}
530530

531-
PyDoc_STRVAR(GMPy_doc_local_context2,
532-
"helper function for local_context");
531+
PyDoc_STRVAR(GMPy_doc_local_context,
532+
"local_context(**kwargs) -> context\n"
533+
"local_context(context, /, **kwargs) -> context\n\n"
534+
"Return a new context for controlling gmpy2 arithmetic, based either\n"
535+
"on the current context or on a ctx value. Context options additionally\n"
536+
"can be overriden by keyword arguments.");
533537

534538
static PyObject *
535-
GMPy_CTXT_Local_Context2(PyObject *self, PyObject *args, PyObject *kwargs)
539+
GMPy_CTXT_Local(PyObject *self, PyObject *args, PyObject *kwargs)
536540
{
537541
CTXT_Object *result = NULL, *temp = NULL;
538542

543+
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
544+
"local_context() is deprecated, "
545+
"use context(get_context()) instead.")) {
546+
return NULL;
547+
}
539548

540549
if (PyTuple_GET_SIZE(args) == 0) {
541550
if (!(temp = (CTXT_Object*)GMPy_CTXT_Get())) {
551+
/* LCOV_EXCL_START */
542552
return NULL;
553+
/* LCOV_EXCL_STOP */
543554
}
544555
if (!(result = (CTXT_Object*)GMPy_CTXT_Copy(temp, NULL))) {
556+
/* LCOV_EXCL_START */
545557
return NULL;
558+
/* LCOV_EXCL_STOP */
546559
}
547560
Py_DECREF((PyObject*)temp);
548561
}
549562
else if (PyTuple_GET_SIZE(args) == 1 && CTXT_Check(PyTuple_GET_ITEM(args, 0))) {
550-
result = (CTXT_Object*)GMPy_CTXT_Copy(PyTuple_GET_ITEM(args, 0), NULL);
563+
if (!(result = (CTXT_Object*)GMPy_CTXT_Copy(PyTuple_GET_ITEM(args, 0), NULL))) {
564+
/* LCOV_EXCL_START */
565+
return NULL;
566+
/* LCOV_EXCL_STOP */
567+
}
551568
}
552569
else {
553570
VALUE_ERROR("_local_context() only supports [[context][,keyword]] arguments");
@@ -569,8 +586,8 @@ PyDoc_STRVAR(GMPy_doc_context,
569586
"context(**kwargs)\n"
570587
"context(ctx, /, **kwargs)\n\n"
571588
"Return a new context for controlling gmpy2 arithmetic, based either\n"
572-
"on the current context or on a ctx value. Context options additionally\n"
573-
"can be overriden by keyword arguments.");
589+
"on the default context or on a given by ctx value. Context options\n"
590+
"additionally can be overriden by keyword arguments.");
574591

575592
static PyObject *
576593
GMPy_CTXT_Context(PyTypeObject *type, PyObject *args, PyObject *kwargs)
@@ -586,7 +603,11 @@ GMPy_CTXT_Context(PyTypeObject *type, PyObject *args, PyObject *kwargs)
586603
}
587604
}
588605
else if (PyTuple_GET_SIZE(args) == 1 && CTXT_Check(PyTuple_GET_ITEM(args, 0))) {
589-
result = (CTXT_Object*)GMPy_CTXT_Copy(PyTuple_GET_ITEM(args, 0), NULL);
606+
if (!(result = (CTXT_Object*)GMPy_CTXT_Copy(PyTuple_GET_ITEM(args, 0), NULL))) {
607+
/* LCOV_EXCL_START */
608+
return NULL;
609+
/* LCOV_EXCL_STOP */
610+
}
590611
}
591612
else {
592613
VALUE_ERROR("context() only supports [[context][,keyword]] arguments");

test/test_context.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import warnings
2+
13
import pytest
24

35
import gmpy2
4-
from gmpy2 import context, get_context, ieee, mpc, mpfr, mpz, set_context
6+
from gmpy2 import (context, get_context, ieee, local_context, mpc, mpfr, mpz,
7+
set_context)
58

69

710
def test_context_abs():
@@ -160,9 +163,9 @@ def test_context():
160163

161164
def test_nested_context():
162165
set_context(context())
163-
166+
164167
r = [get_context().precision]
165-
168+
166169
with ieee(128):
167170
r.append(get_context().precision)
168171
with ieee(256):
@@ -177,9 +180,9 @@ def test_nested_context():
177180

178181
def test_nested_context():
179182
set_context(context())
180-
183+
181184
r = [get_context().precision]
182-
185+
183186
with context(ieee(128)):
184187
r.append(get_context().precision)
185188
with context(ieee(256)):
@@ -254,3 +257,24 @@ def test_context_repr():
254257
erange=False,\n trap_divzero=False, divzero=False,\n\
255258
allow_complex=False,\n rational_division=False,\n\
256259
allow_release_gil=False)"""
260+
261+
262+
def test_local_context_deprecated():
263+
with pytest.deprecated_call():
264+
local_context()
265+
266+
with warnings.catch_warnings():
267+
warnings.simplefilter("error", DeprecationWarning)
268+
pytest.raises(DeprecationWarning, lambda: local_context())
269+
270+
271+
@pytest.mark.filterwarnings("ignore:.*:DeprecationWarning")
272+
def test_local_context():
273+
get_context().precision = 123
274+
with context() as ctx:
275+
assert ctx.precision == 53
276+
with local_context() as ctx:
277+
assert ctx.precision == 123
278+
279+
pytest.raises(ValueError, lambda: local_context(1, 2))
280+
pytest.raises(ValueError, lambda: local_context(spam=123))

0 commit comments

Comments
 (0)