Commit Graph

6 Commits

Author SHA1 Message Date
Dean Moldovan b4498ef44d Add py::isinstance<T>(obj) for generalized Python type checking
Allows checking the Python types before creating an object instead of
after. For example:
```c++
auto l = list(ptr, true);
if (l.check())
   // ...
```
The above is replaced with:
```c++
if (isinstance<list>(ptr)) {
    auto l = reinterpret_borrow(ptr);
    // ...
}
```

This deprecates `py::object::check()`. `py::isinstance()` covers the
same use case, but it can also check for user-defined types:
```c++
class Pet { ... };
py::class_<Pet>(...);

m.def("is_pet", [](py::object obj) {
    return py::isinstance<Pet>(obj); // works as expected
});
```
2016-11-17 08:55:42 +01:00
Wenzel Jakob e99ebaedcf nicer error message for invalid function arguments 2016-09-19 13:43:43 +02:00
Jason Rhinelander 0e489777ff Added a test to detect invalid RTTI caching
The current inheritance testing isn't sufficient to detect a cache
failure; the test added here breaks PR #390, which caches the
run-time-determined return type the first time a function is called,
then reuses that cached type even though the run-time type could be
different for a future call.
2016-09-11 18:41:28 -04:00
Jason Rhinelander 6b52c838d7 Allow passing base types as a template parameter
This allows a slightly cleaner base type specification of:

    py::class_<Type, Base>("Type")

as an alternative to

    py::class_<Type>("Type", py::base<Base>())

As with the other template parameters, the order relative to the holder
or trampoline types doesn't matter.

This also includes a compile-time assertion failure if attempting to
specify more than one base class (but is easily extendible to support
multiple inheritance, someday, by updating the class_selector::set_bases
function to set multiple bases).
2016-09-06 20:34:24 -04:00
Dean Moldovan 382db5b2e7 Move inheritance tests into the proper file 2016-08-19 13:19:38 +02:00
Dean Moldovan a0c1ccf0a9 Port tests to pytest
Use simple asserts and pytest's powerful introspection to make testing
simpler. This merges the old .py/.ref file pairs into simple .py files
where the expected values are right next to the code being tested.

This commit does not touch the C++ part of the code and replicates the
Python tests exactly like the old .ref-file-based approach.
2016-08-19 13:19:38 +02:00