Skip to content

Commit

Permalink
Add missing clang-tidy fixes (#3715)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylion007 authored Feb 10, 2022
1 parent 7f97581 commit dc9803c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
9 changes: 6 additions & 3 deletions include/pybind11/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,16 @@ template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
if (!a.value) {
#if !defined(NDEBUG)
std::string descr("'");
if (a.name) descr += std::string(a.name) + ": ";
if (a.name) {
descr += std::string(a.name) + ": ";
}
descr += a.type + "'";
if (r->is_method) {
if (r->name)
if (r->name) {
descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'";
else
} else {
descr += " in method of '" + (std::string) str(r->scope) + "'";
}
} else if (r->name) {
descr += " in function '" + (std::string) r->name + "'";
}
Expand Down
6 changes: 4 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,11 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
return false;
}
#else
for (bool r : {std::get<Is>(subcasters).load(seq[Is], convert)...})
if (!r)
for (bool r : {std::get<Is>(subcasters).load(seq[Is], convert)...}) {
if (!r) {
return false;
}
}
#endif
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ struct type_hash {
size_t operator()(const std::type_index &t) const {
size_t hash = 5381;
const char *ptr = t.name();
while (auto c = static_cast<unsigned char>(*ptr++))
while (auto c = static_cast<unsigned char>(*ptr++)) {
hash = (hash * 33) ^ c;
}
return hash;
}
};
Expand Down Expand Up @@ -388,7 +389,9 @@ inline void translate_exception(std::exception_ptr p) {
#if !defined(__GLIBCXX__)
inline void translate_local_exception(std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
if (p) {
std::rethrow_exception(p);
}
} catch (error_already_set &e) { e.restore(); return;
} catch (const builtin_exception &e) { e.set_error(); return;
}
Expand Down
26 changes: 15 additions & 11 deletions include/pybind11/gil.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ class gil_scoped_acquire {
if (!tstate) {
tstate = PyThreadState_New(internals.istate);
#if !defined(NDEBUG)

This comment has been minimized.

Copy link
@Skylion007

Skylion007 Feb 10, 2022

Author Collaborator

@rwgk Probably should have fixed these ifdefs move, but whatever.

This comment has been minimized.

Copy link
@rwgk

rwgk via email Feb 10, 2022

Collaborator
if (!tstate)
pybind11_fail("scoped_acquire: could not create thread state!");
#endif
if (!tstate) {
pybind11_fail("scoped_acquire: could not create thread state!");
}
#endif
tstate->gilstate_counter = 0;
PYBIND11_TLS_REPLACE_VALUE(internals.tstate, tstate);
} else {
Expand All @@ -88,16 +89,19 @@ class gil_scoped_acquire {
PYBIND11_NOINLINE void dec_ref() {
--tstate->gilstate_counter;
#if !defined(NDEBUG)
if (detail::get_thread_state_unchecked() != tstate)
pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
if (tstate->gilstate_counter < 0)
pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
#endif
if (detail::get_thread_state_unchecked() != tstate) {
pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
}
if (tstate->gilstate_counter < 0) {
pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
}
#endif
if (tstate->gilstate_counter == 0) {
#if !defined(NDEBUG)
if (!release)
pybind11_fail("scoped_acquire::dec_ref(): internal error!");
#endif
if (!release) {
pybind11_fail("scoped_acquire::dec_ref(): internal error!");
}
#endif
PyThreadState_Clear(tstate);
if (active) {
PyThreadState_DeleteCurrent();
Expand Down
8 changes: 5 additions & 3 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,10 @@ class cpp_function : public function {
// 5. Put everything in a vector. Not technically step 5, we've been building it
// in `call.args` all along.
#if !defined(NDEBUG)
if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs)
if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs) {
pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!");
#endif
}
#endif

std::vector<bool> second_pass_convert;
if (overloaded) {
Expand Down Expand Up @@ -2527,8 +2528,9 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty
PyFrame_FastToLocals(frame);
PyObject *self_caller = dict_getitem(
frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
if (self_caller == self.ptr())
if (self_caller == self.ptr()) {
return function();
}
}
#endif

Expand Down
8 changes: 5 additions & 3 deletions tests/test_cmake_build/embed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ PYBIND11_EMBEDDED_MODULE(test_cmake_build, m) {
}

int main(int argc, char *argv[]) {
if (argc != 2)
if (argc != 2) {
throw std::runtime_error("Expected test.py file as the first argument");
auto test_py_file = argv[1];
}
auto *test_py_file = argv[1];

py::scoped_interpreter guard{};

auto m = py::module_::import("test_cmake_build");
if (m.attr("add")(1, 2).cast<int>() != 3)
if (m.attr("add")(1, 2).cast<int>() != 3) {
throw std::runtime_error("embed.cpp failed");
}

py::module_::import("sys").attr("argv") = py::make_tuple("test.py", "embed.cpp");
py::eval_file(test_py_file, py::globals());
Expand Down

0 comments on commit dc9803c

Please sign in to comment.