diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-01-22 10:37:39 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-01-22 10:37:39 +0100 |
commit | c62b41f3df3e3ee98cc605a64b90152d3a9c631e (patch) | |
tree | 8c840394825f0890285abfc6dc4c68b526f7f269 /psutil | |
parent | 0aa753fff8dcbf0ca36acfd8cfd9ae196ce000f9 (diff) | |
download | psutil-fbenkstein-non-unicode.tar.gz |
expose https://github.com/fbenkstein and fix name() / cmdline() encoding errors on linux / py3fbenkstein-non-unicode
Diffstat (limited to 'psutil')
-rw-r--r-- | psutil/__init__.py | 9 | ||||
-rw-r--r-- | psutil/_pslinux.py | 19 |
2 files changed, 21 insertions, 7 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index d46e034e..f3425de7 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -141,6 +141,12 @@ else: # pragma: no cover raise NotImplementedError('platform %s is not supported' % sys.platform) +# Dictates how to handle encoding and decoding errors (with open()) +# on Python 3. This is public API and it will be retrieved from _ps*.py +# modules via sys.modules. +ENCODING_ERRORS_HANDLER = 'surrogateescape' + + __all__ = [ # exceptions "Error", "NoSuchProcess", "ZombieProcess", "AccessDenied", @@ -155,6 +161,7 @@ __all__ = [ "CONN_LAST_ACK", "CONN_LISTEN", "CONN_CLOSING", "CONN_NONE", "AF_LINK", "NIC_DUPLEX_FULL", "NIC_DUPLEX_HALF", "NIC_DUPLEX_UNKNOWN", + "ENCODING_ERRORS_HANDLER", # classes "Process", "Popen", # functions @@ -168,7 +175,7 @@ __all__ = [ ] __all__.extend(_psplatform.__extra__all__) __author__ = "Giampaolo Rodola'" -__version__ = "3.4.2" +__version__ = "3.5.0" version_info = tuple([int(num) for num in __version__.split('.')]) AF_LINK = _psplatform.AF_LINK _TOTAL_PHYMEM = None diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 243f1626..f96bcc95 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -139,11 +139,16 @@ def open_binary(fname, **kwargs): def open_text(fname, **kwargs): - """On Python 3 opens a file in text mode by using fs encoding. + """On Python 3 opens a file in text mode by using fs encoding and + a proper en/decoding errors handler. On Python 2 this is just an alias for open(name, 'rt'). """ - if PY3 and 'encoding' not in kwargs: - kwargs['encoding'] = FS_ENCODING + if PY3: + # See: + # https://github.com/giampaolo/psutil/issues/675 + # https://github.com/giampaolo/psutil/pull/733 + kwargs.setdefault('encoding', FS_ENCODING) + kwargs.setdefault('errors', get_encoding_errors_handler()) return open(fname, "rt", **kwargs) @@ -151,6 +156,10 @@ def get_procfs_path(): return sys.modules['psutil'].PROCFS_PATH +def get_encoding_errors_handler(): + return sys.modules['psutil'].ENCODING_ERRORS_HANDLER + + def readlink(path): """Wrapper around os.readlink().""" assert isinstance(path, basestring), path @@ -600,9 +609,7 @@ class Connections: def process_unix(self, file, family, inodes, filter_pid=None): """Parse /proc/net/unix files.""" - # see: https://github.com/giampaolo/psutil/issues/675 - kw = dict(errors='replace') if PY3 else dict() - with open_text(file, buffering=BIGGER_FILE_BUFFERING, **kw) as f: + with open_text(file, buffering=BIGGER_FILE_BUFFERING) as f: f.readline() # skip the first line for line in f: tokens = line.split() |