Skip to content

Commit

Permalink
Treat warnings as errors on MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Oct 15, 2024
1 parent 6d8c17a commit 27d2204
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- os: windows-latest
python: 3.6
- os: windows-latest
python: 3.12
python: 3.13

# macOS: test only new Python
- os: macos-latest
Expand Down
4 changes: 2 additions & 2 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ static inline int PyUnicode_Equal(PyObject *str1, PyObject *str2)
}

#if PY_VERSION_HEX >= 0x030d0000 && !defined(PYPY_VERSION)
PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *str1, PyObject *str2);
extern int _PyUnicode_Equal(PyObject *str1, PyObject *str2);

return _PyUnicode_Equal(str1, str2);
#elif PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION)
Expand All @@ -1564,7 +1564,7 @@ static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
static inline Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)
{
#if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION)
PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void *src, Py_ssize_t len);
extern Py_hash_t _Py_HashBytes(const void *src, Py_ssize_t len);

return _Py_HashBytes(ptr, len);
#else
Expand Down
79 changes: 48 additions & 31 deletions tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,53 @@


# C++ is only supported on Python 3.6 and newer
TEST_CPP = (sys.version_info >= (3, 6))
TEST_CXX = (sys.version_info >= (3, 6))

SRC_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))

# Windows uses MSVC compiler
MSVC = (os.name == "nt")

# C compiler flags for GCC and clang
COMMON_FLAGS = [
# Treat warnings as error
'-Werror',
# Enable all warnings
'-Wall', '-Wextra',
# Extra warnings
'-Wconversion',
# /usr/lib64/pypy3.7/include/pyport.h:68:20: error: redefinition of typedef
# 'Py_hash_t' is a C11 feature
"-Wno-typedef-redefinition",
]
CFLAGS = COMMON_FLAGS + [
# Use C99 for pythoncapi_compat.c which initializes PyModuleDef with a
# mixture of designated and non-designated initializers
'-std=c99',
]
CPPFLAGS = list(COMMON_FLAGS)
# FIXME: _Py_CAST() emits C++ compilers on Python 3.12.
# See: https://github.com/python/cpython/issues/94731
if 0:
CPPFLAGS.extend((
'-Wold-style-cast',
'-Wzero-as-null-pointer-constant',
))
if not MSVC:
# C compiler flags for GCC and clang
COMMON_FLAGS = [
# Treat warnings as error
'-Werror',
# Enable all warnings
'-Wall', '-Wextra',
# Extra warnings
'-Wconversion',
# /usr/lib64/pypy3.7/include/pyport.h:68:20: error: redefinition of typedef
# 'Py_hash_t' is a C11 feature
"-Wno-typedef-redefinition",
]
CFLAGS = COMMON_FLAGS + [
# Use C99 for pythoncapi_compat.c which initializes PyModuleDef with a
# mixture of designated and non-designated initializers
'-std=c99',
]
CXXFLAGS = list(COMMON_FLAGS)
# FIXME: _Py_CAST() emits C++ compilers on Python 3.12.
# See: https://github.com/python/cpython/issues/94731
if 0:
CXXFLAGS.extend((
'-Wold-style-cast',
'-Wzero-as-null-pointer-constant',
))
else:
COMMON_FLAGS = [
# Treat all compiler warnings as compiler errors
'/WX',
]
if sys.version_info >= (3, 13):
COMMON_FLAGS.append(
# Display warnings level 1 to 4
'/W4'
)
CFLAGS = COMMON_FLAGS + [
'/std:c99'
]
CXXFLAGS = list(COMMON_FLAGS)


def main():
Expand All @@ -68,9 +83,8 @@ def main():

cflags = ['-I' + SRC_DIR]
cppflags = list(cflags)
if not MSVC:
cflags.extend(CFLAGS)
cppflags.extend(CPPFLAGS)
cflags.extend(CFLAGS)
cppflags.extend(CXXFLAGS)

# C extension
c_ext = Extension(
Expand All @@ -79,7 +93,7 @@ def main():
extra_compile_args=cflags)
extensions = [c_ext]

if TEST_CPP:
if TEST_CXX:
# C++ extension

# MSVC has /std flag but doesn't support /std:c++11
Expand All @@ -89,7 +103,10 @@ def main():
('test_pythoncapi_compat_cpp11ext', '-std=c++11'),
]
else:
versions = [('test_pythoncapi_compat_cppext', None)]
versions = [
('test_pythoncapi_compat_cpp03ext', '/std:c++03'),
('test_pythoncapi_compat_cpp11ext', '/std:c++11'),
]
for name, flag in versions:
flags = list(cppflags)
if flag is not None:
Expand Down

0 comments on commit 27d2204

Please sign in to comment.