Skip to content

Commit

Permalink
Merge from 3.x: PR #4691
Browse files Browse the repository at this point in the history
Fixes #4418
  • Loading branch information
ccordoba12 committed Jul 4, 2017
2 parents aa081f3 + 8dc9163 commit fe001d5
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 84 deletions.
7 changes: 4 additions & 3 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,8 @@ def set_splash(self, message):

def remove_tmpdir(self):
"""Remove Spyder temporary directory"""
shutil.rmtree(programs.TEMPDIR, ignore_errors=True)
if CONF.get('main', 'single_instance') and not self.new_instance:
shutil.rmtree(programs.TEMPDIR, ignore_errors=True)

def closeEvent(self, event):
"""closeEvent reimplementation"""
Expand Down Expand Up @@ -2429,8 +2430,8 @@ def open_external_console(self, fname, wdir, args, interact, debug, python,

def execute_in_external_console(self, lines, focus_to_editor):
"""
Execute lines in external or IPython console and eventually set focus
to the editor
Execute lines in IPython console and eventually set focus
to the Editor.
"""
console = self.ipyconsole
console.visibility_changed(True)
Expand Down
6 changes: 3 additions & 3 deletions spyder/plugins/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class Editor(SpyderPluginWidget):
DISABLE_ACTIONS_WHEN_HIDDEN = False # SpyderPluginWidget class attribute

# Signals
run_in_current_ipyclient = Signal(str, str, str, bool, bool, bool)
run_in_current_ipyclient = Signal(str, str, str, bool, bool, bool, bool)
exec_in_extconsole = Signal(str, bool)
redirect_stdio = Signal(bool)
open_dir = Signal(str)
Expand Down Expand Up @@ -2436,10 +2436,10 @@ def re_run_file(self):
(fname, wdir, args, interact, debug,
python, python_args, current, systerm,
post_mortem, clear_namespace) = self.__last_ec_exec
if current:
if not systerm:
self.run_in_current_ipyclient.emit(fname, wdir, args,
debug, post_mortem,
clear_namespace)
current, clear_namespace)
else:
self.main.open_external_console(fname, wdir, args, interact,
debug, python, python_args,
Expand Down
92 changes: 65 additions & 27 deletions spyder/plugins/ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ def __init__(self, parent, testing=False):
os.mkdir(programs.TEMPDIR)

layout = QVBoxLayout()
self.tabwidget = Tabs(self, self.menu_actions, rename_tabs=True)
self.tabwidget = Tabs(self, self.menu_actions, rename_tabs=True,
split_char='/', split_index=0)
if hasattr(self.tabwidget, 'setDocumentMode')\
and not sys.platform == 'darwin':
# Don't set document mode to true on OSX because it generates
Expand Down Expand Up @@ -795,8 +796,7 @@ def register_plugin(self):
self.editor.load(fname, lineno, word,
processevents=processevents))
self.editor.breakpoints_saved.connect(self.set_spyder_breakpoints)
self.editor.run_in_current_ipyclient.connect(
self.run_script_in_current_client)
self.editor.run_in_current_ipyclient.connect(self.run_script)
self.main.workingdirectory.set_current_console_wd.connect(
self.set_current_client_working_directory)

Expand Down Expand Up @@ -824,11 +824,20 @@ def get_current_shellwidget(self):
if client is not None:
return client.shellwidget

def run_script_in_current_client(self, filename, wdir, args, debug,
post_mortem, clear_variables):
"""Run script in current client, if any"""
def run_script(self, filename, wdir, args, debug, post_mortem,
current_client, clear_variables):
"""Run script in current or dedicated client"""
norm = lambda text: remove_backslashes(to_text_string(text))
client = self.get_current_client()

# Select client to execute code on it
if current_client:
client = self.get_current_client()
else:
client = self.get_client_for_file(filename)
if client is None:
self.create_client_for_file(filename)
client = self.get_current_client()

if client is not None:
# Internal kernels, use runfile
if client.get_kernel() is not None:
Expand All @@ -848,7 +857,7 @@ def run_script_in_current_client(self, filename, wdir, args, debug,
line += "\"%s\"" % to_text_string(filename)
if args:
line += " %s" % norm(args)
self.execute_code(line, clear_variables)
self.execute_code(line, current_client, clear_variables)
self.visibility_changed(True)
self.raise_()
else:
Expand All @@ -865,14 +874,21 @@ def set_current_client_working_directory(self, directory):
directory = encoding.to_unicode_from_fs(directory)
shellwidget.set_cwd(directory)

def execute_code(self, lines, clear_variables=False):
def execute_code(self, lines, current_client=True, clear_variables=False):
"""Execute code instructions."""
sw = self.get_current_shellwidget()
if sw is not None:
if sw._reading:
pass
else:
if clear_variables:
if not current_client:
# Clear console and reset namespace for
# dedicated clients
sw.silent_execute('%clear')
sw.silent_execute(
'get_ipython().kernel.close_all_mpl_figures()')
sw.reset_namespace(force=True)
elif current_client and clear_variables:
sw.reset_namespace(force=True)
sw.execute(to_text_string(to_text_string(lines)))
self.activateWindow()
Expand All @@ -885,9 +901,7 @@ def write_to_stdin(self, line):

@Slot()
@Slot(bool)
@Slot(str)
@Slot(bool, str)
def create_new_client(self, give_focus=True, path=''):
def create_new_client(self, give_focus=True):
"""Create a new client"""
self.master_clients += 1
client_id = dict(int_id=to_text_string(self.master_clients),
Expand Down Expand Up @@ -929,7 +943,7 @@ def create_new_client(self, give_focus=True, path=''):
"<tt>conda install ipykernel</tt>"))
return

self.connect_client_to_kernel(client, path)
self.connect_client_to_kernel(client)
if client.shellwidget.kernel_manager is None:
return
self.register_client(client)
Expand All @@ -945,7 +959,7 @@ def create_client_for_kernel(self):
self._create_client_for_kernel(connection_file, hostname, sshkey,
password)

def connect_client_to_kernel(self, client, path):
def connect_client_to_kernel(self, client):
"""Connect a client to its kernel"""
connection_file = client.connection_file
stderr_file = client.stderr_file
Expand All @@ -965,9 +979,6 @@ def connect_client_to_kernel(self, client, path):
shellwidget.kernel_manager = km
shellwidget.kernel_client = kc

if path:
shellwidget.set_cwd(path)

def set_editor(self):
"""Set the editor used by the %edit magic"""
python = sys.executable
Expand Down Expand Up @@ -1240,8 +1251,32 @@ def set_spyder_breakpoints(self):

@Slot(str)
def create_client_from_path(self, path):
"""Create a client with its cwd pointing to path"""
self.create_new_client(path=path)
"""Create a client with its cwd pointing to path."""
self.create_new_client()
sw = self.get_current_shellwidget()
sw.set_cwd(path)

def create_client_for_file(self, filename):
"""Create a client to execute code related to a file."""
# Create client
self.create_new_client()

# Don't increase the count of master clients
self.master_clients -= 1

# Rename client tab with filename
client = self.get_current_client()
client.allow_rename = False
self.rename_client_tab(client, filename)

def get_client_for_file(self, filename):
"""Get client associated with a given file."""
client = None
for cl in self.get_clients():
if cl.given_name == filename:
client = cl
break
return client

#------ Public API (for kernels) ------------------------------------------
def ssh_tunnel(self, *args, **kwargs):
Expand Down Expand Up @@ -1333,24 +1368,27 @@ def move_tab(self, index_from, index_to):
self.clients.insert(index_to, client)
self.sig_update_plugin_title.emit()

def rename_client_tab(self, client):
def rename_client_tab(self, client, given_name):
"""Rename client's tab"""
index = self.get_client_index_from_id(id(client))

if given_name is not None:
client.given_name = given_name
self.tabwidget.setTabText(index, client.get_name())

def rename_tabs_after_change(self, given_name):
client = self.get_current_client()

# Rename current client tab to add str_id
if client.allow_rename:
client.given_name = given_name
self.rename_client_tab(client)
if client.allow_rename and not u'/' in given_name:
self.rename_client_tab(client, given_name)
else:
self.rename_client_tab(client, None)

# Rename related clients
if client.allow_rename:
if client.allow_rename and not u'/' in given_name:
for cl in self.get_related_clients(client):
cl.given_name = given_name
self.rename_client_tab(cl)
self.rename_client_tab(cl, given_name)

def tab_name_editor(self):
"""Trigger the tab name editor."""
Expand Down
Loading

0 comments on commit fe001d5

Please sign in to comment.