File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -316,6 +316,7 @@ readline_read_history_file_impl(PyObject *module, PyObject *filename_obj)
316316}
317317
318318static  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 )
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments