# Buggy code
def get_selection_as_executable_code(self):
    """Return selected text as a processed text,
    to be executable in a Python/IPython interpreter"""
    ls = self.get_line_separator()

    _indent = lambda line: len(line) - len(line.lstrip())

    line_from, line_to = self.get_selection_bounds()
    text = self.get_selected_text()
    if not text:
        return

    lines = text.split(ls)
    if len(lines) > 1:
        # Multiline selection -> eventually fixing indentation
        original_indent = _indent(self.get_text_line(line_from))
        text = (" " * (original_indent - _indent(lines[0]))) + text

    # If there is a common indent to all lines, find it.
    # Moving from bottom line to top line ensures that blank
    # lines inherit the indent of the line *below* it,
    # which is the desired behavior.
    min_indent = 999
    current_indent = 0
    lines = text.split(ls)
    for i in range(len(lines) - 1, -1, -1):
        line = lines[i]
        if line.strip():
            current_indent = _indent(line)
            min_indent = min(current_indent, min_indent)
        else:
            lines[i] = " " * current_indent
    if min_indent:
        lines = [line[min_indent:] for line in lines]

    # Remove any leading whitespace or comment lines
    # since they confuse the reserved word detector that follows below
    lines_removed = 0
    while lines:
        first_line = lines[0].lstrip()
        if first_line == "" or first_line[0] == "#":
            lines_removed += 1
            lines.pop(0)
        else:
            break

    # Add an EOL character after indentation blocks that start with some
    # Python reserved words, so that it gets evaluated automatically
    # by the console
    varname = re.compile(r"[a-zA-Z0-9_]*")  # Matches valid variable names.
    maybe = False
    nextexcept = ()
    for n, line in enumerate(lines):
        if not _indent(line):
            word = varname.match(line).group()
            if maybe and word not in nextexcept:
                lines[n - 1] += ls
                maybe = False
            if word:
                if word in ("def", "for", "while", "with", "class"):
                    maybe = True
                    nextexcept = ()
                elif word == "if":
                    maybe = True
                    nextexcept = ("elif", "else")
                elif word == "try":
                    maybe = True
                    nextexcept = ("except", "finally")
    if maybe:
        if lines[-1].strip() == "":
            lines[-1] += ls
        else:
            lines.append(ls)

    # Add removed lines back to have correct traceback line numbers
    leading_lines_str = ls * lines_removed

    return leading_lines_str + ls.join(lines)


# Traceback (SyntaxError)
runcell('','/home/bcolsen/Documents/projects/bcp/cong_ebeam/untitled1.py')
File "<ipython-input-138-196bcb471293>", line 28
"hello""""

^
SyntaxError: EOL while scanning string literal

--Run Cell Error--
Please use only through Spyder's Editor; shouldn't be called manually from the console

# Fixed code
def get_selection_as_executable_code(self):
    """Return selected text as a processed text,
    to be executable in a Python/IPython interpreter"""
    ls = self.get_line_separator()

    _indent = lambda line: len(line) - len(line.lstrip())

    line_from, line_to = self.get_selection_bounds()
    text = self.get_selected_text()
    if not text:
        return

    lines = text.split(ls)
    if len(lines) > 1:
        # Multiline selection -> eventually fixing indentation
        original_indent = _indent(self.get_text_line(line_from))
        text = (" " * (original_indent - _indent(lines[0]))) + text

    # If there is a common indent to all lines, find it.
    # Moving from bottom line to top line ensures that blank
    # lines inherit the indent of the line *below* it,
    # which is the desired behavior.
    min_indent = 999
    current_indent = 0
    lines = text.split(ls)
    for i in range(len(lines) - 1, -1, -1):
        line = lines[i]
        if line.strip():
            current_indent = _indent(line)
            min_indent = min(current_indent, min_indent)
        else:
            lines[i] = " " * current_indent
    if min_indent:
        lines = [line[min_indent:] for line in lines]

    # Remove any leading whitespace or comment lines
    # since they confuse the reserved word detector that follows below
    lines_removed = 0
    while lines:
        first_line = lines[0].lstrip()
        if first_line == "" or first_line[0] == "#":
            lines_removed += 1
            lines.pop(0)
        else:
            break

    # Add an EOL character after the last line of code so that it gets
    # evaluated automatically by the console and any quote characters
    # are separated from the triple quotes of runcell
    lines.append(ls)

    # Add removed lines back to have correct traceback line numbers
    leading_lines_str = ls * lines_removed

    return leading_lines_str + ls.join(lines)