Skip to content

Commit 3178358

Browse files
committed
Make cmdline decoding more robust
At least don't fail with unrelated inputs. As long as we can reasonably get the shell, it's good enough. We can deal with these edge cases if the proc parsing code is to be extracted somewhere else one day. Fix #10.
1 parent 32fb85b commit 3178358

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

news/10.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve ``/proc`` content parsing robustness to not fail with non-decodable command line arguments.

src/shellingham/posix/proc.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import io
12
import os
23
import re
4+
import sys
35

46
from ._core import Process
57

@@ -27,14 +29,22 @@ def detect_proc():
2729

2830

2931
def _get_stat(pid, name):
30-
with open(os.path.join('/proc', str(pid), name)) as f:
32+
path = os.path.join('/proc', str(pid), name)
33+
with io.open(path, encoding='ascii', errors='replace') as f:
34+
# We only care about TTY and PPID -- all numbers.
3135
parts = STAT_PATTERN.findall(f.read())
3236
return parts[STAT_TTY], parts[STAT_PPID]
3337

3438

3539
def _get_cmdline(pid):
36-
with open(os.path.join('/proc', str(pid), 'cmdline')) as f:
37-
return tuple(f.read().split('\0')[:-1])
40+
path = os.path.join('/proc', str(pid), 'cmdline')
41+
encoding = sys.getfilesystemencoding() or 'utf-8'
42+
with io.open(path, encoding=encoding, errors='replace') as f:
43+
# XXX: Command line arguments can be arbitrary byte sequences, not
44+
# necessarily decodable. For Shellingham's purpose, however, we don't
45+
# care. (pypa/pipenv#2820)
46+
# cmdline appends an extra NULL at the end, hence the [:-1].
47+
return tuple(f.read().split(b'\0')[:-1])
3848

3949

4050
class ProcFormatError(EnvironmentError):

0 commit comments

Comments
 (0)