本文整理汇总了Python中psutil._common方法的典型用法代码示例。如果您正苦于以下问题:Python psutil._common方法的具体用法?Python psutil._common怎么用?Python psutil._common使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil
的用法示例。
在下文中一共展示了psutil._common方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_environ_block
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_parse_environ_block(self):
from psutil._common import parse_environ_block
def k(s):
return s.upper() if WINDOWS else s
self.assertEqual(parse_environ_block("a=1\0"),
{k("a"): "1"})
self.assertEqual(parse_environ_block("a=1\0b=2\0\0"),
{k("a"): "1", k("b"): "2"})
self.assertEqual(parse_environ_block("a=1\0b=\0\0"),
{k("a"): "1", k("b"): ""})
# ignore everything after \0\0
self.assertEqual(parse_environ_block("a=1\0b=2\0\0c=3\0"),
{k("a"): "1", k("b"): "2"})
# ignore everything that is not an assignment
self.assertEqual(parse_environ_block("xxx\0a=1\0"), {k("a"): "1"})
self.assertEqual(parse_environ_block("a=1\0=b=2\0"), {k("a"): "1"})
# do not fail if the block is incomplete
self.assertEqual(parse_environ_block("a=1\0b=2"), {k("a"): "1"})
示例2: test_isfile_strict
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_isfile_strict(self):
from psutil._common import isfile_strict
this_file = os.path.abspath(__file__)
assert isfile_strict(this_file)
assert not isfile_strict(os.path.dirname(this_file))
with mock.patch('psutil._common.os.stat',
side_effect=OSError(errno.EPERM, "foo")):
self.assertRaises(OSError, isfile_strict, this_file)
with mock.patch('psutil._common.os.stat',
side_effect=OSError(errno.EACCES, "foo")):
self.assertRaises(OSError, isfile_strict, this_file)
with mock.patch('psutil._common.os.stat',
side_effect=OSError(errno.EINVAL, "foo")):
assert not isfile_strict(this_file)
with mock.patch('psutil._common.stat.S_ISREG', return_value=False):
assert not isfile_strict(this_file)
示例3: test_it
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_it(self):
def check(cons, families, types_):
AF_UNIX = getattr(socket, 'AF_UNIX', object())
for conn in cons:
self.assertIn(conn.family, families, msg=conn)
if conn.family != AF_UNIX:
self.assertIn(conn.type, types_, msg=conn)
check_connection_ntuple(conn)
with create_sockets():
from psutil._common import conn_tmap
for kind, groups in conn_tmap.items():
# XXX: SunOS does not retrieve UNIX sockets.
if kind == 'unix' and not HAS_CONNECTIONS_UNIX:
continue
families, types_ = groups
cons = psutil.net_connections(kind)
self.assertEqual(len(cons), len(set(cons)))
check(cons, families, types_)
self.assertRaises(ValueError, psutil.net_connections, kind='???')
示例4: test_isfile_strict
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_isfile_strict(self):
from psutil._common import isfile_strict
this_file = os.path.abspath(__file__)
assert isfile_strict(this_file)
assert not isfile_strict(os.path.dirname(this_file))
with mock.patch('psutil._common.os.stat',
side_effect=OSError(errno.EPERM, "foo")):
self.assertRaises(OSError, isfile_strict, this_file)
with mock.patch('psutil._common.os.stat',
side_effect=OSError(errno.EACCES, "foo")):
self.assertRaises(OSError, isfile_strict, this_file)
with mock.patch('psutil._common.os.stat',
side_effect=OSError(errno.ENOENT, "foo")):
assert not isfile_strict(this_file)
with mock.patch('psutil._common.stat.S_ISREG', return_value=False):
assert not isfile_strict(this_file)
示例5: test_it
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_it(self):
def check(cons, families, types_):
for conn in cons:
self.assertIn(conn.family, families, msg=conn)
if conn.family != AF_UNIX:
self.assertIn(conn.type, types_, msg=conn)
self.check_connection_ntuple(conn)
with create_sockets():
from psutil._common import conn_tmap
for kind, groups in conn_tmap.items():
# XXX: SunOS does not retrieve UNIX sockets.
if kind == 'unix' and not HAS_CONNECTIONS_UNIX:
continue
families, types_ = groups
cons = psutil.net_connections(kind)
self.assertEqual(len(cons), len(set(cons)))
check(cons, families, types_)
# See: https://travis-ci.org/giampaolo/psutil/jobs/237566297
示例6: test_supports_ipv6
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_supports_ipv6(self):
self.addCleanup(supports_ipv6.cache_clear)
if supports_ipv6():
with mock.patch('psutil._common.socket') as s:
s.has_ipv6 = False
supports_ipv6.cache_clear()
assert not supports_ipv6()
supports_ipv6.cache_clear()
with mock.patch('psutil._common.socket.socket',
side_effect=socket.error) as s:
assert not supports_ipv6()
assert s.called
supports_ipv6.cache_clear()
with mock.patch('psutil._common.socket.socket',
side_effect=socket.gaierror) as s:
assert not supports_ipv6()
supports_ipv6.cache_clear()
assert s.called
supports_ipv6.cache_clear()
with mock.patch('psutil._common.socket.socket.bind',
side_effect=socket.gaierror) as s:
assert not supports_ipv6()
supports_ipv6.cache_clear()
assert s.called
else:
with self.assertRaises(Exception):
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.bind(("::1", 0))
示例7: test_supports_ipv6
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import _common [as 别名]
def test_supports_ipv6(self):
self.addCleanup(supports_ipv6.cache_clear)
if supports_ipv6():
with mock.patch('psutil._common.socket') as s:
s.has_ipv6 = False
supports_ipv6.cache_clear()
assert not supports_ipv6()
supports_ipv6.cache_clear()
with mock.patch('psutil._common.socket.socket',
side_effect=socket.error) as s:
assert not supports_ipv6()
assert s.called
supports_ipv6.cache_clear()
with mock.patch('psutil._common.socket.socket',
side_effect=socket.gaierror) as s:
assert not supports_ipv6()
supports_ipv6.cache_clear()
assert s.called
supports_ipv6.cache_clear()
with mock.patch('psutil._common.socket.socket.bind',
side_effect=socket.gaierror) as s:
assert not supports_ipv6()
supports_ipv6.cache_clear()
assert s.called
else:
with self.assertRaises(Exception):
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
try:
sock.bind(("::1", 0))
finally:
sock.close()