@@ -206,6 +206,14 @@ impl<'py, T> Bound<'py, T> {
206
206
)
207
207
}
208
208
209
+ /// Removes the connection for this `Bound<T>` from the GIL, allowing
210
+ /// it to cross thread boundaries.
211
+ pub fn unbind ( self ) -> Py < T > {
212
+ // Safety: the type T is known to be correct and the ownership of the
213
+ // pointer is transferred to the new Py<T> instance.
214
+ unsafe { Py :: from_non_null ( self . into_non_null ( ) ) }
215
+ }
216
+
209
217
/// Casts this `Bound<T>` as the corresponding "GIL Ref" type.
210
218
///
211
219
/// This is a helper to be used for migration from the deprecated "GIL Refs" API.
@@ -973,7 +981,7 @@ impl<T> Py<T> {
973
981
where
974
982
N : IntoPy < Py < PyString > > ,
975
983
{
976
- self . bind ( py) . as_any ( ) . getattr ( attr_name) . map ( Into :: into )
984
+ self . bind ( py) . as_any ( ) . getattr ( attr_name) . map ( Bound :: unbind )
977
985
}
978
986
979
987
/// Sets an attribute value.
@@ -1017,21 +1025,21 @@ impl<T> Py<T> {
1017
1025
args : impl IntoPy < Py < PyTuple > > ,
1018
1026
kwargs : Option < & PyDict > ,
1019
1027
) -> PyResult < PyObject > {
1020
- self . bind ( py) . as_any ( ) . call ( args, kwargs) . map ( Into :: into )
1028
+ self . bind ( py) . as_any ( ) . call ( args, kwargs) . map ( Bound :: unbind )
1021
1029
}
1022
1030
1023
1031
/// Calls the object with only positional arguments.
1024
1032
///
1025
1033
/// This is equivalent to the Python expression `self(*args)`.
1026
1034
pub fn call1 ( & self , py : Python < ' _ > , args : impl IntoPy < Py < PyTuple > > ) -> PyResult < PyObject > {
1027
- self . bind ( py) . as_any ( ) . call1 ( args) . map ( Into :: into )
1035
+ self . bind ( py) . as_any ( ) . call1 ( args) . map ( Bound :: unbind )
1028
1036
}
1029
1037
1030
1038
/// Calls the object without arguments.
1031
1039
///
1032
1040
/// This is equivalent to the Python expression `self()`.
1033
1041
pub fn call0 ( & self , py : Python < ' _ > ) -> PyResult < PyObject > {
1034
- self . bind ( py) . as_any ( ) . call0 ( ) . map ( Into :: into )
1042
+ self . bind ( py) . as_any ( ) . call0 ( ) . map ( Bound :: unbind )
1035
1043
}
1036
1044
1037
1045
/// Calls a method on the object.
@@ -1054,7 +1062,7 @@ impl<T> Py<T> {
1054
1062
self . bind ( py)
1055
1063
. as_any ( )
1056
1064
. call_method ( name, args, kwargs)
1057
- . map ( Into :: into )
1065
+ . map ( Bound :: unbind )
1058
1066
}
1059
1067
1060
1068
/// Calls a method on the object with only positional arguments.
@@ -1071,7 +1079,7 @@ impl<T> Py<T> {
1071
1079
self . bind ( py)
1072
1080
. as_any ( )
1073
1081
. call_method1 ( name, args)
1074
- . map ( Into :: into )
1082
+ . map ( Bound :: unbind )
1075
1083
}
1076
1084
1077
1085
/// Calls a method on the object with no arguments.
@@ -1084,7 +1092,7 @@ impl<T> Py<T> {
1084
1092
where
1085
1093
N : IntoPy < Py < PyString > > ,
1086
1094
{
1087
- self . bind ( py) . as_any ( ) . call_method0 ( name) . map ( Into :: into )
1095
+ self . bind ( py) . as_any ( ) . call_method0 ( name) . map ( Bound :: unbind )
1088
1096
}
1089
1097
1090
1098
/// Create a `Py<T>` instance by taking ownership of the given FFI pointer.
@@ -1292,7 +1300,7 @@ where
1292
1300
impl < T > std:: convert:: From < Bound < ' _ , T > > for Py < T > {
1293
1301
#[ inline]
1294
1302
fn from ( other : Bound < ' _ , T > ) -> Self {
1295
- unsafe { Self :: from_non_null ( other. into_non_null ( ) ) }
1303
+ other. unbind ( )
1296
1304
}
1297
1305
}
1298
1306
@@ -1618,7 +1626,7 @@ a = A()
1618
1626
. as_borrowed ( )
1619
1627
. to_owned ( ) ;
1620
1628
let ptr = instance. as_ptr ( ) ;
1621
- let instance: PyObject = instance. clone ( ) . into ( ) ;
1629
+ let instance: PyObject = instance. clone ( ) . unbind ( ) ;
1622
1630
assert_eq ! ( instance. as_ptr( ) , ptr) ;
1623
1631
} )
1624
1632
}
0 commit comments