Closed cottsay closed 9 months ago
Thanks for this. Seems just fine to me. The following tries to present a counter example and fails:
import subprocess
def run(cmd, shell=False, timeout=1):
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell
)
try:
outs, errs = proc.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
proc.kill()
outs, errs = proc.communicate()
if outs:
outs = outs.decode("utf-8").strip()
else:
outs = None
if errs and proc.returncode:
errs = errs.decode("utf-8").strip()
else:
errs = None
return outs, errs
# Command with single backslash (will not behave as intended for logical OR in grep)
cmd_single_backslash = "cat testfile | grep 'Units\|Unidades'"
outs_single, errs_single = run(cmd_single_backslash, shell=True)
print("Single Backslash Output:", outs_single or "No output", "\nError:", errs_single or "No error")
# Command with double backslash (correct for escaping in the shell)
cmd_double_backslash = "cat testfile | grep 'Units\\|Unidades'"
outs_double, errs_double = run(cmd_double_backslash, shell=True)
print("\nDouble Backslash Output:", outs_double or "No output", "\nError:", errs_double or "No error")
with testfile
content:
Units: 100
Unidades: 200
The backslash character is an escape sequence in Python strings. These strings contain backslashes which do not intend to escape the following character, but rather include the backslash in the string.
There are two possible resolutions:
I used both approaches in this change where appropriate.
Should resolve:
(note that I have not tested the functionality of this extension, and there are no tests for it. I only know that it clears the warnings and builds successfully.)