diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2018-09-26 13:13:36 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2018-09-26 13:13:36 +0200 |
commit | ab9a381f5cd9b33b244a4883d6ba55208c049dcf (patch) | |
tree | e12c75ea886bc18cf3e6c41fd4e566a0cdeb36b7 | |
parent | 772c675c39f02d94eba5d727f3dd2861569d8c6c (diff) | |
download | psutil-ab9a381f5cd9b33b244a4883d6ba55208c049dcf.tar.gz |
#1341: move open() utilities/wrappers in _common.py
-rw-r--r-- | psutil/_common.py | 18 | ||||
-rw-r--r-- | psutil/_pslinux.py | 20 | ||||
-rwxr-xr-x | psutil/tests/test_linux.py | 22 |
3 files changed, 31 insertions, 29 deletions
diff --git a/psutil/_common.py b/psutil/_common.py index 2cc3939a..bee95792 100644 --- a/psutil/_common.py +++ b/psutil/_common.py @@ -576,3 +576,21 @@ def wrap_numbers(input_dict, name): _wn = _WrapNumbers() wrap_numbers.cache_clear = _wn.cache_clear wrap_numbers.cache_info = _wn.cache_info + + +def open_binary(fname, **kwargs): + return open(fname, "rb", **kwargs) + + +def open_text(fname, **kwargs): + """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: + # See: + # https://github.com/giampaolo/psutil/issues/675 + # https://github.com/giampaolo/psutil/pull/733 + kwargs.setdefault('encoding', ENCODING) + kwargs.setdefault('errors', ENCODING_ERRS) + return open(fname, "rt", **kwargs) diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index df624de3..1575dad9 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -33,6 +33,8 @@ from ._common import memoize_when_activated from ._common import NIC_DUPLEX_FULL from ._common import NIC_DUPLEX_HALF from ._common import NIC_DUPLEX_UNKNOWN +from ._common import open_binary +from ._common import open_text from ._common import parse_environ_block from ._common import path_exists_strict from ._common import supports_ipv6 @@ -201,24 +203,6 @@ pio = namedtuple('pio', ['read_count', 'write_count', # ===================================================================== -def open_binary(fname, **kwargs): - return open(fname, "rb", **kwargs) - - -def open_text(fname, **kwargs): - """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: - # See: - # https://github.com/giampaolo/psutil/issues/675 - # https://github.com/giampaolo/psutil/pull/733 - kwargs.setdefault('encoding', ENCODING) - kwargs.setdefault('errors', ENCODING_ERRS) - return open(fname, "rt", **kwargs) - - if PY3: def decode(s): return s.decode(encoding=ENCODING, errors=ENCODING_ERRS) diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 5e3c834e..a0527851 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -528,7 +528,7 @@ class TestSystemSwapMemory(unittest.TestCase): free_value, psutil_value, delta=MEMORY_TOLERANCE) def test_missing_sin_sout(self): - with mock.patch('psutil._pslinux.open', create=True) as m: + with mock.patch('psutil._common.open', create=True) as m: with warnings.catch_warnings(record=True) as ws: warnings.simplefilter("always") ret = psutil.swap_memory() @@ -651,7 +651,7 @@ class TestSystemCPU(unittest.TestCase): # Let's have open() return emtpy data and make sure None is # returned ('cause we mimick os.cpu_count()). - with mock.patch('psutil._pslinux.open', create=True) as m: + with mock.patch('psutil._common.open', create=True) as m: self.assertIsNone(psutil._pslinux.cpu_count_logical()) self.assertEqual(m.call_count, 2) # /proc/stat should be the last one @@ -662,7 +662,7 @@ class TestSystemCPU(unittest.TestCase): with open('/proc/cpuinfo', 'rb') as f: cpuinfo_data = f.read() fake_file = io.BytesIO(cpuinfo_data) - with mock.patch('psutil._pslinux.open', + with mock.patch('psutil._common.open', return_value=fake_file, create=True) as m: self.assertEqual(psutil._pslinux.cpu_count_logical(), original) @@ -675,7 +675,7 @@ class TestSystemCPU(unittest.TestCase): def test_cpu_count_physical_mocked(self): # Have open() return emtpy data and make sure None is returned # ('cause we want to mimick os.cpu_count()) - with mock.patch('psutil._pslinux.open', create=True) as m: + with mock.patch('psutil._common.open', create=True) as m: self.assertIsNone(psutil._pslinux.cpu_count_physical()) assert m.called @@ -971,7 +971,7 @@ class TestSystemDisks(unittest.TestCase): else: # No ZFS partitions on this system. Let's fake one. fake_file = io.StringIO(u("nodev\tzfs\n")) - with mock.patch('psutil._pslinux.open', + with mock.patch('psutil._common.open', return_value=fake_file, create=True) as m1: with mock.patch( 'psutil._pslinux.cext.disk_partitions', @@ -1232,7 +1232,7 @@ class TestMisc(unittest.TestCase): self.assertNotEqual(cpu_times_percent.user, 0) def test_boot_time_mocked(self): - with mock.patch('psutil._pslinux.open', create=True) as m: + with mock.patch('psutil._common.open', create=True) as m: self.assertRaises( RuntimeError, psutil._pslinux.boot_time) @@ -1679,7 +1679,7 @@ class TestProcess(unittest.TestCase): # TODO: re-enable this test. # def test_num_ctx_switches_mocked(self): - # with mock.patch('psutil._pslinux.open', create=True) as m: + # with mock.patch('psutil._common.open', create=True) as m: # self.assertRaises( # NotImplementedError, # psutil._pslinux.Process(os.getpid()).num_ctx_switches) @@ -1689,12 +1689,12 @@ class TestProcess(unittest.TestCase): # see: https://github.com/giampaolo/psutil/issues/639 p = psutil.Process() fake_file = io.StringIO(u('foo\x00bar\x00')) - with mock.patch('psutil._pslinux.open', + with mock.patch('psutil._common.open', return_value=fake_file, create=True) as m: self.assertEqual(p.cmdline(), ['foo', 'bar']) assert m.called fake_file = io.StringIO(u('foo\x00bar\x00\x00')) - with mock.patch('psutil._pslinux.open', + with mock.patch('psutil._common.open', return_value=fake_file, create=True) as m: self.assertEqual(p.cmdline(), ['foo', 'bar', '']) assert m.called @@ -1703,12 +1703,12 @@ class TestProcess(unittest.TestCase): # see: https://github.com/giampaolo/psutil/issues/1179 p = psutil.Process() fake_file = io.StringIO(u('foo bar ')) - with mock.patch('psutil._pslinux.open', + with mock.patch('psutil._common.open', return_value=fake_file, create=True) as m: self.assertEqual(p.cmdline(), ['foo', 'bar']) assert m.called fake_file = io.StringIO(u('foo bar ')) - with mock.patch('psutil._pslinux.open', + with mock.patch('psutil._common.open', return_value=fake_file, create=True) as m: self.assertEqual(p.cmdline(), ['foo', 'bar', '']) assert m.called |