| OLD | NEW |
|---|---|
| 1 """Supporting definitions for the Python regression tests.""" | 1 """Supporting definitions for the Python regression tests.""" |
| 2 | 2 |
| 3 if __name__ != 'test.test_support': | 3 if __name__ != 'test.test_support': |
| 4 raise ImportError('test_support must be imported from the test package') | 4 raise ImportError('test_support must be imported from the test package') |
| 5 | 5 |
| 6 import contextlib | 6 import contextlib |
| 7 import errno | 7 import errno |
| 8 import socket | 8 import socket |
| 9 import sys | 9 import sys |
| 10 import os | 10 import os |
| 11 import shutil | 11 import shutil |
| 12 import warnings | 12 import warnings |
| 13 import unittest | 13 import unittest |
| 14 | 14 |
| 15 __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul e", | 15 __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_modul e", |
| 16 "verbose", "use_resources", "max_memuse", "record_original_stdout", | 16 "verbose", "use_resources", "max_memuse", "record_original_stdout", |
| 17 "get_original_stdout", "unload", "unlink", "rmtree", "forget", | 17 "get_original_stdout", "unload", "unlink", "rmtree", "forget", |
| 18 "is_resource_enabled", "requires", "find_unused_port", "bind_port", | 18 "is_resource_enabled", "requires", "find_unused_port", "bind_port", |
| 19 "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ", | 19 "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ", |
| 20 "findfile", "verify", "vereq", "sortdict", "check_syntax_error", | 20 "findfile", "verify", "vereq", "sortdict", "check_syntax_error", |
| 21 "open_urlresource", "WarningMessage", "catch_warning", "CleanImport", | 21 "open_urlresource", "catch_warning", "CleanImport", |
| 22 "EnvironmentVarGuard", "captured_output", | 22 "EnvironmentVarGuard", "captured_output", |
| 23 "captured_stdout", "TransientResource", "transient_internet", | 23 "captured_stdout", "TransientResource", "transient_internet", |
| 24 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", | 24 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", |
| 25 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", | 25 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", |
| 26 "threading_cleanup", "reap_children"] | 26 "threading_cleanup", "reap_children"] |
| 27 | 27 |
| 28 class Error(Exception): | 28 class Error(Exception): |
| 29 """Base class for regression test exceptions.""" | 29 """Base class for regression test exceptions.""" |
| 30 | 30 |
| 31 class TestFailed(Error): | 31 class TestFailed(Error): |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 for path in [os.path.curdir, os.path.pardir]: | 374 for path in [os.path.curdir, os.path.pardir]: |
| 375 fn = os.path.join(path, filename) | 375 fn = os.path.join(path, filename) |
| 376 if os.path.exists(fn): | 376 if os.path.exists(fn): |
| 377 return open(fn) | 377 return open(fn) |
| 378 | 378 |
| 379 print >> get_original_stdout(), '\tfetching %s ...' % url | 379 print >> get_original_stdout(), '\tfetching %s ...' % url |
| 380 fn, _ = urllib.urlretrieve(url, filename) | 380 fn, _ = urllib.urlretrieve(url, filename) |
| 381 return open(fn) | 381 return open(fn) |
| 382 | 382 |
| 383 | 383 |
| 384 class WarningMessage(object): | |
| 385 "Holds the result of a single showwarning() call" | |
| 386 _WARNING_DETAILS = "message category filename lineno line".split() | |
| 387 def __init__(self, message, category, filename, lineno, line=None): | |
| 388 for attr in self._WARNING_DETAILS: | |
| 389 setattr(self, attr, locals()[attr]) | |
| 390 self._category_name = category.__name__ if category else None | |
| 391 | |
| 392 def __str__(self): | |
| 393 return ("{message : %r, category : %r, filename : %r, lineno : %s, " | |
| 394 "line : %r}" % (self.message, self._category_name, | |
| 395 self.filename, self.lineno, self.line)) | |
| 396 | |
| 397 class WarningRecorder(object): | |
| 398 "Records the result of any showwarning calls" | |
| 399 def __init__(self): | |
| 400 self.warnings = [] | |
| 401 self._set_last(None) | |
| 402 | |
| 403 def _showwarning(self, message, category, filename, lineno, | |
| 404 file=None, line=None): | |
| 405 wm = WarningMessage(message, category, filename, lineno, line) | |
| 406 self.warnings.append(wm) | |
| 407 self._set_last(wm) | |
| 408 | |
| 409 def _set_last(self, last_warning): | |
| 410 if last_warning is None: | |
| 411 for attr in WarningMessage._WARNING_DETAILS: | |
| 412 setattr(self, attr, None) | |
| 413 else: | |
| 414 for attr in WarningMessage._WARNING_DETAILS: | |
| 415 setattr(self, attr, getattr(last_warning, attr)) | |
| 416 | |
| 417 def reset(self): | |
| 418 self.warnings = [] | |
| 419 self._set_last(None) | |
| 420 | |
| 421 def __str__(self): | |
| 422 return '[%s]' % (', '.join(map(str, self.warnings))) | |
| 423 | |
| 424 @contextlib.contextmanager | |
| 425 def catch_warning(module=warnings, record=True): | 384 def catch_warning(module=warnings, record=True): |
| 426 """Guard the warnings filter from being permanently changed and | 385 return warnings.catch_warnings(record=record, module=module) |
|
Benjamin
2008/08/23 01:46:36
Why not just do "from warnings import catch_warnin
brett.cannon
2008/08/23 02:19:54
On 2008/08/23 01:46:36, Benjamin wrote:
> Why not
| |
| 427 optionally record the details of any warnings that are issued. | |
| 428 | |
| 429 Use like this: | |
| 430 | |
| 431 with catch_warning() as w: | |
| 432 warnings.warn("foo") | |
| 433 assert str(w.message) == "foo" | |
| 434 """ | |
| 435 original_filters = module.filters | |
| 436 original_showwarning = module.showwarning | |
| 437 if record: | |
| 438 recorder = WarningRecorder() | |
| 439 module.showwarning = recorder._showwarning | |
| 440 else: | |
| 441 recorder = None | |
| 442 try: | |
| 443 # Replace the filters with a copy of the original | |
| 444 module.filters = module.filters[:] | |
| 445 yield recorder | |
| 446 finally: | |
| 447 module.showwarning = original_showwarning | |
| 448 module.filters = original_filters | |
| 449 | 386 |
| 450 | 387 |
| 451 class CleanImport(object): | 388 class CleanImport(object): |
| 452 """Context manager to force import to return a new module reference. | 389 """Context manager to force import to return a new module reference. |
| 453 | 390 |
| 454 This is useful for testing module-level behaviours, such as | 391 This is useful for testing module-level behaviours, such as |
| 455 the emission of a DeprecationWarning on import. | 392 the emission of a DeprecationWarning on import. |
| 456 | 393 |
| 457 Use like this: | 394 Use like this: |
| 458 | 395 |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 820 if hasattr(os, 'waitpid'): | 757 if hasattr(os, 'waitpid'): |
| 821 any_process = -1 | 758 any_process = -1 |
| 822 while True: | 759 while True: |
| 823 try: | 760 try: |
| 824 # This will raise an exception on Windows. That's ok. | 761 # This will raise an exception on Windows. That's ok. |
| 825 pid, status = os.waitpid(any_process, os.WNOHANG) | 762 pid, status = os.waitpid(any_process, os.WNOHANG) |
| 826 if pid == 0: | 763 if pid == 0: |
| 827 break | 764 break |
| 828 except: | 765 except: |
| 829 break | 766 break |
| OLD | NEW |