Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import sys 

2 

3 

4# ======================================================================================================================= 

5# BaseStdIn 

6# ======================================================================================================================= 

7class BaseStdIn: 

8 def __init__(self, original_stdin=sys.stdin, *args, **kwargs): 

9 try: 

10 self.encoding = sys.stdin.encoding 

11 except: 

12 # Not sure if it's available in all Python versions... 

13 pass 

14 self.original_stdin = original_stdin 

15 

16 def readline(self, *args, **kwargs): 

17 # sys.stderr.write('Cannot readline out of the console evaluation\n') -- don't show anything 

18 # This could happen if the user had done input('enter number).<-- upon entering this, that message would appear, 

19 # which is not something we want. 

20 return '\n' 

21 

22 def write(self, *args, **kwargs): 

23 pass # not available StdIn (but it can be expected to be in the stream interface) 

24 

25 def flush(self, *args, **kwargs): 

26 pass # not available StdIn (but it can be expected to be in the stream interface) 

27 

28 def read(self, *args, **kwargs): 

29 # in the interactive interpreter, a read and a readline are the same. 

30 return self.readline() 

31 

32 def close(self, *args, **kwargs): 

33 pass # expected in StdIn 

34 

35 def __iter__(self): 

36 # BaseStdIn would not be considered as Iterable in Python 3 without explicit `__iter__` implementation 

37 return self.original_stdin.__iter__() 

38 

39 def __getattr__(self, item): 

40 # it's called if the attribute wasn't found 

41 if hasattr(self.original_stdin, item): 

42 return getattr(self.original_stdin, item) 

43 raise AttributeError("%s has no attribute %s" % (self.original_stdin, item)) 

44 

45 

46# ======================================================================================================================= 

47# StdIn 

48# ======================================================================================================================= 

49class StdIn(BaseStdIn): 

50 ''' 

51 Object to be added to stdin (to emulate it as non-blocking while the next line arrives) 

52 ''' 

53 

54 def __init__(self, interpreter, rpc_client, original_stdin=sys.stdin): 

55 BaseStdIn.__init__(self, original_stdin) 

56 self.interpreter = interpreter 

57 self.rpc_client = rpc_client 

58 

59 def readline(self, *args, **kwargs): 

60 from pydev_console.pydev_protocol import KeyboardInterruptException 

61 

62 # Ok, callback into the client to get the new input 

63 try: 

64 requested_input = self.rpc_client.requestInput() 

65 if not requested_input: 

66 return '\n' # Yes, a readline must return something (otherwise we can get an EOFError on the input() call). 

67 return requested_input 

68 except KeyboardInterrupt: 

69 raise # Let KeyboardInterrupt go through -- #PyDev-816: Interrupting infinite loop in the Interactive Console 

70 except KeyboardInterruptException: 

71 # this exception is explicitly declared in `requestInput()` method of `PythonConsoleFrontendService` Thrift service 

72 # it is thrown on the IDE side and transferred by Thrift library as the response to `requestInput()` method 

73 raise 

74 except: 

75 return '\n' 

76 

77 def close(self, *args, **kwargs): 

78 pass # expected in StdIn 

79 

80#======================================================================================================================= 

81# DebugConsoleStdIn 

82#======================================================================================================================= 

83class DebugConsoleStdIn(BaseStdIn): 

84 ''' 

85 Object to be added to stdin (to emulate it as non-blocking while the next line arrives) 

86 ''' 

87 

88 def __init__(self, dbg, original_stdin): 

89 BaseStdIn.__init__(self, original_stdin) 

90 self.debugger = dbg 

91 

92 def __pydev_run_command(self, is_started): 

93 try: 

94 cmd = self.debugger.cmd_factory.make_input_requested_message(is_started) 

95 self.debugger.writer.add_command(cmd) 

96 except Exception: 

97 import traceback 

98 traceback.print_exc() 

99 return '\n' 

100 

101 def readline(self, *args, **kwargs): 

102 # Notify Java side about input and call original function 

103 self.__pydev_run_command(True) 

104 result = self.original_stdin.readline(*args, **kwargs) 

105 self.__pydev_run_command(False) 

106 return result