| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4489dc3 commit dd78c51
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,7 @@ | |||
| 26 | 26 | os.path.join(SOURCE_DIR, 'extension.cpp'), | |
| 27 | 27 | os.path.join(SOURCE_DIR, 'setup.py'), | |
| 28 | 28 | ] | |
| 29 | + RUNTESTS_PY = os.path.join(SOURCE_DIR, 'runtests.py') | ||
| 29 | 30 | MSVC = support.MS_WINDOWS | |
| 30 | 31 | ||
| 31 | 32 | ||
@@ -97,21 +98,10 @@ def run_cmd(operation, cmd): | |||
| 97 | 98 | cmd.append('-v') | |
| 98 | 99 | run_cmd('Install', cmd) | |
| 99 | 100 | ||
| 100 | - # Do a reference run. Until we test that running python | ||
| 101 | - # doesn't leak references (gh-94755), run it so one can manually check | ||
| 102 | - # -X showrefcount results against this baseline. | ||
| 103 | - cmd = [python_exe, | ||
| 104 | - '-X', 'dev', | ||
| 105 | - '-X', 'showrefcount', | ||
| 106 | - '-c', 'pass'] | ||
| 107 | - run_cmd('Reference run', cmd) | ||
| 108 | - | ||
| 109 | - # Import the C/C++ extension | ||
| 110 | - cmd = [python_exe, | ||
| 111 | - '-X', 'dev', | ||
| 112 | - '-X', 'showrefcount', | ||
| 113 | - '-c', f"import {extension_name}"] | ||
| 114 | - run_cmd('Import', cmd) | ||
| 101 | + # Import the extension module and run tests. | ||
| 102 | + # On a debug build, check also for reference leaks. | ||
| 103 | + cmd = [python_exe, '-X', 'dev', RUNTESTS_PY, extension_name] | ||
| 104 | + run_cmd('Tests', cmd) | ||
| 115 | 105 | ||
| 116 | 106 | ||
| 117 | 107 | class TestPublicC(BaseTests, unittest.TestCase): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -130,9 +130,11 @@ test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | |||
| 130 | 130 | { | |
| 131 | 131 | // datetime.h is excluded from the limited C API | |
| 132 | 132 | #ifndef Py_LIMITED_API | |
| 133 | - PyDateTime_IMPORT; | ||
| 134 | - if (PyErr_Occurred()) { | ||
| 135 | - return NULL; | ||
| 133 | + if (PyDateTimeAPI == NULL) { | ||
| 134 | + PyDateTime_IMPORT; | ||
| 135 | + if (PyErr_Occurred()) { | ||
| 136 | + return NULL; | ||
| 137 | + } | ||
| 136 | 138 | } | |
| 137 | 139 | #endif | |
| 138 | 140 | ||
@@ -365,8 +367,6 @@ static PyMethodDef module_methods[] = { | |||
| 365 | 367 | static int | |
| 366 | 368 | module_exec(PyObject *module) | |
| 367 | 369 | { | |
| 368 | - PyObject *result; | ||
| 369 | - | ||
| 370 | 370 | #ifdef __STDC_VERSION__ | |
| 371 | 371 | if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) { | |
| 372 | 372 | return -1; | |
@@ -387,31 +387,8 @@ module_exec(PyObject *module) | |||
| 387 | 387 | return -1; | |
| 388 | 388 | } | |
| 389 | 389 | #endif | |
| 390 | - | ||
| 391 | - result = PyObject_CallMethod(module, "test_macros", ""); | ||
| 392 | - if (!result) return -1; | ||
| 393 | - Py_DECREF(result); | ||
| 394 | - | ||
| 395 | - result = PyObject_CallMethod(module, "test_datetime", ""); | ||
| 396 | - if (!result) return -1; | ||
| 397 | - Py_DECREF(result); | ||
| 398 | - | ||
| 399 | - result = PyObject_CallMethod(module, "test_unicode", ""); | ||
| 400 | - if (!result) return -1; | ||
| 401 | - Py_DECREF(result); | ||
| 402 | - | ||
| 403 | - #ifdef __cplusplus | ||
| 404 | - result = PyObject_CallMethod(module, "test_api_casts", ""); | ||
| 405 | - if (!result) return -1; | ||
| 406 | - Py_DECREF(result); | ||
| 407 | - #endif | ||
| 408 | - | ||
| 409 | - #if defined(__cplusplus) && !defined(Py_TARGET_ABI3T) | ||
| 410 | - result = PyObject_CallMethod(module, "test_virtual_object", ""); | ||
| 411 | - if (!result) return -1; | ||
| 412 | - Py_DECREF(result); | ||
| 413 | - #endif | ||
| 414 | - | ||
| 390 | + // Ignore "unused argument" warning when none of these macros is defined | ||
| 391 | + (void)module; | ||
| 415 | 392 | return 0; | |
| 416 | 393 | } | |
| 417 | 394 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,68 @@ | |||
| 1 | + import array | ||
| 2 | + import gc | ||
| 3 | + import importlib | ||
| 4 | + import sys | ||
| 5 | + | ||
| 6 | + | ||
| 7 | + def run_tests(testmod, verbose=True): | ||
| 8 | + for name in dir(testmod): | ||
| 9 | + if not name.startswith('test'): | ||
| 10 | + continue | ||
| 11 | + func = getattr(testmod, name) | ||
| 12 | + print(f"{name}()") | ||
| 13 | + func() | ||
| 14 | + | ||
| 15 | + print("add()") | ||
| 16 | + if testmod.add(11, 23) != 34: | ||
| 17 | + raise AssertionError("add() failed badly") | ||
| 18 | + | ||
| 19 | + print(flush=True) | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + def main(): | ||
| 23 | + if len(sys.argv) < 2: | ||
| 24 | + print("usage: python runtests.py TEST_MODULE_NAME") | ||
| 25 | + sys.exit(1) | ||
| 26 | + module_name = sys.argv[1] | ||
| 27 | + | ||
| 28 | + testmod = importlib.import_module(module_name) | ||
| 29 | + | ||
| 30 | + newline = False | ||
| 31 | + for name in ( | ||
| 32 | + '__STDC_VERSION__', | ||
| 33 | + '__STRICT_ANSI__', | ||
| 34 | + '__cplusplus', | ||
| 35 | + '_MSVC_LANG', | ||
| 36 | + ): | ||
| 37 | + try: | ||
| 38 | + value = getattr(testmod, name) | ||
| 39 | + except AttributeError: | ||
| 40 | + pass | ||
| 41 | + else: | ||
| 42 | + print(f'{name}: {value}') | ||
| 43 | + newline = True | ||
| 44 | + if newline: | ||
| 45 | + print() | ||
| 46 | + | ||
| 47 | + if hasattr(sys, 'gettotalrefcount'): | ||
| 48 | + # First run to warm up Python. For example, test_datetime() imports | ||
| 49 | + # the datetime module. | ||
| 50 | + run_tests(testmod, verbose=False) | ||
| 51 | + | ||
| 52 | + refcount = array.array('q', [0, 0]) | ||
| 53 | + gc.collect() | ||
| 54 | + | ||
| 55 | + # Check for reference leak | ||
| 56 | + refcount[0] = sys.gettotalrefcount() | ||
| 57 | + run_tests(testmod) | ||
| 58 | + refcount[1] = sys.gettotalrefcount() | ||
| 59 | + | ||
| 60 | + diff = refcount[1] - refcount[0] | ||
| 61 | + if diff >= 1: | ||
| 62 | + raise AssertionError(f'Tests leaked {diff} references') | ||
| 63 | + else: | ||
| 64 | + run_tests(testmod) | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + if __name__ == "__main__": | ||
| 68 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,8 @@ | |||
| 21 | 21 | if not MSVC: | |
| 22 | 22 | # C compiler flags for GCC and clang | |
| 23 | 23 | BASE_CFLAGS = [ | |
| 24 | + '-Wall', | ||
| 25 | + '-Wextra', | ||
| 24 | 26 | # The purpose of test_cext extension is to check that building a C | |
| 25 | 27 | # extension using the Python C API does not emit C compiler warnings. | |
| 26 | 28 | '-Werror', | |
@@ -42,7 +44,11 @@ | |||
| 42 | 44 | # with the -Werror=declaration-after-statement compiler flag. | |
| 43 | 45 | '-Werror=declaration-after-statement', | |
| 44 | 46 | ) | |
| 45 | - INTERNAL_CFLAGS = [*BASE_CFLAGS] | ||
| 47 | + INTERNAL_CFLAGS = [ | ||
| 48 | + *BASE_CFLAGS, | ||
| 49 | + # Do not complain about unused parameteres in the internal C API | ||
| 50 | + '-Wno-unused-parameter', | ||
| 51 | + ] | ||
| 46 | 52 | else: | |
| 47 | 53 | # MSVC compiler flags | |
| 48 | 54 | BASE_CFLAGS = [ | |
@@ -65,14 +71,16 @@ | |||
| 65 | 71 | ||
| 66 | 72 | if not MSVC: | |
| 67 | 73 | # C++ compiler flags for GCC and clang | |
| 68 | - CPPFLAGS = [ | ||
| 74 | + CXXFLAGS = [ | ||
| 75 | + '-Wall', | ||
| 76 | + '-Wextra', | ||
| 69 | 77 | # gh-91321: The purpose of _testcppext extension is to check that building | |
| 70 | 78 | # a C++ extension using the Python C API does not emit C++ compiler | |
| 71 | 79 | # warnings | |
| 72 | 80 | '-Werror', | |
| 73 | 81 | ] | |
| 74 | 82 | ||
| 75 | - CPPFLAGS_PEDANTIC = [ | ||
| 83 | + PEDANTIC_CXXFLAGS = [ | ||
| 76 | 84 | # Ask for strict(er) compliance with the standard. | |
| 77 | 85 | # We cannot do this for c++03 unlimited API, since several headers in | |
| 78 | 86 | # Include/cpython/ use commas at end of `enum` declarations, a C++11 | |
@@ -82,15 +90,20 @@ | |||
| 82 | 90 | # We also use `long long`, a C++11 feature we can enable individually. | |
| 83 | 91 | '-Wno-long-long', | |
| 84 | 92 | ] | |
| 93 | + INTERNAL_CXXFLAGS = [ | ||
| 94 | + # Do not complain about unused parameteres in the internal C API | ||
| 95 | + '-Wno-unused-parameter', | ||
| 96 | + ] | ||
| 85 | 97 | else: | |
| 86 | 98 | # MSVC compiler flags | |
| 87 | - CPPFLAGS = [ | ||
| 99 | + CXXFLAGS = [ | ||
| 88 | 100 | # Display warnings level 1 to 4 | |
| 89 | 101 | '/W4', | |
| 90 | 102 | # Treat all compiler warnings as compiler errors | |
| 91 | 103 | '/WX', | |
| 92 | 104 | ] | |
| 93 | - CPPFLAGS_PEDANTIC = [] | ||
| 105 | + PEDANTIC_CXXFLAGS = [] | ||
| 106 | + INTERNAL_CXXFLAGS = [] | ||
| 94 | 107 | ||
| 95 | 108 | ||
| 96 | 109 | def main(): | |
@@ -103,10 +116,13 @@ def main(): | |||
| 103 | 116 | libdirs = os.environ.get("CPYTHON_TEST_EXTRA_LIBDIRS", "") | |
| 104 | 117 | extra_cflags = os.environ.get("CPYTHON_TEST_EXTRA_CFLAGS", "") | |
| 105 | 118 | ||
| 119 | + if language not in ('C', 'C++'): | ||
| 120 | + raise ValueError(f"invalid language: {language}") | ||
| 121 | + | ||
| 106 | 122 | source = SOURCE[language] | |
| 107 | 123 | ||
| 108 | 124 | if language == 'C++': | |
| 109 | - flags = list(CPPFLAGS) | ||
| 125 | + flags = list(CXXFLAGS) | ||
| 110 | 126 | else: | |
| 111 | 127 | if not internal: | |
| 112 | 128 | flags = list(PUBLIC_CFLAGS) | |
@@ -121,9 +137,13 @@ def main(): | |||
| 121 | 137 | else: | |
| 122 | 138 | flags.append(f'-std={std}') | |
| 123 | 139 | ||
| 124 | - if language == 'C++' and (limited or (std != 'c++03') and not internal): | ||
| 125 | - # See CPPFLAGS_PEDANTIC docstring | ||
| 126 | - flags.extend(CPPFLAGS_PEDANTIC) | ||
| 140 | + if language == 'C++': | ||
| 141 | + if limited or (std != 'c++03') and not internal: | ||
| 142 | + # See PEDANTIC_CXXFLAGS comment | ||
| 143 | + flags.extend(PEDANTIC_CXXFLAGS) | ||
| 144 | + elif internal: | ||
| 145 | + flags.extend(INTERNAL_CXXFLAGS) | ||
| 146 | + | ||
| 127 | 147 | ||
| 128 | 148 | # gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11 | |
| 129 | 149 | # option emits a C++ compiler warning. Remove "-std11" option from the | |
| Back | FazBrowse Home | New Git URL |
0 commit comments