Skip to content

Commit

Permalink
Merge pull request #6244 from ccordoba12/fix-edit-windows
Browse files Browse the repository at this point in the history
PR: Fix %edit magic on Windows
  • Loading branch information
ccordoba12 authored Jan 21, 2018
2 parents a771d31 + cc436b3 commit aa84819
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 34 deletions.
5 changes: 5 additions & 0 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3093,6 +3093,11 @@ def run_spyder(app, options, args):
#==============================================================================
def main():
"""Main function"""
if PYTEST:
options, args = get_options()
app = initialize()
window = run_spyder(app, options, args)
return window

# **** Collect command line options ****
# Note regarding Options:
Expand Down
19 changes: 14 additions & 5 deletions spyder/app/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# Local imports
from spyder.app.cli_options import get_options
from spyder.config.base import get_conf_path, running_in_mac_app
from spyder.config.base import PYTEST, get_conf_path, running_in_mac_app
from spyder.config.main import CONF
from spyder.utils.external import lockfile
from spyder.py3compat import is_unicode
Expand Down Expand Up @@ -135,13 +135,19 @@ def main():
# executing this script because it doesn't make
# sense
from spyder.app import mainwindow
mainwindow.main()
return
if PYTEST:
return mainwindow.main()
else:
mainwindow.main()
return

if lock_created:
# Start a new instance
from spyder.app import mainwindow
mainwindow.main()
if PYTEST:
return mainwindow.main()
else:
mainwindow.main()
else:
# Pass args to Spyder or print an informative
# message
Expand All @@ -152,7 +158,10 @@ def main():
"instance, please pass to it the --new-instance option")
else:
from spyder.app import mainwindow
mainwindow.main()
if PYTEST:
return mainwindow.main()
else:
mainwindow.main()


if __name__ == "__main__":
Expand Down
52 changes: 44 additions & 8 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from qtpy.QtWidgets import QApplication, QFileDialog, QLineEdit

from spyder import __trouble_url__
from spyder.app.cli_options import get_options
from spyder.app.mainwindow import initialize, run_spyder, MainWindow
from spyder.app.mainwindow import MainWindow # Tests fail without this import
from spyder.app import start
from spyder.config.base import get_home_dir
from spyder.config.main import CONF
from spyder.plugins.runconfig import RunConfiguration
Expand Down Expand Up @@ -63,9 +63,6 @@
# miliseconds)
EVAL_TIMEOUT = 3000

# Test for PyQt 5 wheels
PYQT_WHEEL = PYQT_VERSION > '5.6'

# Temporary directory
TEMP_DIRECTORY = tempfile.gettempdir()

Expand Down Expand Up @@ -139,10 +136,15 @@ def main_window(request):
except KeyError:
pass

# Only use single_instance mode for tests that require it
single_instance = request.node.get_marker('single_instance')
if single_instance:
CONF.set('main', 'single_instance', True)
else:
CONF.set('main', 'single_instance', False)

# Start the window
app = initialize()
options, args = get_options()
window = run_spyder(app, options, args)
window = start.main()

# Teardown
def close_window():
Expand Down Expand Up @@ -189,6 +191,40 @@ def test_calltip(main_window, qtbot):
main_window.editor.close_file()


@pytest.mark.slow
@pytest.mark.single_instance
def test_single_instance_and_edit_magic(main_window, qtbot, tmpdir):
"""Test single instance mode and for %edit magic."""
editorstack = main_window.editor.get_current_editorstack()
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)

lock_code = ("from spyder.config.base import get_conf_path\n"
"from spyder.utils.external import lockfile\n"
"lock_file = get_conf_path('spyder.lock')\n"
"lock = lockfile.FilesystemLock(lock_file)\n"
"lock_created = lock.lock()")

# Test single instance
with qtbot.waitSignal(shell.executed):
shell.execute(lock_code)
assert not shell.get_value('lock_created')

# Test %edit magic
n_editors = editorstack.get_stack_count()
p = tmpdir.mkdir("foo").join("bar.py")
p.write(lock_code)

with qtbot.waitSignal(shell.executed):
shell.execute('%edit {}'.format(to_text_string(p)))

qtbot.wait(3000)
assert editorstack.get_stack_count() == n_editors + 1
assert editorstack.get_current_editor().toPlainText() == lock_code

main_window.editor.close_file()


@pytest.mark.slow
@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt' or PY2 or PYQT4,
Expand Down
38 changes: 27 additions & 11 deletions spyder/plugins/ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# Local imports
from spyder import dependencies
from spyder.config.base import (_, DEV, get_conf_path, get_home_dir,
get_module_path)
get_module_path, PYTEST)
from spyder.config.main import CONF
from spyder.plugins import SpyderPluginWidget
from spyder.plugins.configdialog import PluginConfigPage
Expand All @@ -52,7 +52,7 @@
from spyder.utils.ipython.style import create_qss_style
from spyder.utils.qthelpers import create_action, MENU_SEPARATOR
from spyder.utils import icon_manager as ima
from spyder.utils import programs, sourcecode
from spyder.utils import encoding, programs, sourcecode
from spyder.utils.programs import TEMPDIR
from spyder.utils.misc import get_error_match, remove_backslashes
from spyder.widgets.findreplace import FindReplace
Expand Down Expand Up @@ -1073,21 +1073,37 @@ def connect_client_to_kernel(self, client):

def set_editor(self):
"""Set the editor used by the %edit magic"""
# Get Python executable used by Spyder
python = sys.executable
if PY2:
python = encoding.to_unicode_from_fs(python)

# Compose command for %edit
spy_dir = osp.dirname(get_module_path('spyder'))
if DEV:
spyder_start_directory = get_module_path('spyder')
bootstrap_script = osp.join(osp.dirname(spyder_start_directory),
'bootstrap.py')
editor = u'{0} {1} --'.format(python, bootstrap_script)
bootstrap = osp.join(spy_dir, 'bootstrap.py')
if PY2:
bootstrap = encoding.to_unicode_from_fs(bootstrap)
editor = u'"{0}" "{1}" --'.format(python, bootstrap)
else:
import1 = "import sys"
# We need to add spy_dir to sys.path so this test can be
# run in our CIs
if PYTEST:
if os.name == 'nt':
import1 = (import1 +
'; sys.path.append(""{}"")'.format(spy_dir))
else:
import1 = (import1 +
"; sys.path.append('{}')".format(spy_dir))
import2 = "from spyder.app.start import send_args_to_spyder"
code = "send_args_to_spyder([sys.argv[-1]])"
editor = u"{0} -c '{1}; {2}; {3}'".format(python,
import1,
import2,
code)
return to_text_string(editor)
editor = u"\"{0}\" -c \"{1}; {2}; {3}\"".format(python,
import1,
import2,
code)

return editor

def config_options(self):
"""
Expand Down
21 changes: 11 additions & 10 deletions spyder/utils/external/lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from time import time as _uniquefloat

import psutil
from spyder.config.base import PYTEST
from spyder.py3compat import PY2, to_binary_string

def unique():
Expand Down Expand Up @@ -178,16 +179,16 @@ def lock(self):
# Verify that the running process corresponds to
# a Spyder one
p = psutil.Process(int(pid))
if os.name == 'nt':
conditions = ['spyder' in c.lower()
for c in p.cmdline()]
else:
# Valid names for main script
names = set(['spyder', 'spyder3', 'bootstrap.py'])
# Check the first three command line arguments
arguments = set(os.path.basename(arg)
for arg in p.cmdline()[:3])
conditions = [names & arguments]

# Valid names for main script
names = set(['spyder', 'spyder3', 'bootstrap.py'])
if PYTEST:
names.add('runtests.py')

# Check the first three command line arguments
arguments = set(os.path.basename(arg)
for arg in p.cmdline()[:3])
conditions = [names & arguments]
if not any(conditions):
raise(OSError(errno.ESRCH, 'No such process'))
except OSError as e:
Expand Down

0 comments on commit aa84819

Please sign in to comment.