Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Check whether Python.h is included first in a file #20536

Merged
merged 27 commits into from
Apr 23, 2024
Merged

Conversation

DWesl
Copy link
Contributor

@DWesl DWesl commented Apr 20, 2024

This makes some allowance for including headers that include python as well

Reference issue

#20149 proposed this CI job, #20491 motivated actually getting this working.

What does this implement/fix?

There is a script, adapted from #20149, to check whether a given file includes system headers before Python.h. This expands that to check headers before source files, and to check the source files for python constructs implying either an indirect path to Python.h or a new extension beyond numpy and pybind. This then adds a CI job to perform that check on the contents of the SciPy repository.

Additional information

I made a brief attempt to flag duplicate includes, but stopped when I noticed there were reasons for doing that (one header includes sys/time.h on every platform but Windows, another includes mutex under two orthogonal conditions).

A few of the changes aren't necessary to include Python.h first, but do make it easier to automate the checking process.

A NumPy maintainer mentioned it is possible to do something similar with clang-format, but I don't know how to do that.

@github-actions github-actions bot added scipy.special scipy.io scipy.optimize Build issues Issues with building from source, including different choices of architecture, compilers and OS CI Items related to the CI tools such as CircleCI, GitHub Actions or Azure C/C++ Items related to the internal C/C++ code base labels Apr 20, 2024
Copy link
Member

@j-bowhay j-bowhay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably just tack this onto the lint CI job rather than an entirely new job?

@DWesl
Copy link
Contributor Author

DWesl commented Apr 20, 2024

We could probably just tack this onto the lint CI job rather than an entirely new job?

That sounds sensible

@thalassemia
Copy link
Contributor

The Accelerate failure is on me. Should be resolved in gh-20542.

@DWesl
Copy link
Contributor Author

DWesl commented Apr 20, 2024

Wait, that include order actually was important? I could change the script to find and allow headers that included no other headers.

@thalassemia
Copy link
Contributor

It was important, but it should not have been haha. My PR should make it so that it no longer matters.

DWesl added 12 commits April 21, 2024 11:17
Based on script from scipy#20149.

Doesn't know about Boost::Python yet.

DEV: Print name of file check_python_h_first is checking

DEV: Add numpy/ndarrayobject.h to list of Python-including headers

One file includes this first.
BLD: Move python-including file earlier in io/_fast_matrix_market/src/_fmm_core.cpp

BLD: Move stdlib header after python-including headers in optimize/_pava/pava_pybind.cpp

BLD:BUG: Move python-including file to top of special/_special_ufuncs.cpp

DEV: Move python-including file earlier in wrap_g77_abi.c

Wasn't actually a problem, but this makes it easier for my script to check.

DEV: Move python including file earlier in wrap_dummy_g77_abi.c

Not actually a problem, but this makes it easier for my script to check.

STY: Remove duplicate headers at start of special/sf_error.cc
Hopefully this reduces false-positives.
It does include python first, and this will note that.
The file including the header should have already included Python.h (and whatever file for the specific constructs, or the compilation would fail everywhere)
This can cause errors if the header has no guard, or just slow things a bit if it does have a guard.

CI: Exclude boost_math and highs from header check.

Entirely too many false positives.

CI: Don't check fast_float

It seems to be a bunch of headers concatenated, which naturally results in many duplicate includes.
@rgommers
Copy link
Member

I couldn't reproduce the Accelerate job's build issue locally, so pushed a rebase on top of the now-merged gh-20542.

@rgommers rgommers added this to the 1.14.0 milestone Apr 21, 2024
@DWesl DWesl requested a review from peterbell10 as a code owner April 22, 2024 14:12
tools/lint.py Outdated Show resolved Hide resolved
tools/lint.py Outdated Show resolved Hide resolved
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @DWesl. I pushed a few tweaks (please check) and added a few review comments on the changes to headers.

@@ -2,7 +2,8 @@
// Use of this source code is governed by the BSD 2-clause license found in the LICENSE.txt file.
// SPDX-License-Identifier: BSD-2-Clause

#include <Python.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look correct, since there is direct usage of Python C API in this file. The linter also seems happy with the original version of the file. Unless I'm missing something here, I think you can revert this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_fmm_core.hpp is included next, and includes Python.h first, which the checker script knows. Either way would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the rationale for removing this where I kept the others was that I was the one to add this include, and recently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, fair enough. I think it's technically more correct to keep it, but both work - whatever you prefer.

.github/workflows/lint.yml Outdated Show resolved Hide resolved
@@ -1,3 +1,6 @@
#include "ckdtree_decl.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all correct indeed - necessary because "ckdtree_decl.h" includes npy_common.h

scipy/special/sf_error.cc Show resolved Hide resolved
.github/workflows/lint.yml Outdated Show resolved Hide resolved
scipy/special/sf_error.cc Show resolved Hide resolved
@@ -2,7 +2,8 @@
// Use of this source code is governed by the BSD 2-clause license found in the LICENSE.txt file.
// SPDX-License-Identifier: BSD-2-Clause

#include <Python.h>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the rationale for removing this where I kept the others was that I was the one to add this include, and recently.

[skip circle] [skip cirrus]

Co-authored-by: DWesl <22566757+DWesl@users.noreply.github.com>
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty clean now, and CI is happy. Let's give it a go! Thanks @DWesl, much appreciated

@rgommers rgommers merged commit d55cb95 into scipy:main Apr 23, 2024
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build issues Issues with building from source, including different choices of architecture, compilers and OS C/C++ Items related to the internal C/C++ code base CI Items related to the CI tools such as CircleCI, GitHub Actions or Azure scipy.io scipy.optimize scipy.special
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants