1
1
use crate :: callback:: IntoPyCallbackOutput ;
2
+ use crate :: conversion:: IntoPyObject ;
2
3
use crate :: err:: { PyErr , PyResult } ;
3
4
use crate :: ffi_ptr_ext:: FfiPtrExt ;
4
5
use crate :: py_result_ext:: PyResultExt ;
5
6
use crate :: pyclass:: PyClass ;
6
7
use crate :: types:: {
7
8
any:: PyAnyMethods , list:: PyListMethods , PyAny , PyCFunction , PyDict , PyList , PyString ,
8
9
} ;
9
- use crate :: { exceptions, ffi, Bound , IntoPy , Py , PyObject , Python } ;
10
+ use crate :: { exceptions, ffi, Borrowed , Bound , BoundObject , Py , PyObject , Python } ;
10
11
use std:: ffi:: { CStr , CString } ;
11
12
use std:: str;
12
13
@@ -82,11 +83,11 @@ impl PyModule {
82
83
///
83
84
/// If you want to import a class, you can store a reference to it with
84
85
/// [`GILOnceCell::import`][crate::sync::GILOnceCell#method.import].
85
- pub fn import < N > ( py : Python < ' _ > , name : N ) -> PyResult < Bound < ' _ , PyModule > >
86
+ pub fn import < ' py , N > ( py : Python < ' py > , name : N ) -> PyResult < Bound < ' py , PyModule > >
86
87
where
87
- N : IntoPy < Py < PyString > > ,
88
+ N : IntoPyObject < ' py , Target = PyString > ,
88
89
{
89
- let name: Py < PyString > = name. into_py ( py) ;
90
+ let name = name. into_pyobject ( py) . map_err ( Into :: into ) ? ;
90
91
unsafe {
91
92
ffi:: PyImport_Import ( name. as_ptr ( ) )
92
93
. assume_owned_or_err ( py)
@@ -99,9 +100,9 @@ impl PyModule {
99
100
#[ inline]
100
101
pub fn import_bound < N > ( py : Python < ' _ > , name : N ) -> PyResult < Bound < ' _ , PyModule > >
101
102
where
102
- N : IntoPy < Py < PyString > > ,
103
+ N : crate :: IntoPy < Py < PyString > > ,
103
104
{
104
- Self :: import ( py, name)
105
+ Self :: import ( py, name. into_py ( py ) )
105
106
}
106
107
107
108
/// Creates and loads a module named `module_name`,
@@ -253,8 +254,8 @@ pub trait PyModuleMethods<'py>: crate::sealed::Sealed {
253
254
/// ```
254
255
fn add < N , V > ( & self , name : N , value : V ) -> PyResult < ( ) >
255
256
where
256
- N : IntoPy < Py < PyString > > ,
257
- V : IntoPy < PyObject > ;
257
+ N : IntoPyObject < ' py , Target = PyString > ,
258
+ V : IntoPyObject < ' py > ;
258
259
259
260
/// Adds a new class to the module.
260
261
///
@@ -452,26 +453,30 @@ impl<'py> PyModuleMethods<'py> for Bound<'py, PyModule> {
452
453
453
454
fn add < N , V > ( & self , name : N , value : V ) -> PyResult < ( ) >
454
455
where
455
- N : IntoPy < Py < PyString > > ,
456
- V : IntoPy < PyObject > ,
456
+ N : IntoPyObject < ' py , Target = PyString > ,
457
+ V : IntoPyObject < ' py > ,
457
458
{
458
459
fn inner (
459
460
module : & Bound < ' _ , PyModule > ,
460
- name : Bound < ' _ , PyString > ,
461
- value : Bound < ' _ , PyAny > ,
461
+ name : Borrowed < ' _ , ' _ , PyString > ,
462
+ value : Borrowed < ' _ , ' _ , PyAny > ,
462
463
) -> PyResult < ( ) > {
463
464
module
464
465
. index ( ) ?
465
466
. append ( & name)
466
467
. expect ( "could not append __name__ to __all__" ) ;
467
- module. setattr ( name, value. into_py ( module . py ( ) ) )
468
+ module. setattr ( name, value)
468
469
}
469
470
470
471
let py = self . py ( ) ;
471
472
inner (
472
473
self ,
473
- name. into_py ( py) . into_bound ( py) ,
474
- value. into_py ( py) . into_bound ( py) ,
474
+ name. into_pyobject ( py) . map_err ( Into :: into) ?. as_borrowed ( ) ,
475
+ value
476
+ . into_pyobject ( py)
477
+ . map_err ( Into :: into) ?
478
+ . into_any ( )
479
+ . as_borrowed ( ) ,
475
480
)
476
481
}
477
482
0 commit comments