MCPcopy Index your code
hub / github.com/python/cpython / push

Method push

Lib/_pyrepl/base_eventqueue.py:72–110  ·  view source on GitHub ↗

Processes a character by updating the buffer and handling special key mappings.

(self, char: int | bytes)

Source from the content-addressed store, hash-verified

70 self.events.append(event)
71
72 def push(self, char: int | bytes) -> None:
73 """
74 Processes a character by updating the buffer and handling special key mappings.
75 """
76 assert isinstance(char, (int, bytes))
77 ord_char = char if isinstance(char, int) else ord(char)
78 char = ord_char.to_bytes()
79 self.buf.append(ord_char)
80
81 if char in self.keymap:
82 if self.keymap is self.compiled_keymap:
83 # sanity check, buffer is empty when a special key comes
84 assert len(self.buf) == 1
85 k = self.keymap[char]
86 trace('found map {k!r}', k=k)
87 if isinstance(k, dict):
88 self.keymap = k
89 else:
90 self.insert(Event('key', k, bytes(self.flush_buf())))
91 self.keymap = self.compiled_keymap
92
93 elif self.buf and self.buf[0] == 27: # escape
94 # escape sequence not recognized by our keymap: propagate it
95 # outside so that i can be recognized as an M-... key (see also
96 # the docstring in keymap.py
97 trace('unrecognized escape sequence, propagating...')
98 self.keymap = self.compiled_keymap
99 self.insert(Event('key', '\033', b'\033'))
100 for _c in self.flush_buf()[1:]:
101 self.push(_c)
102
103 else:
104 try:
105 decoded = bytes(self.buf).decode(self.encoding)
106 except UnicodeError:
107 return
108 else:
109 self.insert(Event('key', decoded, bytes(self.flush_buf())))
110 self.keymap = self.compiled_keymap

Callers

nothing calls this directly

Calls 6

insertMethod · 0.95
flush_bufMethod · 0.95
traceFunction · 0.90
EventClass · 0.70
appendMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected