@@ -111,12 +111,16 @@ def _stringifyText(text) -> str:
111
111
def init_osx_pbcopy_clipboard ():
112
112
def copy_osx_pbcopy (text ):
113
113
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 ))
116
118
117
119
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 ]
120
124
return stdout .decode (ENCODING )
121
125
122
126
return copy_osx_pbcopy , paste_osx_pbcopy
@@ -179,22 +183,22 @@ def copy_xclip(text, primary=False):
179
183
selection = DEFAULT_SELECTION
180
184
if primary :
181
185
selection = PRIMARY_SELECTION
182
- p = subprocess .Popen (
186
+ with subprocess .Popen (
183
187
["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 ))
186
190
187
191
def paste_xclip (primary = False ):
188
192
selection = DEFAULT_SELECTION
189
193
if primary :
190
194
selection = PRIMARY_SELECTION
191
- p = subprocess .Popen (
195
+ with subprocess .Popen (
192
196
["xclip" , "-selection" , selection , "-o" ],
193
197
stdout = subprocess .PIPE ,
194
198
stderr = subprocess .PIPE ,
195
199
close_fds = True ,
196
- )
197
- stdout = p .communicate ()[0 ]
200
+ ) as p :
201
+ stdout = p .communicate ()[0 ]
198
202
# Intentionally ignore extraneous output on stderr when clipboard is empty
199
203
return stdout .decode (ENCODING )
200
204
@@ -210,19 +214,19 @@ def copy_xsel(text, primary=False):
210
214
selection_flag = DEFAULT_SELECTION
211
215
if primary :
212
216
selection_flag = PRIMARY_SELECTION
213
- p = subprocess .Popen (
217
+ with subprocess .Popen (
214
218
["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 ))
217
221
218
222
def paste_xsel (primary = False ):
219
223
selection_flag = DEFAULT_SELECTION
220
224
if primary :
221
225
selection_flag = PRIMARY_SELECTION
222
- p = subprocess .Popen (
226
+ with subprocess .Popen (
223
227
["xsel" , selection_flag , "-o" ], stdout = subprocess .PIPE , close_fds = True
224
- )
225
- stdout = p .communicate ()[0 ]
228
+ ) as p :
229
+ stdout = p .communicate ()[0 ]
226
230
return stdout .decode (ENCODING )
227
231
228
232
return copy_xsel , paste_xsel
@@ -231,7 +235,7 @@ def paste_xsel(primary=False):
231
235
def init_klipper_clipboard ():
232
236
def copy_klipper (text ):
233
237
text = _stringifyText (text ) # Converts non-str values to str.
234
- p = subprocess .Popen (
238
+ with subprocess .Popen (
235
239
[
236
240
"qdbus" ,
237
241
"org.kde.klipper" ,
@@ -241,16 +245,16 @@ def copy_klipper(text):
241
245
],
242
246
stdin = subprocess .PIPE ,
243
247
close_fds = True ,
244
- )
245
- p .communicate (input = None )
248
+ ) as p :
249
+ p .communicate (input = None )
246
250
247
251
def paste_klipper ():
248
- p = subprocess .Popen (
252
+ with subprocess .Popen (
249
253
["qdbus" , "org.kde.klipper" , "/klipper" , "getClipboardContents" ],
250
254
stdout = subprocess .PIPE ,
251
255
close_fds = True ,
252
- )
253
- stdout = p .communicate ()[0 ]
256
+ ) as p :
257
+ stdout = p .communicate ()[0 ]
254
258
255
259
# Workaround for https://bugs.kde.org/show_bug.cgi?id=342874
256
260
# TODO: https://github.com/asweigart/pyperclip/issues/43
@@ -483,17 +487,17 @@ def paste_windows():
483
487
def init_wsl_clipboard ():
484
488
def copy_wsl (text ):
485
489
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 ))
488
492
489
493
def paste_wsl ():
490
- p = subprocess .Popen (
494
+ with subprocess .Popen (
491
495
["powershell.exe" , "-command" , "Get-Clipboard" ],
492
496
stdout = subprocess .PIPE ,
493
497
stderr = subprocess .PIPE ,
494
498
close_fds = True ,
495
- )
496
- stdout = p .communicate ()[0 ]
499
+ ) as p :
500
+ stdout = p .communicate ()[0 ]
497
501
# WSL appends "\r\n" to the contents.
498
502
return stdout [:- 2 ].decode (ENCODING )
499
503
0 commit comments