mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	gh-118894: Make asyncio REPL use pyrepl (GH-119433)
This commit is contained in:
		
							parent
							
								
									f9d47fed9f
								
							
						
					
					
						commit
						2237946af0
					
				
					 7 changed files with 143 additions and 65 deletions
				
			
		|  | @ -19,10 +19,14 @@ | |||
| 
 | ||||
| from __future__ import annotations | ||||
| 
 | ||||
| import sys | ||||
| import _colorize  # type: ignore[import-not-found] | ||||
| 
 | ||||
| from abc import ABC, abstractmethod | ||||
| import ast | ||||
| import code | ||||
| from dataclasses import dataclass, field | ||||
| import os.path | ||||
| import sys | ||||
| 
 | ||||
| 
 | ||||
| TYPE_CHECKING = False | ||||
|  | @ -136,3 +140,54 @@ def wait(self) -> None: | |||
| 
 | ||||
|     @abstractmethod | ||||
|     def repaint(self) -> None: ... | ||||
| 
 | ||||
| 
 | ||||
| class InteractiveColoredConsole(code.InteractiveConsole): | ||||
|     def __init__( | ||||
|         self, | ||||
|         locals: dict[str, object] | None = None, | ||||
|         filename: str = "<console>", | ||||
|         *, | ||||
|         local_exit: bool = False, | ||||
|     ) -> None: | ||||
|         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 (SyntaxError, OverflowError, 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 = self.compile.compiler(item, filename, the_symbol, dont_inherit=True) | ||||
|             except SyntaxError as e: | ||||
|                 if e.args[0] == "'await' outside function": | ||||
|                     python = os.path.basename(sys.executable) | ||||
|                     e.add_note( | ||||
|                         f"Try the asyncio REPL ({python} -m asyncio) to use" | ||||
|                         f" top-level 'await' and run background asyncio tasks." | ||||
|                     ) | ||||
|                 self.showsyntaxerror(filename) | ||||
|                 return False | ||||
|             except (OverflowError, ValueError): | ||||
|                 self.showsyntaxerror(filename) | ||||
|                 return False | ||||
| 
 | ||||
|             if code is None: | ||||
|                 return True | ||||
| 
 | ||||
|             self.runcode(code) | ||||
|         return False | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Łukasz Langa
						Łukasz Langa