Skip to content

Commit c0c29df

Browse files
author
Stefan Krah
authored
bpo-31403: Remove WITHOUT_THREADS from _decimal. (#3474)
1 parent a7fbad9 commit c0c29df

File tree

2 files changed

+16
-113
lines changed

2 files changed

+16
-113
lines changed

Modules/_decimal/_decimal.c

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ typedef struct {
8080
PyObject *traps;
8181
PyObject *flags;
8282
int capitals;
83-
#ifndef WITHOUT_THREADS
8483
PyThreadState *tstate;
85-
#endif
8684
} PyDecContextObject;
8785

8886
typedef struct {
@@ -124,15 +122,10 @@ incr_false(void)
124122
}
125123

126124

127-
#ifdef WITHOUT_THREADS
128-
/* Default module context */
129-
static PyObject *module_context = NULL;
130-
#else
131125
/* Key for thread state dictionary */
132126
static PyObject *tls_context_key = NULL;
133127
/* Invariant: NULL or the most recently accessed thread local context */
134128
static PyDecContextObject *cached_context = NULL;
135-
#endif
136129

137130
/* Template for creating new thread contexts, calling Context() without
138131
* arguments and initializing the module_context on first access. */
@@ -1219,21 +1212,18 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
12191212
SdFlagAddr(self->flags) = &ctx->status;
12201213

12211214
CtxCaps(self) = 1;
1222-
#ifndef WITHOUT_THREADS
12231215
self->tstate = NULL;
1224-
#endif
12251216

12261217
return (PyObject *)self;
12271218
}
12281219

12291220
static void
12301221
context_dealloc(PyDecContextObject *self)
12311222
{
1232-
#ifndef WITHOUT_THREADS
12331223
if (self == cached_context) {
12341224
cached_context = NULL;
12351225
}
1236-
#endif
1226+
12371227
Py_XDECREF(self->traps);
12381228
Py_XDECREF(self->flags);
12391229
Py_TYPE(self)->tp_free(self);
@@ -1501,76 +1491,6 @@ static PyGetSetDef context_getsets [] =
15011491
/* Global, thread local and temporary contexts */
15021492
/******************************************************************************/
15031493

1504-
#ifdef WITHOUT_THREADS
1505-
/* Return borrowed reference to the current context. When compiled
1506-
* without threads, this is always the module context. */
1507-
static int module_context_set = 0;
1508-
static PyObject *
1509-
current_context(void)
1510-
{
1511-
/* In decimal.py, the module context is automatically initialized
1512-
* from the DefaultContext when it is first accessed. This
1513-
* complicates the code and has a speed penalty of 1-2%. */
1514-
if (module_context_set) {
1515-
return module_context;
1516-
}
1517-
1518-
*CTX(module_context) = *CTX(default_context_template);
1519-
CTX(module_context)->status = 0;
1520-
CTX(module_context)->newtrap = 0;
1521-
CtxCaps(module_context) = CtxCaps(default_context_template);
1522-
1523-
module_context_set = 1;
1524-
return module_context;
1525-
}
1526-
1527-
/* ctxobj := borrowed reference to the current context */
1528-
#define CURRENT_CONTEXT(ctxobj) \
1529-
ctxobj = current_context()
1530-
1531-
/* ctx := pointer to the mpd_context_t struct of the current context */
1532-
#define CURRENT_CONTEXT_ADDR(ctx) \
1533-
ctx = CTX(current_context())
1534-
1535-
/* Return a new reference to the current context */
1536-
static PyObject *
1537-
PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED)
1538-
{
1539-
PyObject *context;
1540-
1541-
CURRENT_CONTEXT(context);
1542-
1543-
Py_INCREF(context);
1544-
return context;
1545-
}
1546-
1547-
/* Set the module context to a new context, decrement old reference */
1548-
static PyObject *
1549-
PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
1550-
{
1551-
CONTEXT_CHECK(v);
1552-
1553-
/* If the new context is one of the templates, make a copy.
1554-
* This is the current behavior of decimal.py. */
1555-
if (v == default_context_template ||
1556-
v == basic_context_template ||
1557-
v == extended_context_template) {
1558-
v = context_copy(v, NULL);
1559-
if (v == NULL) {
1560-
return NULL;
1561-
}
1562-
CTX(v)->status = 0;
1563-
}
1564-
else {
1565-
Py_INCREF(v);
1566-
}
1567-
1568-
Py_XDECREF(module_context);
1569-
module_context = v;
1570-
module_context_set = 1;
1571-
Py_RETURN_NONE;
1572-
}
1573-
#else
15741494
/*
15751495
* Thread local storage currently has a speed penalty of about 4%.
15761496
* All functions that map Python's arithmetic operators to mpdecimal
@@ -1713,7 +1633,6 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
17131633
Py_DECREF(v);
17141634
Py_RETURN_NONE;
17151635
}
1716-
#endif
17171636

17181637
/* Context manager object for the 'with' statement. The manager
17191638
* owns one reference to the global (outer) context and one
@@ -5840,17 +5759,9 @@ PyInit__decimal(void)
58405759
CHECK_INT(PyModule_AddObject(m, "DefaultContext",
58415760
default_context_template));
58425761

5843-
#ifdef WITHOUT_THREADS
5844-
/* Init module context */
5845-
ASSIGN_PTR(module_context,
5846-
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
5847-
Py_INCREF(Py_False);
5848-
CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_False));
5849-
#else
58505762
ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__"));
58515763
Py_INCREF(Py_True);
58525764
CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True));
5853-
#endif
58545765

58555766
/* Init basic context template */
58565767
ASSIGN_PTR(basic_context_template,
@@ -5906,12 +5817,8 @@ PyInit__decimal(void)
59065817
Py_CLEAR(MutableMapping); /* GCOV_NOT_REACHED */
59075818
Py_CLEAR(SignalTuple); /* GCOV_NOT_REACHED */
59085819
Py_CLEAR(DecimalTuple); /* GCOV_NOT_REACHED */
5909-
#ifdef WITHOUT_THREADS
5910-
Py_CLEAR(module_context); /* GCOV_NOT_REACHED */
5911-
#else
59125820
Py_CLEAR(default_context_template); /* GCOV_NOT_REACHED */
59135821
Py_CLEAR(tls_context_key); /* GCOV_NOT_REACHED */
5914-
#endif
59155822
Py_CLEAR(basic_context_template); /* GCOV_NOT_REACHED */
59165823
Py_CLEAR(extended_context_template); /* GCOV_NOT_REACHED */
59175824
Py_CLEAR(m); /* GCOV_NOT_REACHED */

Modules/_decimal/tests/runall-memorydebugger.sh

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/sh
22

33
#
4-
# Purpose: test with and without threads, all machine configurations, pydebug,
5-
# refleaks, release build and release build with valgrind.
4+
# Purpose: test all machine configurations, pydebug, refleaks, release build
5+
# and release build with valgrind.
66
#
77
# Synopsis: ./runall-memorydebugger.sh [--all-configs64 | --all-configs32]
88
#
@@ -57,8 +57,7 @@ print_config ()
5757
cd ..
5858

5959
# test_decimal: refleak, regular and Valgrind tests
60-
for args in "--without-threads" ""; do
61-
for config in $CONFIGS; do
60+
for config in $CONFIGS; do
6261

6362
unset PYTHON_DECIMAL_WITH_MACHINE
6463
libmpdec_config=$config
@@ -70,24 +69,24 @@ for args in "--without-threads" ""; do
7069
fi
7170

7271
############ refleak tests ###########
73-
print_config "refleak tests: config=$config" $args
72+
print_config "refleak tests: config=$config"
7473
printf "\nbuilding python ...\n\n"
7574

7675
cd ../../
7776
$GMAKE distclean > /dev/null 2>&1
78-
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --with-pydebug $args > /dev/null 2>&1
77+
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --with-pydebug > /dev/null 2>&1
7978
$GMAKE | grep _decimal
8079

8180
printf "\n\n# ======================== refleak tests ===========================\n\n"
8281
./python -m test -uall -R 2:2 test_decimal
8382

8483

8584
############ regular tests ###########
86-
print_config "regular tests: config=$config" $args
85+
print_config "regular tests: config=$config"
8786
printf "\nbuilding python ...\n\n"
8887

8988
$GMAKE distclean > /dev/null 2>&1
90-
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" $args > /dev/null 2>&1
89+
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" > /dev/null 2>&1
9190
$GMAKE | grep _decimal
9291

9392
printf "\n\n# ======================== regular tests ===========================\n\n"
@@ -104,23 +103,21 @@ for args in "--without-threads" ""; do
104103
esac
105104
esac
106105

107-
print_config "valgrind tests: config=$config" $args
106+
print_config "valgrind tests: config=$config"
108107
printf "\nbuilding python ...\n\n"
109108
$GMAKE distclean > /dev/null 2>&1
110-
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --without-pymalloc $args > /dev/null 2>&1
109+
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --without-pymalloc > /dev/null 2>&1
111110
$GMAKE | grep _decimal
112111

113112
printf "\n\n# ======================== valgrind tests ===========================\n\n"
114113
$valgrind ./python -m test -uall test_decimal
115114

116115
cd Modules/_decimal
117-
done
118116
done
119117

120118
# deccheck
121119
cd ../../
122120
for config in $CONFIGS; do
123-
for args in "--without-threads" ""; do
124121

125122
unset PYTHON_DECIMAL_WITH_MACHINE
126123
if [ X"$config" != X"auto" ]; then
@@ -129,22 +126,22 @@ for config in $CONFIGS; do
129126
fi
130127

131128
############ debug ############
132-
print_config "deccheck: config=$config --with-pydebug" $args
129+
print_config "deccheck: config=$config --with-pydebug"
133130
printf "\nbuilding python ...\n\n"
134131

135132
$GMAKE distclean > /dev/null 2>&1
136-
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --with-pydebug $args > /dev/null 2>&1
133+
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --with-pydebug > /dev/null 2>&1
137134
$GMAKE | grep _decimal
138135

139136
printf "\n\n# ========================== debug ===========================\n\n"
140137
./python Modules/_decimal/tests/deccheck.py
141138

142139
########### regular ###########
143-
print_config "deccheck: config=$config " $args
140+
print_config "deccheck: config=$config "
144141
printf "\nbuilding python ...\n\n"
145142

146143
$GMAKE distclean > /dev/null 2>&1
147-
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" $args > /dev/null 2>&1
144+
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" > /dev/null 2>&1
148145
$GMAKE | grep _decimal
149146

150147
printf "\n\n# ======================== regular ===========================\n\n"
@@ -160,16 +157,15 @@ for config in $CONFIGS; do
160157
esac
161158
esac
162159

163-
print_config "valgrind deccheck: config=$config " $args
160+
print_config "valgrind deccheck: config=$config "
164161
printf "\nbuilding python ...\n\n"
165162

166163
$GMAKE distclean > /dev/null 2>&1
167-
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --without-pymalloc $args > /dev/null 2>&1
164+
./configure CFLAGS="$ADD_CFLAGS" LDFLAGS="$ADD_LDFLAGS" --without-pymalloc > /dev/null 2>&1
168165
$GMAKE | grep _decimal
169166

170167
printf "\n\n# ======================== valgrind ==========================\n\n"
171168
$valgrind ./python Modules/_decimal/tests/deccheck.py
172-
done
173169
done
174170

175171

0 commit comments

Comments
 (0)