Show More
@@ -2,19 +2,6 | |||
|
2 | 2 | |
|
3 | 3 | This file is only meant to be imported by process.py, not by end-users. |
|
4 | 4 | """ |
|
5 | ||
|
6 | #----------------------------------------------------------------------------- | |
|
7 | # Copyright (C) 2010-2011 The IPython Development Team | |
|
8 | # | |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
|
10 | # the file COPYING, distributed as part of this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
13 | #----------------------------------------------------------------------------- | |
|
14 | # Imports | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||
|
17 | # stdlib | |
|
18 | 5 | import ctypes |
|
19 | 6 | import os |
|
20 | 7 | import subprocess |
@@ -25,18 +12,15 from ctypes.wintypes import HLOCAL, LPCWSTR | |||
|
25 | 12 | from subprocess import STDOUT |
|
26 | 13 | from threading import Thread |
|
27 | 14 | from types import TracebackType |
|
28 |
from typing import |
|
|
15 | from typing import List, Optional | |
|
29 | 16 | |
|
30 | 17 | from . import py3compat |
|
31 | 18 | from ._process_common import arg_split as py_arg_split |
|
32 | 19 | |
|
33 | # our own imports | |
|
34 | 20 | from ._process_common import process_handler, read_no_interrupt |
|
35 | 21 | from .encoding import DEFAULT_ENCODING |
|
36 | 22 | |
|
37 | #----------------------------------------------------------------------------- | |
|
38 | # Function definitions | |
|
39 | #----------------------------------------------------------------------------- | |
|
23 | ||
|
40 | 24 | |
|
41 | 25 | class AvoidUNCPath: |
|
42 | 26 | """A context manager to protect command execution from UNC paths. |
@@ -72,7 +56,10 class AvoidUNCPath: | |||
|
72 | 56 | return None |
|
73 | 57 | |
|
74 | 58 | def __exit__( |
|
75 | self, exc_type: Optional[type[BaseException]], exc_value: Optional[BaseException], traceback:TracebackType | |
|
59 | self, | |
|
60 | exc_type: Optional[type[BaseException]], | |
|
61 | exc_value: Optional[BaseException], | |
|
62 | traceback: TracebackType, | |
|
76 | 63 | ) -> None: |
|
77 | 64 | if self.is_unc_path: |
|
78 | 65 | os.chdir(self.path) |
@@ -142,14 +129,15 def system(cmd: str) -> Optional[int]: | |||
|
142 | 129 | """ |
|
143 | 130 | # The controller provides interactivity with both |
|
144 | 131 | # stdin and stdout |
|
145 | #import _process_win32_controller | |
|
146 | #_process_win32_controller.system(cmd) | |
|
132 | # import _process_win32_controller | |
|
133 | # _process_win32_controller.system(cmd) | |
|
147 | 134 | |
|
148 | 135 | with AvoidUNCPath() as path: |
|
149 | 136 | if path is not None: |
|
150 | 137 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
151 | 138 | return process_handler(cmd, _system_body) |
|
152 | 139 | |
|
140 | ||
|
153 | 141 | def getoutput(cmd: str) -> str: |
|
154 | 142 | """Return standard output of executing cmd in a shell. |
|
155 | 143 | |
@@ -171,9 +159,10 def getoutput(cmd: str) -> str: | |||
|
171 | 159 | out = process_handler(cmd, lambda p: p.communicate()[0], STDOUT) |
|
172 | 160 | |
|
173 | 161 | if out is None: |
|
174 |
out = b |
|
|
162 | out = b"" | |
|
175 | 163 | return py3compat.decode(out) |
|
176 | 164 | |
|
165 | ||
|
177 | 166 | try: |
|
178 | 167 | windll = ctypes.windll # type: ignore [attr-defined] |
|
179 | 168 | CommandLineToArgvW = windll.shell32.CommandLineToArgvW |
@@ -193,7 +182,7 try: | |||
|
193 | 182 | |
|
194 | 183 | If strict=False, process_common.arg_split(...strict=False) is used instead. |
|
195 | 184 | """ |
|
196 | #CommandLineToArgvW returns path to executable if called with empty string. | |
|
185 | # CommandLineToArgvW returns path to executable if called with empty string. | |
|
197 | 186 | if commandline.strip() == "": |
|
198 | 187 | return [] |
|
199 | 188 | if not strict: |
@@ -209,11 +198,13 try: | |||
|
209 | 198 | ) |
|
210 | 199 | if arg is not None |
|
211 | 200 | ] |
|
212 | LocalFree(result_pointer) | |
|
201 | # for side effects | |
|
202 | _ = LocalFree(result_pointer) | |
|
213 | 203 | return result |
|
214 | 204 | except AttributeError: |
|
215 | 205 | arg_split = py_arg_split |
|
216 | 206 | |
|
207 | ||
|
217 | 208 | def check_pid(pid: int) -> bool: |
|
218 | 209 | # OpenProcess returns 0 if no such process (of ours) exists |
|
219 | 210 | # positive int otherwise |
General Comments 0
You need to be logged in to leave comments.
Login now