mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	gh-122273: Support PyREPL history on Windows (#127141)
Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									f46d847574
								
							
						
					
					
						commit
						3c7a90a831
					
				
					 3 changed files with 40 additions and 20 deletions
				
			
		|  | @ -450,7 +450,9 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None: | ||||||
|     def write_history_file(self, filename: str = gethistoryfile()) -> None: |     def write_history_file(self, filename: str = gethistoryfile()) -> None: | ||||||
|         maxlength = self.saved_history_length |         maxlength = self.saved_history_length | ||||||
|         history = self.get_reader().get_trimmed_history(maxlength) |         history = self.get_reader().get_trimmed_history(maxlength) | ||||||
|         with open(os.path.expanduser(filename), "w", encoding="utf-8") as f: |         f = open(os.path.expanduser(filename), "w", | ||||||
|  |                  encoding="utf-8", newline="\n") | ||||||
|  |         with f: | ||||||
|             for entry in history: |             for entry in history: | ||||||
|                 entry = entry.replace("\n", "\r\n")  # multiline history support |                 entry = entry.replace("\n", "\r\n")  # multiline history support | ||||||
|                 f.write(entry + "\n") |                 f.write(entry + "\n") | ||||||
|  |  | ||||||
							
								
								
									
										55
									
								
								Lib/site.py
									
										
									
									
									
								
							
							
						
						
									
										55
									
								
								Lib/site.py
									
										
									
									
									
								
							|  | @ -498,9 +498,18 @@ def register_readline(): | ||||||
|         PYTHON_BASIC_REPL = False |         PYTHON_BASIC_REPL = False | ||||||
| 
 | 
 | ||||||
|     import atexit |     import atexit | ||||||
|  | 
 | ||||||
|  |     try: | ||||||
|  |         try: | ||||||
|  |             import readline | ||||||
|  |         except ImportError: | ||||||
|  |             readline = None | ||||||
|  |         else: | ||||||
|  |             import rlcompleter  # noqa: F401 | ||||||
|  |     except ImportError: | ||||||
|  |         return | ||||||
|  | 
 | ||||||
|     try: |     try: | ||||||
|         import readline |  | ||||||
|         import rlcompleter  # noqa: F401 |  | ||||||
|         if PYTHON_BASIC_REPL: |         if PYTHON_BASIC_REPL: | ||||||
|             CAN_USE_PYREPL = False |             CAN_USE_PYREPL = False | ||||||
|         else: |         else: | ||||||
|  | @ -508,30 +517,36 @@ def register_readline(): | ||||||
|             sys.path = [p for p in original_path if p != ''] |             sys.path = [p for p in original_path if p != ''] | ||||||
|             try: |             try: | ||||||
|                 import _pyrepl.readline |                 import _pyrepl.readline | ||||||
|                 import _pyrepl.unix_console |                 if os.name == "nt": | ||||||
|  |                     import _pyrepl.windows_console | ||||||
|  |                     console_errors = (_pyrepl.windows_console._error,) | ||||||
|  |                 else: | ||||||
|  |                     import _pyrepl.unix_console | ||||||
|  |                     console_errors = _pyrepl.unix_console._error | ||||||
|                 from _pyrepl.main import CAN_USE_PYREPL |                 from _pyrepl.main import CAN_USE_PYREPL | ||||||
|             finally: |             finally: | ||||||
|                 sys.path = original_path |                 sys.path = original_path | ||||||
|     except ImportError: |     except ImportError: | ||||||
|         return |         return | ||||||
| 
 | 
 | ||||||
|     # Reading the initialization (config) file may not be enough to set a |     if readline is not None: | ||||||
|     # completion key, so we set one first and then read the file. |         # Reading the initialization (config) file may not be enough to set a | ||||||
|     if readline.backend == 'editline': |         # completion key, so we set one first and then read the file. | ||||||
|         readline.parse_and_bind('bind ^I rl_complete') |         if readline.backend == 'editline': | ||||||
|     else: |             readline.parse_and_bind('bind ^I rl_complete') | ||||||
|         readline.parse_and_bind('tab: complete') |         else: | ||||||
|  |             readline.parse_and_bind('tab: complete') | ||||||
| 
 | 
 | ||||||
|     try: |         try: | ||||||
|         readline.read_init_file() |             readline.read_init_file() | ||||||
|     except OSError: |         except OSError: | ||||||
|         # An OSError here could have many causes, but the most likely one |             # An OSError here could have many causes, but the most likely one | ||||||
|         # is that there's no .inputrc file (or .editrc file in the case of |             # is that there's no .inputrc file (or .editrc file in the case of | ||||||
|         # Mac OS X + libedit) in the expected location.  In that case, we |             # Mac OS X + libedit) in the expected location.  In that case, we | ||||||
|         # want to ignore the exception. |             # want to ignore the exception. | ||||||
|         pass |             pass | ||||||
| 
 | 
 | ||||||
|     if readline.get_current_history_length() == 0: |     if readline is None or readline.get_current_history_length() == 0: | ||||||
|         # If no history was loaded, default to .python_history, |         # If no history was loaded, default to .python_history, | ||||||
|         # or PYTHON_HISTORY. |         # or PYTHON_HISTORY. | ||||||
|         # The guard is necessary to avoid doubling history size at |         # The guard is necessary to avoid doubling history size at | ||||||
|  | @ -542,8 +557,10 @@ def register_readline(): | ||||||
| 
 | 
 | ||||||
|         if CAN_USE_PYREPL: |         if CAN_USE_PYREPL: | ||||||
|             readline_module = _pyrepl.readline |             readline_module = _pyrepl.readline | ||||||
|             exceptions = (OSError, *_pyrepl.unix_console._error) |             exceptions = (OSError, *console_errors) | ||||||
|         else: |         else: | ||||||
|  |             if readline is None: | ||||||
|  |                 return | ||||||
|             readline_module = readline |             readline_module = readline | ||||||
|             exceptions = OSError |             exceptions = OSError | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | Support PyREPL history on Windows. Patch by devdanzin and Victor Stinner. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner