Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def open_in_external_editor(self, filename):
encoding = getpreferredencoding()
editor_args = shlex.split(
prepare_for_exec(self.config.editor, encoding)
)
args = editor_args + [prepare_for_exec(filename, encoding)]
return subprocess.call(args) == 0
# current prompt run will end up appended to the edit widget
# sitting above this prompt:
self.current_output = None
# XXX is this the right place?
self.rl_history.reset()
# XXX what is s_hist?
# We need the caption to use unicode as urwid normalizes later
# input to be the same type, using ascii as encoding. If the
# caption is bytes this breaks typing non-ascii into bpython.
if not more:
caption = ("prompt", self.ps1)
if py3:
self.stdout_hist += self.ps1
else:
self.stdout_hist += self.ps1.encode(getpreferredencoding())
else:
caption = ("prompt_more", self.ps2)
if py3:
self.stdout_hist += self.ps2
else:
self.stdout_hist += self.ps2.encode(getpreferredencoding())
self.edit = BPythonEdit(self.config, caption=caption)
urwid.connect_signal(self.edit, "change", self.on_input_change)
urwid.connect_signal(
self.edit, "edit-pos-changed", self.on_edit_pos_changed
)
# Do this after connecting the change signal handler:
self.edit.insert_text(4 * self.next_indentation() * " ")
self.edits.append(self.edit)
self.listbox.body.append(self.edit)
def write(self, s):
"""For overriding stdout defaults"""
if "\x04" in s:
for block in s.split("\x04"):
self.write(block)
return
if s.rstrip() and "\x03" in s:
t = s.split("\x03")[1]
else:
t = s
if not py3 and isinstance(t, unicode):
t = t.encode(getpreferredencoding())
if not self.stdout_hist:
self.stdout_hist = t
else:
self.stdout_hist += t
self.echo(s)
self.s_hist.append(s.rstrip())
def pager(self, text):
"""Runs an external pager on text
text must be a unicode"""
command = get_pager_command()
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(text.encode(getpreferredencoding()))
tmp.flush()
self.focus_on_subprocess(command + [tmp.name])
value that will be set after a prompt or message. c is the optional
curses colour pair to use (if not specified the last specified colour
pair will be used). p is True if the cursor is expected to stay in the
status window (e.g. when prompting)."""
self.win.erase()
if len(s) >= self.w:
s = s[: self.w - 1]
self.s = s
if c:
self.c = c
if s:
if not py3 and isinstance(s, unicode):
s = s.encode(getpreferredencoding())
if self.c:
self.win.addstr(s, self.c)
else:
self.win.addstr(s)
if not p:
self.win.noutrefresh()
self.pwin.refresh()
else:
self.win.refresh()
# Include the \n in the buffer - raw_input() seems to deal with trailing
# linebreaks and will break if it gets an empty string.
buffer += key
finally:
curses.raw(False)
if size > 0:
rest = buffer[size:]
if rest:
self.buffer.append(rest)
buffer = buffer[:size]
if py3:
return buffer
else:
return buffer.encode(getpreferredencoding())
self.docstring, max_w - 2, max_h - height_offset
)
docstring_string = "".join(docstring)
rows += len(docstring)
self.list_win.resize(rows, max_w)
if down:
self.list_win.mvwin(y + 1, 0)
else:
self.list_win.mvwin(y - rows - 2, 0)
if v_items:
self.list_win.addstr("\n ")
if not py3:
encoding = getpreferredencoding()
for ix, i in enumerate(v_items):
padding = (wl - len(i)) * " "
if i == current_item:
color = get_colpair(self.config, "operator")
else:
color = get_colpair(self.config, "main")
if not py3:
i = i.encode(encoding)
self.list_win.addstr(i + padding, color)
if (cols == 1 or (ix and not (ix + 1) % cols)) and ix + 1 < len(
v_items
):
self.list_win.addstr("\n ")
if self.docstring is not None:
if not py3 and isinstance(docstring_string, unicode):
def echo(self, s, redraw=True):
"""Parse and echo a formatted string with appropriate attributes. It
uses the formatting method as defined in formatter.py to parse the
srings. It won't update the screen if it's reevaluating the code (as it
does with undo)."""
if not py3 and isinstance(s, unicode):
s = s.encode(getpreferredencoding())
a = get_colpair(self.config, "output")
if "\x01" in s:
rx = re.search("\x01([A-Za-z])([A-Za-z]?)", s)
if rx:
fg = rx.groups()[0]
bg = rx.groups()[1]
col_num = self._C[fg.lower()]
if bg and bg != "I":
col_num *= self._C[bg.lower()]
a = curses.color_pair(int(col_num) + 1)
if bg == "I":
a = a | curses.A_REVERSE
s = re.sub("\x01[A-Za-z][A-Za-z]?", "", s)
if fg.isupper():