1. 24 Jun, 2017 4 commits
    • Bruce Merry's avatar
      Hold strong references to keep_alive patients · 9d698f7f
      Bruce Merry authored
      This fixes #856. Instead of the weakref trick, the internals structure
      holds an unordered_map from PyObject* to a vector of references. To
      avoid the cost of the unordered_map lookup for objects that don't have
      any keep_alive patients, a flag is added to each instance to indicate
      whether there is anything to do.
      9d698f7f
    • Jason Rhinelander's avatar
      Use std::type_info::name() for type lookups outside stdlibc++ · 21966967
      Jason Rhinelander authored
      Using `std::type_info::operator==` fails under libc++ because the .so
      is loaded with RTLD_LOCAL.  libc++ considers types under such .sos
      distinct, and so comparing typeid() values directly isn't going to work.
      
      This adds a custom hasher and equality class for the type lookup maps
      when not under stdlibc++, and adds a `detail::same_type` function to
      perform the equality test.  It also converts a few pointer arguments to
      const lvalue references, particularly since doing the pointer
      comparison wasn't technically valid to being with (though in practice,
      appeared to work everywhere).
      
      This fixes #912.
      21966967
    • Dean Moldovan's avatar
      Fix GIL release and acquire when embedding the interpreter · 2d6116b5
      Dean Moldovan authored
      Fixes a race condition when multiple threads try to acquire the GIL
      before `detail::internals` have been initialized. `gil_scoped_release`
      is now tasked with initializing `internals` (guaranteed single-threaded)
      to ensure the safety of subsequent `acquire` calls from multiple threads.
      2d6116b5
    • Jason Rhinelander's avatar
      f42af24a
  2. 23 Jun, 2017 2 commits
  3. 15 Jun, 2017 1 commit
  4. 12 Jun, 2017 1 commit
    • Jason Rhinelander's avatar
      Support multiple inheritance from python · e45c2114
      Jason Rhinelander authored
      This commit allows multiple inheritance of pybind11 classes from
      Python, e.g.
      
          class MyType(Base1, Base2):
              def __init__(self):
                  Base1.__init__(self)
                  Base2.__init__(self)
      
      where Base1 and Base2 are pybind11-exported classes.
      
      This requires collapsing the various builtin base objects
      (pybind11_object_56, ...) introduced in 2.1 into a single
      pybind11_object of a fixed size; this fixed size object allocates enough
      space to contain either a simple object (one base class & small* holder
      instance), or a pointer to a new allocation that can contain an
      arbitrary number of base classes and holders, with holder size
      unrestricted.
      
      * "small" here means having a sizeof() of at most 2 pointers, which is
      enough to fit unique_ptr (sizeof is 1 ptr) and shared_ptr (sizeof is 2
      ptrs).
      
      To minimize the performance impact, this repurposes
      `internals::registered_types_py` to store a vector of pybind-registered
      base types.  For direct-use pybind types (e.g. the `PyA` for a C++ `A`)
      this is simply storing the same thing as before, but now in a vector;
      for Python-side inherited types, the map lets us avoid having to do a
      base class traversal as long as we've seen the class before.  The
      change to vector is needed for multiple inheritance: Python types
      inheriting from multiple registered bases have one entry per base.
      e45c2114
  5. 10 Jun, 2017 2 commits
    • Dean Moldovan's avatar
      Fix py::make_iterator's __next__() for past-the-end calls · caedf74a
      Dean Moldovan authored
      Fixes #896.
      
      From Python docs: "Once an iterator’s `__next__()` method raises
      `StopIteration`, it must continue to do so on subsequent calls.
      Implementations that do not obey this property are deemed broken."
      caedf74a
    • Ben Frederickson's avatar
      Fix passing in utf8 encoded strings with python 2 · 74b501cd
      Ben Frederickson authored
      Passing utf8 encoded strings from python to a C++ function taking a
      std::string was broken.  The previous version was trying to call
      'PyUnicode_FromObject' on this data, which failed to convert the string
      to unicode with the default ascii codec. Also this incurs an unnecessary
      conversion to unicode for data this is immediately converted back to
      utf8.
      
      Fix by treating python 2 strings the same python 3 bytes objects, and just
      copying over the data if possible.
      74b501cd
  6. 08 Jun, 2017 3 commits
    • Jason Rhinelander's avatar
      Remove feature macro for <experimental/optional> · 0365d491
      Jason Rhinelander authored
      libc++ 3.8 (and possibly others--including the derived version on OS X),
      doesn't define the macro, but does support std::experimental::optional.
      This removes the extra macro check and just assumes the header existing
      is enough, which is what we do for <optional> and <variant>.
      0365d491
    • Dean Moldovan's avatar
      Enable detection of private operator new on MSVC · e27ea47c
      Dean Moldovan authored
      MSVC 2015 Update 3 and 2017 can handle enough expression SFINAE
      to make this work now.
      e27ea47c
    • Jason Rhinelander's avatar
      Destroy internals if created during Py_Finalize() · 4edb1ce2
      Jason Rhinelander authored
      Py_Finalize could potentially invoke code that calls `get_internals()`,
      which could create a new internals object if one didn't exist.
      `finalize_interpreter()` didn't catch this because it only used the
      pre-finalize interpreter pointer status; if this happens, it results in
      the internals pointer not being properly destroyed with the interpreter,
      which leaks, and also causes a `get_internals()` under a future
      interpreter to return an internals object that is wrong in various ways.
      4edb1ce2
  7. 07 Jun, 2017 1 commit
  8. 06 Jun, 2017 1 commit
    • eirrgang's avatar
      Explicitly define copy/move constructors for accessor · 91bbe2f2
      eirrgang authored
      `accessor` currently relies on an implicit default copy constructor, but that is deprecated in C++11 when a copy assignment operator is present and can, in some cases, raise deprecation warnings (see #888).  This commit explicitly specifies the default copy constructor and also adds a default move constructor.
      91bbe2f2
  9. 30 May, 2017 1 commit
    • Jason Rhinelander's avatar
      std::reference_wrapper: non-generic types; no None · acedd6c7
      Jason Rhinelander authored
      This reimplements the std::reference_wrapper<T> caster to be a shell
      around the underlying T caster (rather than assuming T is a generic
      type), which lets it work for things like `std::reference_wrapper<int>`
      or anything else custom type caster with a lvalue cast operator.
      
      This also makes it properly fail when None is provided, just as an
      ordinary lvalue reference argument would similarly fail.
      
      This also adds a static assert to test that T has an appropriate type
      caster.  It triggers for casters like `std::pair`, which have
      return-by-value cast operators.  (In theory this could be supported by
      storing a local temporary for such types, but that's beyond the scope
      of this PR).
      
      This also replaces `automatic` or `take_ownership` return value policies
      with `automatic_reference` as taking ownership of a reference inside a
      reference_wrapper is not valid.
      acedd6c7
  10. 29 May, 2017 3 commits
    • Dean Moldovan's avatar
      Replace PYBIND11_PLUGIN with PYBIND11_MODULE · 443ab594
      Dean Moldovan authored
      This commit also adds `doc()` to `object_api` as a shortcut for the
      `attr("__doc__")` accessor.
      
      The module macro changes from:
      ```c++
      PYBIND11_PLUGIN(example) {
          pybind11::module m("example", "pybind11 example plugin");
          m.def("add", [](int a, int b) { return a + b; });
          return m.ptr();
      }
      ```
      
      to:
      
      ```c++
      PYBIND11_MODULE(example, m) {
          m.doc() = "pybind11 example plugin";
          m.def("add", [](int a, int b) { return a + b; });
      }
      ```
      
      Using the old macro results in a deprecation warning. The warning
      actually points to the `pybind11_init` function (since attributes
      don't bind to macros), but the message should be quite clear:
      "PYBIND11_PLUGIN is deprecated, use PYBIND11_MODULE".
      443ab594
    • Yannick Jadoul's avatar
      Convenience constructor templates for buffer_info (#860) · b700c5d6
      Yannick Jadoul authored
      * Added template constructors to buffer_info that can deduce the item size, format string, and number of dimensions from the pointer type and the shape container
      
      * Implemented actual buffer_info constructor as private delegate constructor taking rvalue reference as a workaround for the evaluation order move problem on GCC 4.8
      b700c5d6
    • Dean Moldovan's avatar
      Fix buffer protocol inheritance · 427e4afc
      Dean Moldovan authored
      Fixes #878.
      427e4afc
  11. 28 May, 2017 2 commits
  12. 27 May, 2017 1 commit
    • Bruce Merry's avatar
      Avoid explicitly resetting a std::[experimental::]optional · 46dbee7d
      Bruce Merry authored
      Now that #851 has removed all multiple uses of a caster, it can just use
      the default-constructed value with needing a reset. This fixes two
      issues:
      
      1. With std::experimental::optional (at least under GCC 5.4), the `= {}`
      would construct an instance of the optional type and then move-assign
      it, which fails if the value type isn't move-assignable.
      
      2. With older versions of Boost, the `= {}` could fail because it is
      ambiguous, allowing construction of either `boost::none` or the value
      type.
      46dbee7d
  13. 25 May, 2017 6 commits
    • Bruce Merry's avatar
      Fix invalid memory access in vector insert method · eee4f4fc
      Bruce Merry authored
      The stl_bind.h wrapper for `Vector.insert` neglected to do a bounds
      check.
      eee4f4fc
    • Bruce Merry's avatar
      Fix quadratic-time behaviour of vector extend · 6a0f6c40
      Bruce Merry authored
      The "extend" method for vectors defined in stl_bind.h used `reserve` to
      allocate space for the extra growth. While this can sometimes make a
      constant-factor improvement in performance, it can also cause
      construction of a vector by repeated extension to take quadratic rather
      than linear time, as memory is reallocated in small increments rather
      than on an exponential schedule. For example, this Python code would
      take time proportional to the square of the trip count:
      
      ```python
      a = VectorInt([1, 2, 3])
      b = VectorInt()
      for i in range(100000):
          b.extend(a)
      ```
      
      This commit removes the `reserve` call. The alternative would be to try
      to add some smarter heuristics, but the standard library may well have
      its own heuristics (the iterators are random access iterators, so it can
      easily determine the number of items being added) and trying to add more
      heuristics on top of that seems like a bad idea.
      6a0f6c40
    • Jason Rhinelander's avatar
      vectorize: pass-through of non-vectorizable args · f3ce00ea
      Jason Rhinelander authored
      This extends py::vectorize to automatically pass through
      non-vectorizable arguments.  This removes the need for the documented
      "explicitly exclude an argument" workaround.
      
      Vectorization now applies to arithmetic, std::complex, and POD types,
      passed as plain value or by const lvalue reference (previously only
      pass-by-value types were supported).  Non-const lvalue references and
      any other types are passed through as-is.
      
      Functions with rvalue reference arguments (whether vectorizable or not)
      are explicitly prohibited: an rvalue reference is inherently not
      something that can be passed multiple times and is thus unsuitable to
      being in a vectorized function.
      
      The vectorize returned value is also now more sensitive to inputs:
      previously it would return by value when all inputs are of size 1; this
      is now amended to having all inputs of size 1 *and* 0 dimensions.  Thus
      if you pass in, for example, [[1]], you get back a 1x1, 2D array, while
      previously you got back just the resulting single value.
      
      Vectorization of member function specializations is now also supported
      via `py::vectorize(&Class::method)`; this required passthrough support
      for the initial object pointer on the wrapping function pointer.
      f3ce00ea
    • Jason Rhinelander's avatar
      array_t: make c_style/f_style work for array creation · 41f8da4a
      Jason Rhinelander authored
      Currently if you construct an `array_t<T, array::f_style>` with a shape
      but not strides you get a C-style array; the only way to get F-style
      strides was to calculate the strides manually.  This commit fixes that
      by adding logic to use f_style strides when the flag is set.
      
      This also simplifies the existing c_style stride logic.
      41f8da4a
    • Jason Rhinelander's avatar
      Add and use detail::remove_reference_t · 129a7256
      Jason Rhinelander authored
      Adds `remove_reference_t` and converts various `typename
      std::remove_reference<...>::type` to using it.
      129a7256
    • Jason Rhinelander's avatar
      Add PYBIND11_EXPAND_SIDE_EFFECTS macro · 926e2cf3
      Jason Rhinelander authored
      This allows calling of functions (typically void) over a parameter
      pack, replacing usage such as:
      
          bool unused[] = { (voidfunc(param_pack_arg), false)..., false };
          (void) unused;
      
      with a much cleaner:
      
          PYBIND11_EXPAND_SIDE_EFFECTS(voidfunc(param_pack_arg));
      926e2cf3
  14. 24 May, 2017 2 commits
    • Jason Rhinelander's avatar
      Allow py::arg().none(false) argument attribute · 4e1e4a58
      Jason Rhinelander authored
      This attribute lets you disable (or explicitly enable) passing None to
      an argument that otherwise would allow it by accepting
      a value by raw pointer or shared_ptr.
      4e1e4a58
    • Jason Rhinelander's avatar
      Add movable cast support to type casters · 813d7e86
      Jason Rhinelander authored
      This commit allows type_casters to allow their local values to be moved
      away, rather than copied, when the type caster instance itself is an rvalue.
      
      This only applies (automatically) to type casters using
      PYBIND11_TYPE_CASTER; the generic type type casters don't own their own
      pointer, and various value casters (e.g. std::string, std::pair,
      arithmetic types) already cast to an rvalue (i.e. they return by value).
      
      This updates various calling code to attempt to get a movable value
      whenever the value is itself coming from a type caster about to be
      destroyed: for example, when constructing an std::pair or various stl.h
      containers.  For types that don't support value moving, the cast_op
      falls back to an lvalue cast.
      
      There wasn't an obvious place to add the tests, so I added them to
      test_copy_move_policies, but also renamed it to drop the _policies as it
      now tests more than just policies.
      813d7e86
  15. 22 May, 2017 3 commits
    • Bruce Merry's avatar
      Support pointers to member functions in def_buffer. · fe0cf8b7
      Bruce Merry authored
      Closes #857, by adding overloads to def_buffer that match pointers to
      member functions and wrap them in lambdas.
      fe0cf8b7
    • Jason Rhinelander's avatar
      Style cleanup of javadoc-style comments · 37b2383a
      Jason Rhinelander authored
      This changes javadoc-style documenting comments from:
      
          /** Text starts here
           * and continues here
           */
      
      to:
      
          /**
           * Test starts here
           * and continues here
           */
      
      which looks a little better, and also matches the javadoc-recommended
      way of writing documenting comments.
      37b2383a
    • Jason Rhinelander's avatar
      Use dynamic cast for shared_from_this holder init · b8ac4383
      Jason Rhinelander authored
      Using a dynamic_cast instead of a static_cast is needed to safely cast
      from a base to a derived type.  The previous static_pointer_cast isn't
      safe, however, when downcasting (and fails to compile when downcasting
      with virtual inheritance).
      
      Switching this to always use a dynamic_pointer_cast shouldn't incur any
      additional overhead when a static_pointer_cast is safe (i.e. when
      upcasting, or self-casting): compilers don't need RTTI checks in those
      cases.
      b8ac4383
  16. 21 May, 2017 3 commits
    • Jason Rhinelander's avatar
      Define both __div__ and __truediv__ for Python 2 · 35998a03
      Jason Rhinelander authored
      Python 2 requires both `__div__` and `__truediv__` (and variants) for
      compatibility with both regular Python 2 and Python 2 under `from
      __future__ import division`.  Without both, division fails in one or the
      other case.
      35998a03
    • Jason Rhinelander's avatar
      Minor operators.h style cleanups · 1ac51a02
      Jason Rhinelander authored
      - realign \ at end of macro lines
      - use 'using A = B;' rather than 'typedef B A;'
      - use conditional_t
      1ac51a02
    • Jason Rhinelander's avatar
      Fix /= operator under Python 3 · acad05cb
      Jason Rhinelander authored
      The Python method for /= was set as `__idiv__`, which should be
      `__itruediv__` under Python 3.
      
      This wasn't totally broken in that without it defined, Python constructs
      a new object by calling __truediv__.  The operator tests, however,
      didn't actually test the /= operator: when I added it, I saw an extra
      construction, leading to the problem.  This commit also includes tests
      for the previously untested *= operator, and adds some element-wise
      vector multiplication and division operators.
      acad05cb
  17. 20 May, 2017 1 commit
  18. 18 May, 2017 1 commit
  19. 16 May, 2017 1 commit
  20. 11 May, 2017 1 commit