https://github.com/python/cpython/commit/863d54cbaf6c0b45fff691ab275515c1483ad68d
commit: 863d54cbaf6c0b45fff691ab275515c1483ad68d
branch: main
author: Tomasz Pytel <[email protected]>
committer: colesbury <[email protected]>
date: 2025-03-17T11:57:11-04:00
summary:
gh-126895: Fix readline module in free-threaded build (#131208)
The underlying readline library is not thread-safe so this adds
`@critical_section` to functions that use it.
files:
A Misc/NEWS.d/next/Library/2025-03-13-19-54-57.gh-issue-126895.eJP9l0.rst
M Lib/test/test_readline.py
M Modules/clinic/readline.c.h
M Modules/readline.c
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 8b8772c66ee654..b9d082b3597f13 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -6,7 +6,10 @@
import sys
import tempfile
import textwrap
+import threading
import unittest
+from test import support
+from test.support import threading_helper
from test.support import verbose
from test.support.import_helper import import_module
from test.support.os_helper import unlink, temp_dir, TESTFN
@@ -403,5 +406,26 @@ def test_write_read_limited_history(self):
# See TestHistoryManipulation for the full test.
[email protected](support.Py_GIL_DISABLED, 'these tests can only possibly
fail with GIL disabled')
+class FreeThreadingTest(unittest.TestCase):
+ @threading_helper.reap_threads
+ @threading_helper.requires_working_threading()
+ def test_free_threading(self):
+ def completer_delims(b):
+ b.wait()
+ for _ in range(100):
+ readline.get_completer_delims()
+ readline.set_completer_delims('
\t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
+ readline.set_completer_delims('
\t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
+ readline.get_completer_delims()
+
+ count = 40
+ barrier = threading.Barrier(count)
+ threads = [threading.Thread(target=completer_delims, args=(barrier,))
for _ in range(count)]
+
+ with threading_helper.start_threads(threads):
+ pass
+
+
if __name__ == "__main__":
unittest.main()
diff --git
a/Misc/NEWS.d/next/Library/2025-03-13-19-54-57.gh-issue-126895.eJP9l0.rst
b/Misc/NEWS.d/next/Library/2025-03-13-19-54-57.gh-issue-126895.eJP9l0.rst
new file mode 100644
index 00000000000000..ea5b54ae89b7c2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-03-13-19-54-57.gh-issue-126895.eJP9l0.rst
@@ -0,0 +1 @@
+Fix :mod:`readline` in :term:`free-threaded <free threading>` build.
diff --git a/Modules/clinic/readline.c.h b/Modules/clinic/readline.c.h
index 1c616d60f097cc..696475f7d00f5b 100644
--- a/Modules/clinic/readline.c.h
+++ b/Modules/clinic/readline.c.h
@@ -2,6 +2,7 @@
preserve
[clinic start generated code]*/
+#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(readline_parse_and_bind__doc__,
@@ -13,6 +14,21 @@ PyDoc_STRVAR(readline_parse_and_bind__doc__,
#define READLINE_PARSE_AND_BIND_METHODDEF \
{"parse_and_bind", (PyCFunction)readline_parse_and_bind, METH_O,
readline_parse_and_bind__doc__},
+static PyObject *
+readline_parse_and_bind_impl(PyObject *module, PyObject *string);
+
+static PyObject *
+readline_parse_and_bind(PyObject *module, PyObject *string)
+{
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_parse_and_bind_impl(module, string);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
+}
+
PyDoc_STRVAR(readline_read_init_file__doc__,
"read_init_file($module, filename=None, /)\n"
"--\n"
@@ -41,7 +57,9 @@ readline_read_init_file(PyObject *module, PyObject *const
*args, Py_ssize_t narg
}
filename_obj = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_read_init_file_impl(module, filename_obj);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -75,7 +93,9 @@ readline_read_history_file(PyObject *module, PyObject *const
*args, Py_ssize_t n
}
filename_obj = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_read_history_file_impl(module, filename_obj);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -109,7 +129,9 @@ readline_write_history_file(PyObject *module, PyObject
*const *args, Py_ssize_t
}
filename_obj = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_write_history_file_impl(module, filename_obj);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -151,7 +173,9 @@ readline_append_history_file(PyObject *module, PyObject
*const *args, Py_ssize_t
}
filename_obj = args[1];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_append_history_file_impl(module, nelements,
filename_obj);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -238,7 +262,9 @@ readline_set_completion_display_matches_hook(PyObject
*module, PyObject *const *
}
function = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_set_completion_display_matches_hook_impl(module,
function);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -273,7 +299,9 @@ readline_set_startup_hook(PyObject *module, PyObject *const
*args, Py_ssize_t na
}
function = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_set_startup_hook_impl(module, function);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -311,7 +339,9 @@ readline_set_pre_input_hook(PyObject *module, PyObject
*const *args, Py_ssize_t
}
function = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_set_pre_input_hook_impl(module, function);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -382,6 +412,21 @@ PyDoc_STRVAR(readline_set_completer_delims__doc__,
#define READLINE_SET_COMPLETER_DELIMS_METHODDEF \
{"set_completer_delims", (PyCFunction)readline_set_completer_delims,
METH_O, readline_set_completer_delims__doc__},
+static PyObject *
+readline_set_completer_delims_impl(PyObject *module, PyObject *string);
+
+static PyObject *
+readline_set_completer_delims(PyObject *module, PyObject *string)
+{
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_set_completer_delims_impl(module, string);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
+}
+
PyDoc_STRVAR(readline_remove_history_item__doc__,
"remove_history_item($module, pos, /)\n"
"--\n"
@@ -404,7 +449,9 @@ readline_remove_history_item(PyObject *module, PyObject
*arg)
if (entry_number == -1 && PyErr_Occurred()) {
goto exit;
}
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_remove_history_item_impl(module, entry_number);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -444,7 +491,9 @@ readline_replace_history_item(PyObject *module, PyObject
*const *args, Py_ssize_
goto exit;
}
line = args[1];
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_replace_history_item_impl(module, entry_number,
line);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -459,6 +508,21 @@ PyDoc_STRVAR(readline_add_history__doc__,
#define READLINE_ADD_HISTORY_METHODDEF \
{"add_history", (PyCFunction)readline_add_history, METH_O,
readline_add_history__doc__},
+static PyObject *
+readline_add_history_impl(PyObject *module, PyObject *string);
+
+static PyObject *
+readline_add_history(PyObject *module, PyObject *string)
+{
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_add_history_impl(module, string);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
+}
+
PyDoc_STRVAR(readline_set_auto_history__doc__,
"set_auto_history($module, enabled, /)\n"
"--\n"
@@ -503,7 +567,13 @@ readline_get_completer_delims_impl(PyObject *module);
static PyObject *
readline_get_completer_delims(PyObject *module, PyObject *Py_UNUSED(ignored))
{
- return readline_get_completer_delims_impl(module);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_get_completer_delims_impl(module);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(readline_set_completer__doc__,
@@ -536,7 +606,9 @@ readline_set_completer(PyObject *module, PyObject *const
*args, Py_ssize_t nargs
}
function = args[0];
skip_optional:
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_set_completer_impl(module, function);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -582,7 +654,9 @@ readline_get_history_item(PyObject *module, PyObject *arg)
if (idx == -1 && PyErr_Occurred()) {
goto exit;
}
+ Py_BEGIN_CRITICAL_SECTION(module);
return_value = readline_get_history_item_impl(module, idx);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -603,7 +677,13 @@ readline_get_current_history_length_impl(PyObject *module);
static PyObject *
readline_get_current_history_length(PyObject *module, PyObject
*Py_UNUSED(ignored))
{
- return readline_get_current_history_length_impl(module);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_get_current_history_length_impl(module);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
PyDoc_STRVAR(readline_get_line_buffer__doc__,
@@ -621,7 +701,13 @@ readline_get_line_buffer_impl(PyObject *module);
static PyObject *
readline_get_line_buffer(PyObject *module, PyObject *Py_UNUSED(ignored))
{
- return readline_get_line_buffer_impl(module);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_get_line_buffer_impl(module);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
#if defined(HAVE_RL_COMPLETION_APPEND_CHARACTER)
@@ -641,7 +727,13 @@ readline_clear_history_impl(PyObject *module);
static PyObject *
readline_clear_history(PyObject *module, PyObject *Py_UNUSED(ignored))
{
- return readline_clear_history_impl(module);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_clear_history_impl(module);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
#endif /* defined(HAVE_RL_COMPLETION_APPEND_CHARACTER) */
@@ -655,6 +747,21 @@ PyDoc_STRVAR(readline_insert_text__doc__,
#define READLINE_INSERT_TEXT_METHODDEF \
{"insert_text", (PyCFunction)readline_insert_text, METH_O,
readline_insert_text__doc__},
+static PyObject *
+readline_insert_text_impl(PyObject *module, PyObject *string);
+
+static PyObject *
+readline_insert_text(PyObject *module, PyObject *string)
+{
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_insert_text_impl(module, string);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
+}
+
PyDoc_STRVAR(readline_redisplay__doc__,
"redisplay($module, /)\n"
"--\n"
@@ -670,7 +777,13 @@ readline_redisplay_impl(PyObject *module);
static PyObject *
readline_redisplay(PyObject *module, PyObject *Py_UNUSED(ignored))
{
- return readline_redisplay_impl(module);
+ PyObject *return_value = NULL;
+
+ Py_BEGIN_CRITICAL_SECTION(module);
+ return_value = readline_redisplay_impl(module);
+ Py_END_CRITICAL_SECTION();
+
+ return return_value;
}
#ifndef READLINE_APPEND_HISTORY_FILE_METHODDEF
@@ -684,4 +797,4 @@ readline_redisplay(PyObject *module, PyObject
*Py_UNUSED(ignored))
#ifndef READLINE_CLEAR_HISTORY_METHODDEF
#define READLINE_CLEAR_HISTORY_METHODDEF
#endif /* !defined(READLINE_CLEAR_HISTORY_METHODDEF) */
-/*[clinic end generated code: output=358ab465285c7949 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=88d9812b6caa2102 input=a9049054013a1b77]*/
diff --git a/Modules/readline.c b/Modules/readline.c
index 7d1f703f7dbdde..dd75f256d92142 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -10,6 +10,7 @@
/* Standard definitions */
#include "Python.h"
+#include "pycore_pyatomic_ft_wrappers.h"
#include "pycore_pylifecycle.h" // _Py_SetLocaleFromEnv()
#include <errno.h> // errno
@@ -199,6 +200,7 @@ disable_bracketed_paste(void)
/* Exported function to send one line to readline's init file parser */
/*[clinic input]
+@critical_section
readline.parse_and_bind
string: object
@@ -208,8 +210,8 @@ Execute the init line provided in the string argument.
[clinic start generated code]*/
static PyObject *
-readline_parse_and_bind(PyObject *module, PyObject *string)
-/*[clinic end generated code: output=1a1ede8afb9546c1 input=8a28a00bb4d61eec]*/
+readline_parse_and_bind_impl(PyObject *module, PyObject *string)
+/*[clinic end generated code: output=828d9b6630d434f5 input=cefdc0f9f62f9fcc]*/
{
char *copy;
PyObject *encoded = encode(string);
@@ -233,6 +235,7 @@ readline_parse_and_bind(PyObject *module, PyObject *string)
/* Exported function to parse a readline init file */
/*[clinic input]
+@critical_section
readline.read_init_file
filename as filename_obj: object = None
@@ -245,7 +248,7 @@ The default filename is the last filename used.
static PyObject *
readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
-/*[clinic end generated code: output=8e059b676142831e input=4c80c473e448139d]*/
+/*[clinic end generated code: output=8e059b676142831e input=62b767adfab6cc15]*/
{
PyObject *filename_bytes;
if (filename_obj != Py_None) {
@@ -264,6 +267,7 @@ readline_read_init_file_impl(PyObject *module, PyObject
*filename_obj)
/* Exported function to load a readline history file */
/*[clinic input]
+@critical_section
readline.read_history_file
filename as filename_obj: object = None
@@ -276,7 +280,7 @@ The default filename is ~/.history.
static PyObject *
readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
-/*[clinic end generated code: output=66a951836fb54fbb input=3d29d755b7e6932e]*/
+/*[clinic end generated code: output=66a951836fb54fbb input=5d86fd7813172a67]*/
{
PyObject *filename_bytes;
if (filename_obj != Py_None) {
@@ -296,6 +300,7 @@ static int _history_length = -1; /* do not truncate history
by default */
/* Exported function to save a readline history file */
/*[clinic input]
+@critical_section
readline.write_history_file
filename as filename_obj: object = None
@@ -308,7 +313,7 @@ The default filename is ~/.history.
static PyObject *
readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
-/*[clinic end generated code: output=fbcad13d8ef59ae6 input=28a8e062fe363703]*/
+/*[clinic end generated code: output=fbcad13d8ef59ae6 input=34aaada95120cfaa]*/
{
PyObject *filename_bytes;
const char *filename;
@@ -322,8 +327,9 @@ readline_write_history_file_impl(PyObject *module, PyObject
*filename_obj)
filename = NULL;
}
errno = err = write_history(filename);
- if (!err && _history_length >= 0)
- history_truncate_file(filename, _history_length);
+ int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
+ if (!err && history_length >= 0)
+ history_truncate_file(filename, history_length);
Py_XDECREF(filename_bytes);
errno = err;
if (errno)
@@ -335,6 +341,7 @@ readline_write_history_file_impl(PyObject *module, PyObject
*filename_obj)
/* Exported function to save part of a readline history file */
/*[clinic input]
+@critical_section
readline.append_history_file
nelements: int
@@ -349,7 +356,7 @@ The default filename is ~/.history.
static PyObject *
readline_append_history_file_impl(PyObject *module, int nelements,
PyObject *filename_obj)
-/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
+/*[clinic end generated code: output=5df06fc9da56e4e4 input=78a6061a8d3a0275]*/
{
if (nelements < 0)
{
@@ -370,8 +377,9 @@ readline_append_history_file_impl(PyObject *module, int
nelements,
}
errno = err = append_history(
nelements - libedit_append_replace_history_offset, filename);
- if (!err && _history_length >= 0)
- history_truncate_file(filename, _history_length);
+ int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
+ if (!err && history_length >= 0)
+ history_truncate_file(filename, history_length);
Py_XDECREF(filename_bytes);
errno = err;
if (errno)
@@ -398,7 +406,7 @@ static PyObject *
readline_set_history_length_impl(PyObject *module, int length)
/*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
{
- _history_length = length;
+ FT_ATOMIC_STORE_INT_RELAXED(_history_length, length);
Py_RETURN_NONE;
}
@@ -414,7 +422,8 @@ static PyObject *
readline_get_history_length_impl(PyObject *module)
/*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
{
- return PyLong_FromLong(_history_length);
+ int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
+ return PyLong_FromLong(history_length);
}
/* Generic hook function setter */
@@ -438,6 +447,7 @@ set_hook(const char *funcname, PyObject **hook_var,
PyObject *function)
}
/*[clinic input]
+@critical_section
readline.set_completion_display_matches_hook
function: object = None
@@ -453,7 +463,7 @@ once each time matches need to be displayed.
static PyObject *
readline_set_completion_display_matches_hook_impl(PyObject *module,
PyObject *function)
-/*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
+/*[clinic end generated code: output=516e5cb8db75a328 input=ea4191e4a07d28d3]*/
{
readlinestate *state = get_readline_state(module);
PyObject *result = set_hook("completion_display_matches_hook",
@@ -475,6 +485,7 @@ readline_set_completion_display_matches_hook_impl(PyObject
*module,
}
/*[clinic input]
+@critical_section
readline.set_startup_hook
function: object = None
@@ -488,7 +499,7 @@ before readline prints the first prompt.
static PyObject *
readline_set_startup_hook_impl(PyObject *module, PyObject *function)
-/*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
+/*[clinic end generated code: output=02cd0e0c4fa082ad input=11fce34992f1125e]*/
{
readlinestate *state = get_readline_state(module);
return set_hook("startup_hook", &state->startup_hook,
@@ -500,6 +511,7 @@ readline_set_startup_hook_impl(PyObject *module, PyObject
*function)
/* Set pre-input hook */
/*[clinic input]
+@critical_section
readline.set_pre_input_hook
function: object = None
@@ -514,7 +526,7 @@ characters.
static PyObject *
readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
-/*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
+/*[clinic end generated code: output=fe1a96505096f464 input=96d3d5ff4a0c7c28]*/
{
readlinestate *state = get_readline_state(module);
return set_hook("pre_input_hook", &state->pre_input_hook,
@@ -573,6 +585,7 @@ readline_get_endidx_impl(PyObject *module)
/* Set the tab-completion word-delimiters that readline uses */
/*[clinic input]
+@critical_section
readline.set_completer_delims
string: object
@@ -582,8 +595,8 @@ Set the word delimiters for completion.
[clinic start generated code]*/
static PyObject *
-readline_set_completer_delims(PyObject *module, PyObject *string)
-/*[clinic end generated code: output=4305b266106c4f1f input=ae945337ebd01e20]*/
+readline_set_completer_delims_impl(PyObject *module, PyObject *string)
+/*[clinic end generated code: output=017e48e9704a2f64 input=6c87bb1cbed7fcf1]*/
{
char *break_chars;
PyObject *encoded = encode(string);
@@ -612,7 +625,7 @@ readline_set_completer_delims(PyObject *module, PyObject
*string)
return PyErr_NoMemory();
}
-/* _py_free_history_entry: Utility function to free a history entry. */
+/* _py_free_history_entry_lock_held: Utility function to free a history entry.
*/
#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
@@ -622,7 +635,7 @@ readline_set_completer_delims(PyObject *module, PyObject
*string)
takes care of the timestamp. */
static void
-_py_free_history_entry(HIST_ENTRY *entry)
+_py_free_history_entry_lock_held(HIST_ENTRY *entry)
{
histdata_t data = free_history_entry(entry);
free(data);
@@ -633,7 +646,7 @@ _py_free_history_entry(HIST_ENTRY *entry)
/* No free_history_entry function; free everything manually. */
static void
-_py_free_history_entry(HIST_ENTRY *entry)
+_py_free_history_entry_lock_held(HIST_ENTRY *entry)
{
if (entry->line)
free((void *)entry->line);
@@ -645,6 +658,7 @@ _py_free_history_entry(HIST_ENTRY *entry)
#endif
/*[clinic input]
+@critical_section
readline.remove_history_item
pos as entry_number: int
@@ -655,7 +669,7 @@ Remove history item given by its zero-based position.
static PyObject *
readline_remove_history_item_impl(PyObject *module, int entry_number)
-/*[clinic end generated code: output=ab114f029208c7e8 input=f248beb720ff1838]*/
+/*[clinic end generated code: output=ab114f029208c7e8 input=847d7cc7e7c25852]*/
{
HIST_ENTRY *entry;
@@ -672,11 +686,12 @@ readline_remove_history_item_impl(PyObject *module, int
entry_number)
return NULL;
}
/* free memory allocated for the history entry */
- _py_free_history_entry(entry);
+ _py_free_history_entry_lock_held(entry);
Py_RETURN_NONE;
}
/*[clinic input]
+@critical_section
readline.replace_history_item
pos as entry_number: int
@@ -691,7 +706,7 @@ pos is zero-based.
static PyObject *
readline_replace_history_item_impl(PyObject *module, int entry_number,
PyObject *line)
-/*[clinic end generated code: output=f8cec2770ca125eb input=368bb66fe5ee5222]*/
+/*[clinic end generated code: output=f8cec2770ca125eb input=b44c8dcdc2dd87fe]*/
{
PyObject *encoded;
HIST_ENTRY *old_entry;
@@ -716,13 +731,14 @@ readline_replace_history_item_impl(PyObject *module, int
entry_number,
return NULL;
}
/* free memory allocated for the old history entry */
- _py_free_history_entry(old_entry);
+ _py_free_history_entry_lock_held(old_entry);
Py_RETURN_NONE;
}
/* Add a line to the history buffer */
/*[clinic input]
+@critical_section
readline.add_history
string: object
@@ -732,8 +748,8 @@ Add an item to the history buffer.
[clinic start generated code]*/
static PyObject *
-readline_add_history(PyObject *module, PyObject *string)
-/*[clinic end generated code: output=b107b7e8106e803d input=e57c1cf6bc68d7e3]*/
+readline_add_history_impl(PyObject *module, PyObject *string)
+/*[clinic end generated code: output=89047062042ac344 input=faa7053b8612513b]*/
{
PyObject *encoded = encode(string);
if (encoded == NULL) {
@@ -770,6 +786,7 @@ readline_set_auto_history_impl(PyObject *module,
/* Get the tab-completion word-delimiters that readline uses */
/*[clinic input]
+@critical_section
readline.get_completer_delims
Get the word delimiters for completion.
@@ -777,7 +794,7 @@ Get the word delimiters for completion.
static PyObject *
readline_get_completer_delims_impl(PyObject *module)
-/*[clinic end generated code: output=6b060280fa68ef43 input=e36eb14fb8a1f08a]*/
+/*[clinic end generated code: output=6b060280fa68ef43 input=80583cdf8176bcdd]*/
{
return decode(rl_completer_word_break_characters);
}
@@ -785,6 +802,7 @@ readline_get_completer_delims_impl(PyObject *module)
/* Set the completer function */
/*[clinic input]
+@critical_section
readline.set_completer
function: object = None
@@ -799,7 +817,7 @@ It should return the next possible completion starting with
'text'.
static PyObject *
readline_set_completer_impl(PyObject *module, PyObject *function)
-/*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
+/*[clinic end generated code: output=171a2a60f81d3204 input=97f539d8d0bfcb95]*/
{
readlinestate *state = get_readline_state(module);
return set_hook("completer", &state->completer, function);
@@ -828,7 +846,7 @@ readline_get_completer_impl(PyObject *module)
* See issue #8065.*/
static int
-_py_get_history_length(void)
+_py_get_history_length_lock_held(void)
{
HISTORY_STATE *hist_st = history_get_history_state();
int length = hist_st->length;
@@ -844,6 +862,7 @@ _py_get_history_length(void)
/* Exported function to get any element of history */
/*[clinic input]
+@critical_section
readline.get_history_item
index as idx: int
@@ -854,7 +873,7 @@ Return the current contents of history item at one-based
index.
static PyObject *
readline_get_history_item_impl(PyObject *module, int idx)
-/*[clinic end generated code: output=83d3e53ea5f34b3d input=8adf5c80e6c7ff2b]*/
+/*[clinic end generated code: output=83d3e53ea5f34b3d input=2835b50c7bde705f]*/
{
HIST_ENTRY *hist_ent;
@@ -863,7 +882,7 @@ readline_get_history_item_impl(PyObject *module, int idx)
* use 0-based indexes, while readline and newer
* versions of libedit use 1-based indexes.
*/
- int length = _py_get_history_length();
+ int length = _py_get_history_length_lock_held();
idx = idx - 1 + libedit_history_start;
@@ -887,6 +906,7 @@ readline_get_history_item_impl(PyObject *module, int idx)
/* Exported function to get current length of history */
/*[clinic input]
+@critical_section
readline.get_current_history_length
Return the current (not the maximum) length of history.
@@ -894,14 +914,15 @@ Return the current (not the maximum) length of history.
static PyObject *
readline_get_current_history_length_impl(PyObject *module)
-/*[clinic end generated code: output=436b294f12ba1e3f input=9cb3f431a68d071f]*/
+/*[clinic end generated code: output=436b294f12ba1e3f input=22e9fd0abbc2fd8d]*/
{
- return PyLong_FromLong((long)_py_get_history_length());
+ return PyLong_FromLong((long)_py_get_history_length_lock_held());
}
/* Exported function to read the current line buffer */
/*[clinic input]
+@critical_section
readline.get_line_buffer
Return the current contents of the line buffer.
@@ -909,7 +930,7 @@ Return the current contents of the line buffer.
static PyObject *
readline_get_line_buffer_impl(PyObject *module)
-/*[clinic end generated code: output=d22f9025ecad80e4 input=5f5fbc0d12c69412]*/
+/*[clinic end generated code: output=d22f9025ecad80e4 input=8e02e0fe081feece]*/
{
return decode(rl_line_buffer);
}
@@ -919,6 +940,7 @@ readline_get_line_buffer_impl(PyObject *module)
/* Exported function to clear the current history */
/*[clinic input]
+@critical_section
readline.clear_history
Clear the current readline history.
@@ -926,7 +948,7 @@ Clear the current readline history.
static PyObject *
readline_clear_history_impl(PyObject *module)
-/*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=208962c4393f5d16]*/
+/*[clinic end generated code: output=1f2dbb0dfa5d5ebb input=b2c6b11551593053]*/
{
clear_history();
Py_RETURN_NONE;
@@ -937,6 +959,7 @@ readline_clear_history_impl(PyObject *module)
/* Exported function to insert text into the line buffer */
/*[clinic input]
+@critical_section
readline.insert_text
string: object
@@ -946,8 +969,8 @@ Insert text into the line buffer at the cursor position.
[clinic start generated code]*/
static PyObject *
-readline_insert_text(PyObject *module, PyObject *string)
-/*[clinic end generated code: output=23d792821d320c19 input=bc96c3c848d5ccb5]*/
+readline_insert_text_impl(PyObject *module, PyObject *string)
+/*[clinic end generated code: output=4bf4e176f68750e0 input=2f401f4316df33c2]*/
{
PyObject *encoded = encode(string);
if (encoded == NULL) {
@@ -961,6 +984,7 @@ readline_insert_text(PyObject *module, PyObject *string)
/* Redisplay the line buffer */
/*[clinic input]
+@critical_section
readline.redisplay
Change what's displayed on the screen to reflect contents of the line buffer.
@@ -968,7 +992,7 @@ Change what's displayed on the screen to reflect contents
of the line buffer.
static PyObject *
readline_redisplay_impl(PyObject *module)
-/*[clinic end generated code: output=a8b9725827c3c34b input=b485151058d75edc]*/
+/*[clinic end generated code: output=a8b9725827c3c34b input=5895fd014615ff58]*/
{
rl_redisplay();
Py_RETURN_NONE;
@@ -1286,7 +1310,7 @@ setup_readline(readlinestate *mod_state)
{
add_history("2");
HIST_ENTRY *old_entry = replace_history_entry(1, "X", NULL);
- _py_free_history_entry(old_entry);
+ _py_free_history_entry_lock_held(old_entry);
HIST_ENTRY *item = history_get(libedit_history_start);
if (item && item->line && strcmp(item->line, "X")) {
libedit_append_replace_history_offset = 0;
@@ -1487,7 +1511,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, const
char *prompt)
n = strlen(p);
if (should_auto_add_history && n > 0) {
const char *line;
- int length = _py_get_history_length();
+ int length = _py_get_history_length_lock_held();
if (length > 0) {
HIST_ENTRY *hist_ent;
if (using_libedit_emulation) {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]