Commit Graph

76 Commits

Author SHA1 Message Date
Dean Moldovan
5d28dd1194 Support std::shared_ptr holder type out of the box
With this there is no more need for manual user declarations like
`PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)`. Existing ones
will still compile without error -- they will just be ignored silently.

Resolves #446.
2016-10-20 16:19:58 +02:00
Ivan Smirnov
2f3f3687dc Add tests for numpy enum descriptors 2016-10-20 12:51:56 +01:00
Wenzel Jakob
946f897da0 Merge pull request #445 from lsst-dm/master
Accept any sequence type as std::vector (or std::list)
2016-10-15 23:50:06 +02:00
Dean Moldovan
b8cb5ca7bd Fix dynamic attribute inheritance in C++
`PyType_Ready` would usually perform the inheritance for us, but it
can't adjust `tp_basicsize` appropriately.
2016-10-14 18:01:17 +02:00
Wenzel Jakob
5c13749aea Merge pull request #437 from dean0x7d/dynamic-attrs
Add dynamic attribute support
2016-10-14 08:57:12 +02:00
Wenzel Jakob
fac7c09458 NumPy "base" feature: integrated feedback by @aldanor 2016-10-13 10:49:53 +02:00
Wenzel Jakob
369e9b3937 Permit creation of NumPy arrays with a "base" object that owns the data
This patch adds an extra base handle parameter to most ``py::array`` and
``py::array_t<>`` constructors. If specified along with a pointer to
data, the base object will be registered within NumPy, which increases
the base's reference count. This feature is useful to create shallow
copies of C++ or Python arrays while ensuring that the owners of the
underlying can't be garbage collected while referenced by NumPy.

The commit also adds a simple test function involving a ``wrap()``
function that creates shallow copies of various N-D arrays.
2016-10-13 01:03:40 +02:00
Wenzel Jakob
43f6aa6846 added numpy test (minor): check that 'strides' is respected even when creating new arrays
- This actually works with no changes, I just wasn't 100% convinced and
  decided to write a test to see if it's true.
2016-10-12 23:34:13 +02:00
Pim Schellart
d2afe7f001 Accept any sequence type as std::vector (or std::list) 2016-10-12 12:35:36 -04:00
Dean Moldovan
6fccf69360 Add dynamic attribute support 2016-10-11 22:13:02 +02:00
Wenzel Jakob
6a1734af23 minor cmake cleanups 2016-10-09 20:14:23 +02:00
Wenzel Jakob
b55a5c5660 fixed typo in cmake file 2016-10-09 13:51:05 +02:00
Wenzel Jakob
dac3858e7d Make header files viewable in IDEs (fixes #424) 2016-09-29 21:30:00 +02:00
Wenzel Jakob
632dee1e11 Merge pull request #356 from TrentHouliston/master
Add in casts for c++11s chrono classes to pythons datetime
2016-09-27 17:58:34 +02:00
Trent Houliston
253e41ccad Relax constraints on testing to ensure they work in all cases. 2016-09-28 00:59:21 +10:00
Dean Moldovan
2bab5793f7 Later assignments to accessors should not overwrite the original field
`auto var = l[0]` has a strange quirk: `var` is actually an accessor and
not an object, so any later assignment of `var = ...` would modify l[0]
instead of `var`. This is surprising compared to the non-auto assignment
`py::object var = l[0]; var = ...`.

By overloading `operator=` on lvalue/rvalue, the expected behavior is
restored even for `auto` variables.
2016-09-23 02:00:01 +02:00
Dean Moldovan
ea763a57a8 Extend tuple and list accessor interface 2016-09-23 02:00:01 +02:00
Dean Moldovan
242b146a51 Extend attribute and item accessor interface using object_api 2016-09-23 02:00:01 +02:00
Dean Moldovan
865e43034b Make attr and item accessors throw on error instead of returning nullptr
This also adds the `hasattr` and `getattr` functions which are needed
with the new attribute behavior. The new functions behave exactly like
their Python counterparts.

Similarly `object` gets a `contains` method which calls `__contains__`,
i.e. it's the same as the `in` keyword in Python.
2016-09-23 01:40:22 +02:00
Dean Moldovan
568ec6b35a Fix missing smart_ptr test 2016-09-20 11:52:25 +02:00
Wenzel Jakob
d922dffec4 Merge pull request #410 from wjakob/mi
WIP: Multiple inheritance support
2016-09-19 18:55:05 +02:00
Wenzel Jakob
8e5dceb6a6 Multiple inheritance support 2016-09-19 13:45:31 +02:00
Wenzel Jakob
e99ebaedcf nicer error message for invalid function arguments 2016-09-19 13:43:43 +02:00
Wenzel Jakob
7962f30d70 set possible build types in cmake build system 2016-09-17 12:58:18 +02:00
Jason Rhinelander
b3794f1087 Added py::register_exception for simple case (#296)
The custom exception handling added in PR #273 is robust, but is overly
complex for declaring the most common simple C++ -> Python exception
mapping that needs only to copy `what()`.  This add a simpler
`py::register_exception<CppExp>(module, "PyExp");` function that greatly
simplifies the common basic case of translation of a simple CppException
into a simple PythonException, while not removing the more advanced
capabilities of defining custom exception handlers.
2016-09-16 08:04:15 +02:00
Trent Houliston
2f597687e7 Changed non system clocks to be time deltas
Allowed durations and non system clocks to be set from floats.
2016-09-13 20:40:28 +10:00
Trent Houliston
8fe2fa7eba Increase the amount of time to execute the functions to 50ms 2016-09-13 19:58:05 +10:00
Trent Houliston
352149e892 Refactor the chrono cast functions into chrono.h.
Add unit tests and documentation for the chrono cast.
2016-09-13 19:58:05 +10: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
Wenzel Jakob
f22683806e Merge pull request #400 from jagerman/add-ref-virtual-macros
Add a way to deal with copied value references
2016-09-12 06:32:39 +09:00
Wenzel Jakob
b2eda9ac7c Merge pull request #408 from dean0x7d/exc-destructors
Fix Python C API calls in desctuctors triggered by error_already_set
2016-09-11 21:33:33 +09:00
Wenzel Jakob
e3c297f03e Merge pull request #407 from wjakob/fix_iterator
parameterize iterators by return value policy (fixes #388)
2016-09-11 20:02:32 +09:00
Jason Rhinelander
7dfb932e70 Update OVERLOAD macros to support ref/ptr return type overloads
This adds a static local variable (in dead code unless actually needed)
in the overload code that is used for storage if the overload is for
some convert-by-value type (such as numeric values or std::string).

This has limitations (as written up in the advanced doc), but is better
than simply not being able to overload reference or pointer methods.
2016-09-11 01:21:53 -04:00
Jason Rhinelander
116d37c9ba Use 'override' rather than 'virtual' for overrides
Minor change that makes this example more compliant with the C++ Core
Guidelines.
2016-09-11 01:16:19 -04:00
Ivan Smirnov
aca6bcaea5 Add tests for array data access /index methods 2016-09-10 16:42:17 +01:00
Ivan Smirnov
91b3d681ad Expose some dtype/array attributes via NumPy C API 2016-09-10 16:24:00 +01:00
Dean Moldovan
135ba8deaf Make error_already_set fetch and hold the Python error
This clears the Python error at the error_already_set throw site, thus
allowing Python calls to be made in destructors which are triggered by
the exception. This is preferable to the alternative, which would be
guarding every Python API call with an error_scope.

This effectively flips the behavior of error_already_set. Previously,
it was assumed that the error stays in Python, so handling the exception
in C++ would require explicitly calling PyErr_Clear(), but nothing was
needed to propagate the error to Python. With this change, handling the
error in C++ does not require a PyErr_Clear() call, but propagating the
error to Python requires an explicit error_already_set::restore().

The change does not break old code which explicitly calls PyErr_Clear()
for cleanup, which should be the majority of user code. The need for an
explicit restore() call does break old code, but this should be mostly
confined to the library and not user code.
2016-09-10 12:08:32 +02:00
Wenzel Jakob
b212f6c416 parameterize iterators by return value policy (fixes #388) 2016-09-10 17:16:16 +09:00
Wenzel Jakob
1f2e417d8c Merge pull request #403 from jagerman/alias-initialization
Implement py::init_alias<>() constructors
2016-09-10 16:12:19 +09:00
Wenzel Jakob
382484ae56 operators should return NotImplemented given unsupported input (fixes #393) 2016-09-10 15:34:26 +09:00
Jason Rhinelander
ec62d977c4 Implement py::init_alias<>() constructors
This commit adds support for forcing alias type initialization by
defining constructors with `py::init_alias<arg1, arg2>()` instead of
`py::init<arg1, arg2>()`.  Currently py::init<> only results in Alias
initialization if the type is extended in python, or the given
arguments can't be used to construct the base type, but can be used to
construct the alias.  py::init_alias<>, in contrast, always invokes the
constructor of the alias type.

It looks like this was already the intention of
`py::detail::init_alias`, which was forward-declared in
86d825f330, but was apparently never
finished: despite the existance of a .def method accepting it, the
`detail::init_alias` class isn't actually defined anywhere.

This commit completes the feature (or possibly repurposes it), allowing
declaration of classes that will always initialize the trampoline which
is (as I argued in #397) sometimes useful.
2016-09-09 03:04:09 -04:00
Jason Rhinelander
9c6859ee6e Fix type alias initialization
Type alias for alias classes with members didn't work properly: space
was only allocated for sizeof(type), but if we want to be able to put a
type_alias instance there, we need sizeof(type_alias), but
sizeof(type_alias) > sizeof(type) whenever type_alias has members.
2016-09-08 11:10:18 -04:00
Wenzel Jakob
5812d64ba2 Merge pull request #394 from jagerman/fix-ref-heap-casts
Fix ref heap casts
2016-09-08 09:05:15 +09:00
Ivan Smirnov
67b54894b2 Set error if it's not set in error_already_set() 2016-09-07 21:10:16 +01:00
Jason Rhinelander
c03db9bad9 Fail static_assert when trying to reference non-referencable types
The previous commit to address #392 triggers a compiler warning about
returning a reference to a local variable, which is *not* a false alarm:
the following:

    py::cast<int &>(o)

(which happens internally in an overload declaration) really is
returning a reference to a local, because the cast operators for the
type_caster for numeric types returns a reference to its own member.

This commit adds a static_assert to make that a compilation failure
rather than returning a reference into about-to-be-freed memory.

Incidentally, this is also a fix for #219, which is exactly the same
issue: we can't reference numeric primitives that are cast from
wrappers around python numeric types.
2016-09-07 16:07:59 -04:00
Jason Rhinelander
56f717756b Fix type caster for heap reference types
Need to use the intrinsic type, not the raw type.

Fixes #392.
2016-09-07 14:14:11 -04:00
Wenzel Jakob
6fd3132e81 Merge pull request #385 from jagerman/relax-class-arguments
Allow arbitrary class_ template option ordering
2016-09-07 23:49:00 +09: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
81511be341 Replace std::cout with py::print in tests
With this change both C++ and Python write to sys.stdout which resolves
the capture issues noted in #351. Therefore, the related workarounds are
removed.
2016-09-07 01:25:27 +02:00
Jason Rhinelander
5fffe200e3 Allow arbitrary class_ template option ordering
The current pybind11::class_<Type, Holder, Trampoline> fixed template
ordering results in a requirement to repeat the Holder with its default
value (std::unique_ptr<Type>) argument, which is a little bit annoying:
it needs to be specified not because we want to override the default,
but rather because we need to specify the third argument.

This commit removes this limitation by making the class_ template take
the type name plus a parameter pack of options.  It then extracts the
first valid holder type and the first subclass type for holder_type and
trampoline type_alias, respectively.  (If unfound, both fall back to
their current defaults, `std::unique_ptr<type>` and `type`,
respectively).  If any unmatched template arguments are provided, a
static assertion fails.

What this means is that you can specify or omit the arguments in any
order:

    py::class_<A, PyA> c1(m, "A");
    py::class_<B, PyB, std::shared_ptr<B>> c2(m, "B");
    py::class_<C, std::shared_ptr<C>, PyB> c3(m, "C");

It also allows future class attributes (such as base types in the next
commit) to be passed as class template types rather than needing to use
a py::base<> wrapper.
2016-09-06 12:22:13 -04:00