@@ -1653,49 +1653,8 @@ struct move_only_holder_caster : type_caster_base<type> {
1653
1653
1654
1654
explicit operator holder_type&&() { return std::move (holder); }
1655
1655
1656
- object extract_obj (handle src) {
1657
- // See if this is a supported `move` container.
1658
- bool is_move_container = false ;
1659
- object obj = none ();
1660
- // TODO(eric.cousineau): See if we might need to safeguard against objects that are
1661
- // implicitly convertible from `list`.
1662
- // Can try to cast to T* first, and if that fails, assume it's a move container.
1663
- if (isinstance (src, (PyObject*)&PyList_Type) && PyList_Size (src.ptr ()) == 1 ) {
1664
- // Extract the object from a single-item list, and remove the existing reference so we have exclusive control.
1665
- // @note This will break implicit casting when constructing from vectors, but eh, who cares.
1666
- // Swap.
1667
- list li = src.cast <list>();
1668
- obj = li[0 ];
1669
- li[0 ] = none ();
1670
- is_move_container = true ;
1671
- } else if (hasattr (src, " _is_move_container" )) {
1672
- // Try to extract the value with `release()`.
1673
- obj = src.attr (" release" )();
1674
- is_move_container = true ;
1675
- } else {
1676
- obj = reinterpret_borrow<object>(src);
1677
- }
1678
- if (is_move_container && obj.ref_count () != 1 ) {
1679
- throw std::runtime_error (" Non-unique reference from a move-container, cannot cast to unique_ptr." );
1680
- }
1681
- return obj;
1682
- }
1683
-
1684
- bool load (handle src, bool /* convert*/ ) {
1685
- if (src.is (none ())) {
1686
- holder.reset ();
1687
- return true ;
1688
- }
1689
- // Allow loose reference management (if it's just a plain object) or require tighter reference
1690
- // management if it's a move container.
1691
- object obj = extract_obj (src);
1692
- // Do not use `load_impl`, as it's not structured conveniently for `unique_ptr`.
1693
- // Specifically, trying to delegate to resolving to conversion.
1694
- // return base::template load_impl<move_only_holder_caster<type, holder_type>>(src, convert);
1695
- check_holder_compat ();
1696
- auto v_h = reinterpret_cast <instance*>(obj.ptr ())->get_value_and_holder ();
1697
- LoadType load_type = determine_load_type (obj, typeinfo);
1698
- return load_value (std::move (obj), std::move (v_h), load_type);
1656
+ bool load (handle src, bool convert) {
1657
+ return base::template load_impl<move_only_holder_caster>(src, convert);
1699
1658
}
1700
1659
1701
1660
static constexpr auto name = type_caster_base<type>::name;
@@ -1707,7 +1666,7 @@ struct move_only_holder_caster : type_caster_base<type> {
1707
1666
throw cast_error (" Unable to load a non-default holder type (unique_ptr)" );
1708
1667
}
1709
1668
1710
- bool load_value (object obj_exclusive, value_and_holder &&v_h, LoadType load_type) {
1669
+ bool load_value (value_and_holder &&v_h, LoadType load_type) {
1711
1670
// TODO(eric.cousineau): This should try and find the downcast-lowest
1712
1671
// level (closest to child) `release_to_cpp` method that is derived-releasable
1713
1672
// (which derives from `wrapper<type>`).
@@ -1720,7 +1679,8 @@ struct move_only_holder_caster : type_caster_base<type> {
1720
1679
// NOT try to release using `PyBase`s mechanism.
1721
1680
// Additionally, if `Child` does not have a wrapper (for whatever reason) and is extended,
1722
1681
// then we still can NOT use `PyBase` since it's not part of the hierachy.
1723
-
1682
+ handle src = (PyObject*)v_h.inst ;
1683
+ object obj = reinterpret_borrow<object>(src);
1724
1684
// Try to get the lowest-hierarchy level of the type.
1725
1685
// This requires that we are single-inheritance at most.
1726
1686
const detail::type_info* lowest_type = nullptr ;
@@ -1738,7 +1698,7 @@ struct move_only_holder_caster : type_caster_base<type> {
1738
1698
case LoadType::ConversionNeeded: {
1739
1699
// Try to get the lowest-hierarchy (closets to child class) of the type.
1740
1700
// The usage of `get_type_info` implicitly requires single inheritance.
1741
- auto * py_type = (PyTypeObject*)obj_exclusive .get_type ().ptr ();
1701
+ auto * py_type = (PyTypeObject*)obj .get_type ().ptr ();
1742
1702
lowest_type = detail::get_type_info (py_type);
1743
1703
break ;
1744
1704
}
@@ -1756,7 +1716,7 @@ struct move_only_holder_caster : type_caster_base<type> {
1756
1716
auto & release_info = lowest_type->release_info ;
1757
1717
if (!release_info.release_to_cpp )
1758
1718
throw std::runtime_error (" No release mechanism in lowest type?" );
1759
- release_info.release_to_cpp (v_h.inst , &holder, std::move (obj_exclusive ));
1719
+ release_info.release_to_cpp (v_h.inst , &holder, std::move (obj ));
1760
1720
return true ;
1761
1721
}
1762
1722
0 commit comments