Commit Graph

2926 Commits

Author SHA1 Message Date
Jason Rhinelander
617fbcfc1e Fix stl_bind to support movable, non-copyable value types (#490)
This commit includes the following changes:

* Don't provide make_copy_constructor for non-copyable container

make_copy_constructor currently fails for various stl containers (e.g.
std::vector, std::unordered_map, std::deque, etc.) when the container's
value type (e.g. the "T" or the std::pair<K,T> for a map) is
non-copyable.  This adds an override that, for types that look like
containers, also requires that the value_type be copyable.

* stl_bind.h: make bind_{vector,map} work for non-copy-constructible types

Most stl_bind modifiers require copying, so if the type isn't copy
constructible, we provide a read-only interface instead.

In practice, this means that if the type is non-copyable, it will be,
for all intents and purposes, read-only from the Python side (but
currently it simply fails to compile with such a container).

It is still possible for the caller to provide an interface manually
(by defining methods on the returned class_ object), but this isn't
something stl_bind can handle because the C++ code to construct values
is going to be highly dependent on the container value_type.

* stl_bind: copy only for arithmetic value types

For non-primitive types, we may well be copying some complex type, when
returning by reference is more appropriate.  This commit returns by
internal reference for all but basic arithmetic types.

* Return by reference whenever possible

Only if we definitely can't--i.e. std::vector<bool>--because v[i]
returns something that isn't a T& do we copy; for everything else, we
return by reference.

For the map case, we can always return by reference (at least for the
default stl map/unordered_map).
2016-11-15 12:30:38 +01:00
Wenzel Jakob
06bd27f536 import size_t into pybind11 namespace (fixes #498) 2016-11-15 06:37:39 +01:00
Jason Rhinelander
0780655808 Fix test compilation failure under gcc 4.9 (#496) 2016-11-13 10:41:31 +09:00
Jason Rhinelander
920e0e349d Add cmake option to override tests (#489)
When working on some particular feature, it's nice to be able to disable
all the tests except for the one I'm working on; this is currently
possible by editing tests/CMakeLists.txt, and commenting out the tests
you don't want.

This commit goes a step further by letting you give a list of tests you
do want when invoking cmake, e.g.:

    cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_pickling.cpp" ..

changes the build to build just those two tests (and changes the `pytest`
target to invoke just the two associated tests).

This persists in the build directory until you disable it again by
running cmake with `-DPYBIND11_TEST_OVERRIDE=`.  It also adds a message
after the pytest output to remind you that it is in effect:

    Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect
2016-11-13 09:10:53 +09:00
Wenzel Jakob
5e1c0445cf include backtrace in pybind11::detail::error_string (#494) 2016-11-12 16:57:30 +09:00
Jason Rhinelander
219b10ac99 Update travis and appveyor builds to eigen 3.3.0 (#491) 2016-11-11 16:15:38 +09:00
Wenzel Jakob
2b92a49115 updated check-style.sh docs 2016-11-08 10:58:22 +01:00
Wenzel Jakob
cc4efe69c2 more code style checks in Travis CI :) 2016-11-08 10:53:30 +01:00
Wenzel Jakob
fe40dfe67d address number caster regression (fixes #484) 2016-11-07 15:59:01 +01:00
Jason Rhinelander
c07ec31edf Don't construct unique_ptr around unowned pointers (#478)
If we need to initialize a holder around an unowned instance, and the
holder type is non-copyable (i.e. a unique_ptr), we currently construct
the holder type around the value pointer, but then never actually
destruct the holder: the holder destructor is called only for the
instance that actually has `inst->owned = true` set.

This seems no pointer, however, in creating such a holder around an
unowned instance: we never actually intend to use anything that the
unique_ptr gives us: and, in fact, do not want the unique_ptr (because
if it ever actually got destroyed, it would cause destruction of the
wrapped pointer, despite the fact that that wrapped pointer isn't
owned).

This commit changes the logic to only create a unique_ptr holder if we
actually own the instance, and to destruct via the constructed holder
whenever we have a constructed holder--which will now only be the case
for owned-unique-holder or shared-holder types.

Other changes include:

* Added test for non-movable holder constructor/destructor counts

The three alive assertions now pass, before #478 they fail with counts
of 2/2/1 respectively, because of the unique_ptr that we don't want and
don't destroy (because we don't *want* its destructor to run).

* Return cstats reference; fix ConstructStats doc

Small cleanup to the #478 test code, and fix to the ConstructStats
documentation (the static method definition should use `reference` not
`reference_internal`).

* Rename inst->constructed to inst->holder_constructed

This makes it clearer exactly what it's referring to.
2016-11-06 19:12:48 +01:00
Wenzel Jakob
e916d846bf minor: have enum::export_values() return a reference to *this as usual 2016-11-04 16:51:14 +01:00
Jason Rhinelander
f1b44a051a <optional> requires -std=c++17 (#479)
There are now more places than just descr.h that make use of these.
The new macro isn't quite the same: the old one only tested for a
couple features, while the new one checks for the __cplusplus version
(but doesn't even try to enable C++14 for MSVC/ICC).

g++ 7 adds <optional>, but including it in C++14 mode isn't allowed
(just as including <experimental/optional> isn't allowed in C++11 mode).
(This wasn't triggered in g++-6 because it doesn't provide <optional>
yet.)
2016-11-04 14:49:37 +01:00
Jason Rhinelander
dc0b4bd2c9 Add debugging info about .so size to build output (#477)
* Add debugging info about so size to build output

This adds a small python script to tools that captures before-and-after
.so sizes between builds and outputs this in the build output via a
string such as:

------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 924696 (decrease of 73680 bytes = 7.38%)

------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 (increase of 73680 bytes = 7.97%)

------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 (no change)

Or, if there was no .so during the build, just the .so size by itself:

------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376

This allows you to, for example, build, checkout a different branch,
rebuild, and easily see exactly the change in the pybind11_tests.so
size.

It also allows looking at the travis and appveyor build logs to get an
idea of .so/.dll sizes across different build systems.

* Minor libsize.py script changes

- Use RAII open
- Remove unused libsize=-1
- Report change as [+-]xyz bytes = [+-]a.bc%
2016-11-04 14:47:41 +01:00
Wenzel Jakob
45e6e6f6eb add note about custom type casters (fixes #480) 2016-11-04 11:06:22 +01:00
Jason Rhinelander
12edaaa66a Only enable std::optional if compiling in >= C++14 (#476) 2016-11-03 16:17:11 +01:00
Ivan Smirnov
44a69f78cf std::experimental::optional (#475)
* Add type caster for std::experimental::optional

* Add tests for std::experimental::optional

* Support both <optional> / <experimental/optional>

* Mention std{::experimental,}::optional in the docs
2016-11-03 13:42:46 +01:00
Wenzel Jakob
bd560acf40 smart pointer refcount fix by @dean0x7d with slight modifications (fixes #471) 2016-11-03 11:53:35 +01:00
Wenzel Jakob
0a9ef9c300 Merge pull request #472 from aldanor/feature/shared-dtypes
Support for sharing dtypes across extensions + public shared data API
2016-11-03 11:08:50 +01:00
Ivan Smirnov
cc8ff16547 Move register_dtype() outside of the template
(avoid code bloat if possible)
2016-11-03 09:35:05 +00:00
Ivan Smirnov
f95fda0eb2 Add docs re: shared data API 2016-11-03 09:35:05 +00:00
Ivan Smirnov
2dbf029705 Add public shared_data API
NumPy internals are stored under "_numpy_internals" key.
2016-11-03 09:35:05 +00:00
Ivan Smirnov
c546655dc2 Use pytest fixtures in numpy dtypes test module 2016-11-03 09:35:05 +00:00
Ivan Smirnov
2184f6d4d6 NumPy dtypes are now shared across extensions 2016-11-03 09:35:05 +00:00
Wenzel Jakob
a743ead455 Merge pull request #474 from aldanor/feature/numpy-dtype-ex
Overriding field names when binding structured dtypes
2016-11-03 09:44:30 +01:00
Ivan Smirnov
abd3429ce9 Add a test for numpy dtypes with custom names 2016-11-01 13:29:32 +00:00
Ivan Smirnov
e8b50360fe Add dtype binding macro that allows setting names
PYBIND11_NUMPY_DTYPE_EX(Type, F1, "N1", F2, "N2", ...)
2016-11-01 13:27:35 +00:00
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