Skip to content

Commit

Permalink
Merge from 3.x: PR #4828
Browse files Browse the repository at this point in the history
Fixes #4816
  • Loading branch information
ccordoba12 committed Jul 30, 2017
2 parents e09da2f + c410298 commit b14e9e1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
46 changes: 29 additions & 17 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from spyder.config.base import get_home_dir
from spyder.config.main import CONF
from spyder.plugins.runconfig import RunConfiguration
from spyder.py3compat import PY2
from spyder.py3compat import PY2, to_text_string
from spyder.utils.ipython.kernelspec import SpyderKernelSpec
from spyder.utils.programs import is_module_installed
from spyder.utils.test import close_save_message_box
Expand Down Expand Up @@ -346,47 +346,59 @@ def test_change_types_in_varexp(main_window, qtbot):


@flaky(max_runs=3)
def test_change_cwd_ipython_console(main_window, qtbot, tmpdir):
@pytest.mark.parametrize("test_directory", [u"non_ascii_ñ_í_ç", u"test_dir"])
def test_change_cwd_ipython_console(main_window, qtbot, tmpdir, test_directory):
"""
Test synchronization with working directory and File Explorer when
changing cwd in the IPython console.
"""
# Wait until the window is fully up
wdir = main_window.workingdirectory
treewidget = main_window.explorer.fileexplorer.treewidget
shell = main_window.ipyconsole.get_current_shellwidget()

# Wait until the window is fully up
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)

# Change directory in ipython console using %cd
temp_dir = str(tmpdir.mkdir("test_dir"))
# Create temp dir
temp_dir = to_text_string(tmpdir.mkdir(test_directory))

# Change directory in IPython console using %cd
with qtbot.waitSignal(shell.executed):
shell.execute("%cd {}".format(temp_dir))
shell.execute(u"%cd {}".format(temp_dir))
qtbot.wait(1000)

# assert that cwd changed in workingdirectory
assert osp.normpath(main_window.workingdirectory.history[-1]) == osp.normpath(temp_dir)
# Assert that cwd changed in workingdirectory
assert osp.normpath(wdir.history[-1]) == osp.normpath(temp_dir)

# assert that cwd changed in explorer
assert osp.normpath(main_window.explorer.fileexplorer.treewidget.get_current_folder()) == osp.normpath(temp_dir)
# Assert that cwd changed in explorer
assert osp.normpath(treewidget.get_current_folder()) == osp.normpath(temp_dir)


@flaky(max_runs=3)
def test_change_cwd_explorer(main_window, qtbot, tmpdir):
@pytest.mark.parametrize("test_directory", [u"non_ascii_ñ_í_ç", u"test_dir"])
def test_change_cwd_explorer(main_window, qtbot, tmpdir, test_directory):
"""
Test synchronization with working directory and IPython console when
changing directories in the File Explorer.
"""
# Wait until the window is fully up
wdir = main_window.workingdirectory
explorer = main_window.explorer
shell = main_window.ipyconsole.get_current_shellwidget()

# Wait until the window is fully up
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)

# Create temp directory
temp_dir = to_text_string(tmpdir.mkdir(test_directory))

# Change directory in the explorer widget
temp_dir = str(tmpdir.mkdir("test_dir"))
main_window.explorer.chdir(temp_dir)
explorer.chdir(temp_dir)
qtbot.wait(1000)

# assert that cwd changed in workingdirectory
assert osp.normpath(main_window.workingdirectory.history[-1]) == osp.normpath(temp_dir)
# Assert that cwd changed in workingdirectory
assert osp.normpath(wdir.history[-1]) == osp.normpath(temp_dir)

# assert that cwd changed in ipythonconsole
# Assert that cwd changed in IPython console
assert osp.normpath(temp_dir) == osp.normpath(shell._cwd)


Expand Down
1 change: 0 additions & 1 deletion spyder/plugins/ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@ def set_current_client_working_directory(self, directory):
"""Set current client working directory."""
shellwidget = self.get_current_shellwidget()
if shellwidget is not None:
directory = encoding.to_unicode_from_fs(directory)
shellwidget.set_cwd(directory)

def set_working_directory(self, dirname):
Expand Down
9 changes: 3 additions & 6 deletions spyder/plugins/workingdirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,11 @@ def chdir(self, directory, browsing_history=False,
self.histindex = len(self.history)-1

# Changing working directory
os.chdir( to_text_string(directory) )
os.chdir(directory)
self.refresh_plugin()

if refresh_explorer:
self.set_explorer_cwd.emit(directory)
if refresh_console:
self.set_as_current_console_wd()
self.set_current_console_wd.emit(directory)
self.refresh_findinfiles.emit()

def set_as_current_console_wd(self):
"""Set as current console working directory"""
self.set_current_console_wd.emit(getcwd())
2 changes: 1 addition & 1 deletion spyder/utils/ipython/spyder_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def get_source(self, objtxt):
# --- Additional methods
def set_cwd(self, dirname):
"""Set current working directory."""
return os.chdir(dirname)
os.chdir(dirname)

def get_cwd(self):
"""Get current working directory."""
Expand Down
2 changes: 1 addition & 1 deletion spyder/utils/tests/test_programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


if os.name == 'nt':
python_dir = os.environ['PYTHON']
python_dir = os.environ['PYTHON'] if os.environ.get('CI', None) else ''
VALID_INTERPRETER = os.path.join(python_dir, 'python.exe')
INVALID_INTERPRETER = os.path.join(python_dir, 'Scripts', 'ipython.exe')
else:
Expand Down
12 changes: 10 additions & 2 deletions spyder/widgets/ipythonconsole/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"""

import ast
import os
import uuid

from qtpy.QtCore import Signal
from qtpy.QtWidgets import QMessageBox
from spyder.config.base import _
from spyder.config.gui import config_shortcut
from spyder.py3compat import to_text_string
from spyder.py3compat import PY2, to_text_string
from spyder.utils import encoding
from spyder.utils import programs
from spyder.utils import syntaxhighlighters as sh
from spyder.utils.ipython.style import create_qss_style, create_style_class
Expand Down Expand Up @@ -98,7 +100,11 @@ def is_spyder_kernel(self):

def set_cwd(self, dirname):
"""Set shell current working directory."""
code = u"get_ipython().kernel.set_cwd(r'{}')".format(dirname)
# Replace single for double backslashes on Windows
if os.name == 'nt':
dirname = dirname.replace(u"\\", u"\\\\")

code = u"get_ipython().kernel.set_cwd(u'{}')".format(dirname)
if self._reading:
self.kernel_client.input(u'!' + code)
else:
Expand Down Expand Up @@ -334,6 +340,8 @@ def handle_exec_method(self, msg):
elif 'get_cwd' in method:
if data is not None and 'text/plain' in data:
self._cwd = ast.literal_eval(data['text/plain'])
if PY2:
self._cwd = encoding.to_unicode_from_fs(self._cwd)
else:
self._cwd = ''
self.sig_change_cwd.emit(self._cwd)
Expand Down

0 comments on commit b14e9e1

Please sign in to comment.