mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	gh-118893: Evaluate all statements in the new REPL separately (#119318)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
		
							parent
							
								
									9fa206aaec
								
							
						
					
					
						commit
						a3e4fec873
					
				
					 5 changed files with 128 additions and 9 deletions
				
			
		|  | @ -30,6 +30,7 @@ | |||
| import linecache | ||||
| import sys | ||||
| import code | ||||
| import ast | ||||
| from types import ModuleType | ||||
| 
 | ||||
| from .readline import _get_reader, multiline_input | ||||
|  | @ -74,9 +75,36 @@ def __init__( | |||
|         super().__init__(locals=locals, filename=filename, local_exit=local_exit)  # type: ignore[call-arg] | ||||
|         self.can_colorize = _colorize.can_colorize() | ||||
| 
 | ||||
|     def showsyntaxerror(self, filename=None): | ||||
|         super().showsyntaxerror(colorize=self.can_colorize) | ||||
| 
 | ||||
|     def showtraceback(self): | ||||
|         super().showtraceback(colorize=self.can_colorize) | ||||
| 
 | ||||
|     def runsource(self, source, filename="<input>", symbol="single"): | ||||
|         try: | ||||
|             tree = ast.parse(source) | ||||
|         except (OverflowError, SyntaxError, ValueError): | ||||
|             self.showsyntaxerror(filename) | ||||
|             return False | ||||
|         if tree.body: | ||||
|             *_, last_stmt = tree.body | ||||
|         for stmt in tree.body: | ||||
|             wrapper = ast.Interactive if stmt is last_stmt else ast.Module | ||||
|             the_symbol = symbol if stmt is last_stmt else "exec" | ||||
|             item = wrapper([stmt]) | ||||
|             try: | ||||
|                 code = compile(item, filename, the_symbol) | ||||
|             except (OverflowError, ValueError): | ||||
|                     self.showsyntaxerror(filename) | ||||
|                     return False | ||||
| 
 | ||||
|             if code is None: | ||||
|                 return True | ||||
| 
 | ||||
|             self.runcode(code) | ||||
|         return False | ||||
| 
 | ||||
| 
 | ||||
| def run_multiline_interactive_console( | ||||
|     mainmodule: ModuleType | None= None, future_flags: int = 0 | ||||
|  | @ -144,10 +172,7 @@ def more_lines(unicodetext: str) -> bool: | |||
| 
 | ||||
|             input_name = f"<python-input-{input_n}>" | ||||
|             linecache._register_code(input_name, statement, "<stdin>")  # type: ignore[attr-defined] | ||||
|             symbol = "single" if not contains_pasted_code else "exec" | ||||
|             more = console.push(_strip_final_indent(statement), filename=input_name, _symbol=symbol)  # type: ignore[call-arg] | ||||
|             if contains_pasted_code and more: | ||||
|                 more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")  # type: ignore[call-arg] | ||||
|             more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")  # type: ignore[call-arg] | ||||
|             assert not more | ||||
|             input_n += 1 | ||||
|         except KeyboardInterrupt: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Pablo Galindo Salgado
						Pablo Galindo Salgado