From b37c7cb8e95fd2579a54c011a0b47eb0545498a5 Mon Sep 17 00:00:00 2001 From: Leeh Peter Date: Wed, 18 Aug 2021 09:47:27 +0200 Subject: [PATCH 1/5] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7fe6726913..f1b128b44c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Loosened the lower bound on the `num-complex` optional dependency to support interop with `rust-numpy` and `ndarray` when building with the MSRV of 1.41 [#1799](https://github.com/PyO3/pyo3/pull/1799) -- Add missing `Py_DECREF` to `Python::run_code` which fixes a memory leak when +- Add missing `Py_DECREF` to `Python::run_code` and `PyModule::from_code` which fixes a memory leak when calling Python from Rust. [#1806](https://github.com/PyO3/pyo3/pull/1806) ## [0.14.2] - 2021-08-09 From d980f578402381fd2eaa4c9a3a9938699a8656b3 Mon Sep 17 00:00:00 2001 From: Leeh Peter Date: Wed, 18 Aug 2021 09:48:46 +0200 Subject: [PATCH 2/5] fix memory leak in PyModule::from_code --- src/types/module.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types/module.rs b/src/types/module.rs index 4d9c43559a2..d302281d01d 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -130,6 +130,7 @@ impl PyModule { return Err(PyErr::api_call_failed(py)); } + ffi::Py_DECREF(cptr); <&PyModule as crate::FromPyObject>::extract(py.from_owned_ptr_or_err(mptr)?) } } From 9b84358bf2723579857cfe1cdc17a838dbfbea6d Mon Sep 17 00:00:00 2001 From: Leeh Peter Date: Wed, 18 Aug 2021 10:22:20 +0200 Subject: [PATCH 3/5] add PR link to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1b128b44c0..fe3d8c14923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. interop with `rust-numpy` and `ndarray` when building with the MSRV of 1.41 [#1799](https://github.com/PyO3/pyo3/pull/1799) - Add missing `Py_DECREF` to `Python::run_code` and `PyModule::from_code` which fixes a memory leak when - calling Python from Rust. [#1806](https://github.com/PyO3/pyo3/pull/1806) + calling Python from Rust. [#1806](https://github.com/PyO3/pyo3/pull/1806), [#1810](https://github.com/PyO3/pyo3/pull/1810) ## [0.14.2] - 2021-08-09 From 61a8c0c6ddb0d24d38701281ffd5b871918cf416 Mon Sep 17 00:00:00 2001 From: Leeh Peter Date: Wed, 18 Aug 2021 12:15:56 +0200 Subject: [PATCH 4/5] Add Py_DECREF also when PyImport_ExecCodeModuleEx fails --- src/types/module.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types/module.rs b/src/types/module.rs index d302281d01d..4f1af2d49dd 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -127,6 +127,7 @@ impl PyModule { let mptr = ffi::PyImport_ExecCodeModuleEx(module.as_ptr(), cptr, filename.as_ptr()); if mptr.is_null() { + ffi::Py_DECREF(cptr); return Err(PyErr::api_call_failed(py)); } From 6713ae3cd833b6c7d5c7f34cbba1f1ccd7da0f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Le=C3=A9h?= <52788117+Ptrskay3@users.noreply.github.com> Date: Wed, 18 Aug 2021 12:25:53 +0200 Subject: [PATCH 5/5] Remove duplicated calls, simplify logic Co-authored-by: messense --- src/types/module.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/types/module.rs b/src/types/module.rs index 4f1af2d49dd..97bdef366e1 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -126,12 +126,11 @@ impl PyModule { } let mptr = ffi::PyImport_ExecCodeModuleEx(module.as_ptr(), cptr, filename.as_ptr()); + ffi::Py_DECREF(cptr); if mptr.is_null() { - ffi::Py_DECREF(cptr); return Err(PyErr::api_call_failed(py)); } - ffi::Py_DECREF(cptr); <&PyModule as crate::FromPyObject>::extract(py.from_owned_ptr_or_err(mptr)?) } }