Skip to content

Commit

Permalink
Merge branch 'main' into fix-complex_pow-117999
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev authored Jan 20, 2025
2 parents 4f2ce53 + a30277a commit 5d4f11a
Show file tree
Hide file tree
Showing 108 changed files with 2,999 additions and 2,972 deletions.
2,461 changes: 1,231 additions & 1,230 deletions Doc/c-api/init_config.rst

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
// See also PyInterpreterState_Get() and _PyInterpreterState_GET().
extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);

#ifndef NDEBUG
/* Modern equivalent of assert(PyGILState_Check()) */
static inline void
_Py_AssertHoldsTstateFunc(const char *func)
{
PyThreadState *tstate = _PyThreadState_GET();
_Py_EnsureFuncTstateNotNULL(func, tstate);
}
#define _Py_AssertHoldsTstate() _Py_AssertHoldsTstateFunc(__func__)
#else
#define _Py_AssertHoldsTstate()
#endif

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 9 additions & 6 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ class ANSIColors:
setattr(NoColors, attr, "")


def get_colors(colorize: bool = False) -> ANSIColors:
if colorize or can_colorize():
def get_colors(colorize: bool = False, *, file=None) -> ANSIColors:
if colorize or can_colorize(file=file):
return ANSIColors()
else:
return NoColors


def can_colorize() -> bool:
def can_colorize(*, file=None) -> bool:
if file is None:
file = sys.stdout

if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
Expand All @@ -49,7 +52,7 @@ def can_colorize() -> bool:
if os.environ.get("TERM") == "dumb":
return False

if not hasattr(sys.stderr, "fileno"):
if not hasattr(file, "fileno"):
return False

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

try:
return os.isatty(sys.stderr.fileno())
return os.isatty(file.fileno())
except io.UnsupportedOperation:
return sys.stderr.isatty()
return file.isatty()
2 changes: 1 addition & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ def out(s):
save_displayhook = sys.displayhook
sys.displayhook = sys.__displayhook__
saved_can_colorize = _colorize.can_colorize
_colorize.can_colorize = lambda: False
_colorize.can_colorize = lambda *args, **kwargs: False
color_variables = {"PYTHON_COLORS": None, "FORCE_COLOR": None}
for key in color_variables:
color_variables[key] = os.environ.pop(key, None)
Expand Down
58 changes: 29 additions & 29 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4758,7 +4758,7 @@ static PyObject *
Test_cls_with_param_impl(TestObj *self, PyTypeObject *cls, int a);

static PyObject *
Test_cls_with_param(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Test_cls_with_param(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
Expand Down Expand Up @@ -4798,15 +4798,15 @@ Test_cls_with_param(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_
if (a == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = Test_cls_with_param_impl(self, cls, a);
return_value = Test_cls_with_param_impl((TestObj *)self, cls, a);

exit:
return return_value;
}

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


/*[clinic input]
Expand Down Expand Up @@ -4908,18 +4908,18 @@ static PyObject *
Test_cls_no_params_impl(TestObj *self, PyTypeObject *cls);

static PyObject *
Test_cls_no_params(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Test_cls_no_params(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
PyErr_SetString(PyExc_TypeError, "cls_no_params() takes no arguments");
return NULL;
}
return Test_cls_no_params_impl(self, cls);
return Test_cls_no_params_impl((TestObj *)self, cls);
}

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


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

_return_value = Test_metho_not_default_return_converter_impl(self, a);
_return_value = Test_metho_not_default_return_converter_impl((TestObj *)self, a);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
Expand All @@ -4957,7 +4957,7 @@ Test_metho_not_default_return_converter(TestObj *self, PyObject *a)

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


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

static PyObject *
Test_an_metho_arg_named_arg(TestObj *self, PyObject *arg_)
Test_an_metho_arg_named_arg(PyObject *self, PyObject *arg_)
{
PyObject *return_value = NULL;
int arg;
Expand All @@ -4992,15 +4992,15 @@ Test_an_metho_arg_named_arg(TestObj *self, PyObject *arg_)
if (arg == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = Test_an_metho_arg_named_arg_impl(self, arg);
return_value = Test_an_metho_arg_named_arg_impl((TestObj *)self, arg);

exit:
return return_value;
}

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


/*[clinic input]
Expand Down Expand Up @@ -5289,14 +5289,14 @@ static PyObject *
Test_meth_coexist_impl(TestObj *self);

static PyObject *
Test_meth_coexist(TestObj *self, PyObject *Py_UNUSED(ignored))
Test_meth_coexist(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return Test_meth_coexist_impl(self);
return Test_meth_coexist_impl((TestObj *)self);
}

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

/*[clinic input]
@getter
Expand All @@ -5317,14 +5317,14 @@ static PyObject *
Test_property_get_impl(TestObj *self);

static PyObject *
Test_property_get(TestObj *self, void *Py_UNUSED(context))
Test_property_get(PyObject *self, void *Py_UNUSED(context))
{
return Test_property_get_impl(self);
return Test_property_get_impl((TestObj *)self);
}

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

/*[clinic input]
@setter
Expand All @@ -5345,18 +5345,18 @@ static int
Test_property_set_impl(TestObj *self, PyObject *value);

static int
Test_property_set(TestObj *self, PyObject *value, void *Py_UNUSED(context))
Test_property_set(PyObject *self, PyObject *value, void *Py_UNUSED(context))
{
int return_value;

return_value = Test_property_set_impl(self, value);
return_value = Test_property_set_impl((TestObj *)self, value);

return return_value;
}

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

/*[clinic input]
@setter
Expand All @@ -5377,18 +5377,18 @@ static int
Test_setter_first_with_docstr_set_impl(TestObj *self, PyObject *value);

static int
Test_setter_first_with_docstr_set(TestObj *self, PyObject *value, void *Py_UNUSED(context))
Test_setter_first_with_docstr_set(PyObject *self, PyObject *value, void *Py_UNUSED(context))
{
int return_value;

return_value = Test_setter_first_with_docstr_set_impl(self, value);
return_value = Test_setter_first_with_docstr_set_impl((TestObj *)self, value);

return return_value;
}

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

/*[clinic input]
@getter
Expand Down Expand Up @@ -5418,14 +5418,14 @@ static PyObject *
Test_setter_first_with_docstr_get_impl(TestObj *self);

static PyObject *
Test_setter_first_with_docstr_get(TestObj *self, void *Py_UNUSED(context))
Test_setter_first_with_docstr_get(PyObject *self, void *Py_UNUSED(context))
{
return Test_setter_first_with_docstr_get_impl(self);
return Test_setter_first_with_docstr_get_impl((TestObj *)self);
}

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

/*[clinic input]
output push
Expand Down Expand Up @@ -5708,7 +5708,7 @@ Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
Py_ssize_t key_length);

static PyObject *
Test__pyarg_parsestackandkeywords(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Test__pyarg_parsestackandkeywords(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
Expand All @@ -5731,7 +5731,7 @@ Test__pyarg_parsestackandkeywords(TestObj *self, PyTypeObject *cls, PyObject *co
&key, &key_length)) {
goto exit;
}
return_value = Test__pyarg_parsestackandkeywords_impl(self, cls, key, key_length);
return_value = Test__pyarg_parsestackandkeywords_impl((TestObj *)self, cls, key, key_length);

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


/*[clinic input]
Expand Down
31 changes: 20 additions & 11 deletions Lib/test/libregrtest/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def test_func():
def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
display_failure: bool = True) -> None:
# Handle exceptions, detect environment changes.
ansi = get_colors()
red, reset, yellow = ansi.RED, ansi.RESET, ansi.YELLOW
stdout = get_colors(file=sys.stdout)
stderr = get_colors(file=sys.stderr)

# Reset the environment_altered flag to detect if a test altered
# the environment
Expand All @@ -184,28 +184,34 @@ def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
_load_run_test(result, runtests)
except support.ResourceDenied as exc:
if not quiet and not pgo:
print(f"{yellow}{test_name} skipped -- {exc}{reset}", flush=True)
print(
f"{stdout.YELLOW}{test_name} skipped -- {exc}{stdout.RESET}",
flush=True,
)
result.state = State.RESOURCE_DENIED
return
except unittest.SkipTest as exc:
if not quiet and not pgo:
print(f"{yellow}{test_name} skipped -- {exc}{reset}", flush=True)
print(
f"{stdout.YELLOW}{test_name} skipped -- {exc}{stdout.RESET}",
flush=True,
)
result.state = State.SKIPPED
return
except support.TestFailedWithDetails as exc:
msg = f"{red}test {test_name} failed{reset}"
msg = f"{stderr.RED}test {test_name} failed{stderr.RESET}"
if display_failure:
msg = f"{red}{msg} -- {exc}{reset}"
msg = f"{stderr.RED}{msg} -- {exc}{stderr.RESET}"
print(msg, file=sys.stderr, flush=True)
result.state = State.FAILED
result.errors = exc.errors
result.failures = exc.failures
result.stats = exc.stats
return
except support.TestFailed as exc:
msg = f"{red}test {test_name} failed{reset}"
msg = f"{stderr.RED}test {test_name} failed{stderr.RESET}"
if display_failure:
msg = f"{red}{msg} -- {exc}{reset}"
msg = f"{stderr.RED}{msg} -- {exc}{stderr.RESET}"
print(msg, file=sys.stderr, flush=True)
result.state = State.FAILED
result.stats = exc.stats
Expand All @@ -220,8 +226,11 @@ def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
except:
if not pgo:
msg = traceback.format_exc()
print(f"{red}test {test_name} crashed -- {msg}{reset}",
file=sys.stderr, flush=True)
print(
f"{stderr.RED}test {test_name} crashed -- {msg}{stderr.RESET}",
file=sys.stderr,
flush=True,
)
result.state = State.UNCAUGHT_EXC
return

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

start_time = time.perf_counter()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,7 @@ def no_color():
from .os_helper import EnvironmentVarGuard

with (
swap_attr(_colorize, "can_colorize", lambda: False),
swap_attr(_colorize, "can_colorize", lambda file=None: False),
EnvironmentVarGuard() as env,
):
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ def getaddrinfo_task(*args, **kwds):
with self.assertRaises(OSError) as cm:
self.loop.run_until_complete(coro)

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

coro = self.loop.create_connection(
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ def test_subprocess_stderr(self):

transp.close()
self.assertEqual(b'OUT:test', proto.data[1])
self.assertTrue(proto.data[2].startswith(b'ERR:test'), proto.data[2])
self.assertStartsWith(proto.data[2], b'ERR:test')
self.assertEqual(0, proto.returncode)

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

stdin.write(b'test')
self.loop.run_until_complete(proto.completed)
self.assertTrue(proto.data[1].startswith(b'OUT:testERR:test'),
proto.data[1])
self.assertStartsWith(proto.data[1], b'OUT:testERR:test')
self.assertEqual(b'', proto.data[2])

transp.close()
Expand Down
Loading

0 comments on commit 5d4f11a

Please sign in to comment.