diff --git a/CHANGES.rst b/CHANGES.rst index d075f83..13b94da 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,7 @@ Changes 3.1 (unreleased) ================ -- Nothing changed yet. +- Modify read/write operations to use Path() to avoid dangling file handles 3.0 (2023-02-07) diff --git a/setup.py b/setup.py index 1908255..b081d2d 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,12 @@ import os +from pathlib import Path from setuptools import find_packages from setuptools import setup def read(*rnames): - return open(os.path.join(os.path.dirname(__file__), *rnames)).read() + return Path(os.path.join(os.path.dirname(__file__), *rnames)).read_text() long_description = ( diff --git a/src/zc/zodbrecipes/__init__.py b/src/zc/zodbrecipes/__init__.py index fe32113..cdeba8e 100644 --- a/src/zc/zodbrecipes/__init__.py +++ b/src/zc/zodbrecipes/__init__.py @@ -15,6 +15,7 @@ import logging import os from io import StringIO +from pathlib import Path import zc.buildout import zc.recipe.egg @@ -106,7 +107,7 @@ def install(self): logrotate = options['logrotate'] if logrotate: - open(logrotate, 'w').write(logrotate_template % dict( + Path(logrotate).write_text(logrotate_template % dict( logfile=event_log_path, rc=os.path.join(options['rc-directory'], rc), conf=zdaemon_conf_path, @@ -202,8 +203,8 @@ def install(self): self.egg.install() requirements, ws = self.egg.working_set() - open(zeo_conf_path, 'w').write(str(zeo_conf)) - open(zdaemon_conf_path, 'w').write(str(zdaemon_conf)) + Path(zeo_conf_path).write_text(str(zeo_conf)) + Path(zdaemon_conf_path).write_text(str(zdaemon_conf)) if options.get('shell-script') == 'true': if not os.path.exists(options['zdaemon']): @@ -221,8 +222,9 @@ def install(self): contents = "#!/bin/sh\n%s\n" % contents dest = os.path.join(options['rc-directory'], rc) - if not (os.path.exists(dest) and open(dest).read() == contents): - open(dest, 'w').write(contents) + if not (os.path.exists(dest) and + Path(dest).read_text() == contents): + Path(dest).write_text(contents) os.chmod(dest, 0o755) logger.info("Generated shell script %r.", dest)