Skip to content

Correctly change the working directory in python #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Using pip

You can install the package without being root by adding the ``--user`` flag::

pip2 install neovim
pip3 install neovim
pip2 install --user neovim
pip3 install --user neovim

.. note::

If you only use one of python2 or python3,
it is enough to install that version.

If you follow Neovim master,
If you follow Neovim HEAD,
make sure to upgrade the ``python-client`` when you upgrade Neovim::

pip2 install --upgrade neovim
Expand Down
13 changes: 13 additions & 0 deletions neovim/plugin/script_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def __init__(self, nvim):
self.legacy_vim = LegacyVim.from_nvim(nvim)
sys.modules['vim'] = self.legacy_vim

# Handle DirChanged. #296
nvim.command(
'autocmd DirChanged * call rpcrequest({}, "python_chdir", v:event)'
.format(nvim.channel_id), async=True)
# XXX: Avoid race condition.
# https://github.com/neovim/python-client/pull/296#issuecomment-358970531
os.chdir(nvim.eval('getcwd()', async=False))

def setup(self, nvim):
"""Setup import hooks and global streams.

Expand Down Expand Up @@ -153,6 +161,11 @@ def python_eval(self, expr):
"""Handle the `pyeval` vim function."""
return eval(expr, self.module.__dict__)

@rpc_export('python_chdir', sync=True)
def python_chdir(self, args):
"""Handle working directory changes."""
os.chdir(args['cwd'])

def _set_current_range(self, start, stop):
current = self.legacy_vim.current
current.range = current.buffer.range(start, stop)
Expand Down
21 changes: 19 additions & 2 deletions test/test_vim.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import os, tempfile
import os, sys, tempfile
from nose.tools import with_setup, eq_ as eq, ok_ as ok
from test_common import vim, cleanup

Expand All @@ -15,7 +15,7 @@ def source(code):
def test_command():
fname = tempfile.mkstemp()[1]
vim.command('new')
vim.command('edit %s' % fname)
vim.command('edit {}'.format(fname))
# skip the "press return" state, which does not handle deferred calls
vim.input('\r')
vim.command('normal itesting\npython\napi')
Expand Down Expand Up @@ -162,3 +162,20 @@ def test_hash():
eq(d[vim.current.buffer], "alpha")
vim.command('winc w')
eq(d[vim.current.buffer], "beta")


@with_setup(setup=cleanup)
def test_cwd():
pycmd = 'python'
if sys.version_info >= (3, 0):
pycmd = 'python3'

vim.command('{} import os'.format(pycmd))
cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd))

vim.command('cd test')
cwd_vim = vim.command_output('pwd')
cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd))
eq(cwd_vim, cwd_python)
ok(cwd_python != cwd_before)