MCPcopy Create free account
hub / github.com/ipython/ipython / AvoidUNCPath

Class AvoidUNCPath

IPython/utils/_process_win32.py:37–71  ·  view source on GitHub ↗

A context manager to protect command execution from UNC paths. In the Win32 API, commands can't be invoked with the cwd being a UNC path. This context manager temporarily changes directory to the 'C:' drive on entering, and restores the original working directory on exit. The conte

Source from the content-addressed store, hash-verified

35#-----------------------------------------------------------------------------
36
37class AvoidUNCPath(object):
38 """A context manager to protect command execution from UNC paths.
39
40 In the Win32 API, commands can't be invoked with the cwd being a UNC path.
41 This context manager temporarily changes directory to the 'C:' drive on
42 entering, and restores the original working directory on exit.
43
44 The context manager returns the starting working directory *if* it made a
45 change and None otherwise, so that users can apply the necessary adjustment
46 to their system calls in the event of a change.
47
48 Examples
49 --------
50 ::
51 cmd = 'dir'
52 with AvoidUNCPath() as path:
53 if path is not None:
54 cmd = '"pushd %s &&"%s' % (path, cmd)
55 os.system(cmd)
56 """
57 def __enter__(self):
58 self.path = os.getcwd()
59 self.is_unc_path = self.path.startswith(r"\\")
60 if self.is_unc_path:
61 # change to c drive (as cmd.exe cannot handle UNC addresses)
62 os.chdir("C:")
63 return self.path
64 else:
65 # We return None to signal that there was no change in the working
66 # directory
67 return None
68
69 def __exit__(self, exc_type, exc_value, traceback):
70 if self.is_unc_path:
71 os.chdir(self.path)
72
73
74def _find_cmd(cmd):

Callers 3

system_rawMethod · 0.90
systemFunction · 0.70
getoutputFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected