Skip to content

Commit 8939460

Browse files
committed
fix C readline
1 parent 21b3083 commit 8939460

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Modules/readline.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
316316
}
317317

318318
static int _history_length = -1; /* do not truncate history by default */
319+
static int _py_get_history_length_lock_held(void);
319320

320321
/* Exported function to save a readline history file */
321322

@@ -354,6 +355,18 @@ readline_write_history_file_impl(PyObject *module, PyObject *filename_obj)
354355
return NULL;
355356
}
356357
}
358+
359+
/*
360+
* If the current history is empty, we do not re-create an empty
361+
* file we want to preserve the first line marker '_HiStOrY_V2_'.
362+
*
363+
* See https://github.com/python/cpython/issues/139352.
364+
*/
365+
if (_py_get_history_length_lock_held() == 0) {
366+
Py_XDECREF(filename_bytes);
367+
Py_RETURN_NONE;
368+
}
369+
357370
errno = err = write_history(filename);
358371
int history_length = FT_ATOMIC_LOAD_INT_RELAXED(_history_length);
359372
if (!err && history_length >= 0)

0 commit comments

Comments
 (0)