diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-03-21 14:04:43 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-03-21 14:04:43 (GMT) |
commit | efde146b0c42f2643f96d00896c99a90d501fb69 (patch) | |
tree | ccb5f7484b0c55bf005ec9b5eb80558d8adb7e46 /Lib | |
parent | 6921c13bbbb2c9695edd87331d98f7aa1e48f7f2 (diff) | |
download | cpython-efde146b0c42f2643f96d00896c99a90d501fb69.zip cpython-efde146b0c42f2643f96d00896c99a90d501fb69.tar.gz cpython-efde146b0c42f2643f96d00896c99a90d501fb69.tar.bz2 |
Issue #23571: _Py_CheckFunctionResult() now gives the name of the function
which returned an invalid result (result+error or no result without error) in
the exception message.
Add also unit test to check that the exception contains the name of the
function.
Special case: the final _PyEval_EvalFrameEx() check doesn't mention the
function since it didn't execute a single function but a whole frame.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_capi.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index de8d65a..ef6e94b 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -6,10 +6,12 @@ import pickle import random import subprocess import sys +import textwrap import time import unittest from test import support from test.support import MISSING_C_DOCSTRINGS +from test.script_helper import assert_python_failure try: import _posixsubprocess except ImportError: @@ -21,6 +23,9 @@ except ImportError: # Skip this test if the _testcapi module isn't available. _testcapi = support.import_module('_testcapi') +# Were we compiled --with-pydebug or with #define Py_DEBUG? +Py_DEBUG = hasattr(sys, 'gettotalrefcount') + def testfunction(self): """some doc""" @@ -167,6 +172,45 @@ class CAPITest(unittest.TestCase): o @= m1 self.assertEqual(o, ("matmul", 42, m1)) + def test_return_null_without_error(self): + # Issue #23571: A function must not return NULL without setting an + # error + if Py_DEBUG: + code = textwrap.dedent(""" + import _testcapi + from test import support + + with support.SuppressCrashReport(): + _testcapi.return_null_without_error() + """) + rc, out, err = assert_python_failure('-c', code) + self.assertIn(b'_Py_CheckFunctionResult: Assertion', err) + else: + with self.assertRaises(SystemError) as cm: + _testcapi.return_null_without_error() + self.assertRegex(str(cm.exception), + 'return_null_without_error.* ' + 'returned NULL without setting an error') + + def test_return_result_with_error(self): + # Issue #23571: A function must not return a result with an error set + if Py_DEBUG: + code = textwrap.dedent(""" + import _testcapi + from test import support + + with support.SuppressCrashReport(): + _testcapi.return_result_with_error() + """) + rc, out, err = assert_python_failure('-c', code) + self.assertIn(b'_Py_CheckFunctionResult: Assertion', err) + else: + with self.assertRaises(SystemError) as cm: + _testcapi.return_result_with_error() + self.assertRegex(str(cm.exception), + 'return_result_with_error.* ' + 'returned a result with an error set') + @unittest.skipUnless(threading, 'Threading required for this test.') class TestPendingCalls(unittest.TestCase): |