Skip to content

Commit 81fa367

Browse files
authored
TST: Use Popen in contextmanagers (#44611)
1 parent b8a8049 commit 81fa367

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

pandas/io/clipboard/__init__.py

+31-27
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ def _stringifyText(text) -> str:
111111
def init_osx_pbcopy_clipboard():
112112
def copy_osx_pbcopy(text):
113113
text = _stringifyText(text) # Converts non-str values to str.
114-
p = subprocess.Popen(["pbcopy", "w"], stdin=subprocess.PIPE, close_fds=True)
115-
p.communicate(input=text.encode(ENCODING))
114+
with subprocess.Popen(
115+
["pbcopy", "w"], stdin=subprocess.PIPE, close_fds=True
116+
) as p:
117+
p.communicate(input=text.encode(ENCODING))
116118

117119
def paste_osx_pbcopy():
118-
p = subprocess.Popen(["pbpaste", "r"], stdout=subprocess.PIPE, close_fds=True)
119-
stdout = p.communicate()[0]
120+
with subprocess.Popen(
121+
["pbpaste", "r"], stdout=subprocess.PIPE, close_fds=True
122+
) as p:
123+
stdout = p.communicate()[0]
120124
return stdout.decode(ENCODING)
121125

122126
return copy_osx_pbcopy, paste_osx_pbcopy
@@ -179,22 +183,22 @@ def copy_xclip(text, primary=False):
179183
selection = DEFAULT_SELECTION
180184
if primary:
181185
selection = PRIMARY_SELECTION
182-
p = subprocess.Popen(
186+
with subprocess.Popen(
183187
["xclip", "-selection", selection], stdin=subprocess.PIPE, close_fds=True
184-
)
185-
p.communicate(input=text.encode(ENCODING))
188+
) as p:
189+
p.communicate(input=text.encode(ENCODING))
186190

187191
def paste_xclip(primary=False):
188192
selection = DEFAULT_SELECTION
189193
if primary:
190194
selection = PRIMARY_SELECTION
191-
p = subprocess.Popen(
195+
with subprocess.Popen(
192196
["xclip", "-selection", selection, "-o"],
193197
stdout=subprocess.PIPE,
194198
stderr=subprocess.PIPE,
195199
close_fds=True,
196-
)
197-
stdout = p.communicate()[0]
200+
) as p:
201+
stdout = p.communicate()[0]
198202
# Intentionally ignore extraneous output on stderr when clipboard is empty
199203
return stdout.decode(ENCODING)
200204

@@ -210,19 +214,19 @@ def copy_xsel(text, primary=False):
210214
selection_flag = DEFAULT_SELECTION
211215
if primary:
212216
selection_flag = PRIMARY_SELECTION
213-
p = subprocess.Popen(
217+
with subprocess.Popen(
214218
["xsel", selection_flag, "-i"], stdin=subprocess.PIPE, close_fds=True
215-
)
216-
p.communicate(input=text.encode(ENCODING))
219+
) as p:
220+
p.communicate(input=text.encode(ENCODING))
217221

218222
def paste_xsel(primary=False):
219223
selection_flag = DEFAULT_SELECTION
220224
if primary:
221225
selection_flag = PRIMARY_SELECTION
222-
p = subprocess.Popen(
226+
with subprocess.Popen(
223227
["xsel", selection_flag, "-o"], stdout=subprocess.PIPE, close_fds=True
224-
)
225-
stdout = p.communicate()[0]
228+
) as p:
229+
stdout = p.communicate()[0]
226230
return stdout.decode(ENCODING)
227231

228232
return copy_xsel, paste_xsel
@@ -231,7 +235,7 @@ def paste_xsel(primary=False):
231235
def init_klipper_clipboard():
232236
def copy_klipper(text):
233237
text = _stringifyText(text) # Converts non-str values to str.
234-
p = subprocess.Popen(
238+
with subprocess.Popen(
235239
[
236240
"qdbus",
237241
"org.kde.klipper",
@@ -241,16 +245,16 @@ def copy_klipper(text):
241245
],
242246
stdin=subprocess.PIPE,
243247
close_fds=True,
244-
)
245-
p.communicate(input=None)
248+
) as p:
249+
p.communicate(input=None)
246250

247251
def paste_klipper():
248-
p = subprocess.Popen(
252+
with subprocess.Popen(
249253
["qdbus", "org.kde.klipper", "/klipper", "getClipboardContents"],
250254
stdout=subprocess.PIPE,
251255
close_fds=True,
252-
)
253-
stdout = p.communicate()[0]
256+
) as p:
257+
stdout = p.communicate()[0]
254258

255259
# Workaround for https://bugs.kde.org/show_bug.cgi?id=342874
256260
# TODO: https://github.com/asweigart/pyperclip/issues/43
@@ -483,17 +487,17 @@ def paste_windows():
483487
def init_wsl_clipboard():
484488
def copy_wsl(text):
485489
text = _stringifyText(text) # Converts non-str values to str.
486-
p = subprocess.Popen(["clip.exe"], stdin=subprocess.PIPE, close_fds=True)
487-
p.communicate(input=text.encode(ENCODING))
490+
with subprocess.Popen(["clip.exe"], stdin=subprocess.PIPE, close_fds=True) as p:
491+
p.communicate(input=text.encode(ENCODING))
488492

489493
def paste_wsl():
490-
p = subprocess.Popen(
494+
with subprocess.Popen(
491495
["powershell.exe", "-command", "Get-Clipboard"],
492496
stdout=subprocess.PIPE,
493497
stderr=subprocess.PIPE,
494498
close_fds=True,
495-
)
496-
stdout = p.communicate()[0]
499+
) as p:
500+
stdout = p.communicate()[0]
497501
# WSL appends "\r\n" to the contents.
498502
return stdout[:-2].decode(ENCODING)
499503

pandas/tests/io/conftest.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,26 @@ def s3_base(worker_id):
6969
endpoint_uri = f"http://127.0.0.1:{endpoint_port}/"
7070

7171
# pipe to null to avoid logging in terminal
72-
proc = subprocess.Popen(
72+
with subprocess.Popen(
7373
shlex.split(f"moto_server s3 -p {endpoint_port}"),
7474
stdout=subprocess.DEVNULL,
7575
stderr=subprocess.DEVNULL,
76-
)
77-
78-
timeout = 5
79-
while timeout > 0:
80-
try:
81-
# OK to go once server is accepting connections
82-
r = requests.get(endpoint_uri)
83-
if r.ok:
84-
break
85-
except Exception:
86-
pass
87-
timeout -= 0.1
88-
time.sleep(0.1)
89-
yield endpoint_uri
90-
91-
proc.terminate()
92-
proc.wait()
76+
) as proc:
77+
78+
timeout = 5
79+
while timeout > 0:
80+
try:
81+
# OK to go once server is accepting connections
82+
r = requests.get(endpoint_uri)
83+
if r.ok:
84+
break
85+
except Exception:
86+
pass
87+
timeout -= 0.1
88+
time.sleep(0.1)
89+
yield endpoint_uri
90+
91+
proc.terminate()
9392

9493

9594
@pytest.fixture()

0 commit comments

Comments
 (0)