mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Fully-automatic clang-format with include reordering (#3713)
* chore: add clang-format
* Removing check-style (Classic check-style)
Ported from @henryiii's 53056b1b0e
* Automatic clang-format changes (NO manual changes).
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
committed by
GitHub
parent
e96221beff
commit
ec24786eab
@@ -7,22 +7,21 @@
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "pybind11_tests.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
TEST_SUBMODULE(pytypes, m) {
|
||||
// test_bool
|
||||
m.def("get_bool", []{return py::bool_(false);});
|
||||
m.def("get_bool", [] { return py::bool_(false); });
|
||||
// test_int
|
||||
m.def("get_int", []{return py::int_(0);});
|
||||
m.def("get_int", [] { return py::int_(0); });
|
||||
// test_iterator
|
||||
m.def("get_iterator", []{return py::iterator();});
|
||||
m.def("get_iterator", [] { return py::iterator(); });
|
||||
// test_iterable
|
||||
m.def("get_iterable", []{return py::iterable();});
|
||||
m.def("get_iterable", [] { return py::iterable(); });
|
||||
// test_float
|
||||
m.def("get_float", []{return py::float_(0.0f);});
|
||||
m.def("get_float", [] { return py::float_(0.0f); });
|
||||
// test_list
|
||||
m.def("list_no_args", []() { return py::list{}; });
|
||||
m.def("list_ssize_t", []() { return py::list{(py::ssize_t) 0}; });
|
||||
@@ -45,7 +44,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
}
|
||||
});
|
||||
// test_none
|
||||
m.def("get_none", []{return py::none();});
|
||||
m.def("get_none", [] { return py::none(); });
|
||||
m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); });
|
||||
|
||||
// test_set
|
||||
@@ -66,15 +65,15 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("set_contains", [](const py::set &set, const char *key) { return set.contains(key); });
|
||||
|
||||
// test_dict
|
||||
m.def("get_dict", []() { return py::dict("key"_a="value"); });
|
||||
m.def("get_dict", []() { return py::dict("key"_a = "value"); });
|
||||
m.def("print_dict", [](const py::dict &dict) {
|
||||
for (auto item : dict) {
|
||||
py::print("key: {}, value={}"_s.format(item.first, item.second));
|
||||
}
|
||||
});
|
||||
m.def("dict_keyword_constructor", []() {
|
||||
auto d1 = py::dict("x"_a=1, "y"_a=2);
|
||||
auto d2 = py::dict("z"_a=3, **d1);
|
||||
auto d1 = py::dict("x"_a = 1, "y"_a = 2);
|
||||
auto d2 = py::dict("z"_a = 3, **d1);
|
||||
return d2;
|
||||
});
|
||||
m.def("dict_contains",
|
||||
@@ -91,7 +90,8 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
#if PY_VERSION_HEX >= 0x03030000
|
||||
// test_simple_namespace
|
||||
m.def("get_simple_namespace", []() {
|
||||
auto ns = py::module_::import("types").attr("SimpleNamespace")("attr"_a=42, "x"_a="foo", "wrong"_a=1);
|
||||
auto ns = py::module_::import("types").attr("SimpleNamespace")(
|
||||
"attr"_a = 42, "x"_a = "foo", "wrong"_a = 1);
|
||||
py::delattr(ns, "wrong");
|
||||
py::setattr(ns, "right", py::int_(2));
|
||||
return ns;
|
||||
@@ -103,16 +103,15 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("str_from_char_size_t", []() { return py::str{"blue", (py::size_t) 4}; });
|
||||
m.def("str_from_string", []() { return py::str(std::string("baz")); });
|
||||
m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); });
|
||||
m.def("str_from_object", [](const py::object& obj) { return py::str(obj); });
|
||||
m.def("repr_from_object", [](const py::object& obj) { return py::repr(obj); });
|
||||
m.def("str_from_object", [](const py::object &obj) { return py::str(obj); });
|
||||
m.def("repr_from_object", [](const py::object &obj) { return py::repr(obj); });
|
||||
m.def("str_from_handle", [](py::handle h) { return py::str(h); });
|
||||
m.def("str_from_string_from_str", [](const py::str& obj) {
|
||||
return py::str(static_cast<std::string>(obj));
|
||||
});
|
||||
m.def("str_from_string_from_str",
|
||||
[](const py::str &obj) { return py::str(static_cast<std::string>(obj)); });
|
||||
|
||||
m.def("str_format", []() {
|
||||
auto s1 = "{} + {} = {}"_s.format(1, 2, 3);
|
||||
auto s2 = "{a} + {b} = {c}"_s.format("a"_a=1, "b"_a=2, "c"_a=3);
|
||||
auto s2 = "{a} + {b} = {c}"_s.format("a"_a = 1, "b"_a = 2, "c"_a = 3);
|
||||
return py::make_tuple(s1, s2);
|
||||
});
|
||||
|
||||
@@ -131,9 +130,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
// test_capsule
|
||||
m.def("return_capsule_with_destructor", []() {
|
||||
py::print("creating capsule");
|
||||
return py::capsule([]() {
|
||||
py::print("destructing capsule");
|
||||
});
|
||||
return py::capsule([]() { py::print("destructing capsule"); });
|
||||
});
|
||||
|
||||
m.def("return_capsule_with_destructor_2", []() {
|
||||
@@ -148,23 +145,23 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
if (ptr) {
|
||||
const auto *name = PyCapsule_GetName(ptr);
|
||||
py::print("destructing capsule ({}, '{}')"_s.format(
|
||||
(size_t) PyCapsule_GetPointer(ptr, name), name
|
||||
));
|
||||
(size_t) PyCapsule_GetPointer(ptr, name), name));
|
||||
}
|
||||
});
|
||||
|
||||
capsule.set_pointer((void *) 1234);
|
||||
|
||||
// Using get_pointer<T>()
|
||||
void* contents1 = static_cast<void*>(capsule);
|
||||
void* contents2 = capsule.get_pointer();
|
||||
void* contents3 = capsule.get_pointer<void>();
|
||||
void *contents1 = static_cast<void *>(capsule);
|
||||
void *contents2 = capsule.get_pointer();
|
||||
void *contents3 = capsule.get_pointer<void>();
|
||||
|
||||
auto result1 = reinterpret_cast<size_t>(contents1);
|
||||
auto result2 = reinterpret_cast<size_t>(contents2);
|
||||
auto result3 = reinterpret_cast<size_t>(contents3);
|
||||
|
||||
py::print("created capsule ({}, '{}')"_s.format(result1 & result2 & result3, capsule.name()));
|
||||
py::print(
|
||||
"created capsule ({}, '{}')"_s.format(result1 & result2 & result3, capsule.name()));
|
||||
return capsule;
|
||||
});
|
||||
|
||||
@@ -244,51 +241,45 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
|
||||
// test_constructors
|
||||
m.def("default_constructors", []() {
|
||||
return py::dict(
|
||||
"bytes"_a=py::bytes(),
|
||||
"bytearray"_a=py::bytearray(),
|
||||
"str"_a=py::str(),
|
||||
"bool"_a=py::bool_(),
|
||||
"int"_a=py::int_(),
|
||||
"float"_a=py::float_(),
|
||||
"tuple"_a=py::tuple(),
|
||||
"list"_a=py::list(),
|
||||
"dict"_a=py::dict(),
|
||||
"set"_a=py::set()
|
||||
);
|
||||
return py::dict("bytes"_a = py::bytes(),
|
||||
"bytearray"_a = py::bytearray(),
|
||||
"str"_a = py::str(),
|
||||
"bool"_a = py::bool_(),
|
||||
"int"_a = py::int_(),
|
||||
"float"_a = py::float_(),
|
||||
"tuple"_a = py::tuple(),
|
||||
"list"_a = py::list(),
|
||||
"dict"_a = py::dict(),
|
||||
"set"_a = py::set());
|
||||
});
|
||||
|
||||
m.def("converting_constructors", [](const py::dict &d) {
|
||||
return py::dict(
|
||||
"bytes"_a=py::bytes(d["bytes"]),
|
||||
"bytearray"_a=py::bytearray(d["bytearray"]),
|
||||
"str"_a=py::str(d["str"]),
|
||||
"bool"_a=py::bool_(d["bool"]),
|
||||
"int"_a=py::int_(d["int"]),
|
||||
"float"_a=py::float_(d["float"]),
|
||||
"tuple"_a=py::tuple(d["tuple"]),
|
||||
"list"_a=py::list(d["list"]),
|
||||
"dict"_a=py::dict(d["dict"]),
|
||||
"set"_a=py::set(d["set"]),
|
||||
"memoryview"_a=py::memoryview(d["memoryview"])
|
||||
);
|
||||
return py::dict("bytes"_a = py::bytes(d["bytes"]),
|
||||
"bytearray"_a = py::bytearray(d["bytearray"]),
|
||||
"str"_a = py::str(d["str"]),
|
||||
"bool"_a = py::bool_(d["bool"]),
|
||||
"int"_a = py::int_(d["int"]),
|
||||
"float"_a = py::float_(d["float"]),
|
||||
"tuple"_a = py::tuple(d["tuple"]),
|
||||
"list"_a = py::list(d["list"]),
|
||||
"dict"_a = py::dict(d["dict"]),
|
||||
"set"_a = py::set(d["set"]),
|
||||
"memoryview"_a = py::memoryview(d["memoryview"]));
|
||||
});
|
||||
|
||||
m.def("cast_functions", [](const py::dict &d) {
|
||||
// When converting between Python types, obj.cast<T>() should be the same as T(obj)
|
||||
return py::dict(
|
||||
"bytes"_a=d["bytes"].cast<py::bytes>(),
|
||||
"bytearray"_a=d["bytearray"].cast<py::bytearray>(),
|
||||
"str"_a=d["str"].cast<py::str>(),
|
||||
"bool"_a=d["bool"].cast<py::bool_>(),
|
||||
"int"_a=d["int"].cast<py::int_>(),
|
||||
"float"_a=d["float"].cast<py::float_>(),
|
||||
"tuple"_a=d["tuple"].cast<py::tuple>(),
|
||||
"list"_a=d["list"].cast<py::list>(),
|
||||
"dict"_a=d["dict"].cast<py::dict>(),
|
||||
"set"_a=d["set"].cast<py::set>(),
|
||||
"memoryview"_a=d["memoryview"].cast<py::memoryview>()
|
||||
);
|
||||
return py::dict("bytes"_a = d["bytes"].cast<py::bytes>(),
|
||||
"bytearray"_a = d["bytearray"].cast<py::bytearray>(),
|
||||
"str"_a = d["str"].cast<py::str>(),
|
||||
"bool"_a = d["bool"].cast<py::bool_>(),
|
||||
"int"_a = d["int"].cast<py::int_>(),
|
||||
"float"_a = d["float"].cast<py::float_>(),
|
||||
"tuple"_a = d["tuple"].cast<py::tuple>(),
|
||||
"list"_a = d["list"].cast<py::list>(),
|
||||
"dict"_a = d["dict"].cast<py::dict>(),
|
||||
"set"_a = d["set"].cast<py::set>(),
|
||||
"memoryview"_a = d["memoryview"].cast<py::memoryview>());
|
||||
});
|
||||
|
||||
m.def("convert_to_pybind11_str", [](const py::object &o) { return py::str(o); });
|
||||
@@ -341,10 +332,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
l.append(py::cast(12));
|
||||
l.append(py::int_(15));
|
||||
|
||||
return py::dict(
|
||||
"d"_a=d,
|
||||
"l"_a=l
|
||||
);
|
||||
return py::dict("d"_a = d, "l"_a = l);
|
||||
});
|
||||
|
||||
// test_print
|
||||
@@ -352,16 +340,17 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
py::print("Hello, World!");
|
||||
py::print(1, 2.0, "three", true, std::string("-- multiple args"));
|
||||
auto args = py::make_tuple("and", "a", "custom", "separator");
|
||||
py::print("*args", *args, "sep"_a="-");
|
||||
py::print("no new line here", "end"_a=" -- ");
|
||||
py::print("*args", *args, "sep"_a = "-");
|
||||
py::print("no new line here", "end"_a = " -- ");
|
||||
py::print("next print");
|
||||
|
||||
auto py_stderr = py::module_::import("sys").attr("stderr");
|
||||
py::print("this goes to stderr", "file"_a=py_stderr);
|
||||
py::print("this goes to stderr", "file"_a = py_stderr);
|
||||
|
||||
py::print("flush", "flush"_a=true);
|
||||
py::print("flush", "flush"_a = true);
|
||||
|
||||
py::print("{a} + {b} = {c}"_s.format("a"_a="py::print", "b"_a="str.format", "c"_a="this"));
|
||||
py::print(
|
||||
"{a} + {b} = {c}"_s.format("a"_a = "py::print", "b"_a = "str.format", "c"_a = "this"));
|
||||
});
|
||||
|
||||
m.def("print_failure", []() { py::print(42, UnregisteredType()); });
|
||||
@@ -406,8 +395,8 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
[](const py::buffer &b) { return py::memoryview(b.request()); });
|
||||
|
||||
m.def("test_memoryview_from_buffer", [](bool is_unsigned) {
|
||||
static const int16_t si16[] = { 3, 1, 4, 1, 5 };
|
||||
static const uint16_t ui16[] = { 2, 7, 1, 8 };
|
||||
static const int16_t si16[] = {3, 1, 4, 1, 5};
|
||||
static const uint16_t ui16[] = {2, 7, 1, 8};
|
||||
if (is_unsigned) {
|
||||
return py::memoryview::from_buffer(ui16, {4}, {sizeof(uint16_t)});
|
||||
}
|
||||
@@ -415,32 +404,29 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_nativeformat", []() {
|
||||
static const char* format = "@i";
|
||||
static const int32_t arr[] = { 4, 7, 5 };
|
||||
return py::memoryview::from_buffer(
|
||||
arr, sizeof(int32_t), format, { 3 }, { sizeof(int32_t) });
|
||||
static const char *format = "@i";
|
||||
static const int32_t arr[] = {4, 7, 5};
|
||||
return py::memoryview::from_buffer(arr, sizeof(int32_t), format, {3}, {sizeof(int32_t)});
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_empty_shape", []() {
|
||||
static const char* buf = "";
|
||||
return py::memoryview::from_buffer(buf, 1, "B", { }, { });
|
||||
static const char *buf = "";
|
||||
return py::memoryview::from_buffer(buf, 1, "B", {}, {});
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_invalid_strides", []() {
|
||||
static const char* buf = "\x02\x03\x04";
|
||||
return py::memoryview::from_buffer(buf, 1, "B", { 3 }, { });
|
||||
static const char *buf = "\x02\x03\x04";
|
||||
return py::memoryview::from_buffer(buf, 1, "B", {3}, {});
|
||||
});
|
||||
|
||||
m.def("test_memoryview_from_buffer_nullptr", []() {
|
||||
return py::memoryview::from_buffer(
|
||||
static_cast<void*>(nullptr), 1, "B", { }, { });
|
||||
return py::memoryview::from_buffer(static_cast<void *>(nullptr), 1, "B", {}, {});
|
||||
});
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
m.def("test_memoryview_from_memory", []() {
|
||||
const char* buf = "\xff\xe1\xab\x37";
|
||||
return py::memoryview::from_memory(
|
||||
buf, static_cast<py::ssize_t>(strlen(buf)));
|
||||
const char *buf = "\xff\xe1\xab\x37";
|
||||
return py::memoryview::from_memory(buf, static_cast<py::ssize_t>(strlen(buf)));
|
||||
});
|
||||
#endif
|
||||
|
||||
@@ -461,8 +447,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("pass_to_std_string", [](const std::string &s) { return s.size(); });
|
||||
|
||||
// test_weakref
|
||||
m.def("weakref_from_handle",
|
||||
[](py::handle h) { return py::weakref(h); });
|
||||
m.def("weakref_from_handle", [](py::handle h) { return py::weakref(h); });
|
||||
m.def("weakref_from_handle_and_function",
|
||||
[](py::handle h, py::function f) { return py::weakref(h, std::move(f)); });
|
||||
m.def("weakref_from_object", [](const py::object &o) { return py::weakref(o); });
|
||||
|
||||
Reference in New Issue
Block a user