#14 Don't leak descriptors in Python API call
Merged 3 years ago by praiskup. Opened 3 years ago by praiskup.
praiskup/prunerepo res-leak  into  main

file modified
+13 -7
@@ -10,6 +10,7 @@ 

  

  import re

  import os

+ from contextlib import contextmanager

  

  import dnf

  
@@ -26,9 +27,11 @@ 

      return repo_url

  

  

- def get_initialized_dnf(repo, log):

+ @contextmanager

+ def initialized_dnf(repo, log):

      """

-     Return DNF Base object pre-configured to work with the given REPO.

+     Prepare and yield DNF Base object pre-configured to work with the given

+     REPO.  Make sure you run this in `with` context.

      """

      base = dnf.Base()

      base.read_all_repos()
@@ -50,7 +53,10 @@ 

  

      # read the metadata

      base.fill_sack()

-     return base

+     try:

+         yield base

+     finally:

+         base.close()

  

  

  def get_mapping(repo, log):
@@ -59,10 +65,10 @@ 

      and map the source RPM name to SRPMs and vice versa.

      """

      # query the metadata

-     base = get_initialized_dnf(repo, log)

-     query = base.sack.query()

-     remote = query.filter(reponame__neq="@System")

-     available_packages = list(remote)

+     with initialized_dnf(repo, log) as base:

+         query = base.sack.query()

+         remote = query.filter(reponame__neq="@System")

+         available_packages = list(remote)

  

      found_srpms = set()

      map_srpm_to_rpms = {}

+1

FTR it should be possible to use with dnf.Base() as base:

rebased onto 57d511a

3 years ago

That is actually a good point. I updated the code so it doesn't look like an unintentional close() statement. PTAL

Commit 2fc2496 fixes this pull-request

Pull-Request has been merged by praiskup

3 years ago
Metadata