1. 01 Jan, 2021 1 commit
    • Yannick Jadoul's avatar
      Fix various minor memory leaks in the tests (found by Valgrind in #2746) (#2758) · e57dd471
      Yannick Jadoul authored
      * Fix leak in the test_copy_move::test_move_fallback
      
      * Fix leaking PyMethodDef in test_class::test_implicit_conversion_life_support
      
      * Plumb leak in test_buffer, occuring when a mutable buffer is requested for a read-only object, and enable test_buffer.py
      
      * Fix weird return_value_policy::reference in test_stl_binders, and enable those tests
      
      * Cleanup nodelete holder objects in test_smart_ptr, and enable those tests
      e57dd471
  2. 16 Dec, 2020 1 commit
  3. 05 Nov, 2020 1 commit
  4. 16 Oct, 2020 1 commit
    • Yannick Jadoul's avatar
      fix: test_factory_constructors.py failure triggered by test_register_duplicate_class (#2564) · 6364b732
      Yannick Jadoul authored
      * Demonstrate test_factory_constructors.py failure without functional changes from #2335
      
      * Revert "Demonstrate test_factory_constructors.py failure without functional changes from #2335"
      
      This reverts commit ca33a8021fc2a3617c3356b188796528f4594419.
      
      * Fix test crash where registered Python type gets garbage collected
      
      * Clean up some more internal structures when class objects go out of scope
      
      * Reduce length of std::erase_if-in-C++20 comment
      
      * Clean up code for cleaning up type internals
      
      * Move cleaning up of type info in internals to tp_dealloc on pybind11_metaclass
      6364b732
  5. 05 Oct, 2020 2 commits
    • Yannick Jadoul's avatar
      Fail on passing py::object with wrong Python type to py::object subclass using... · f537093a
      Yannick Jadoul authored
      Fail on passing py::object with wrong Python type to py::object subclass using PYBIND11_OBJECT macro (#2349)
      
      * Fail on passing py::object with wrong Python type to py::object subclass using PYBIND11_OBJECT macro
      
      * Split off test_non_converting_constructors from test_constructors
      
      * Fix test_as_type, as py::type constructor now throws an error itself if the argument is not a type
      
      * Replace tp_name access by pybind11::detail::get_fully_qualified_tp_name
      
      * Move forward-declaration of get_fully_qualified_tp_name to detail/common.h
      
      * Don't add the builtins module name in get_fully_qualified_tp_name for PyPy
      
      * Add PYBIND11_BUILTINS_MODULE macro, and use it in get_fully_qualified_tp_name
      f537093a
    • Yannick Jadoul's avatar
      chore: drop support for PyPy < 7.3.1 and clean up old PyPy workarounds (#2456) · 14112077
      Yannick Jadoul authored
      
      
      * Remove code inside 'PYPY_VERSION_NUM < 0x06000000' preprocessor if branch
      
      * fix: more cleanup
      
      * Remove more references to PyPy 5.7 and 5.9 in the docs
      
      * Update comment on PyUnicode_UTF* in PyPy
      Co-authored-by: default avatarHenry Schreiner <henryschreineriii@gmail.com>
      14112077
  6. 03 Oct, 2020 1 commit
  7. 02 Oct, 2020 2 commits
  8. 15 Sep, 2020 1 commit
  9. 14 Aug, 2020 1 commit
  10. 08 Jul, 2020 1 commit
  11. 07 Jul, 2020 2 commits
  12. 26 Apr, 2020 1 commit
  13. 24 Nov, 2019 1 commit
  14. 08 Oct, 2019 1 commit
    • Sergei Izmailov's avatar
      Adapt to python3.8 C API change (#1950) · 6cb584e9
      Sergei Izmailov authored
      * Adapt to python3.8 C API change
      
      Do `Py_DECREF(type)` on all python objects on deallocation
      
      fix #1946
      
      * Add bare python3.8 build to CI matrix
      
      While numpy/scipy wheels are available, run python3.8 test without them
      6cb584e9
  15. 18 Jul, 2019 1 commit
  16. 10 Jun, 2019 2 commits
  17. 07 Nov, 2017 1 commit
    • Jason Rhinelander's avatar
      __qualname__ and nested class naming fixes (#1171) · 71178922
      Jason Rhinelander authored
      A few fixes related to how we set `__qualname__` and how we show the
      type name in function signatures:
      
      - `__qualname__` isn't supposed to have the module name at the
      beginning, but we've been putting it there.  This removes it, while
      keeping the `Nested.Class` name chaining.
      
      - print `__module__.__qualname__` rather than `type->tp_name`; the
      latter doesn't work properly for nested classes, so we would get
      `module.B` rather than `module.A.B` for a class `B` with parent `A`.
      This also unifies the Python 3 and PyPy code.  Fixes #1166.
      
      - This now sets a `__qualname__` attribute on the type (as would happen
      in Python 3.3+) for Python <3.3, including PyPy.  While not particularly
      important to have in earlier Python versions, it's useful for us to be
      able to extracted the nested name, which is why `__qualname__` was
      invented in the first place.
      
      - Added tests for the above.
      71178922
  18. 25 Aug, 2017 1 commit
    • Wenzel Jakob's avatar
      Address reference leak issue (fixes #1029) · c14c2762
      Wenzel Jakob authored
      Creating an instance of of a pybind11-bound type caused a reference leak in the
      associated Python type object, which could prevent these from being collected
      upon interpreter shutdown. This commit fixes that issue for all types that are
      defined in a scope (e.g. a module). Unscoped anonymous types (e.g. custom
      iterator types) always retain a positive reference count to prevent their
      collection.
      c14c2762
  19. 17 Aug, 2017 2 commits
    • Jason Rhinelander's avatar
      Lazy instance value pointer allocation · fd81a03e
      Jason Rhinelander authored
      We currently allocate instance values when creating the instance itself
      (except when constructing the instance for a `cast()`), but there is no
      particular reason to do so: the instance itself and the internals (for
      a non-simple layout) are allocated via Python, with no reason to
      expect better locality from the invoked `operator new`.  Moreover, it
      makes implementation of factory function constructors trickier and
      slightly less efficient: they don't use the pre-eallocate the memory,
      which means there is a pointless allocation and free.
      
      This commit makes the allocation lazy: instead of preallocating when
      creating the instance, the allocation happens when the instance is
      first loaded (if null at that time).
      
      In addition to making it more efficient to deal with cases that don't
      need preallocation, this also allows for a very slight performance
      increase by not needing to look up the instances types during
      allocation.  (There is a lookup during the eventual load, of course, but
      that is happening already).
      fd81a03e
    • Dean Moldovan's avatar
      f5806498
  20. 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
  21. 05 Aug, 2017 1 commit
    • Jason Rhinelander's avatar
      Stash std::strings used for tp_name in internals · 2640c950
      Jason Rhinelander authored
      Types need `tp_name` set to a C-style string, but the current `strdup`
      ends up with a leak (issue #977).  This avoids the strdup by storing
      the `std::string` in internals so that during interpreter shutdown it
      will be properly destroyed.
      2640c950
  22. 29 Jul, 2017 2 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
      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
  23. 20 Jul, 2017 1 commit
  24. 24 Jun, 2017 1 commit
    • 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
  25. 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
  26. 29 May, 2017 1 commit
  27. 22 May, 2017 1 commit
    • 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
  28. 07 May, 2017 2 commits
    • Cris Luengo's avatar
    • Jason Rhinelander's avatar
      Use numpy rather than Eigen for copying · b68959e8
      Jason Rhinelander authored
      We're current copy by creating an Eigen::Map into the input numpy
      array, then assigning that to the basic eigen type, effectively having
      Eigen do the copy.  That doesn't work for negative strides, though:
      Eigen doesn't allow them.
      
      This commit makes numpy do the copying instead by allocating the eigen
      type, then having numpy copy from the input array into a numpy reference
      into the eigen object's data.  This also saves a copy when type
      conversion is required: numpy can do the conversion on-the-fly as part
      of the copy.
      
      Finally this commit also makes non-reference parameters respect the
      convert flag, declining the load when called in a noconvert pass with a
      convertible, but non-array input or an array with the wrong dtype.
      b68959e8
  29. 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
  30. 27 Apr, 2017 2 commits
    • 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
    • Jason Rhinelander's avatar
      Expose more instance management functions · 92900995
      Jason Rhinelander authored
      This breaks up the instance management functions in class_support.h a
      little bit so that other pybind11 code can use it.  In particular:
      
      - added make_new_instance() which does what pybind11_object_new does,
        but also allows instance allocation without `value` allocation.  This
        lets `cast.h` use the same instance allocation rather than having its
        own separate implementation.
      - instance registration is now moved to a
        `register_instance()`/deregister_instance()` pair (rather than having
        individual code add or remove things from `registered_instances`
        directory).
      - clear_instance() does everything `pybind11_object_dealloc()` needs
        except for the deallocation; this is helpful for factory construction
        which needs to be able to replace the internals of an instance without
        deallocating it.
      - clear_instance() now also calls `dealloc` when `holder_constructed`
        is true, even if `value` is false.  This can happen in factory
        construction when the pointer is moved from one instance to another,
        but the holder itself is only copied (i.e. for a shared_ptr holder).
      92900995
  31. 07 Apr, 2017 1 commit
  32. 22 Mar, 2017 1 commit