Skip to content

Commit de8202e

Browse files
Run through tests
1 parent 041280d commit de8202e

7 files changed

+16
-244
lines changed

include/pybind11/attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ template <>
345345
struct process_attribute<is_method> : process_attribute_default<is_method> {
346346
template <typename RecordType>
347347
static void init(const is_method &s, RecordType &r) {
348-
if (PYBIND11_SILENCE_MSVC_C4127(RecordType::nargs > 0)) {
348+
if (RecordType::nargs > 0) {
349349
if (r.arginfo_index != 0) {
350350
pybind11_fail("is_method must come before any arguments when mapping");
351351
}

include/pybind11/detail/function_record.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ struct functor_metadata<Return, std::tuple<Args...>, std::tuple<Extra...>> {
179179
}
180180
// Separator for keyword-only arguments, placed before the kw
181181
// arguments start (unless we are already putting an *args)
182-
if (PYBIND11_SILENCE_MSVC_C4127(!has_args && arg_index == nargs_pos)) {
182+
if (!has_args && arg_index == nargs_pos) {
183183
signature += "*, ";
184184
}
185185
if (!argument_info[arg_index].name.empty()) {
@@ -196,8 +196,7 @@ struct functor_metadata<Return, std::tuple<Args...>, std::tuple<Extra...>> {
196196
}
197197
// Separator for positional-only arguments (placed after the
198198
// argument, rather than before like *
199-
if (PYBIND11_SILENCE_MSVC_C4127(has_pos_only_args
200-
&& (arg_index + 1) == nargs_pos_only)) {
199+
if (has_pos_only_args && (arg_index + 1) == nargs_pos_only) {
201200
signature += ", /";
202201
}
203202
if (!is_starred) {
@@ -212,7 +211,7 @@ struct functor_metadata<Return, std::tuple<Args...>, std::tuple<Extra...>> {
212211
handle th((PyObject *) tinfo->type);
213212
signature += th.attr("__module__").cast<std::string>() + "."
214213
+ th.attr("__qualname__").cast<std::string>();
215-
} else if (PYBIND11_SILENCE_MSVC_C4127(new_style_constructor && arg_index == 0)) {
214+
} else if (new_style_constructor && arg_index == 0) {
216215
// A new-style `__init__` takes `self` as `value_and_holder`.
217216
// Rewrite it to the proper class type.
218217
signature += current_scope.attr("__module__").template cast<std::string>()
@@ -400,7 +399,7 @@ struct function_wrapper<Return, std::tuple<Args...>, std::tuple<Extra...>, Func>
400399

401400
{
402401
tl::optional<loader_life_support> life_support;
403-
if (PYBIND11_SILENCE_MSVC_C4127(metadata.is_temporary_casts)) {
402+
if (metadata.is_temporary_casts) {
404403
life_support.emplace();
405404
}
406405

@@ -422,8 +421,8 @@ struct function_wrapper<Return, std::tuple<Args...>, std::tuple<Extra...>, Func>
422421
return tl::nullopt;
423422
}
424423

425-
if (PYBIND11_SILENCE_MSVC_C4127(metadata_type::new_style_constructor
426-
&& self_value_and_holder.instance_registered())) {
424+
if (metadata_type::new_style_constructor
425+
&& self_value_and_holder.instance_registered()) {
427426
PyErr_SetString(PyExc_SystemError,
428427
"Trying to call __init__ a second time on a C++ class, invalid");
429428
return nullptr;
@@ -463,8 +462,8 @@ struct function_wrapper<Return, std::tuple<Args...>, std::tuple<Extra...>, Func>
463462
/* Invoke call policy post-call hook */
464463
process_attributes<Extra...>::postcall(call_args, parent, result);
465464

466-
if (PYBIND11_SILENCE_MSVC_C4127(metadata_type::new_style_constructor
467-
&& !self_value_and_holder.holder_constructed())) {
465+
if (metadata_type::new_style_constructor
466+
&& !self_value_and_holder.holder_constructed()) {
468467
auto *pi = reinterpret_cast<instance *>(parent.ptr());
469468
self_value_and_holder.type->init_instance(pi, nullptr);
470469
}

include/pybind11/detail/internal_pytypes.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,8 @@ struct pybind_function {
9494
std::string n = func->name;
9595
TypeToStore *functor = (TypeToStore *) func->data;
9696
size_t nargs = pybind_vectorcall_nargs(nargs_with_flag);
97-
if (n == "foo_helper3") {
98-
std::cout << "Don't even call it!" << std::endl;
99-
100-
PyErr_SetString(PyExc_SystemError, "Double fancy exception for foo");
101-
return nullptr;
102-
}
10397
auto result = (*functor)(args, nargs, kwnames, false);
10498

105-
if (n == "foo" || n == "foo_static" || n == "foo_helper" || n == "foo_helper2") {
106-
std::cout << "Calling method " << func->name << std::endl;
107-
108-
PyErr_SetString(PyExc_SystemError, "Fancy exception for foo");
109-
return nullptr;
110-
}
111-
11299
if (!result) {
113100
return raise_type_error(&func, 1, args, nargs, kwnames);
114101
}
@@ -183,10 +170,10 @@ struct pybind_function_type_data {
183170
std::array<PyMemberDef, 4> members;
184171

185172
pybind_function_type_data() {
186-
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
187-
# pragma GCC diagnostic push
188-
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
189-
#endif
173+
174+
PYBIND11_WARNING_PUSH
175+
PYBIND11_WARNING_DISABLE_GCC("-Wmissing-field-initializers")
176+
190177
type = {PyVarObject_HEAD_INIT(&PyType_Type, 0)};
191178
PyMemberDef name_def = {const_cast<char *>("__name__"),
192179
T_STRING,
@@ -204,9 +191,8 @@ struct pybind_function_type_data {
204191
members[2] = module_def;
205192
members[3] = null_def;
206193

207-
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
208-
# pragma GCC diagnostic pop
209-
#endif
194+
PYBIND11_WARNING_POP
195+
210196
type.tp_getattro = PyObject_GenericGetAttr;
211197
type.tp_name = "pybind11_function";
212198
type.tp_basicsize = sizeof(pybind_function);
@@ -229,7 +215,6 @@ struct pybind_function_type_data {
229215
#endif
230216

231217
type.tp_descr_get = [](PyObject *self, PyObject *obj, PyObject *) -> PyObject * {
232-
// std::cout << "Ideally shouldn't be calling tp_descr_get ... " << obj << std::endl;
233218
if (obj == nullptr) {
234219
Py_INCREF(self);
235220
return self;

include/pybind11/detail/python_compat.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,11 @@ constexpr size_t pybind_vectorcall_arguments_offset
4646
inline size_t pybind_vectorcall_nargs(size_t n) { return n & ~pybind_vectorcall_arguments_offset; }
4747

4848
inline PyObject *pybind_vectorcall_call(PyObject *callable, PyObject *args, PyObject *kwargs) {
49-
PyObject *name = PyObject_GetAttrString(callable, "__name__");
50-
if (name == nullptr) {
51-
std::cout << "This should never happen ... " << std::endl;
52-
abort();
53-
}
54-
Py_ssize_t size;
55-
const char *data = PyUnicode_AsUTF8AndSize(name, &size);
56-
std::string n(data, size);
57-
if (n == "foo_helper3") {
58-
std::cout << "Don't even call it double plus good!" << std::endl;
59-
60-
PyErr_SetString(PyExc_SystemError, "Triple fancy exception for foo");
61-
return nullptr;
62-
}
63-
6449
Py_ssize_t num_pos_args = PyTuple_GET_SIZE(args);
65-
std::cout << "Working through the call " << num_pos_args << " ";
6650

6751
Py_ssize_t num_args = num_pos_args;
6852
if (kwargs != nullptr) {
6953
num_args += PyDict_Size(kwargs);
70-
std::cout << PyDict_Size(kwargs) << std::endl;
71-
} else {
72-
std::cout << std::endl;
7354
}
7455

7556
if (num_args > 1000) {

include/pybind11/pybind11.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,16 +1260,7 @@ struct iterator_state {
12601260
Iterator it;
12611261
Sentinel end;
12621262
bool first_or_done;
1263-
iterator_state(Iterator i, Sentinel e, bool f) : it(i), end(e), first_or_done(f) {
1264-
std::cout << "Creating iterator ... " << this << std::endl;
1265-
}
1266-
1267-
iterator_state(const iterator_state &other)
1268-
: it(other.it), end(other.end), first_or_done(other.first_or_done) {
1269-
std::cout << "Creating copy of iterator " << this << std::endl;
1270-
}
1271-
1272-
~iterator_state() { std::cout << "Destroying iterator ... " << this << std::endl; }
1263+
iterator_state(Iterator i, Sentinel e, bool f) : it(i), end(e), first_or_done(f) {}
12731264
};
12741265

12751266
// Note: these helpers take the iterator by non-const reference because some
@@ -1588,9 +1579,6 @@ inline PyObject *handle_exception(const F &f) {
15881579
exception.
15891580
*/
15901581

1591-
PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
1592-
return nullptr;
1593-
15941582
auto &local_exception_translators = get_local_internals().registered_exception_translators;
15951583
if (apply_exception_translators(local_exception_translators)) {
15961584
return nullptr;

tests/test_sequences_and_iterators.cpp

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -42,61 +42,6 @@ bool operator==(const NonZeroIterator<std::pair<A, B>> &it, const NonZeroSentine
4242
return !(*it).first || !(*it).second;
4343
}
4444

45-
struct custom_function {
46-
PyObject_HEAD
47-
};
48-
49-
struct custom_function_type {
50-
PyTypeObject type;
51-
std::array<PyMemberDef, 4> members;
52-
53-
custom_function_type() {
54-
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
55-
# pragma GCC diagnostic push
56-
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
57-
#endif
58-
type = {PyVarObject_HEAD_INIT(&PyType_Type, 0)};
59-
PyMemberDef name_def = {const_cast<char *>("__name__"),
60-
T_STRING,
61-
static_cast<Py_ssize_t>(offsetof(custom_function, name))};
62-
PyMemberDef doc_def = {const_cast<char *>("__doc__"),
63-
T_STRING,
64-
static_cast<Py_ssize_t>(offsetof(custom_function, doc))};
65-
PyMemberDef module_def = {const_cast<char *>("__module__"),
66-
T_STRING,
67-
static_cast<Py_ssize_t>(offsetof(custom_function, module))};
68-
PyMemberDef null_def = {nullptr};
69-
70-
members[0] = name_def;
71-
members[1] = doc_def;
72-
members[2] = module_def;
73-
members[3] = null_def;
74-
75-
#if defined(__GNUG__) && !defined(__INTEL_COMPILER)
76-
# pragma GCC diagnostic pop
77-
#endif
78-
type.tp_getattro = PyObject_GenericGetAttr;
79-
type.tp_name = "cusotm_function";
80-
type.tp_basicsize = sizeof(custom_function);
81-
type.tp_members = members.data();
82-
type.tp_call = [](PyObject *self, PyObject *args, PyObject *kwargs) -> PyObject * {
83-
std::cout << "Calling custom function with except" << std::endl;
84-
85-
PyErr_SetString(PyExc_SystemError, "Fancier exception for custom fancy func");
86-
return nullptr;
87-
88-
return PyLong_FromLong(34);
89-
};
90-
91-
type.tp_flags = Py_TPFLAGS_DEFAULT;
92-
93-
int ret = PyType_Ready(&type);
94-
if (ret != 0) {
95-
py::pybind11_fail("Could not create pybind_function type");
96-
}
97-
}
98-
};
99-
10045
/* Iterator where dereferencing returns prvalues instead of references. */
10146
template <typename T>
10247
class NonRefIterator {
@@ -312,75 +257,6 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
312257
size_t m_size;
313258
float *m_data;
314259
};
315-
316-
class Simpler {
317-
public:
318-
Simpler(size_t v) : val(v) {}
319-
320-
size_t get_val() const { return val; }
321-
322-
private:
323-
size_t val;
324-
};
325-
326-
{
327-
328-
PyCFunction func = [](PyObject *, PyObject *) -> PyObject * {
329-
std::cout << "Calling the simple func" << std::endl;
330-
331-
return PyLong_FromLong(34);
332-
};
333-
334-
auto def = new PyMethodDef();
335-
std::memset(def, 0, sizeof(PyMethodDef));
336-
def->ml_name = "testing_no_except";
337-
def->ml_meth = func;
338-
def->ml_flags = METH_VARARGS;
339-
340-
PyObject *m_ptr = PyCFunction_NewEx(def, nullptr, nullptr);
341-
m.add_object("simple_func", m_ptr);
342-
}
343-
344-
{
345-
346-
PyCFunction func = [](PyObject *, PyObject *) -> PyObject * {
347-
std::cout << "Calling the fancy func" << std::endl;
348-
349-
PyErr_SetString(PyExc_SystemError, "Fancy exception for fancy func");
350-
return nullptr;
351-
};
352-
353-
auto def = new PyMethodDef();
354-
std::memset(def, 0, sizeof(PyMethodDef));
355-
def->ml_name = "testing_except";
356-
def->ml_meth = func;
357-
def->ml_flags = METH_VARARGS;
358-
359-
PyObject *m_ptr = PyCFunction_NewEx(def, nullptr, nullptr);
360-
m.add_object("fancy_func", m_ptr);
361-
}
362-
363-
{
364-
custom_function_type *t = new custom_function_type();
365-
PyObject *om = (PyObject *) PyObject_New(custom_function, (PyTypeObject *) t);
366-
367-
m.add_object("custom_fancy_func", om);
368-
}
369-
370-
m.add_object("simple_val", py::cast(3));
371-
372-
m.def("foo_helper", [](const Simpler &s) {
373-
std::cout << "Calling simpler helper " << s.get_val() << std::endl;
374-
});
375-
m.def("foo_helper2",
376-
[](const py::handle &s) { std::cout << "Calling simpler helper2 " << std::endl; });
377-
m.def("foo_helper3",
378-
[](const py::handle &s) { std::cout << "Calling simpler helper3 " << std::endl; });
379-
m.def("foo_helper_no_except", [](const py::object &s) {
380-
std::cout << "Calling simpler helper no except " << std::endl;
381-
});
382-
py::class_<Simpler>(m, "Simpler").def(py::init<size_t>());
383-
384260
py::class_<Sequence>(m, "Sequence")
385261
.def(py::init<size_t>())
386262
.def(py::init<const std::vector<float> &>())
@@ -436,11 +312,6 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
436312
start += step;
437313
}
438314
})
439-
.def("foo",
440-
[](Sequence &s) -> void { std::cout << "Calling it " << s.size() << std::endl; })
441-
.def_static(
442-
"foo_static",
443-
[](Sequence &s) -> void { std::cout << "Calling static " << s.size() << std::endl; })
444315
/// Comparisons
445316
.def(py::self == py::self)
446317
.def(py::self != py::self)

tests/test_sequences_and_iterators.py

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import gc
2-
import weakref
3-
41
import pytest
52
from pytest import approx
63

@@ -98,55 +95,6 @@ def test_sliceable():
9895
assert sliceable[50:60:-1] == (50, 60, -1)
9996

10097

101-
counter = None
102-
103-
104-
class NobodyCares:
105-
def __init__(self):
106-
print("Constructing it!")
107-
self.x = 3
108-
109-
def foo(self):
110-
raise RuntimeError("This is really bad")
111-
112-
def blah(self):
113-
print("Got it!")
114-
115-
def __del__(self):
116-
print("This has been deleted")
117-
118-
119-
def test_sequence_simpler2():
120-
global counter
121-
counter = 0
122-
it = NobodyCares()
123-
124-
def done(b):
125-
print("Deleted the it?")
126-
127-
a = weakref.ref(it, done)
128-
try:
129-
it.foo()
130-
except Exception as e:
131-
print("Got exception ", e)
132-
del e
133-
assert counter == 1
134-
print_ref(a)
135-
del it
136-
gc.collect()
137-
assert counter == 0
138-
139-
140-
def print_ref(it):
141-
item = it()
142-
if item is None:
143-
return
144-
referrers = gc.get_referrers(it())
145-
print("referrers: ")
146-
for referrer in referrers:
147-
print(referrer)
148-
149-
15098
def test_sequence():
15199
cstats = ConstructorStats.get(m.Sequence)
152100

0 commit comments

Comments
 (0)