Skip to content

Commit 5d4f11a

Browse files
authored
Merge branch 'main' into fix-complex_pow-117999
2 parents 4f2ce53 + a30277a commit 5d4f11a

File tree

108 files changed

+2999
-2972
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2999
-2972
lines changed

Doc/c-api/init_config.rst

Lines changed: 1231 additions & 1230 deletions
Large diffs are not rendered by default.

Include/internal/pycore_pystate.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
300300
// See also PyInterpreterState_Get() and _PyInterpreterState_GET().
301301
extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
302302

303+
#ifndef NDEBUG
304+
/* Modern equivalent of assert(PyGILState_Check()) */
305+
static inline void
306+
_Py_AssertHoldsTstateFunc(const char *func)
307+
{
308+
PyThreadState *tstate = _PyThreadState_GET();
309+
_Py_EnsureFuncTstateNotNULL(func, tstate);
310+
}
311+
#define _Py_AssertHoldsTstate() _Py_AssertHoldsTstateFunc(__func__)
312+
#else
313+
#define _Py_AssertHoldsTstate()
314+
#endif
315+
303316
#ifdef __cplusplus
304317
}
305318
#endif

Lib/_colorize.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ class ANSIColors:
2626
setattr(NoColors, attr, "")
2727

2828

29-
def get_colors(colorize: bool = False) -> ANSIColors:
30-
if colorize or can_colorize():
29+
def get_colors(colorize: bool = False, *, file=None) -> ANSIColors:
30+
if colorize or can_colorize(file=file):
3131
return ANSIColors()
3232
else:
3333
return NoColors
3434

3535

36-
def can_colorize() -> bool:
36+
def can_colorize(*, file=None) -> bool:
37+
if file is None:
38+
file = sys.stdout
39+
3740
if not sys.flags.ignore_environment:
3841
if os.environ.get("PYTHON_COLORS") == "0":
3942
return False
@@ -49,7 +52,7 @@ def can_colorize() -> bool:
4952
if os.environ.get("TERM") == "dumb":
5053
return False
5154

52-
if not hasattr(sys.stderr, "fileno"):
55+
if not hasattr(file, "fileno"):
5356
return False
5457

5558
if sys.platform == "win32":
@@ -62,6 +65,6 @@ def can_colorize() -> bool:
6265
return False
6366

6467
try:
65-
return os.isatty(sys.stderr.fileno())
68+
return os.isatty(file.fileno())
6669
except io.UnsupportedOperation:
67-
return sys.stderr.isatty()
70+
return file.isatty()

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ def out(s):
15581558
save_displayhook = sys.displayhook
15591559
sys.displayhook = sys.__displayhook__
15601560
saved_can_colorize = _colorize.can_colorize
1561-
_colorize.can_colorize = lambda: False
1561+
_colorize.can_colorize = lambda *args, **kwargs: False
15621562
color_variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
15631563
for key in color_variables:
15641564
color_variables[key] = os.environ.pop(key, None)

Lib/test/clinic.test.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,7 +4758,7 @@ static PyObject *
47584758
Test_cls_with_param_impl(TestObj *self, PyTypeObject *cls, int a);
47594759

47604760
static PyObject *
4761-
Test_cls_with_param(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4761+
Test_cls_with_param(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
47624762
{
47634763
PyObject *return_value = NULL;
47644764
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
@@ -4798,15 +4798,15 @@ Test_cls_with_param(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_
47984798
if (a == -1 && PyErr_Occurred()) {
47994799
goto exit;
48004800
}
4801-
return_value = Test_cls_with_param_impl(self, cls, a);
4801+
return_value = Test_cls_with_param_impl((TestObj *)self, cls, a);
48024802

48034803
exit:
48044804
return return_value;
48054805
}
48064806

48074807
static PyObject *
48084808
Test_cls_with_param_impl(TestObj *self, PyTypeObject *cls, int a)
4809-
/*[clinic end generated code: output=83a391eea66d08f8 input=af158077bd237ef9]*/
4809+
/*[clinic end generated code: output=7e893134a81fef92 input=af158077bd237ef9]*/
48104810

48114811

48124812
/*[clinic input]
@@ -4908,18 +4908,18 @@ static PyObject *
49084908
Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls);
49094909

49104910
static PyObject *
4911-
Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
4911+
Test_cls_no_params(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
49124912
{
49134913
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
49144914
PyErr_SetString(PyExc_TypeError, "cls_no_params() takes no arguments");
49154915
return NULL;
49164916
}
4917-
return Test_cls_no_params_impl(self, cls);
4917+
return Test_cls_no_params_impl((TestObj *)self, cls);
49184918
}
49194919

49204920
static PyObject *
49214921
Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls)
4922-
/*[clinic end generated code: output=4d68b4652c144af3 input=e7e2e4e344e96a11]*/
4922+
/*[clinic end generated code: output=8845de054449f40a input=e7e2e4e344e96a11]*/
49234923

49244924

49254925
/*[clinic input]
@@ -4945,7 +4945,7 @@ Test_metho_not_default_return_converter(TestObj *self, PyObject *a)
49454945
PyObject *return_value = NULL;
49464946
int _return_value;
49474947

4948-
_return_value = Test_metho_not_default_return_converter_impl(self, a);
4948+
_return_value = Test_metho_not_default_return_converter_impl((TestObj *)self, a);
49494949
if ((_return_value == -1) && PyErr_Occurred()) {
49504950
goto exit;
49514951
}
@@ -4957,7 +4957,7 @@ Test_metho_not_default_return_converter(TestObj *self, PyObject *a)
49574957

49584958
static int
49594959
Test_metho_not_default_return_converter_impl(TestObj *self, PyObject *a)
4960-
/*[clinic end generated code: output=3350de11bd538007 input=428657129b521177]*/
4960+
/*[clinic end generated code: output=b2cce75a7af2e6ce input=428657129b521177]*/
49614961

49624962

49634963
/*[clinic input]
@@ -4983,7 +4983,7 @@ static PyObject *
49834983
Test_an_metho_arg_named_arg_impl(TestObj *self, int arg);
49844984

49854985
static PyObject *
4986-
Test_an_metho_arg_named_arg(TestObj *self, PyObject *arg_)
4986+
Test_an_metho_arg_named_arg(PyObject *self, PyObject *arg_)
49874987
{
49884988
PyObject *return_value = NULL;
49894989
int arg;
@@ -4992,15 +4992,15 @@ Test_an_metho_arg_named_arg(TestObj *self, PyObject *arg_)
49924992
if (arg == -1 && PyErr_Occurred()) {
49934993
goto exit;
49944994
}
4995-
return_value = Test_an_metho_arg_named_arg_impl(self, arg);
4995+
return_value = Test_an_metho_arg_named_arg_impl((TestObj *)self, arg);
49964996

49974997
exit:
49984998
return return_value;
49994999
}
50005000

50015001
static PyObject *
50025002
Test_an_metho_arg_named_arg_impl(TestObj *self, int arg)
5003-
/*[clinic end generated code: output=9f04de4a62287e28 input=2a53a57cf5624f95]*/
5003+
/*[clinic end generated code: output=38554f09950d07e7 input=2a53a57cf5624f95]*/
50045004

50055005

50065006
/*[clinic input]
@@ -5289,14 +5289,14 @@ static PyObject *
52895289
Test_meth_coexist_impl(TestObj *self);
52905290

52915291
static PyObject *
5292-
Test_meth_coexist(TestObj *self, PyObject *Py_UNUSED(ignored))
5292+
Test_meth_coexist(PyObject *self, PyObject *Py_UNUSED(ignored))
52935293
{
5294-
return Test_meth_coexist_impl(self);
5294+
return Test_meth_coexist_impl((TestObj *)self);
52955295
}
52965296

52975297
static PyObject *
52985298
Test_meth_coexist_impl(TestObj *self)
5299-
/*[clinic end generated code: output=808a293d0cd27439 input=2a1d75b5e6fec6dd]*/
5299+
/*[clinic end generated code: output=7edf4e95b29f06fa input=2a1d75b5e6fec6dd]*/
53005300

53015301
/*[clinic input]
53025302
@getter
@@ -5317,14 +5317,14 @@ static PyObject *
53175317
Test_property_get_impl(TestObj *self);
53185318

53195319
static PyObject *
5320-
Test_property_get(TestObj *self, void *Py_UNUSED(context))
5320+
Test_property_get(PyObject *self, void *Py_UNUSED(context))
53215321
{
5322-
return Test_property_get_impl(self);
5322+
return Test_property_get_impl((TestObj *)self);
53235323
}
53245324

53255325
static PyObject *
53265326
Test_property_get_impl(TestObj *self)
5327-
/*[clinic end generated code: output=7cadd0f539805266 input=2d92b3449fbc7d2b]*/
5327+
/*[clinic end generated code: output=b38d68abd3466a6e input=2d92b3449fbc7d2b]*/
53285328

53295329
/*[clinic input]
53305330
@setter
@@ -5345,18 +5345,18 @@ static int
53455345
Test_property_set_impl(TestObj *self, PyObject *value);
53465346

53475347
static int
5348-
Test_property_set(TestObj *self, PyObject *value, void *Py_UNUSED(context))
5348+
Test_property_set(PyObject *self, PyObject *value, void *Py_UNUSED(context))
53495349
{
53505350
int return_value;
53515351

5352-
return_value = Test_property_set_impl(self, value);
5352+
return_value = Test_property_set_impl((TestObj *)self, value);
53535353

53545354
return return_value;
53555355
}
53565356

53575357
static int
53585358
Test_property_set_impl(TestObj *self, PyObject *value)
5359-
/*[clinic end generated code: output=e4342fe9bb1d7817 input=3bc3f46a23c83a88]*/
5359+
/*[clinic end generated code: output=49f925ab2a33b637 input=3bc3f46a23c83a88]*/
53605360

53615361
/*[clinic input]
53625362
@setter
@@ -5377,18 +5377,18 @@ static int
53775377
Test_setter_first_with_docstr_set_impl(TestObj *self, PyObject *value);
53785378

53795379
static int
5380-
Test_setter_first_with_docstr_set(TestObj *self, PyObject *value, void *Py_UNUSED(context))
5380+
Test_setter_first_with_docstr_set(PyObject *self, PyObject *value, void *Py_UNUSED(context))
53815381
{
53825382
int return_value;
53835383

5384-
return_value = Test_setter_first_with_docstr_set_impl(self, value);
5384+
return_value = Test_setter_first_with_docstr_set_impl((TestObj *)self, value);
53855385

53865386
return return_value;
53875387
}
53885388

53895389
static int
53905390
Test_setter_first_with_docstr_set_impl(TestObj *self, PyObject *value)
5391-
/*[clinic end generated code: output=e4d76b558a4061db input=31a045ce11bbe961]*/
5391+
/*[clinic end generated code: output=5aaf44373c0af545 input=31a045ce11bbe961]*/
53925392

53935393
/*[clinic input]
53945394
@getter
@@ -5418,14 +5418,14 @@ static PyObject *
54185418
Test_setter_first_with_docstr_get_impl(TestObj *self);
54195419

54205420
static PyObject *
5421-
Test_setter_first_with_docstr_get(TestObj *self, void *Py_UNUSED(context))
5421+
Test_setter_first_with_docstr_get(PyObject *self, void *Py_UNUSED(context))
54225422
{
5423-
return Test_setter_first_with_docstr_get_impl(self);
5423+
return Test_setter_first_with_docstr_get_impl((TestObj *)self);
54245424
}
54255425

54265426
static PyObject *
54275427
Test_setter_first_with_docstr_get_impl(TestObj *self)
5428-
/*[clinic end generated code: output=749a30266f9fb443 input=10af4e43b3cb34dc]*/
5428+
/*[clinic end generated code: output=fe6e3aa844a24920 input=10af4e43b3cb34dc]*/
54295429

54305430
/*[clinic input]
54315431
output push
@@ -5708,7 +5708,7 @@ Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
57085708
Py_ssize_t key_length);
57095709

57105710
static PyObject *
5711-
Test__pyarg_parsestackandkeywords(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5711+
Test__pyarg_parsestackandkeywords(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
57125712
{
57135713
PyObject *return_value = NULL;
57145714
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
@@ -5731,7 +5731,7 @@ Test__pyarg_parsestackandkeywords(TestObj *self, PyTypeObject *cls, PyObject *co
57315731
&key, &key_length)) {
57325732
goto exit;
57335733
}
5734-
return_value = Test__pyarg_parsestackandkeywords_impl(self, cls, key, key_length);
5734+
return_value = Test__pyarg_parsestackandkeywords_impl((TestObj *)self, cls, key, key_length);
57355735

57365736
exit:
57375737
return return_value;
@@ -5741,7 +5741,7 @@ static PyObject *
57415741
Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
57425742
const char *key,
57435743
Py_ssize_t key_length)
5744-
/*[clinic end generated code: output=4fda8a7f2547137c input=fc72ef4b4cfafabc]*/
5744+
/*[clinic end generated code: output=7060c213d7b8200e input=fc72ef4b4cfafabc]*/
57455745

57465746

57475747
/*[clinic input]

Lib/test/libregrtest/single.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ def test_func():
162162
def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
163163
display_failure: bool = True) -> None:
164164
# Handle exceptions, detect environment changes.
165-
ansi = get_colors()
166-
red, reset, yellow = ansi.RED, ansi.RESET, ansi.YELLOW
165+
stdout = get_colors(file=sys.stdout)
166+
stderr = get_colors(file=sys.stderr)
167167

168168
# Reset the environment_altered flag to detect if a test altered
169169
# the environment
@@ -184,28 +184,34 @@ def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
184184
_load_run_test(result, runtests)
185185
except support.ResourceDenied as exc:
186186
if not quiet and not pgo:
187-
print(f"{yellow}{test_name} skipped -- {exc}{reset}", flush=True)
187+
print(
188+
f"{stdout.YELLOW}{test_name} skipped -- {exc}{stdout.RESET}",
189+
flush=True,
190+
)
188191
result.state = State.RESOURCE_DENIED
189192
return
190193
except unittest.SkipTest as exc:
191194
if not quiet and not pgo:
192-
print(f"{yellow}{test_name} skipped -- {exc}{reset}", flush=True)
195+
print(
196+
f"{stdout.YELLOW}{test_name} skipped -- {exc}{stdout.RESET}",
197+
flush=True,
198+
)
193199
result.state = State.SKIPPED
194200
return
195201
except support.TestFailedWithDetails as exc:
196-
msg = f"{red}test {test_name} failed{reset}"
202+
msg = f"{stderr.RED}test {test_name} failed{stderr.RESET}"
197203
if display_failure:
198-
msg = f"{red}{msg} -- {exc}{reset}"
204+
msg = f"{stderr.RED}{msg} -- {exc}{stderr.RESET}"
199205
print(msg, file=sys.stderr, flush=True)
200206
result.state = State.FAILED
201207
result.errors = exc.errors
202208
result.failures = exc.failures
203209
result.stats = exc.stats
204210
return
205211
except support.TestFailed as exc:
206-
msg = f"{red}test {test_name} failed{reset}"
212+
msg = f"{stderr.RED}test {test_name} failed{stderr.RESET}"
207213
if display_failure:
208-
msg = f"{red}{msg} -- {exc}{reset}"
214+
msg = f"{stderr.RED}{msg} -- {exc}{stderr.RESET}"
209215
print(msg, file=sys.stderr, flush=True)
210216
result.state = State.FAILED
211217
result.stats = exc.stats
@@ -220,8 +226,11 @@ def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
220226
except:
221227
if not pgo:
222228
msg = traceback.format_exc()
223-
print(f"{red}test {test_name} crashed -- {msg}{reset}",
224-
file=sys.stderr, flush=True)
229+
print(
230+
f"{stderr.RED}test {test_name} crashed -- {msg}{stderr.RESET}",
231+
file=sys.stderr,
232+
flush=True,
233+
)
225234
result.state = State.UNCAUGHT_EXC
226235
return
227236

@@ -303,7 +312,7 @@ def run_single_test(test_name: TestName, runtests: RunTests) -> TestResult:
303312
If runtests.use_junit, xml_data is a list containing each generated
304313
testsuite element.
305314
"""
306-
ansi = get_colors()
315+
ansi = get_colors(file=sys.stderr)
307316
red, reset, yellow = ansi.BOLD_RED, ansi.RESET, ansi.YELLOW
308317

309318
start_time = time.perf_counter()

Lib/test/support/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2839,7 +2839,7 @@ def no_color():
28392839
from .os_helper import EnvironmentVarGuard
28402840

28412841
with (
2842-
swap_attr(_colorize, "can_colorize", lambda: False),
2842+
swap_attr(_colorize, "can_colorize", lambda file=None: False),
28432843
EnvironmentVarGuard() as env,
28442844
):
28452845
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:

Lib/test/test_asyncio/test_base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ def getaddrinfo_task(*args, **kwds):
13451345
with self.assertRaises(OSError) as cm:
13461346
self.loop.run_until_complete(coro)
13471347

1348-
self.assertTrue(str(cm.exception).startswith('Multiple exceptions: '))
1348+
self.assertStartsWith(str(cm.exception), 'Multiple exceptions: ')
13491349
self.assertTrue(m_socket.socket.return_value.close.called)
13501350

13511351
coro = self.loop.create_connection(

Lib/test/test_asyncio/test_events.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ def test_subprocess_stderr(self):
21842184

21852185
transp.close()
21862186
self.assertEqual(b'OUT:test', proto.data[1])
2187-
self.assertTrue(proto.data[2].startswith(b'ERR:test'), proto.data[2])
2187+
self.assertStartsWith(proto.data[2], b'ERR:test')
21882188
self.assertEqual(0, proto.returncode)
21892189

21902190
@support.requires_subprocess()
@@ -2206,8 +2206,7 @@ def test_subprocess_stderr_redirect_to_stdout(self):
22062206

22072207
stdin.write(b'test')
22082208
self.loop.run_until_complete(proto.completed)
2209-
self.assertTrue(proto.data[1].startswith(b'OUT:testERR:test'),
2210-
proto.data[1])
2209+
self.assertStartsWith(proto.data[1], b'OUT:testERR:test')
22112210
self.assertEqual(b'', proto.data[2])
22122211

22132212
transp.close()

0 commit comments

Comments
 (0)