File pass-test_write_read_limited_history.patch of Package python312

Index: Python-3.12.12/Modules/readline.c
===================================================================
--- Python-3.12.12.orig/Modules/readline.c	2025-11-19 20:17:29.982940492 +0100
+++ Python-3.12.12/Modules/readline.c	2025-11-19 20:36:46.627307300 +0100
@@ -145,6 +145,8 @@
 
 #define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
 
+static int _py_get_history_length(void);
+static void _py_free_history_entry(HIST_ENTRY *entry);
 
 /* Convert to/from multibyte C strings */
 
@@ -384,6 +386,27 @@
 /*[clinic end generated code: output=e161a53e45987dc7 input=b8901bf16488b760]*/
 {
     _history_length = length;
+
+    if (length < 0) {
+        stifle_history(-1);
+    }
+    else {
+        int current_length = _py_get_history_length();
+        if (length < current_length) {
+#if defined(RL_READLINE_VERSION) && RL_READLINE_VERSION >= 0x0500
+            HISTORY_STATE *state = history_get_history_state();
+            if (state) {
+                int i;
+                for (i = 0; i < current_length - length; i++) {
+                    _py_free_history_entry(remove_history(0));
+                }
+                state->length = length;
+                free(state);
+            }
+#endif
+        }
+        stifle_history(length);
+    }
     Py_RETURN_NONE;
 }
 
openSUSE Build Service is sponsored by