Skip to content

Commit 7124d20

Browse files
committed
requested changes
1 parent d4ea7c2 commit 7124d20

File tree

2 files changed

+31
-48
lines changed

2 files changed

+31
-48
lines changed

Lib/test/test_readline.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,6 @@ def test_write_read_limited_history(self):
408408

409409
@unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled')
410410
class FreeThreadingTest(unittest.TestCase):
411-
def check(self, funcs, *args):
412-
barrier = threading.Barrier(len(funcs))
413-
threads = []
414-
415-
for func in funcs:
416-
thread = threading.Thread(target=func, args=(barrier, *args))
417-
418-
threads.append(thread)
419-
420-
with threading_helper.start_threads(threads):
421-
pass
422-
423411
@threading_helper.reap_threads
424412
@threading_helper.requires_working_threading()
425413
def test_free_threading(self):
@@ -431,23 +419,12 @@ def completer_delims(b):
431419
readline.set_completer_delims(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
432420
readline.get_completer_delims()
433421

434-
self.check([completer_delims] * 100)
422+
count = 40
423+
barrier = threading.Barrier(count)
424+
threads = [threading.Thread(target=completer_delims, args=(barrier,)) for _ in range(count)]
435425

436-
def test_free_threading_doctest_difflib(self):
437-
code = textwrap.dedent("""
438-
from threading import Thread
439-
import doctest, difflib
440-
441-
def _test():
442-
try:
443-
doctest.testmod(difflib)
444-
except RecursionError:
445-
pass
446-
447-
for x in range(40):
448-
Thread(target=_test, args=()).start()
449-
""")
450-
assert_python_ok("-c", code)
426+
with threading_helper.start_threads(threads):
427+
pass
451428

452429

453430
if __name__ == "__main__":

Modules/readline.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/* Standard definitions */
1212
#include "Python.h"
13+
#include "pycore_pyatomic_ft_wrappers.h"
1314
#include "pycore_pylifecycle.h" // _Py_SetLocaleFromEnv()
1415

1516
#include <errno.h> // errno
@@ -199,7 +200,7 @@ disable_bracketed_paste(void)
199200
/* Exported function to send one line to readline's init file parser */
200201

201202
/*[clinic input]
202-
@critical_section module
203+
@critical_section
203204
readline.parse_and_bind
204205
205206
string: object
@@ -234,7 +235,7 @@ readline_parse_and_bind_impl(PyObject *module, PyObject *string)
234235
/* Exported function to parse a readline init file */
235236

236237
/*[clinic input]
237-
@critical_section module
238+
@critical_section
238239
readline.read_init_file
239240
240241
filename as filename_obj: object = None
@@ -266,7 +267,7 @@ readline_read_init_file_impl(PyObject *module, PyObject *filename_obj)
266267
/* Exported function to load a readline history file */
267268

268269
/*[clinic input]
269-
@critical_section module
270+
@critical_section
270271
readline.read_history_file
271272
272273
filename as filename_obj: object = None
@@ -299,6 +300,7 @@ static int _history_length = -1; /* do not truncate history by default */
299300
/* Exported function to save a readline history file */
300301

301302
/*[clinic input]
303+
@critical_section
302304
readline.write_history_file
303305
304306
filename as filename_obj: object = None
@@ -325,8 +327,9 @@ readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
325327
filename = NULL;
326328
}
327329
errno = err = write_history(filename);
328-
if (!err && _history_length >= 0)
329-
history_truncate_file(filename, _history_length);
330+
int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
331+
if (!err && history_length >= 0)
332+
history_truncate_file(filename, history_length);
330333
Py_XDECREF(filename_bytes);
331334
errno = err;
332335
if (errno)
@@ -338,6 +341,7 @@ readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
338341
/* Exported function to save part of a readline history file */
339342

340343
/*[clinic input]
344+
@critical_section
341345
readline.append_history_file
342346
343347
nelements: int
@@ -373,8 +377,9 @@ readline_append_history_file_impl(PyObject *module, int nelements,
373377
}
374378
errno = err = append_history(
375379
nelements - libedit_append_replace_history_offset, filename);
376-
if (!err && _history_length >= 0)
377-
history_truncate_file(filename, _history_length);
380+
int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
381+
if (!err && history_length >= 0)
382+
history_truncate_file(filename, history_length);
378383
Py_XDECREF(filename_bytes);
379384
errno = err;
380385
if (errno)
@@ -401,7 +406,7 @@ static PyObject *
401406
readline_set_history_length_impl(PyObject *module, int length)
402407
/*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
403408
{
404-
_history_length = length;
409+
FT_ATOMIC_STORE_INT_RELAXED(_history_length, length);
405410
Py_RETURN_NONE;
406411
}
407412

@@ -417,7 +422,8 @@ static PyObject *
417422
readline_get_history_length_impl(PyObject *module)
418423
/*[clinic end generated code: output=83a2eeae35b6d2b9 input=5dce2eeba4327817]*/
419424
{
420-
return PyLong_FromLong(_history_length);
425+
int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
426+
return PyLong_FromLong(history_length);
421427
}
422428

423429
/* Generic hook function setter */
@@ -577,7 +583,7 @@ readline_get_endidx_impl(PyObject *module)
577583
/* Set the tab-completion word-delimiters that readline uses */
578584

579585
/*[clinic input]
580-
@critical_section module
586+
@critical_section
581587
readline.set_completer_delims
582588
583589
string: object
@@ -650,7 +656,7 @@ _py_free_history_entry_lock_held(HIST_ENTRY *entry)
650656
#endif
651657

652658
/*[clinic input]
653-
@critical_section module
659+
@critical_section
654660
readline.remove_history_item
655661
656662
pos as entry_number: int
@@ -683,7 +689,7 @@ readline_remove_history_item_impl(PyObject *module, int entry_number)
683689
}
684690

685691
/*[clinic input]
686-
@critical_section module
692+
@critical_section
687693
readline.replace_history_item
688694
689695
pos as entry_number: int
@@ -730,7 +736,7 @@ readline_replace_history_item_impl(PyObject *module, int entry_number,
730736
/* Add a line to the history buffer */
731737

732738
/*[clinic input]
733-
@critical_section module
739+
@critical_section
734740
readline.add_history
735741
736742
string: object
@@ -778,7 +784,7 @@ readline_set_auto_history_impl(PyObject *module,
778784
/* Get the tab-completion word-delimiters that readline uses */
779785

780786
/*[clinic input]
781-
@critical_section module
787+
@critical_section
782788
readline.get_completer_delims
783789
784790
Get the word delimiters for completion.
@@ -853,7 +859,7 @@ _py_get_history_length_lock_held(void)
853859
/* Exported function to get any element of history */
854860

855861
/*[clinic input]
856-
@critical_section module
862+
@critical_section
857863
readline.get_history_item
858864
859865
index as idx: int
@@ -897,7 +903,7 @@ readline_get_history_item_impl(PyObject *module, int idx)
897903
/* Exported function to get current length of history */
898904

899905
/*[clinic input]
900-
@critical_section module
906+
@critical_section
901907
readline.get_current_history_length
902908
903909
Return the current (not the maximum) length of history.
@@ -913,7 +919,7 @@ readline_get_current_history_length_impl(PyObject *module)
913919
/* Exported function to read the current line buffer */
914920

915921
/*[clinic input]
916-
@critical_section module
922+
@critical_section
917923
readline.get_line_buffer
918924
919925
Return the current contents of the line buffer.
@@ -931,7 +937,7 @@ readline_get_line_buffer_impl(PyObject *module)
931937
/* Exported function to clear the current history */
932938

933939
/*[clinic input]
934-
@critical_section module
940+
@critical_section
935941
readline.clear_history
936942
937943
Clear the current readline history.
@@ -950,7 +956,7 @@ readline_clear_history_impl(PyObject *module)
950956
/* Exported function to insert text into the line buffer */
951957

952958
/*[clinic input]
953-
@critical_section module
959+
@critical_section
954960
readline.insert_text
955961
956962
string: object
@@ -975,7 +981,7 @@ readline_insert_text_impl(PyObject *module, PyObject *string)
975981
/* Redisplay the line buffer */
976982

977983
/*[clinic input]
978-
@critical_section module
984+
@critical_section
979985
readline.redisplay
980986
981987
Change what's displayed on the screen to reflect contents of the line buffer.

0 commit comments

Comments
 (0)