1. 29 Jul, 2017 3 commits
  2. 23 Jul, 2017 2 commits
    • Ivan Smirnov's avatar
      Implicit conversions to bool + np.bool_ conversion (#925) · e07f7583
      Ivan Smirnov authored
      This adds support for implicit conversions to bool from Python types
      with `__bool__` (Python 3) or `__nonzero__` (Python 2) attributes, and
      adds direct (i.e. non-converting) support for numpy bools.
      e07f7583
    • Jason Rhinelander's avatar
      Add support custom sized operator deletes (#952) · a03408c8
      Jason Rhinelander authored
      If a class doesn't provide a `T::operator delete(void *)` but does have
      a `T::operator delete(void *, size_t)` the latter is invoked by a
      `delete someT`.  Pybind currently only look for and call the former;
      this commit adds detection and calling of the latter when the former
      doesn't exist.
      a03408c8
  3. 16 Jul, 2017 2 commits
    • Jason Rhinelander's avatar
      Support `take_ownership` for custom type casters given a pointer · 60526d46
      Jason Rhinelander authored
      This changes the pointer `cast()` in `PYBIND11_TYPE_CASTER` to recognize
      the `take_ownership` policy: if casting a pointer with take-ownership,
      the `cast()` now recalls `cast()` with a dereferenced rvalue (rather
      than the previous code, which was always calling it with a const lvalue
      reference), and deletes the pointer after the chained `cast()` is
      complete.
      
      This makes code like:
      
          m.def("f", []() { return new std::vector<int>(100, 1); },
              py::return_value_policy::take_ownership);
      
      do the expected thing by taking over ownership of the returned pointer
      (which is deleted once the chained cast completes).
      60526d46
    • Jason Rhinelander's avatar
      Fix regression: container pointers not castable · 67a0cc4e
      Jason Rhinelander authored
      PR #936 broke the ability to return a pointer to a stl container (and,
      likewise, to a tuple) because the added deduced type matched a
      non-const pointer argument: the pointer-accepting `cast` in
      PYBIND11_TYPE_CASTER had a `const type *`, which is a worse match for a
      non-const pointer than the universal reference template #936 added.
      
      This changes the provided TYPE_CASTER cast(ptr) to take the pointer by
      template arg (so that it will accept either const or non-const pointer).
      It has two other effects: it slightly reduces .so size (because many
      type casters never actually need the pointer cast at all), and it allows
      type casters to provide their untemplated pointer `cast()` that will
      take precedence over the templated version provided in the macro.
      67a0cc4e
  4. 12 Jul, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix test suite under MSVC/Debug · 6b51619a
      Jason Rhinelander authored
      In a Debug build, MSVC doesn't apply copy/move elision as often,
      triggering a test failure.  This relaxes the test count requirements
      to let the test suite pass.
      6b51619a
  5. 05 Jul, 2017 2 commits
    • Jason Rhinelander's avatar
      Use rvalue subcasting when casting an rvalue container · b57281bb
      Jason Rhinelander authored
      This updates the std::tuple, std::pair and `stl.h` type casters to
      forward their contained value according to whether the container being
      cast is an lvalue or rvalue reference.  This fixes an issue where
      subcaster casts were always called with a const lvalue which meant
      nested type casters didn't have the desired `cast()` overload invoked.
      For example, this caused Eigen values in a tuple to end up with a
      readonly flag (issue #935) and made it impossible to return a container
      of move-only types (issue #853).
      
      This fixes both issues by adding templated universal reference `cast()`
      methods to the various container types that forward container elements
      according to the container reference type.
      b57281bb
    • Jason Rhinelander's avatar
      Combine std::tuple/std::pair logic · 897d7168
      Jason Rhinelander authored
      The std::pair caster can be written as a special case of the std::tuple
      caster; this combines them via a base `tuple_caster` class (which is
      essentially identical to the previous std::tuple caster).
      
      This also removes the special empty tuple base case: returning an empty
      tuple is relatively rare, and the base case still works perfectly well
      even when the tuple types is an empty list.
      897d7168
  6. 03 Jul, 2017 1 commit
    • Jason Rhinelander's avatar
      Override deduced Base class when defining Derived methods · 23bf8945
      Jason Rhinelander authored
      
      
      When defining method from a member function pointer (e.g. `.def("f",
      &Derived::f)`) we run into a problem if `&Derived::f` is actually
      implemented in some base class `Base` when `Base` isn't
      pybind-registered.
      
      This happens because the class type is deduced from the member function
      pointer, which then becomes a lambda with first argument this deduced
      type.  For a base class implementation, the deduced type is `Base`, not
      `Derived`, and so we generate and registered an overload which takes a
      `Base *` as first argument.  Trying to call this fails if `Base` isn't
      registered (e.g.  because it's an implementation detail class that isn't
      intended to be exposed to Python) because the type caster for an
      unregistered type always fails.
      
      This commit adds a `method_adaptor` function that rebinds a member
      function to a derived type member function and otherwise (i.e. regular
      functions/lambda) leaves the argument as-is.  This is now used for class
      definitions so that they are bound with type being registered rather
      than a potential base type.
      
      A closely related fix in this commit is to similarly update the lambdas
      used for `def_readwrite` (and related) to bind to the class type being
      registered rather than the deduced type so that registering a property
      that resolves to a base class member similarly generates a usable
      function.
      
      Fixes #854, #910.
      Co-Authored-By: default avatarDean Moldovan <dean0x7d@gmail.com>
      23bf8945
  7. 02 Jul, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix unsigned error value casting · 259b2faf
      Jason Rhinelander authored
      When casting to an unsigned type from a python 2 `int`, we currently
      cast using `(unsigned long long) PyLong_AsUnsignedLong(src.ptr())`.
      If the Python cast fails, it returns (unsigned long) -1, but then we
      cast this to `unsigned long long`, which means we get 4294967295, but
      because that isn't equal to `(unsigned long long) -1`, we don't detect
      the failure.
      
      This commit moves the unsigned casting into a `detail::as_unsigned`
      function which, upon error, casts -1 to the final type, and otherwise
      casts the return value to the final type to avoid the problematic double
      cast when an error occurs.
      
      The error most commonly shows up wherever `long` is 32-bits (e.g. under
      both 32- and 64-bit Windows, and under 32-bit linux) when passing a
      negative value to a bound function taking an `unsigned long`.
      
      Fixes #929.
      
      The added tests also trigger a latent segfault under PyPy: when casting
      to an integer smaller than `long` (e.g. casting to a `uint32_t` on a
      64-bit `long` architecture) we check both for a Python error and also
      that the resulting intermediate value will fit in the final type.  If
      there is no conversion error, but we get a value that would overflow, we
      end up calling `PyErr_ExceptionMatches()` illegally: that call is only
      allowed when there is a current exception.  Under PyPy, this segfaults
      the test suite.  It doesn't appear to segfault under CPython, but the
      documentation suggests that it *could* do so.  The fix is to only check
      for the exception match if we actually got an error.
      259b2faf
  8. 29 Jun, 2017 2 commits
  9. 27 Jun, 2017 5 commits
  10. 24 Jun, 2017 4 commits
  11. 15 Jun, 2017 1 commit
  12. 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
  13. 10 Jun, 2017 3 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
    • Dean Moldovan's avatar
      Don't let pytest discover tests from test_cmake_build and test_embed · 17cc39c8
      Dean Moldovan authored
      pytest raises an error if it recurses into these directories.
      17cc39c8
    • 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
  14. 08 Jun, 2017 2 commits
    • 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
  15. 07 Jun, 2017 1 commit
  16. 30 May, 2017 3 commits
    • 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
    • Jason Rhinelander's avatar
      Move reference_wrapper test from test_issues to test_python_types · 7cdf9f1a
      Jason Rhinelander authored
      test_issues is deprecated, and the following commit adds other, related
      reference_wrapper tests.
      7cdf9f1a
    • Dean Moldovan's avatar
      Filter warnings on pytest >= v3.1 · 005fde6a
      Dean Moldovan authored
      The new version of pytest now reports Python warnings by default. This
      commit filters out some third-party extension warnings which are not
      useful for pybind11 tests.
      005fde6a
  17. 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
  18. 28 May, 2017 3 commits