Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ extern int _Py_GetTicksPerSecond(long *ticks_per_second);
// Export for '_testcapi' shared extension
PyAPI_FUNC(int) _Py_IsValidFD(int fd);

// Export for '_remote_debugging' shared extension
PyAPI_FUNC(PyObject*) _Py_strerror(int code);

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 5 additions & 4 deletions Lib/sysconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,10 @@ def parse_config_h(fp, vars=None):
if vars is None:
vars = {}
import re
define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
quoted_re = re.compile('^"(.*)"$')
name_rx = '(?:[A-Z]|_Py_)[A-Za-z0-9_]+'
define_rx = re.compile(fr"#define ({name_rx}) (.*)\n")
undef_rx = re.compile(fr"/[*] #undef ({name_rx}) [*]/\n")
quoted_re = re.compile(r'"(.*)"')

while True:
line = fp.readline()
Expand All @@ -446,7 +447,7 @@ def parse_config_h(fp, vars=None):
m = define_rx.match(line)
if m:
n, v = m.group(1, 2)
if mq := quoted_re.match(v):
if mq := quoted_re.fullmatch(v):
v = mq.group(1)
try:
if n in _ALWAYS_STR:
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_free_threading/test_os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import errno
import os
import sysconfig
import unittest

from test.support import threading_helper
from test.support.threading_helper import run_concurrently


NTHREADS = 10


@threading_helper.requires_working_threading()
class TestOs(unittest.TestCase):
@unittest.skipUnless(sysconfig.get_config_var('_Py_HAVE_STRERROR_R'),
'need _Py_HAVE_STRERROR_R macro')
def test_strerror(self):
# gh-158893: os.strerror() is implemented with strerror_r() which is
# thread safe. Well, check if it's actually the case.
last_error = max([getattr(errno, name) for name in dir(errno)
if name.startswith('E')])
test_errors = tuple(range(1, last_error + 1))
loops = 20

def worker():
for _ in range(loops):
for i in test_errors:
os.strerror(i)

run_concurrently(
worker_func=worker, nthreads=NTHREADS
)


if __name__ == "__main__":
unittest.main()
68 changes: 67 additions & 1 deletion Lib/test/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
get_path, get_path_names, _INSTALL_SCHEMES,
get_default_scheme, get_scheme_names, get_config_var,
_expand_vars, _get_preferred_schemes,
is_python_build, _PROJECT_BASE)
is_python_build, _PROJECT_BASE, parse_config_h)
from sysconfig.__main__ import _main, _parse_makefile, _get_pybuilddir, _get_json_data_name
import _imp
import _osx_support
Expand Down Expand Up @@ -760,6 +760,72 @@ def test_sysconfig_config_vars_no_prefix_cache(self):
self.assertEqual(config_vars['exec_prefix'], sys.exec_prefix)
self.assertEqual(config_vars['platbase'], sys.exec_prefix)

def test_parse_config_h(self):
config = textwrap.dedent('''
#ifndef Py_PYCONFIG_H
#define Py_PYCONFIG_H

/* C comment */

#define ALIGNOF_LONG 8
#define HAVE_ACCEPT 1
#define _Py_HAVE_COSPI 1
#define INVALID_NUMBER abc
#define ALT_SOABI "cpython-316t-x86_64-linux-gnu"

// Undef macros must be written as "/* #undef NAME */":
// name must be valid and there is not value.
/* #undef ANDROID_API_LEVEL */
#undef IGNORE_UNDEF
/* #undef IGNORE_VALUE 1 */

# _ALWAYS_STR: don't convert values to an integer,
# but quotes are removed
#define IPHONEOS_DEPLOYMENT_TARGET "13.0"
#define MACOSX_DEPLOYMENT_TARGET 10

// Spaces are tolerated after the name, not before
#define SPACES_AFTER 1
#define IGNORED_SPACES_BEFORE 1

// Ignore macro without value
#define IGNORE_NO_VALUE

// Ignore macros with an invalid name
#define _PRIVATE_IGNORED 1
#define aLOWER_IGNORED 1
#define 123IGNORED 1
#define INVALID-NAME 1
#define INVALID#NAME 1
#define NONASCII_NAME_é 1

// Ignore single letter names
#define A 1
/* #undef A */

#endif /*Py_PYCONFIG_H*/
''')

filename = TESTFN
self.addCleanup(unlink, filename)
with open(filename, "w", encoding="utf-8") as fp:
fp.write(config)
vars = {}
with open(filename, encoding="utf-8") as fp:
parse_config_h(fp, vars)
expected = {
'ALIGNOF_LONG': 8,
'HAVE_ACCEPT': 1,
'_Py_HAVE_COSPI': 1,
'INVALID_NUMBER': 'abc',
'ALT_SOABI': 'cpython-316t-x86_64-linux-gnu',
'ANDROID_API_LEVEL': 0,
'IPHONEOS_DEPLOYMENT_TARGET': '13.0',
'MACOSX_DEPLOYMENT_TARGET': '10', # str, not int
'SPACES_AFTER': 1,
}
self.assertEqual(vars, expected)


class MakefileTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make :func:`os.strerror` thread-safe: use the reentrant ``strerror_r()``
function if available. Patch by Victor Stinner.
21 changes: 15 additions & 6 deletions Modules/_remote_debugging/subprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
******************************************************************************/

#include "_remote_debugging.h"
#include "pycore_fileutils.h" // _Py_strerror()

#ifndef MS_WINDOWS
#include <unistd.h>
Expand Down Expand Up @@ -229,9 +230,13 @@ get_child_pids_platform(pid_t target_pid, int recursive, pid_array_t *result)
if (entry == NULL) {
if (errno != 0) {
int err = errno;
_set_debug_oserror_from_errno_with_filename(err, "/proc",
"Failed to read process directory '/proc': %s",
strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno_with_filename(err, "/proc",
"Failed to read process directory '/proc': %S",
message);
Py_DECREF(message);
}
goto done;
}
break;
Expand Down Expand Up @@ -259,9 +264,13 @@ get_child_pids_platform(pid_t target_pid, int recursive, pid_array_t *result)
if (closedir(proc_dir) != 0) {
int err = errno;
proc_dir = NULL;
_set_debug_oserror_from_errno_with_filename(err, "/proc",
"Failed to close process directory '/proc': %s",
strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno_with_filename(err, "/proc",
"Failed to close process directory '/proc': %S",
message);
Py_DECREF(message);
}
goto done;
}
proc_dir = NULL;
Expand Down
45 changes: 33 additions & 12 deletions Modules/_remote_debugging/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
******************************************************************************/

#include "_remote_debugging.h"
#include "pycore_fileutils.h" // _Py_strerror()

#ifndef MS_WINDOWS
#include <unistd.h>
Expand Down Expand Up @@ -680,9 +681,13 @@ read_thread_ids(RemoteUnwinderObject *unwinder, _Py_RemoteDebug_ThreadsState *st
int err = errno;
closedir(dir);
_Py_RemoteDebug_InitThreadsState(unwinder, st);
_set_debug_oserror_from_errno_with_filename(err, task_path,
"Failed to read process task directory '%s': %s",
task_path, strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno_with_filename(err, task_path,
"Failed to read process task directory '%s': %S",
task_path, message);
Py_DECREF(message);
}
return -1;
}
break;
Expand Down Expand Up @@ -713,9 +718,13 @@ read_thread_ids(RemoteUnwinderObject *unwinder, _Py_RemoteDebug_ThreadsState *st
if (closedir(dir) != 0) {
int err = errno;
_Py_RemoteDebug_InitThreadsState(unwinder, st);
_set_debug_oserror_from_errno_with_filename(err, task_path,
"Failed to close process task directory '%s': %s",
task_path, strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno_with_filename(err, task_path,
"Failed to close process task directory '%s': %S",
task_path, message);
Py_DECREF(message);
}
return -1;
}
st->tids = unwinder->thread_tids;
Expand Down Expand Up @@ -780,8 +789,12 @@ _Py_RemoteDebug_StopAllThreads(RemoteUnwinderObject *unwinder, _Py_RemoteDebug_T
}
if (ret < 0) {
detach_threads(st, seized);
_set_debug_oserror_from_errno(err,
"Failed to seize thread %d: %s", tid, strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno(err,
"Failed to seize thread %d: %S", tid, message);
Py_DECREF(message);
}
_Py_RemoteDebug_InitThreadsState(unwinder, st);
return -1;
}
Expand All @@ -791,8 +804,12 @@ _Py_RemoteDebug_StopAllThreads(RemoteUnwinderObject *unwinder, _Py_RemoteDebug_T
err = errno;
if (err != ESRCH) {
detach_threads(st, seized);
_set_debug_oserror_from_errno(err,
"Failed to interrupt thread %d: %s", tid, strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno(err,
"Failed to interrupt thread %d: %S", tid, message);
Py_DECREF(message);
}
_Py_RemoteDebug_InitThreadsState(unwinder, st);
return -1;
}
Expand All @@ -803,8 +820,12 @@ _Py_RemoteDebug_StopAllThreads(RemoteUnwinderObject *unwinder, _Py_RemoteDebug_T
err = errno;
if (err != ECHILD && err != ESRCH) {
detach_threads(st, seized);
_set_debug_oserror_from_errno(err,
"waitpid failed for thread %d: %s", tid, strerror(err));
PyObject *message = _Py_strerror(err);
if (message != NULL) {
_set_debug_oserror_from_errno(err,
"waitpid failed for thread %d: %S", tid, message);
Py_DECREF(message);
}
_Py_RemoteDebug_InitThreadsState(unwinder, st);
return -1;
}
Expand Down
8 changes: 1 addition & 7 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13831,13 +13831,7 @@ static PyObject *
os_strerror_impl(PyObject *module, int code)
/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
{
char *message = strerror(code);
if (message == NULL) {
PyErr_SetString(PyExc_ValueError,
"strerror() argument out of range");
return NULL;
}
return PyUnicode_DecodeLocale(message, "surrogateescape");
return _Py_strerror(code);
}


Expand Down
3 changes: 1 addition & 2 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,7 @@ PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, P

#ifndef MS_WINDOWS
if (i != 0) {
const char *s = strerror(i);
message = PyUnicode_DecodeLocale(s, "surrogateescape");
message = _Py_strerror(i);
}
else {
/* Sometimes errno didn't get set */
Expand Down
Loading
Loading