From: Jeremy Stanley Date: Sun, 15 Aug 2021 15:10:18 +0000 (+0000) Subject: Close data files after reading X-Git-Tag: 0.4.1~3 X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=commitdiff_plain;h=cc12b30f1d2a6b0007e39e16fdc3952165a2a730 Close data files after reading Starting with Python 3.8, the interpreter raises a ResourceWarning exception during garbage collection if it finds an unclosed file descriptor. Because of where this exception gets raised, it's ignored (but a warning is emitted on stderr). Use a typical context block in our loop to load data files, so that this condition won't occur. --- diff --git a/mudpy/data.py b/mudpy/data.py index b73959a..eeea8fb 100644 --- a/mudpy/data.py +++ b/mudpy/data.py @@ -1,6 +1,6 @@ """Data interface functions for the mudpy engine.""" -# Copyright (c) 2004-2018 mudpy authors. Permission to use, copy, +# Copyright (c) 2004-2021 mudpy authors. Permission to use, copy, # modify, and distribute this software is granted under terms # provided in the LICENSE file distributed with this software. @@ -57,7 +57,8 @@ class Data: self.source = find_file( self.source, relative=self.relative, universe=self.universe) try: - self.data = yaml.safe_load(open(self.source)) + with open(self.source) as datafd: + self.data = yaml.safe_load(datafd) log_entry = ("Loaded file %s into memory." % self.source, 5) except FileNotFoundError: # it's normal if the file is one which doesn't exist yet