1. 17 Aug, 2017 2 commits
  2. 14 Aug, 2017 1 commit
    • Jason Rhinelander's avatar
      Force hidden visibility on pybind code · a859dd67
      Jason Rhinelander authored
      This adds a PYBIND11_NAMESPACE macro that expands to the `pybind11`
      namespace with hidden visibility under gcc-type compilers, and otherwise
      to the plain `pybind11`.  This then forces hidden visibility on
      everything in pybind, solving the visibility issues discussed at end
      end of #949.
      a859dd67
  3. 05 Aug, 2017 1 commit
    • Jason Rhinelander's avatar
      Made module_local types take precedence over global types · 4b159230
      Jason Rhinelander authored
      Attempting to mix py::module_local and non-module_local classes results
      in some unexpected/undesirable behaviour:
      
      - if a class is registered non-local by some other module, a later
        attempt to register it locally fails.  It doesn't need to: it is
        perfectly acceptable for the local registration to simply override
        the external global registration.
      - going the other way (i.e. module `A` registers a type `T` locally,
        then `B` registers the same type `T` globally) causes a more serious
        issue: `A.T`'s constructors no longer work because the `self` argument
        gets converted to a `B.T`, which then fails to resolve.
      
      Changing the cast precedence to prefer local over global fixes this and
      makes it work more consistently, regardless of module load order.
      4b159230
  4. 04 Aug, 2017 1 commit
    • Jason Rhinelander's avatar
      Add py::module_local() attribute for module-local type bindings · 7437c695
      Jason Rhinelander authored
      This commit adds a `py::module_local` attribute that lets you confine a
      registered type to the module (more technically, the shared object) in
      which it is defined, by registering it with:
      
          py::class_<C>(m, "C", py::module_local())
      
      This will allow the same C++ class `C` to be registered in different
      modules with independent sets of class definitions.  On the Python side,
      two such types will be completely distinct; on the C++ side, the C++
      type resolves to a different Python type in each module.
      
      This applies `py::module_local` automatically to `stl_bind.h` bindings
      when the container value type looks like something global: i.e. when it
      is a converting type (for example, when binding a `std::vector<int>`),
      or when it is a registered type itself bound with `py::module_local`.
      This should help resolve potential future conflicts (e.g. if two
      completely unrelated modules both try to bind a `std::vector<int>`.
      Users can override the automatic selection by adding a
      `py::module_local()` or `py::module_local(false)`.
      
      Note that this does mildly break backwards compatibility: bound stl
      containers of basic types like `std::vector<int>` cannot be bound in one
      module and returned in a different module.  (This can be re-enabled with
      `py::module_local(false)` as described above, but with the potential for
      eventual load conflicts).
      7437c695
  5. 29 Jul, 2017 4 commits
    • Jason Rhinelander's avatar
      Fix occassional segfault introduced by #960 · cca20a7f
      Jason Rhinelander authored
      The fix for #960 could result a type being registered multiple times if
      its `__init__` is called multiple times.  This can happen perfectly
      ordinarily when python-side multiple inheritance is involved: for
      example, with a diamond inheritance pattern with each intermediate
      classes invoking the parent constructor.
      
      With the change in #960, the multiple `__init__` calls meant
      `register_instance` was called multiple times, but the deletion only
      deleted it once.  Thus, if a future instance of the same type was
      allocated at the same location, pybind would pick it up as a registered
      type.
      
      This fixes the issue by tracking whether a value pointer has been
      registered to avoid both double-registering it.  (There's also a slight
      optimization of not needing to do a registered_instances lookup when the
      type is known not registered, but this is secondary).
      cca20a7f
    • Jason Rhinelander's avatar
      Remove debugging · 12be4cd4
      Jason Rhinelander authored
      The "z" wasn't meant to be committed; it meant the C++17 optimization
      here was never being used.
      12be4cd4
    • Jason Rhinelander's avatar
      Simplify error_already_set · 1682b673
      Jason Rhinelander authored
      `error_already_set` is more complicated than it needs to be, partly
      because it manages reference counts itself rather than using
      `py::object`, and partly because it tries to do more exception clearing
      than is needed.  This commit greatly simplifies it, and fixes #927.
      
      Using `py::object` instead of `PyObject *` means we can rely on
      implicit copy/move constructors.
      
      The current logic did both a `PyErr_Clear` on deletion *and* a
      `PyErr_Fetch` on creation.  I can't see how the `PyErr_Clear` on
      deletion is ever useful: the `Fetch` on creation itself clears the
      error, so the only way doing a `PyErr_Clear` on deletion could do
      anything if is some *other* exception was raised while the
      `error_already_set` object was alive--but in that case, clearing some
      other exception seems wrong.  (Code that is worried about an exception
      handler raising another exception would already catch a second
      `error_already_set` from exception code).
      
      The destructor itself called `clear()`, but `clear()` was a little bit
      more paranoid that needed: it called `restore()` to restore the
      currently captured error, but then immediately cleared it, using the
      `PyErr_Restore` to release the references.  That's unnecessary: it's
      valid for us to release the references manually.  This updates the code
      to simply release the references on the three objects (preserving the
      gil acquire).
      
      `clear()`, however, also had the side effect of clearing the current
      error, even if the current `error_already_set` didn't have a current
      error (e.g. because of a previous `restore()` or `clear()` call).  I
      don't really see how clearing the error here can ever actually be
      useful: the only way the current error could be set is if you called
      `restore()` (in which case the current stored error-related members have
      already been released), or if some *other* code raised the error, in
      which case `clear()` on *this* object is clearing an error for which it
      shouldn't be responsible.
      
      Neither of those seem like intentional or desirable features, and
      manually requesting deletion of the stored references similarly seems
      pointless, so I've just made `clear()` an empty method and marked it
      deprecated.
      
      This also fixes a minor potential issue with the destruction: it is
      technically possible for `value` to be null (though this seems likely to
      be rare in practice); this updates the check to look at `type` which
      will always be non-null for a `Fetch`ed exception.
      
      This also adds error_already_set round-trip throw tests to the test
      suite.
      1682b673
    • Jason Rhinelander's avatar
      Make `init_holder` do registration, and rename to `init_instance` · 353615f7
      Jason Rhinelander authored
      The instance registration for offset base types fails (under macOS, with
      a segfault) in the presense of virtual base types.  The issue occurs
      when trying to `static_cast<Base *>(derived_ptr)` when `derived_ptr` has
      been allocated (via `operator new`) but not initialized.
      
      This commit fixes the issue by moving the addition to
      `registered_instances` into `init_holder` rather than immediately after
      value pointer allocation.
      
      This also renames it to `init_instance` since it does more than holder
      initialization now.  (I also further renamed `init_holder_helper` to
      `init_holder` since `init_holder` isn't used anymore).
      
      Fixes #959.
      353615f7
  6. 23 Jul, 2017 1 commit
    • 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
  7. 06 Jul, 2017 1 commit
    • Dean Moldovan's avatar
      Fix compilation with Intel's compiler · 706a7d96
      Dean Moldovan authored
      ICC was reporting that `try_direct_conversions()` cannot be `constexpr`
      because `handle` is not a literal type. The fix removes `constexpr`
      from the function since it isn't strictly needed.
      
      This commit also suppresses new false positive warnings which mostly
      appear in constexpr contexts (where the compiler knows conversions are
      safe).
      706a7d96
  8. 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
  9. 29 Jun, 2017 1 commit
  10. 24 Jun, 2017 3 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
  11. 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
  12. 10 Jun, 2017 1 commit
  13. 07 Jun, 2017 1 commit
  14. 28 May, 2017 1 commit
  15. 25 May, 2017 2 commits
    • 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
  16. 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
  17. 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
  18. 09 May, 2017 3 commits
    • Jason Rhinelander's avatar
      Update PYBIND11_CPP1{4,7} macros for MSVC · ca0e82b7
      Jason Rhinelander authored
      The PYBIND11_CPP14 macro started out as a guard for the compile-time
      path code in `descr.h`, but has since come to mean other things.  This
      means that while the `descr.h` check has just checked the
      `PYBIND11_CPP14` macro, various other places now check `PYBIND11_CPP14
      || _MSC_VER`.  This reverses that by now setting the CPP14 macro when
      MSVC is trying to support C++14, but disabling the `descr.h` C++14 code
      (which still fails under MSVC 2017).
      
      The CPP17 macro also gets enabled when MSVC 2017 is compiling with
      /std:c++latest (the default is /std:c++14), which enables
      `std::optional` and `std::variant` support under MSVC.
      ca0e82b7
    • Jason Rhinelander's avatar
      d15b217f
    • Jason Rhinelander's avatar
      gcc 7 disable warning · 88ebc49b
      Jason Rhinelander authored
      GCC 7 generates (when compiling in C++11/14 mode) warnings such as:
      
          mangled name for ‘pybind11::class_<type_, options>&
          pybind11::class_<type_, options>::def(const char*, Func&&, const Extra&
          ...) [with Func = int (test_exc_sp::C::*)(int) noexcept; Extra = {};
          type_ = test_exc_sp::C; options = {}]’ will change in C++17 because the
          exception specification is part of a function type [-Wnoexcept-type]
      
      There's nothing we can actually do in the code to avoid this, so just
      disable the warning.
      88ebc49b
  19. 07 May, 2017 1 commit
  20. 29 Apr, 2017 1 commit
    • Wenzel Jakob's avatar
      enum_: fix implicit conversion on Python 2.7 · e6fd2cd5
      Wenzel Jakob authored
      Enumerations on Python 2.7 were not always implicitly converted to
      integers (depending on the target size). This patch adds a __long__
      conversion function (only enabled on 2.7) which fixes this issue.
      
      The attached test case fails without this patch.
      e6fd2cd5
  21. 28 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Don't let PyInstanceMethod hide itself · 0a90b2db
      Jason Rhinelander authored
      Python 3's `PyInstanceMethod_Type` hides itself via its `tp_descr_get`,
      which prevents aliasing methods via `cls.attr("m2") = cls.attr("m1")`:
      instead the `tp_descr_get` returns a plain function, when called on a
      class, or a `PyMethod`, when called on an instance.  Override that
      behaviour for pybind11 types with a special bypass for
      `PyInstanceMethod_Types`.
      0a90b2db
  22. 27 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Track base class pointers of instances · 1f8a100d
      Jason Rhinelander authored
      This commits adds base class pointers of offset base classes (i.e. due
      to multiple inheritance) to `registered_instances` so that if such a
      pointer is returned we properly recognize it as an existing instance.
      
      Without this, returning a base class pointer will cast to the existing
      instance if the pointer happens to coincide with the instance pointer,
      but constructs a new instance (quite possibly with a segfault, if
      ownership is applied) for unequal base class pointers due to multiple
      inheritance.
      1f8a100d
  23. 18 Apr, 2017 1 commit
    • Jason Rhinelander's avatar
      Don't allow mixed static/non-static overloads · d355f2fc
      Jason Rhinelander authored
      We currently fail at runtime when trying to call a method that is
      overloaded with both static and non-static methods.  This is something
      python won't allow: the object is either a function or an instance, and
      can't be both.
      d355f2fc
  24. 02 Apr, 2017 2 commits
  25. 30 Mar, 2017 1 commit
  26. 22 Mar, 2017 2 commits