Skip to content

Commit 94a5c67

Browse files
committed
Merge branch 'master' into sh_merge_master
2 parents f33da18 + 6abf2ba commit 94a5c67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+295
-148
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Checks: '
55
cppcoreguidelines-init-variables,
66
cppcoreguidelines-slicing,
77
clang-analyzer-optin.cplusplus.VirtualCall,
8+
google-explicit-constructor,
89
llvm-namespace-comment,
910
misc-misplaced-const,
1011
misc-non-copyable-objects,

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232
exclude: ^noxfile.py$
3333

3434
- repo: https://github.com/asottile/pyupgrade
35-
rev: v2.24.0
35+
rev: v2.25.0
3636
hooks:
3737
- id: pyupgrade
3838

@@ -43,7 +43,7 @@ repos:
4343

4444
# Black, the code formatter, natively supports pre-commit
4545
- repo: https://github.com/psf/black
46-
rev: 21.7b0
46+
rev: 21.8b0
4747
hooks:
4848
- id: black
4949
# By default, this ignores pyi files, though black supports them

docs/compiling.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ with the following:
113113
114114
from pybind11.setup_helpers import ParallelCompile, naive_recompile
115115
116-
SmartCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install()
116+
ParallelCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install()
117117
118118
119119
If you have a more complex build, you can implement a smarter function and pass

include/pybind11/attr.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1818
/// @{
1919

2020
/// Annotation for methods
21-
struct is_method { handle class_; is_method(const handle &c) : class_(c) { } };
21+
struct is_method { handle class_;
22+
explicit is_method(const handle &c) : class_(c) {}
23+
};
2224

2325
/// Annotation for operators
2426
struct is_operator { };
@@ -27,16 +29,24 @@ struct is_operator { };
2729
struct is_final { };
2830

2931
/// Annotation for parent scope
30-
struct scope { handle value; scope(const handle &s) : value(s) { } };
32+
struct scope { handle value;
33+
explicit scope(const handle &s) : value(s) {}
34+
};
3135

3236
/// Annotation for documentation
33-
struct doc { const char *value; doc(const char *value) : value(value) { } };
37+
struct doc { const char *value;
38+
explicit doc(const char *value) : value(value) {}
39+
};
3440

3541
/// Annotation for function names
36-
struct name { const char *value; name(const char *value) : value(value) { } };
42+
struct name { const char *value;
43+
explicit name(const char *value) : value(value) {}
44+
};
3745

3846
/// Annotation indicating that a function is an overload associated with a given "sibling"
39-
struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) { } };
47+
struct sibling { handle value;
48+
explicit sibling(const handle &value) : value(value.ptr()) {}
49+
};
4050

4151
/// Annotation indicating that a class derives from another given type
4252
template <typename T> struct base {
@@ -70,7 +80,9 @@ struct metaclass {
7080
};
7181

7282
/// Annotation that marks a class as local to the module:
73-
struct module_local { const bool value; constexpr module_local(bool v = true) : value(v) { } };
83+
struct module_local { const bool value;
84+
constexpr explicit module_local(bool v = true) : value(v) {}
85+
};
7486

7587
/// Annotation to mark enums as an arithmetic type
7688
struct arithmetic { };

include/pybind11/cast.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ template <typename type> class type_caster<std::reference_wrapper<type>> {
104104
return caster_t::cast(&src.get(), policy, parent);
105105
}
106106
template <typename T> using cast_op_type = std::reference_wrapper<type>;
107-
operator std::reference_wrapper<type>() { return cast_op<type &>(subcaster); }
107+
explicit operator std::reference_wrapper<type>() { return cast_op<type &>(subcaster); }
108108
};
109109

110110
#define PYBIND11_TYPE_CASTER(type, py_name) \
@@ -301,7 +301,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
301301
}
302302

303303
template <typename T> using cast_op_type = void*&;
304-
operator void *&() { return value; }
304+
explicit operator void *&() { return value; }
305305
static constexpr auto name = _("capsule");
306306
private:
307307
void *value = nullptr;
@@ -509,8 +509,10 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
509509
return StringCaster::cast(StringType(1, src), policy, parent);
510510
}
511511

512-
operator CharT*() { return none ? nullptr : const_cast<CharT *>(static_cast<StringType &>(str_caster).c_str()); }
513-
operator CharT&() {
512+
explicit operator CharT *() {
513+
return none ? nullptr : const_cast<CharT *>(static_cast<StringType &>(str_caster).c_str());
514+
}
515+
explicit operator CharT &() {
514516
if (none)
515517
throw value_error("Cannot convert None to a character");
516518

@@ -603,8 +605,8 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
603605

604606
template <typename T> using cast_op_type = type;
605607

606-
operator type() & { return implicit_cast(indices{}); }
607-
operator type() && { return std::move(*this).implicit_cast(indices{}); }
608+
explicit operator type() & { return implicit_cast(indices{}); }
609+
explicit operator type() && { return std::move(*this).implicit_cast(indices{}); }
608610

609611
protected:
610612
template <size_t... Is>

include/pybind11/detail/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ class any_container {
918918

919919
// Implicit conversion constructor from any arbitrary container type with values convertible to T
920920
template <typename Container, typename = enable_if_t<std::is_convertible<decltype(*std::begin(std::declval<const Container &>())), T>::value>>
921+
// NOLINTNEXTLINE(google-explicit-constructor)
921922
any_container(const Container &c) : any_container(std::begin(c), std::end(c)) { }
922923

923924
// initializer_list's aren't deducible, so don't get matched by the above template; we need this
@@ -926,9 +927,11 @@ class any_container {
926927
any_container(const std::initializer_list<TIn> &c) : any_container(c.begin(), c.end()) { }
927928

928929
// Avoid copying if given an rvalue vector of the correct type.
930+
// NOLINTNEXTLINE(google-explicit-constructor)
929931
any_container(std::vector<T> &&v) : v(std::move(v)) { }
930932

931933
// Moves the vector out of an rvalue any_container
934+
// NOLINTNEXTLINE(google-explicit-constructor)
932935
operator std::vector<T> &&() && { return std::move(v); }
933936

934937
// Dereferencing obtains a reference to the underlying vector

include/pybind11/detail/descr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ struct descr {
2626
char text[N + 1]{'\0'};
2727

2828
constexpr descr() = default;
29+
// NOLINTNEXTLINE(google-explicit-constructor)
2930
constexpr descr(char const (&s)[N+1]) : descr(s, make_index_sequence<N>()) { }
3031

3132
template <size_t... Is>
3233
constexpr descr(char const (&s)[N+1], index_sequence<Is...>) : text{s[Is]..., '\0'} { }
3334

3435
template <typename... Chars>
36+
// NOLINTNEXTLINE(google-explicit-constructor)
3537
constexpr descr(char c, Chars... cs) : text{c, static_cast<char>(cs)..., '\0'} { }
3638

3739
static constexpr std::array<const std::type_info *, sizeof...(Ts) + 1> types() {

include/pybind11/detail/init.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class type_caster<value_and_holder> {
2525
}
2626

2727
template <typename> using cast_op_type = value_and_holder &;
28-
operator value_and_holder &() { return *value; }
28+
explicit operator value_and_holder &() { return *value; }
2929
static constexpr auto name = _<value_and_holder>();
3030

3131
private:
@@ -294,7 +294,8 @@ template <typename Func, typename Return, typename... Args>
294294
struct factory<Func, void_type (*)(), Return(Args...)> {
295295
remove_reference_t<Func> class_factory;
296296

297-
factory(Func &&f) : class_factory(std::forward<Func>(f)) { }
297+
// NOLINTNEXTLINE(google-explicit-constructor)
298+
factory(Func &&f) : class_factory(std::forward<Func>(f)) {}
298299

299300
// The given class either has no alias or has no separate alias factory;
300301
// this always constructs the class itself. If the class is registered with an alias

include/pybind11/detail/type_caster_base.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ struct value_and_holder {
225225
value_and_holder() = default;
226226

227227
// Used for past-the-end iterator
228-
value_and_holder(size_t index) : index{index} {}
228+
explicit value_and_holder(size_t index) : index{index} {}
229229

230230
template <typename V = void> V *&value_ptr() const {
231231
return reinterpret_cast<V *&>(vh[0]);
@@ -274,7 +274,8 @@ struct values_and_holders {
274274
const type_vec &tinfo;
275275

276276
public:
277-
values_and_holders(instance *inst) : inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {}
277+
explicit values_and_holders(instance *inst)
278+
: inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {}
278279

279280
struct iterator {
280281
private:
@@ -290,7 +291,8 @@ struct values_and_holders {
290291
0 /* index */)
291292
{}
292293
// Past-the-end iterator:
293-
iterator(size_t end) : curr(end) {}
294+
explicit iterator(size_t end) : curr(end) {}
295+
294296
public:
295297
bool operator==(const iterator &other) const { return curr.index == other.curr.index; }
296298
bool operator!=(const iterator &other) const { return curr.index != other.curr.index; }
@@ -491,11 +493,11 @@ inline PyObject *make_new_instance(PyTypeObject *type);
491493

492494
class type_caster_generic {
493495
public:
494-
PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info)
495-
: typeinfo(get_type_info(type_info)), cpptype(&type_info) { }
496+
PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info)
497+
: typeinfo(get_type_info(type_info)), cpptype(&type_info) {}
496498

497-
type_caster_generic(const type_info *typeinfo)
498-
: typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) { }
499+
explicit type_caster_generic(const type_info *typeinfo)
500+
: typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) {}
499501

500502
bool load(handle src, bool convert) {
501503
return load_impl<type_caster_generic>(src, convert);
@@ -923,7 +925,9 @@ template <typename type> class type_caster_base : public type_caster_generic {
923925

924926
template <typename T> using cast_op_type = detail::cast_op_type<T>;
925927

928+
// NOLINTNEXTLINE(google-explicit-constructor)
926929
operator itype*() { return (type *) value; }
930+
// NOLINTNEXTLINE(google-explicit-constructor)
927931
operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
928932

929933
protected:

include/pybind11/eigen.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ template <bool EigenRowMajor> struct EigenConformable {
6161
EigenDStride stride{0, 0}; // Only valid if negativestrides is false!
6262
bool negativestrides = false; // If true, do not use stride!
6363

64+
// NOLINTNEXTLINE(google-explicit-constructor)
6465
EigenConformable(bool fits = false) : conformable{fits} {}
6566
// Matrix type:
6667
EigenConformable(EigenIndex r, EigenIndex c,
@@ -88,6 +89,7 @@ template <bool EigenRowMajor> struct EigenConformable {
8889
(props::outer_stride == Eigen::Dynamic || props::outer_stride == stride.outer() ||
8990
(EigenRowMajor ? rows : cols) == 1);
9091
}
92+
// NOLINTNEXTLINE(google-explicit-constructor)
9193
operator bool() const { return conformable; }
9294
};
9395

@@ -326,8 +328,11 @@ struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
326328

327329
static constexpr auto name = props::descriptor;
328330

331+
// NOLINTNEXTLINE(google-explicit-constructor)
329332
operator Type*() { return &value; }
333+
// NOLINTNEXTLINE(google-explicit-constructor)
330334
operator Type&() { return value; }
335+
// NOLINTNEXTLINE(google-explicit-constructor)
331336
operator Type&&() && { return std::move(value); }
332337
template <typename T> using cast_op_type = movable_cast_op_type<T>;
333338

@@ -451,7 +456,9 @@ struct type_caster<
451456
return true;
452457
}
453458

459+
// NOLINTNEXTLINE(google-explicit-constructor)
454460
operator Type*() { return ref.get(); }
461+
// NOLINTNEXTLINE(google-explicit-constructor)
455462
operator Type&() { return *ref; }
456463
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
457464

include/pybind11/embed.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ inline void finalize_interpreter() {
260260
\endrst */
261261
class scoped_interpreter {
262262
public:
263-
scoped_interpreter(bool init_signal_handlers = true,
264-
int argc = 0,
265-
const char *const *argv = nullptr,
266-
bool add_program_dir_to_path = true) {
263+
explicit scoped_interpreter(bool init_signal_handlers = true,
264+
int argc = 0,
265+
const char *const *argv = nullptr,
266+
bool add_program_dir_to_path = true) {
267267
initialize_interpreter(init_signal_handlers, argc, argv, add_program_dir_to_path);
268268
}
269269

include/pybind11/functional.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ struct type_caster<std::function<Return(Args...)>> {
6969
// ensure GIL is held during functor destruction
7070
struct func_handle {
7171
function f;
72+
#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17) && PY_MAJOR_VERSION < 3)
73+
// This triggers a syntax error under very special conditions (very weird indeed).
74+
explicit
75+
#endif
7276
func_handle(function &&f_) noexcept : f(std::move(f_)) {}
7377
func_handle(const func_handle &f_) { operator=(f_); }
7478
func_handle &operator=(const func_handle &f_) {
@@ -85,7 +89,7 @@ struct type_caster<std::function<Return(Args...)>> {
8589
// to emulate 'move initialization capture' in C++11
8690
struct func_wrapper {
8791
func_handle hfunc;
88-
func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {}
92+
explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {}
8993
Return operator()(Args... args) const {
9094
gil_scoped_acquire acq;
9195
object retval(hfunc.f(std::forward<Args>(args)...));

include/pybind11/iostream.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class pythonbuf : public std::streambuf {
123123
}
124124

125125
public:
126-
pythonbuf(const object &pyostream, size_t buffer_size = 1024)
126+
explicit pythonbuf(const object &pyostream, size_t buffer_size = 1024)
127127
: buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")),
128128
pyflush(pyostream.attr("flush")) {
129129
setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
@@ -171,8 +171,9 @@ class scoped_ostream_redirect {
171171
detail::pythonbuf buffer;
172172

173173
public:
174-
scoped_ostream_redirect(std::ostream &costream = std::cout,
175-
const object &pyostream = module_::import("sys").attr("stdout"))
174+
explicit scoped_ostream_redirect(std::ostream &costream = std::cout,
175+
const object &pyostream
176+
= module_::import("sys").attr("stdout"))
176177
: costream(costream), buffer(pyostream) {
177178
old = costream.rdbuf(&buffer);
178179
}
@@ -201,8 +202,9 @@ class scoped_ostream_redirect {
201202
\endrst */
202203
class scoped_estream_redirect : public scoped_ostream_redirect {
203204
public:
204-
scoped_estream_redirect(std::ostream &costream = std::cerr,
205-
const object &pyostream = module_::import("sys").attr("stderr"))
205+
explicit scoped_estream_redirect(std::ostream &costream = std::cerr,
206+
const object &pyostream
207+
= module_::import("sys").attr("stderr"))
206208
: scoped_ostream_redirect(costream, pyostream) {}
207209
};
208210

@@ -217,7 +219,7 @@ class OstreamRedirect {
217219
std::unique_ptr<scoped_estream_redirect> redirect_stderr;
218220

219221
public:
220-
OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
222+
explicit OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
221223
: do_stdout_(do_stdout), do_stderr_(do_stderr) {}
222224

223225
void enter() {

include/pybind11/numpy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class dtype : public object {
477477
m_ptr = from_args(pybind11::str(format)).release().ptr();
478478
}
479479

480-
dtype(const char *format) : dtype(std::string(format)) { }
480+
explicit dtype(const char *format) : dtype(std::string(format)) {}
481481

482482
dtype(list names, list formats, list offsets, ssize_t itemsize) {
483483
dict args;
@@ -894,6 +894,7 @@ template <typename T, int ExtraFlags = array::forcecast> class array_t : public
894894
if (!is_borrowed) Py_XDECREF(h.ptr());
895895
}
896896

897+
// NOLINTNEXTLINE(google-explicit-constructor)
897898
array_t(const object &o) : array(raw_array_t(o.ptr()), stolen_t{}) {
898899
if (!m_ptr) throw error_already_set();
899900
}

0 commit comments

Comments
 (0)