Commit Graph

354 Commits

Author SHA1 Message Date
Ivan Smirnov
afb07e7e92 Code reordering / cleanup only 2016-08-13 12:43:16 +01:00
Ivan Smirnov
f9c0defed7 Add numpy wrappers for char[] and std::array<char> 2016-08-13 12:43:16 +01:00
Ivan Smirnov
103d5eadc3 Remove redundant definition 2016-08-13 12:43:16 +01:00
Ivan Smirnov
098f9aef73 Replace 4096B format buffer with std::string 2016-08-13 12:43:16 +01:00
Ivan Smirnov
b37985ee0c Fix a comment and wrong indentation 2016-08-13 12:43:16 +01:00
Ivan Smirnov
bf2510ee86 Make buffer_info::as_pybuffer a memoryview ctor 2016-08-13 12:43:16 +01:00
Ivan Smirnov
41c3399021 Update npy_format_descriptor::name() 2016-08-13 12:43:16 +01:00
Ivan Smirnov
076b953ccd Restore dtype equivalence sanity check 2016-08-13 12:43:16 +01:00
Ivan Smirnov
8f2f7cd61c Various cleanup 2016-08-13 12:43:16 +01:00
Ivan Smirnov
8fa09cb871 Strip padding fields in dtypes, update the tests 2016-08-13 12:43:16 +01:00
Ivan Smirnov
13022f1b8c Bugfix: pass struct size as itemsize to descriptor
Without this, partially bound structs will have incorrect itemsize.
2016-08-13 12:43:16 +01:00
Ivan Smirnov
eeb4c043f9 Change field descriptor offset type to size_t 2016-08-13 12:43:16 +01:00
Ivan Smirnov
511401599c Use malloc insterad of calloc for numpy arrays 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5412a05cf0 Rename PYBIND11_DTYPE to PYBIND11_NUMPY_DTYPE 2016-08-13 12:43:16 +01:00
Ivan Smirnov
872bd92575 Use proper type for an int literal 2016-08-13 12:43:16 +01:00
Ivan Smirnov
4c9a160a1d Exclude double type from is_pod_struct 2016-08-13 12:43:16 +01:00
Ivan Smirnov
3b803846d5 Add a few comments throughout numpy.h 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2f01f01866 Always allocate at least one element 2016-08-13 12:43:16 +01:00
Ivan Smirnov
b38ca22e94 Add a few braces for clarity 2016-08-13 12:43:16 +01:00
Ivan Smirnov
95545e6256 Change PB11_IMPL prefix to PYBIND11, add comment 2016-08-13 12:43:16 +01:00
Ivan Smirnov
7bdd74a9fb Fix PYBIND11_DTYPE to work with MSVC compiler 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5dc6c5445d Cosmetic: fix indentation 2016-08-13 12:43:16 +01:00
Ivan Smirnov
73f56830f8 Add detail::is_pod_struct<T> helper 2016-08-13 12:43:16 +01:00
Ivan Smirnov
a0e37f250e npy_format_descriptor::format() - fail if unbound 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5a47a16e47 Revert accidental whitespace change 2016-08-13 12:43:16 +01:00
Ivan Smirnov
40eadfeb73 Make npy_format_descriptor backwards-compat
The typenum for non-structured types is still accessible at ::value,
and the dtype object for all types is accessible at ::dtype().
2016-08-13 12:43:16 +01:00
Ivan Smirnov
95e9b12322 Prefix the FIELD_DESCRIPTOR macro 2016-08-13 12:43:16 +01:00
Ivan Smirnov
5e71e17bdf Make changes to format_descriptor backwards-compat
The format strings that are known at compile time are now accessible
via both ::value and ::format(), and format strings for everything
else is accessible via ::format(). This makes it backwards compatible.
2016-08-13 12:43:16 +01:00
Ivan Smirnov
4f164217e4 Add dtype_of<T>() function, update the tests 2016-08-13 12:43:16 +01:00
Ivan Smirnov
036e8cd32f Remove erroneous py:: prefix in numpy.h 2016-08-13 12:43:16 +01:00
Ivan Smirnov
873d267471 Prefix all macros in numpy.h to avoid name clashes 2016-08-13 12:43:16 +01:00
Ivan Smirnov
1f54cd9209 Use object instead of ptrs in numpy descriptors 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2a7acb6d55 Incref descriptors properly when creating arrays 2016-08-13 12:43:16 +01:00
Ivan Smirnov
f5b166d042 Simplify npy_format_descriptor slightly 2016-08-13 12:43:16 +01:00
Ivan Smirnov
80a3785a66 Borrow field descriptors for recarray dtype 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2e1565e414 Add empty recarray test, check for calloc fail 2016-08-13 12:43:16 +01:00
Ivan Smirnov
f10c84eb9b Release format descriptor args before converting 2016-08-13 12:43:16 +01:00
Ivan Smirnov
2488b32066 Add PYBIND11_DTYPE macro for registering dtypes 2016-08-13 12:43:16 +01:00
Ivan Smirnov
fab02efb10 Switch away from typenums for numpy descriptors 2016-08-13 12:43:16 +01:00
Ivan Smirnov
a67c2b52e4 Use memoryview for constructing array from buffer 2016-08-13 12:43:16 +01:00
Ivan Smirnov
ea2755ccdc Use a macro for numpy API definitions 2016-08-13 12:43:16 +01:00
Ivan Smirnov
7709d6b77d Add memoryview type 2016-08-13 12:43:16 +01:00
Ivan Smirnov
42ad328481 Change format_descriptor::value to a static func 2016-08-13 12:43:16 +01:00
Ivan Smirnov
a7e62e1ca6 Add buffer_info::as_pybuffer() method 2016-08-13 12:43:16 +01:00
Ivan Smirnov
3dd325b772 Change npy_format_descriptor typenum to static fn 2016-08-13 12:43:16 +01:00
Jason Rhinelander
5aa85be26e Added pybind11::make_key_iterator for map iteration
This allows exposing a dict-like interface to python code, allowing
iteration over keys via:

    for k in custommapping:
        ...

while still allowing iteration over pairs, so that you can also
implement 'dict.items()' functionality which returns a pair iterator,
allowing:

    for k, v in custommapping.items():
        ...

example-sequences-and-iterators is updated with a custom class providing
both types of iteration.
2016-08-11 21:22:05 -04:00
Jason Rhinelander
e20fc61a33 Silence MSVC warning
PR #329 generates the following warning under MSVC:

    ...\cast.h(202): warning C4456: declaration of 'it' hides previous local declaration

This renames the second iterator to silence it.
2016-08-11 16:23:23 -04:00
Jason Rhinelander
f2ecd8927e Implement reference_internal with a keep_alive
reference_internal requires an `instance` field to track the returned
reference's parent, but that's just a duplication of what
keep_alive<0,1> does, so use a keep alive to do this to eliminate the
duplication.
2016-08-10 12:08:04 -04:00
Jason Rhinelander
1b05ce5bc0 Track registered instances that share a pointer address
The pointer to the first member of a class instance is the same as the
pointer to instance itself; pybind11 has some workarounds for this to
not track registered instances that have a registered parent with the
same address.  This doesn't work everywhere, however: issue #328 is a
failure of this for a mutator operator which resolves its argument to
the parent rather than the child, as is needed in #328.

This commit resolves the issue (and restores tracking of same-address
instances) by changing registered_instances from an unordered_map to an
unordered_multimap that allows duplicate instances for the same pointer
to be recorded, then resolves these differences by checking the type of
each matched instance when looking up an instance.  (A
unordered_multimap seems cleaner for this than a unordered_map<list> or
similar because, the vast majority of the time, the instance will be
unique).
2016-08-09 17:57:59 -04:00
Jason Rhinelander
ed14879a19 Move support for return values of called Python functions
Currently pybind11 always translates values returned by Python functions
invoked from C++ code by copying, even when moving is feasible--and,
more importantly, even when moving is required.

The first, and relatively minor, concern is that moving may be
considerably more efficient for some types.  The second problem,
however, is more serious: there's currently no way python code can
return a non-copyable type to C++ code.

I ran into this while trying to add a PYBIND11_OVERLOAD of a virtual
method that returns just such a type: it simply fails to compile because
this:

    overload = ...
    overload(args).template cast<ret_type>();

involves a copy: overload(args) returns an object instance, and the
invoked object::cast() loads the returned value, then returns a copy of
the loaded value.

We can, however, safely move that returned value *if* the object has the
only reference to it (i.e. if ref_count() == 1) and the object is
itself temporary (i.e. if it's an rvalue).

This commit does that by adding an rvalue-qualified object::cast()
method that allows the returned value to be move-constructed out of the
stored instance when feasible.

This basically comes down to three cases:

- For objects that are movable but not copyable, we always try the move,
  with a runtime exception raised if this would involve moving a value
  with multiple references.
- When the type is both movable and non-trivially copyable, the move
  happens only if the invoked object has a ref_count of 1, otherwise the
  object is copied.  (Trivially copyable types are excluded from this
  case because they are typically just collections of primitive types,
  which can be copied just as easily as they can be moved.)
- Non-movable and trivially copy constructible objects are simply
  copied.

This also adds examples to example-virtual-functions that shows both a
non-copyable object and a movable/copyable object in action: the former
raises an exception if returned while holding a reference, the latter
invokes a move constructor if unreferenced, or a copy constructor if
referenced.

Basically this allows code such as:

    class MyClass(Pybind11Class):
        def somemethod(self, whatever):
            mt = MovableType(whatever)
            # ...
            return mt

which allows the MovableType instance to be returned to the C++ code
via its move constructor.

Of course if you attempt to violate this by doing something like:

    self.value = MovableType(whatever)
    return self.value

you get an exception--but right now, the pybind11-side of that code
won't compile at all.
2016-08-08 13:47:37 -04:00