Close included files after reading
authorJeremy Stanley <fungi@yuggoth.org>
Sun, 15 Aug 2021 15:13:32 +0000 (15:13 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Sun, 15 Aug 2021 15:58:26 +0000 (15:58 +0000)
Use a context block where raw files are included by our macro
replacement routine, so that we'll close the file descriptors once
we're finished reading them in.

mudpy/misc.py

index f94c2a8..f111b9e 100644 (file)
@@ -1371,14 +1371,14 @@ def replace_macros(user, text, is_input=False):
         elif macro.startswith("inc:"):
             incfile = mudpy.data.find_file(macro[4:], universe=universe)
             if os.path.exists(incfile):
-                incfd = codecs.open(incfile, "r", "utf-8")
                 replacement = ""
-                for line in incfd:
-                    if line.endswith("\n") and not line.endswith("\r\n"):
-                        line = line.replace("\n", "\r\n")
-                    replacement += line
-                # lose the trailing eol
-                replacement = replacement[:-2]
+                with codecs.open(incfile, "r", "utf-8") as incfd:
+                    for line in incfd:
+                        if line.endswith("\n") and not line.endswith("\r\n"):
+                            line = line.replace("\n", "\r\n")
+                        replacement += line
+                    # lose the trailing eol
+                    replacement = replacement[:-2]
             else:
                 replacement = ""
                 log("Couldn't read included " + incfile + " file.", 7)