@@ -108,12 +108,12 @@ impl From<&ImportError> for Error {
108108/// Import and return a handle to the module `$m`.
109109macro_rules! pyimport { ( $name: path, $m: literal) => {
110110 Python :: with_gil( |py|
111- match PyModule :: import_bound ( py, intern!( py, $m) ) {
111+ match PyModule :: import ( py, intern!( py, $m) ) {
112112 Ok ( m) => Ok ( m. into( ) ) ,
113113 Err ( e) => {
114114 let mut msg = stringify!( $name) . to_string( ) ;
115115 msg. push_str( ": " ) ;
116- if let Ok ( s) = e. value_bound ( py) . str ( ) {
116+ if let Ok ( s) = e. value ( py) . str ( ) {
117117 let s = s. to_str( ) . unwrap_or( "Import error" ) ;
118118 msg. push_str( s)
119119 }
@@ -133,6 +133,13 @@ lazy_static! {
133133 } ;
134134}
135135
136+ // RuntimeWarning: More than 20 figures have been opened. Figures
137+ // created through the pyplot interface (`matplotlib.pyplot.figure`)
138+ // are retained until explicitly closed and may consume too much
139+ // memory. […] Consider using `matplotlib.pyplot.close()`.
140+ //
141+ // => Do not use pyplot interface (since we need handles anyway).
142+
136143
137144/// Container for most of the (sub-)plot elements: Axis, Tick,
138145/// [`Line2D`], Text, Polygon, etc., and sets the coordinate system.
@@ -246,9 +253,9 @@ impl Figure {
246253 /// Default width: 6.4, default height: 4.8
247254 pub fn set_size_inches ( & mut self , width : f64 , height : f64 ) -> & mut Self {
248255 Python :: with_gil ( |py| {
249- let kwargs = PyDict :: new_bound ( py) ;
256+ let kwargs = PyDict :: new ( py) ;
250257 kwargs. set_item ( "size_inches" , ( width, height) ) . unwrap ( ) ;
251- self . fig . call_method_bound ( py, intern ! ( py, "set" ) , ( ) ,
258+ self . fig . call_method ( py, intern ! ( py, "set" ) , ( ) ,
252259 Some ( & kwargs) ) . unwrap ( ) ;
253260 } ) ;
254261 self
@@ -274,11 +281,11 @@ impl<'a> Savefig<'a> {
274281
275282 pub fn to_file ( & self , path : impl AsRef < Path > ) -> Result < ( ) , Error > {
276283 Python :: with_gil ( |py| {
277- let kwargs = PyDict :: new_bound ( py) ;
284+ let kwargs = PyDict :: new ( py) ;
278285 if let Some ( dpi) = self . dpi {
279286 kwargs. set_item ( "dpi" , dpi) . unwrap ( )
280287 }
281- self . fig . call_method_bound (
288+ self . fig . call_method (
282289 py, intern ! ( py, "savefig" ) ,
283290 ( path. as_ref ( ) , ) , Some ( & kwargs)
284291 ) . map_err ( |e| {
@@ -509,8 +516,8 @@ impl Axes {
509516 // FIXME: Do we want to check that `x` and `y` have the same
510517 // dimension? Better error message?
511518 meth ! ( self . ax, scatter, py -> {
512- let xn = x. as_ref( ) . to_pyarray_bound ( py) ;
513- let yn = y. as_ref( ) . to_pyarray_bound ( py) ;
519+ let xn = x. as_ref( ) . to_pyarray ( py) ;
520+ let yn = y. as_ref( ) . to_pyarray ( py) ;
514521 ( xn, yn) } )
515522 . unwrap ( ) ;
516523 self
@@ -588,13 +595,13 @@ impl Axes {
588595 Python :: with_gil ( |py| {
589596 let elements = lines. into_iter ( ) . map ( |l| l. line2d ) ;
590597 if elements. len ( ) == 0 { // FIXME: .is_empty is unstable
591- self . ax . call_method_bound ( py, intern ! ( py, "legend" ) , ( ) , None )
598+ self . ax . call_method ( py, intern ! ( py, "legend" ) , ( ) , None )
592599 . unwrap ( ) ;
593600 } else {
594- let dic = PyDict :: new_bound ( py) ;
595- dic. set_item ( "handles" , PyList :: new_bound ( py, elements) )
601+ let dic = PyDict :: new ( py) ;
602+ dic. set_item ( "handles" , PyList :: new ( py, elements) . unwrap ( ) )
596603 . unwrap ( ) ;
597- self . ax . call_method_bound ( py, intern ! ( py, "legend" ) , ( ) ,
604+ self . ax . call_method ( py, intern ! ( py, "legend" ) , ( ) ,
598605 Some ( & dic) )
599606 . unwrap ( ) ;
600607 }
@@ -634,7 +641,7 @@ impl<'a> PlotOptions<'a> {
634641 }
635642
636643 fn kwargs ( & ' a self , py : Python < ' a > ) -> Bound < ' a , PyDict > {
637- let kwargs = PyDict :: new_bound ( py) ;
644+ let kwargs = PyDict :: new ( py) ;
638645 if self . animated {
639646 kwargs. set_item ( "animated" , true ) . unwrap ( )
640647 }
@@ -650,7 +657,7 @@ impl<'a> PlotOptions<'a> {
650657 kwargs. set_item ( "markersize" , w) . unwrap ( )
651658 }
652659 if let Some ( rgba) = self . color {
653- let color = PyTuple :: new_bound ( py, rgba) ;
660+ let color = PyTuple :: new ( py, rgba) . unwrap ( ) ;
654661 kwargs. set_item ( "color" , color) . unwrap ( )
655662 }
656663 kwargs
@@ -660,9 +667,9 @@ impl<'a> PlotOptions<'a> {
660667 fn plot_xy (
661668 & self , py : Python < ' _ > , axes : & Axes , x : & [ f64 ] , y : & [ f64 ]
662669 ) -> Line2D {
663- let x = x. to_pyarray_bound ( py) ;
664- let y = y. to_pyarray_bound ( py) ;
665- let lines = axes. ax . call_method_bound ( py,
670+ let x = x. to_pyarray ( py) ;
671+ let y = y. to_pyarray ( py) ;
672+ let lines = axes. ax . call_method ( py,
666673 "plot" , ( x, y, self . fmt ) , Some ( & self . kwargs ( py) ) ) . unwrap ( ) ;
667674 let lines: & Bound < PyList > = lines. downcast_bound ( py) . unwrap ( ) ;
668675 // Extract the element from the list of length 1 (1 data plotted)
@@ -672,8 +679,8 @@ impl<'a> PlotOptions<'a> {
672679
673680 fn plot_y ( & self , py : Python < ' _ > , axes : & Axes , y : & [ f64 ] ) -> Line2D
674681 {
675- let y = y. to_pyarray_bound ( py) ;
676- let lines = axes. ax . call_method_bound ( py,
682+ let y = y. to_pyarray ( py) ;
683+ let lines = axes. ax . call_method ( py,
677684 "plot" , ( y, self . fmt ) , Some ( & self . kwargs ( py) ) ) . unwrap ( ) ;
678685 let lines: & Bound < PyList > = lines. downcast_bound ( py) . unwrap ( ) ;
679686 let line2d = lines. get_item ( 0 ) . unwrap ( ) . into ( ) ;
@@ -902,26 +909,26 @@ macro_rules! set_contour_options { () => {
902909 let py = d. py( ) ;
903910 if let Some ( levels) = self . levels {
904911 let n = levels. len( ) ;
905- let levels = levels. to_pyarray_bound ( py) ;
912+ let levels = levels. to_pyarray ( py) ;
906913 d. set_item( "levels" , levels) . unwrap( ) ;
907914
908915 if let Some ( colors) = & self . colors {
909916 if colors. len( ) >= n {
910- let colors = PyList :: new_bound ( py, colors) ;
917+ let colors = PyList :: new ( py, colors) . unwrap ( ) ;
911918 d. set_item( "colors" , colors) . unwrap( ) ;
912919 } else {
913920 let default = self . options. color. unwrap_or( [ 0. , 0. , 0. , 1. ] ) ;
914921 let mut colors = colors. clone( ) ;
915922 for _ in 0 .. n - colors. len( ) {
916923 colors. push( default ) ;
917924 }
918- let colors = PyList :: new_bound ( py, colors) ;
925+ let colors = PyList :: new ( py, colors) . unwrap ( ) ;
919926 d. set_item( "colors" , colors) . unwrap( ) ;
920927 }
921928 } else if let Some ( color) = self . options. color {
922929 // let colors = std::iter::repeat_n(color, n);
923930 let colors = vec![ color; n] ;
924- let colors = PyList :: new_bound ( py, colors) ;
931+ let colors = PyList :: new ( py, colors) . unwrap ( ) ;
925932 d. set_item( "colors" , colors) . unwrap( ) ;
926933 }
927934 }
@@ -946,13 +953,13 @@ where D: AsRef<[f64]> {
946953
947954 pub fn plot ( & self ) -> QuadContourSet {
948955 Python :: with_gil ( |py| {
949- let x = self . x . as_ref ( ) . to_pyarray_bound ( py) ;
950- let y = self . y . as_ref ( ) . to_pyarray_bound ( py) ;
951- let z = self . z . to_pyarray_bound ( py) ;
956+ let x = self . x . as_ref ( ) . to_pyarray ( py) ;
957+ let y = self . y . as_ref ( ) . to_pyarray ( py) ;
958+ let z = self . z . to_pyarray ( py) ;
952959 let mut opt = self . options . kwargs ( py) ;
953960 self . update_dict ( & mut opt) ;
954961 let contours = self . axes . ax
955- . call_method_bound ( py, intern ! ( py, "contour" ) ,
962+ . call_method ( py, intern ! ( py, "contour" ) ,
956963 ( x, y, z) ,
957964 Some ( & opt) )
958965 . unwrap ( ) ;
@@ -1000,13 +1007,13 @@ where F: FnMut(f64, f64) -> f64 {
10001007 }
10011008 }
10021009 Python :: with_gil ( |py| {
1003- let x = x. to_pyarray_bound ( py) ;
1004- let y = y. to_pyarray_bound ( py) ;
1005- let z = z. to_pyarray_bound ( py) ;
1010+ let x = x. to_pyarray ( py) ;
1011+ let y = y. to_pyarray ( py) ;
1012+ let z = z. to_pyarray ( py) ;
10061013 let mut opt = self . options . kwargs ( py) ;
10071014 self . update_dict ( & mut opt) ;
10081015 let contours = self . axes . ax
1009- . call_method_bound ( py, intern ! ( py, "contour" ) ,
1016+ . call_method ( py, intern ! ( py, "contour" ) ,
10101017 ( x, y, z) ,
10111018 Some ( & opt) )
10121019 . unwrap ( ) ;
@@ -1017,18 +1024,22 @@ where F: FnMut(f64, f64) -> f64 {
10171024
10181025
10191026impl Line2D {
1020- fn set_kw ( & self , prop : & str , v : impl ToPyObject ) {
1021- Python :: with_gil ( |py| {
1022- let kwargs = PyDict :: new_bound ( py) ;
1023- kwargs. set_item ( prop, v) . unwrap ( ) ;
1024- self . line2d . call_method_bound ( py, "set" , ( ) , Some ( & kwargs) )
1025- . unwrap ( ) ;
1026- } )
1027+ fn set_kw < ' a > (
1028+ & self ,
1029+ py : Python < ' a > ,
1030+ prop : & str ,
1031+ v : impl IntoPyObject < ' a >
1032+ ) {
1033+ let kwargs = PyDict :: new ( py) ;
1034+ kwargs. set_item ( prop, v) . unwrap ( ) ;
1035+ self . line2d . call_method ( py, "set" , ( ) , Some ( & kwargs) ) . unwrap ( ) ;
10271036 }
10281037
10291038 pub fn set_label ( & mut self , label : impl AsRef < str > ) -> & mut Self {
1030- self . set_kw ( "label" , label. as_ref ( ) ) ;
1031- self
1039+ Python :: with_gil ( |py| {
1040+ self . set_kw ( py, "label" , label. as_ref ( ) ) ;
1041+ self
1042+ } )
10321043 }
10331044
10341045 /// Set the color of the line to `c`.
@@ -1040,13 +1051,17 @@ impl Line2D {
10401051 }
10411052
10421053 pub fn set_linewidth ( & mut self , w : f64 ) -> & mut Self {
1043- self . set_kw ( "linewidth" , w) ;
1044- self
1054+ Python :: with_gil ( |py| {
1055+ self . set_kw ( py, "linewidth" , w) ;
1056+ self
1057+ } )
10451058 }
10461059
10471060 pub fn linewidth ( self , w : f64 ) -> Self {
1048- self . set_kw ( "linewidth" , w) ;
1049- self
1061+ Python :: with_gil ( |py| {
1062+ self . set_kw ( py, "linewidth" , w) ;
1063+ self
1064+ } )
10501065 }
10511066}
10521067
0 commit comments