|
| 1 | +import io |
1 | 2 | import os |
2 | 3 | import re |
| 4 | +import sys |
3 | 5 |
|
4 | 6 | from ._core import Process |
5 | 7 |
|
@@ -27,14 +29,22 @@ def detect_proc(): |
27 | 29 |
|
28 | 30 |
|
29 | 31 | 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. |
31 | 35 | parts = STAT_PATTERN.findall(f.read()) |
32 | 36 | return parts[STAT_TTY], parts[STAT_PPID] |
33 | 37 |
|
34 | 38 |
|
35 | 39 | 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]) |
38 | 48 |
|
39 | 49 |
|
40 | 50 | class ProcFormatError(EnvironmentError): |
|
0 commit comments