本文整理汇总了Python中test.support.check_warnings方法的典型用法代码示例。如果您正苦于以下问题:Python support.check_warnings方法的具体用法?Python support.check_warnings怎么用?Python support.check_warnings使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test.support
的用法示例。
在下文中一共展示了support.check_warnings方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_temp_dir__existing_dir__quiet_true
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_temp_dir__existing_dir__quiet_true(self):
"""Test passing a directory that already exists with quiet=True."""
path = tempfile.mkdtemp()
path = os.path.realpath(path)
try:
with support.check_warnings() as recorder:
with support.temp_dir(path, quiet=True) as temp_path:
self.assertEqual(path, temp_path)
warnings = [str(w.message) for w in recorder.warnings]
# Make sure temp_dir did not delete the original directory.
self.assertTrue(os.path.isdir(path))
finally:
shutil.rmtree(path)
expected = ['tests may fail, unable to create temp dir: ' + path]
self.assertEqual(warnings, expected)
示例2: test_change_cwd__non_existent_dir__quiet_true
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_change_cwd__non_existent_dir__quiet_true(self):
"""Test passing a non-existent directory with quiet=True."""
original_cwd = os.getcwd()
with support.temp_dir() as parent_dir:
bad_dir = os.path.join(parent_dir, 'does_not_exist')
with support.check_warnings() as recorder:
with support.change_cwd(bad_dir, quiet=True) as new_cwd:
self.assertEqual(new_cwd, original_cwd)
self.assertEqual(os.getcwd(), new_cwd)
warnings = [str(w.message) for w in recorder.warnings]
expected = ['tests may fail, unable to change CWD to: ' + bad_dir]
self.assertEqual(warnings, expected)
# Tests for change_cwd()
示例3: test_destructor
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_destructor(self):
record = []
class MyFileIO(self.FileIO):
def __del__(self):
record.append(1)
try:
f = super().__del__
except AttributeError:
pass
else:
f()
def close(self):
record.append(2)
super().close()
def flush(self):
record.append(3)
super().flush()
with support.check_warnings(('', ResourceWarning)):
f = MyFileIO(support.TESTFN, "wb")
f.write(b"xxx")
del f
support.gc_collect()
self.assertEqual(record, [1, 2, 3])
with self.open(support.TESTFN, "rb") as f:
self.assertEqual(f.read(), b"xxx")
示例4: test_garbage_collection
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_garbage_collection(self):
# C TextIOWrapper objects are collected, and collecting them flushes
# all data to disk.
# The Python version has __del__, so it ends in gc.garbage instead.
with support.check_warnings(('', ResourceWarning)):
rawio = io.FileIO(support.TESTFN, "wb")
b = self.BufferedWriter(rawio)
t = self.TextIOWrapper(b, encoding="ascii")
t.write("456def")
t.x = t
wr = weakref.ref(t)
del t
support.gc_collect()
self.assertIsNone(wr(), wr)
with self.open(support.TESTFN, "rb") as f:
self.assertEqual(f.read(), b"456def")
示例5: test_exec
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_exec(self):
g = {}
exec('z = 1', g)
if '__builtins__' in g:
del g['__builtins__']
self.assertEqual(g, {'z': 1})
exec('z = 1+1', g)
if '__builtins__' in g:
del g['__builtins__']
self.assertEqual(g, {'z': 2})
g = {}
l = {}
with check_warnings():
warnings.filterwarnings("ignore", "global statement",
module="<string>")
exec('global a; a = 1; b = 2', g, l)
if '__builtins__' in g:
del g['__builtins__']
if '__builtins__' in l:
del l['__builtins__']
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
示例6: test_temp_dir__existing_dir__quiet_true
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_temp_dir__existing_dir__quiet_true(self):
"""Test passing a directory that already exists with quiet=True."""
path = tempfile.mkdtemp()
path = os.path.realpath(path)
try:
with support.check_warnings() as recorder:
with support.temp_dir(path, quiet=True) as temp_path:
self.assertEqual(path, temp_path)
warnings = [str(w.message) for w in recorder.warnings]
# Make sure temp_dir did not delete the original directory.
self.assertTrue(os.path.isdir(path))
finally:
shutil.rmtree(path)
expected = ['tests may fail, unable to create temp dir: ' + path]
self.assertEqual(warnings, expected)
# Tests for change_cwd()
示例7: test_splitunc
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_splitunc(self):
with self.assertWarns(DeprecationWarning):
ntpath.splitunc('')
with support.check_warnings(('', DeprecationWarning)):
tester('ntpath.splitunc("c:\\foo\\bar")',
('', 'c:\\foo\\bar'))
tester('ntpath.splitunc("c:/foo/bar")',
('', 'c:/foo/bar'))
tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
('\\\\conky\\mountpoint', '\\foo\\bar'))
tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
('//conky/mountpoint', '/foo/bar'))
tester('ntpath.splitunc("\\\\\\conky\\mountpoint\\foo\\bar")',
('', '\\\\\\conky\\mountpoint\\foo\\bar'))
tester('ntpath.splitunc("///conky/mountpoint/foo/bar")',
('', '///conky/mountpoint/foo/bar'))
tester('ntpath.splitunc("\\\\conky\\\\mountpoint\\foo\\bar")',
('', '\\\\conky\\\\mountpoint\\foo\\bar'))
tester('ntpath.splitunc("//conky//mountpoint/foo/bar")',
('', '//conky//mountpoint/foo/bar'))
self.assertEqual(ntpath.splitunc('//conky/MOUNTPOİNT/foo/bar'),
('//conky/MOUNTPOİNT', '/foo/bar'))
示例8: urlopen
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def urlopen(url, data=None, proxies=None):
"""urlopen(url [, data]) -> open file-like object"""
global _urlopener
if proxies is not None:
opener = urllib.request.FancyURLopener(proxies=proxies)
elif not _urlopener:
with support.check_warnings(
('FancyURLopener style of invoking requests is deprecated.',
DeprecationWarning)):
opener = urllib.request.FancyURLopener()
_urlopener = opener
else:
opener = _urlopener
if data is None:
return opener.open(url)
else:
return opener.open(url, data)
示例9: test_resize
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_resize(self):
for length in range(1, 100, 7):
# generate a fresh string (refcount=1)
text = 'a' * length + 'b'
with support.check_warnings(('unicode_internal codec has been '
'deprecated', DeprecationWarning)):
# fill wstr internal field
abc = text.encode('unicode_internal')
self.assertEqual(abc.decode('unicode_internal'), text)
# resize text: wstr field must be cleared and then recomputed
text += 'c'
abcdef = text.encode('unicode_internal')
self.assertNotEqual(abc, abcdef)
self.assertEqual(abcdef.decode('unicode_internal'), text)
示例10: test_opening_mode
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_opening_mode(self):
try:
# invalid mode, should raise ValueError
fi = FileInput(mode="w")
self.fail("FileInput should reject invalid mode argument")
except ValueError:
pass
t1 = None
try:
# try opening in universal newline mode
t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
with check_warnings(('', DeprecationWarning)):
fi = FileInput(files=t1, mode="U")
with check_warnings(('', DeprecationWarning)):
lines = list(fi)
self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
finally:
remove_tempfiles(t1)
示例11: __init__
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def __init__(self, quiet=False):
if sys.flags.optimize >= 2:
# under -OO, doctests cannot be run and therefore not all warnings
# will be emitted
quiet = True
deprecations = (
# Search behaviour is broken if search path starts with "/".
("This search is broken in 1.3 and earlier, and will be fixed "
"in a future version. If you rely on the current behaviour, "
"change it to '.+'", FutureWarning),
# Element.getchildren() and Element.getiterator() are deprecated.
("This method will be removed in future versions. "
"Use .+ instead.", DeprecationWarning),
("This method will be removed in future versions. "
"Use .+ instead.", PendingDeprecationWarning))
self.checkwarnings = support.check_warnings(*deprecations, quiet=quiet)
示例12: test_join_errors
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_join_errors(self):
# Check join() raises friendly TypeErrors.
with support.check_warnings(('', BytesWarning), quiet=True):
errmsg = "Can't mix strings and bytes in path components"
with self.assertRaisesRegex(TypeError, errmsg):
self.pathmodule.join(b'bytes', 'str')
with self.assertRaisesRegex(TypeError, errmsg):
self.pathmodule.join('str', b'bytes')
# regression, see #15377
errmsg = r'join\(\) argument must be str or bytes, not %r'
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
self.pathmodule.join(42, 'str')
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
self.pathmodule.join('str', 42)
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
self.pathmodule.join(42)
with self.assertRaisesRegex(TypeError, errmsg % 'list'):
self.pathmodule.join([])
with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
示例13: test_relpath_errors
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_relpath_errors(self):
# Check relpath() raises friendly TypeErrors.
with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
quiet=True):
errmsg = "Can't mix strings and bytes in path components"
with self.assertRaisesRegex(TypeError, errmsg):
self.pathmodule.relpath(b'bytes', 'str')
with self.assertRaisesRegex(TypeError, errmsg):
self.pathmodule.relpath('str', b'bytes')
errmsg = r'relpath\(\) argument must be str or bytes, not %r'
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
self.pathmodule.relpath(42, 'str')
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
self.pathmodule.relpath('str', 42)
with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
示例14: assertOldResultWarning
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def assertOldResultWarning(self, test, failures):
with support.check_warnings(("TestResult has no add.+ method,",
RuntimeWarning)):
result = OldResult()
test.run(result)
self.assertEqual(len(result.failures), failures)
示例15: test_change_cwd__chdir_warning
# 需要导入模块: from test import support [as 别名]
# 或者: from test.support import check_warnings [as 别名]
def test_change_cwd__chdir_warning(self):
"""Check the warning message when os.chdir() fails."""
path = TESTFN + '_does_not_exist'
with support.check_warnings() as recorder:
with support.change_cwd(path=path, quiet=True):
pass
messages = [str(w.message) for w in recorder.warnings]
self.assertEqual(messages, ['tests may fail, unable to change CWD to: ' + path])
# Tests for temp_cwd()