diff -r df2fdd42b375 Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Mon Aug 26 22:28:21 2013 +0200 +++ b/Lib/test/support/__init__.py Mon Aug 26 16:04:26 2013 -0700 @@ -57,6 +57,11 @@ except ImportError: lzma = None +try: + import resource +except ImportError: + resource = None + __all__ = [ "Error", "TestFailed", "ResourceDenied", "import_module", "verbose", "use_resources", "max_memuse", "record_original_stdout", @@ -77,6 +82,7 @@ "skip_unless_xattr", "import_fresh_module", "requires_zlib", "PIPE_MAX_SIZE", "failfast", "anticipate_failure", "run_with_tz", "requires_gzip", "requires_bz2", "requires_lzma", "suppress_crash_popup", + "SuppressCoreFiles", ] class Error(Exception): @@ -2047,3 +2053,42 @@ # actually override the attribute setattr(object_to_patch, attr_name, new_value) + +class SuppressCoreFiles(object): + + """Try to prevent core files from being created.""" + old_limit = None + + def __enter__(self): + """Try to save previous ulimit, then set the soft limit to 0.""" + if resource is not None: + try: + self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) + resource.setrlimit(resource.RLIMIT_CORE, (0, self.old_limit[1])) + except (ValueError, OSError): + pass + if sys.platform == 'darwin': + # Check if the 'Crash Reporter' on OSX was configured + # in 'Developer' mode and warn that it will get triggered + # when it is. + # + # This assumes that this context manager is used in tests + # that might trigger the next manager. + value = subprocess.Popen(['/usr/bin/defaults', 'read', + 'com.apple.CrashReporter', 'DialogType'], + stdout=subprocess.PIPE).communicate()[0] + if value.strip() == b'developer': + print("this tests triggers the Crash Reporter, " + "that is intentional", end='') + sys.stdout.flush() + + def __exit__(self, *ignore_exc): + """Return core file behavior to default.""" + if self.old_limit is None: + return + if resource is not None: + try: + resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) + except (ValueError, OSError): + pass + diff -r df2fdd42b375 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Mon Aug 26 22:28:21 2013 +0200 +++ b/Lib/test/test_subprocess.py Mon Aug 26 16:04:26 2013 -0700 @@ -18,11 +18,6 @@ import gc import textwrap -try: - import resource -except ImportError: - resource = None - mswindows = (sys.platform == "win32") # @@ -1113,47 +1108,6 @@ fds_after_exception = os.listdir(fd_directory) self.assertEqual(fds_before_popen, fds_after_exception) - -# context manager -class _SuppressCoreFiles(object): - """Try to prevent core files from being created.""" - old_limit = None - - def __enter__(self): - """Try to save previous ulimit, then set it to (0, 0).""" - if resource is not None: - try: - self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) - resource.setrlimit(resource.RLIMIT_CORE, (0, self.old_limit[1])) - except (ValueError, resource.error): - pass - - if sys.platform == 'darwin': - # Check if the 'Crash Reporter' on OSX was configured - # in 'Developer' mode and warn that it will get triggered - # when it is. - # - # This assumes that this context manager is used in tests - # that might trigger the next manager. - value = subprocess.Popen(['/usr/bin/defaults', 'read', - 'com.apple.CrashReporter', 'DialogType'], - stdout=subprocess.PIPE).communicate()[0] - if value.strip() == b'developer': - print("this tests triggers the Crash Reporter, " - "that is intentional", end='') - sys.stdout.flush() - - def __exit__(self, *args): - """Return core file behavior to default.""" - if self.old_limit is None: - return - if resource is not None: - try: - resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) - except (ValueError, resource.error): - pass - - @unittest.skipIf(mswindows, "POSIX specific tests") class POSIXProcessTestCase(BaseTestCase): @@ -1242,7 +1196,7 @@ def test_run_abort(self): # returncode handles signal termination - with _SuppressCoreFiles(): + with support.SuppressCoreFiles(): p = subprocess.Popen([sys.executable, "-c", 'import os; os.abort()']) p.wait() diff -r df2fdd42b375 Lib/test/test_support.py --- a/Lib/test/test_support.py Mon Aug 26 22:28:21 2013 +0200 +++ b/Lib/test/test_support.py Mon Aug 26 16:04:26 2013 -0700 @@ -281,6 +281,16 @@ self.assertEqual(D["item"], 5) self.assertEqual(D["item"], 1) + @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") + def test_SuppressCoreFiles(self): + pid = os.fork() + if pid != 0: + pid, status = os.waitpid(pid, 0) + self.assertFalse(os.WCOREDUMP(status)) + else: + with support.SuppressCoreFiles(): + os.abort() + # XXX -follows a list of untested API # make_legacy_pyc # is_resource_enabled