Commit Graph

950 Commits

Author SHA1 Message Date
Dean Moldovan
03f627ebb1 Make reference(_internal) the default return value policy for properties (#473)
* Make reference(_internal) the default return value policy for properties

Before this, all `def_property*` functions used `automatic` as their
default return value policy. This commit makes it so that:

 * Non-static properties use `reference_interal` by default, thus
   matching `def_readonly` and `def_readwrite`.

 * Static properties use `reference` by default, thus matching
   `def_readonly_static` and `def_readwrite_static`.

In case `cpp_function` is passed to any `def_property*`, its policy will
be used instead of any defaults. User-defined arguments in `extras`
still have top priority and will override both the default policies and
the ones from `cpp_function`.

Resolves #436.

* Almost always use return_value_policy::move for rvalues

For functions which return rvalues or rvalue references, the only viable
return value policies are `copy` and `move`. `reference(_internal)` and
`take_ownership` would take the address of a temporary which is always
an error.

This commit prevents possible user errors by overriding the bad rvalue
policies with `move`. Besides `move`, only `copy` is allowed, and only
if it's explicitly selected by the user.

This is also a necessary safety feature to support the new default
return value policies for properties: `reference(_internal)`.
2016-11-01 11:44:57 +01:00
Wenzel Jakob
030d10e826 minor style fix 2016-10-28 01:23:42 +02:00
Wenzel Jakob
496feacfd0 pybind11: implicitly convert NumPy integer scalars
The current integer caster was unnecessarily strict and rejected
various kinds of NumPy integer types when calling C++ functions
expecting normal integers. This relaxes the current behavior.
2016-10-28 01:02:46 +02:00
Wenzel Jakob
a6f85eb985 Merge pull request #465 from jagerman/prevent-object-overwriting
Prevent overwriting previous declarations
2016-10-25 10:03:53 +02:00
Jason Rhinelander
6873c202b3 Prevent overwriting previous declarations
Currently pybind11 doesn't check when you define a new object (e.g. a
class, function, or exception) that overwrites an existing one.  If the
thing being overwritten is a class, this leads to a segfault (because
pybind still thinks the type is defined, even though Python no longer
has the type).  In other cases this is harmless (e.g. replacing a
function with an exception), but even in that case it's most likely a
bug.

This code doesn't prevent you from actively doing something harmful,
like deliberately overwriting a previous definition, but detects
overwriting with a run-time error if it occurs in the standard
class/function/exception/def registration interfaces.

All of the additions are in non-template code; the result is actually a
tiny decrease in .so size compared to master without the new test code
(977304 to 977272 bytes), and about 4K higher with the new tests.
2016-10-24 22:45:51 -04:00
Wenzel Jakob
dd9bd7778f Merge pull request #453 from aldanor/feature/numpy-scalars
NumPy scalars to ctypes conversion support
2016-10-25 01:15:25 +02:00
Ivan Smirnov
8f3e045deb Use detail::get_type_info() wherever sensible
This reduces direct access to internals.registered_types_cpp to
just a few places.
2016-10-24 23:55:52 +01:00
Wenzel Jakob
6ba98650e2 a bit of work on the new documentation structure 2016-10-24 23:48:20 +02:00
Ivan Smirnov
a6e6a8b108 Require existing typeinfo for direct conversions
This avoid a hashmap lookup since the pointer to the list of
direct converters is now cached in the typeinfo.
2016-10-23 15:29:10 +01:00
Wenzel Jakob
c0d19192d2 minor indentation change 2016-10-22 13:08:44 -04:00
Wenzel Jakob
f4eec65526 Merge pull request #455 from bennorth/bugfix/bad-delete-if-no-copy-ctor
Bugfix: bad delete if no copy ctor
2016-10-22 19:06:50 +02:00
Ivan Smirnov
43a88f4574 Reraise existing exception if dtype ctor fails 2016-10-22 18:57:07 +02:00
Ivan Smirnov
694269435b Allow implicit casts from literal strings to dtype 2016-10-22 18:57:07 +02:00
Ivan Smirnov
ef5a38044c A few dtype method docstrings 2016-10-22 18:57:07 +02:00
Ivan Smirnov
f70cc112f0 Make dtype from string ctor accept const ref 2016-10-22 18:57:07 +02:00
Jason Rhinelander
fb7c9fd326 Remove obsolete example reference (#457)
* Remove obsolete example reference
* Make example fully-working (except for #includes)

Fixes #456.
2016-10-22 18:54:33 +02:00
Wenzel Jakob
18e9590ca9 Merge pull request #458 from dean0x7d/fix-property
Fix def_property and related functions
2016-10-22 18:53:29 +02:00
Dean Moldovan
5b7e190fa2 Fix def_property and related functions
Making `cppfunction` explicit broke `def_property` and friends.
The added tests would not compile without an implicit `cppfunction`.
2016-10-21 18:51:14 +02:00
Ben North
bbe45082f4 Test uncopyable static member
Without the previous commit, this test generates a core dump.
2016-10-20 21:32:55 +01:00
Ben North
24a2054dbc Fix wrapper's 'value' and 'owned' if ctor missing
type_caster_generic::cast(): The values of

    wrapper->value
    wrapper->owned

are incorrect in the case that a return value policy of 'copy' is
requested but there is no copy-constructor.  (Similarly 'move'.)  In
particular, if the source object is a static instance, the destructor of
the 'object' 'inst' leads to class_::dealloc() which incorrectly
attempts to 'delete' the static instance.

This commit re-arranges the code to be clearer as to what the values of
'value' and 'owned' should be in the various cases.  Behaviour is
different to previous code only in two situations:

policy = copy but no copy-ctor: Old code leaves 'value = src, owned =
true', which leads to trouble.  New code leaves 'value = nullptr, owned
= false', which is correct.

policy = move but no move- or copy-ctor: old code leaves 'value = src,
owned = true', which leads to trouble.  New code leaves 'value =
nullptr, owned = false', which is correct.
2016-10-20 21:32:55 +01:00
Wenzel Jakob
77898af0f8 Merge pull request #454 from dean0x7d/shared_ptr
Support std::shared_ptr holder type out of the box
2016-10-20 19:16:32 +02:00
Ivan Smirnov
7edd72db24 Disallow registering dtypes multiple times 2016-10-20 16:57:12 +01:00
Ivan Smirnov
ccc69f91f4 Cache direct converters in the generic type caster 2016-10-20 16:52:24 +01:00
Wenzel Jakob
4f30446164 Merge pull request #448 from dean0x7d/docs
Reorganize documentation
2016-10-20 17:51:30 +02:00
Ivan Smirnov
cbbb7830f2 Add a test for NumPy scalar conversion 2016-10-20 16:47:29 +01:00
Ivan Smirnov
85e16262d6 Enable direct conversions with no typeinfo present 2016-10-20 16:46:40 +01:00
Ivan Smirnov
7bf90e8008 Add a direct converter for numpy scalars 2016-10-20 16:11:08 +01:00
Ivan Smirnov
c275ee6b46 Add support for "direct" converters 2016-10-20 16:09:31 +01:00
Ivan Smirnov
ba08db4da5 Import a few more numpy extern symbols 2016-10-20 16:09:10 +01:00
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
Dean Moldovan
f0b0df58a9 Directly compare 3 ways of moving data between C++ and Python 2016-10-20 15:21:34 +02:00
Dean Moldovan
67b52d808e Reorganize documentation 2016-10-20 15:21:34 +02:00
Wenzel Jakob
3eb569f4df Merge pull request #452 from aldanor/feature/numpy-enum
Auto-implement format/numpy descriptors for enum types
2016-10-20 14:41:38 +02:00
Ivan Smirnov
2f3f3687dc Add tests for numpy enum descriptors 2016-10-20 12:51:56 +01:00
Ivan Smirnov
fb74df50c9 Implement format/numpy descriptors for enums 2016-10-20 12:38:43 +01:00
Wenzel Jakob
35995856dd Merge pull request #450 from dean0x7d/explicit-bool
Make operator bool() explicit
2016-10-17 11:59:29 +02:00
Dean Moldovan
c889ebd0e1 Make operator bool() explicit
This prevents unwanted conversions to bool or int such as:
```
py::object my_object;

std::cout << my_object << std::endl; // compiles and prints 0 or 1
int n = my_object; // compiles and is nonsense
```

With `explicit operator bool()` the above cases become compiler errors.
2016-10-17 02:01:53 +02:00
Wenzel Jakob
135fd14928 Merge pull request #449 from jagerman/no-implicit-conversions
Disable most implicit conversion constructors
2016-10-16 22:47:49 +02:00
Jason Rhinelander
12d76600f8 Disable most implicit conversion constructors
We have various classes that have non-explicit constructors that accept
a single argument, which is implicitly making them implicitly
convertible from the argument.  In a few cases, this is desirable (e.g.
implicit conversion of std::string to py::str, or conversion of double
to py::float_); in many others, however, it is unintended (e.g. implicit
conversion of size_t to some pre-declared py::array_t<T> type).

This disables most of the unwanted implicit conversions by marking them
`explicit`, and comments the ones that are deliberately left implicit.
2016-10-16 16:27:42 -04: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
Pim Schellart
fcab621ee4 Merge pull request #4 from lsst-dm/tickets/DM-7115
Accept any sequence type as std::vector
2016-10-15 11:31:19 -04:00
Wenzel Jakob
8b51ed17c8 Merge pull request #444 from dean0x7d/inherit-dynamic-attr
Fix dynamic attribute inheritance in C++
2016-10-14 19:21:55 +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
Dean Moldovan
9273af4f92 Document dynamic attributes 2016-10-14 01:16:40 +02:00
Wenzel Jakob
c01a1c1ade added array::ensure() function wrapping PyArray_FromAny
This convenience function ensures that a py::object is either a
py::array, or the implementation will try to convert it into one. Layout
requirements (such as c_style or f_style) can be also be provided.
2016-10-14 01:08:07 +02:00
Dean Moldovan
22726c9d22 Only allocate dict pointer when needed for dynamic attributes 2016-10-13 23:37:53 +02:00
Wenzel Jakob
00488a3e2c Merge pull request #440 from wjakob/master
Permit creation of NumPy arrays with a "base" object that owns the data
2016-10-13 10:50:35 +02:00
Wenzel Jakob
fac7c09458 NumPy "base" feature: integrated feedback by @aldanor 2016-10-13 10:49:53 +02:00
Wenzel Jakob
c49d6e508a py::print robustness improvements, added import exception class 2016-10-13 10:34:52 +02:00