Skip to content

Commit

Permalink
Clean up current working directory manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclary committed Mar 2, 2024
1 parent 250da79 commit a7a55e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
10 changes: 3 additions & 7 deletions spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ def __init__(self, *args, **kwargs):
# Socket to signal shell_stream locally
self.loopback_socket = None

# Re-add current working directory path into sys.path after
# removing it before kernel started
if '' not in sys.path:
sys.path.insert(0, '')

# Store original sys.path. Kernels are started with PYTHONPATH
# removed from environment variables, so this will never have
# user paths and should be clean.
Expand Down Expand Up @@ -795,8 +790,9 @@ def update_syspath(self, new_path, prioritize):

# Ensure current directory is always first to imitate Python
# standard behavior
sys.path.remove('')
sys.path.insert(0, '')
if '' in sys.path:
sys.path.remove('')
sys.path.insert(0, '')
else:
sys.path[:] = self._sys_path + new_path
else:
Expand Down
30 changes: 15 additions & 15 deletions spyder_kernels/console/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
import sys
import site

# Remove current directory from sys.path to prevent kernel
# crashes when people name Python files or modules with
# the same name as standard library modules.
# See spyder-ide/spyder#8007
# Inject it back into sys.path after all imports in this module but
# before the kernel is initialized
while '' in sys.path:
sys.path.remove('')

# Third-party imports
from traitlets import DottedObjectName

Expand All @@ -29,14 +38,6 @@ def import_spydercustomize():
parent = osp.dirname(here)
customize_dir = osp.join(parent, 'customize')

# Remove current directory from sys.path to prevent kernel
# crashes when people name Python files or modules with
# the same name as standard library modules.
# See spyder-ide/spyder#8007
# Inject it back into sys.path after kernel starts
while '' in sys.path:
sys.path.remove('')

# Import our customizations
site.addsitedir(customize_dir)
import spydercustomize # noqa
Expand Down Expand Up @@ -177,13 +178,6 @@ def main():
# Import our customizations into the kernel
import_spydercustomize()

# Remove current directory from sys.path to prevent kernel
# crashes when people name Python files or modules with
# the same name as standard library modules.
# See spyder-ide/spyder#8007
while '' in sys.path:
sys.path.remove('')

# Main imports
from ipykernel.kernelapp import IPKernelApp
from spyder_kernels.console.kernel import SpyderKernel
Expand Down Expand Up @@ -216,6 +210,12 @@ def close(self):
kernel.config = kernel_config()
except:
pass

# Re-add current working directory path into sys.path after all of the
# import statements, but before initiializing the kernel.
if '' not in sys.path:
sys.path.insert(0, '')

kernel.initialize()

# Set our own magics
Expand Down
5 changes: 2 additions & 3 deletions spyder_kernels/console/tests/test_console_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,16 @@ def test_cwd_in_sys_path():
def test_prioritize(kernel):
"""Test that user path priority is honored in sys.path."""
syspath = kernel.get_syspath()
syspath.remove('')
append_path = ['/test/append/path']
prepend_path = ['/test/prepend/path']

kernel.update_syspath(append_path, prioritize=False)
new_syspath = kernel.get_syspath()
assert new_syspath == [''] + syspath + append_path
assert new_syspath == syspath + append_path

kernel.update_syspath(prepend_path, prioritize=True)
new_syspath = kernel.get_syspath()
assert new_syspath == [''] + prepend_path + syspath
assert new_syspath == prepend_path + syspath


@flaky(max_runs=3)
Expand Down

0 comments on commit a7a55e2

Please sign in to comment.