From c53c2aaea781ae7d8081d52c46683495c19ccbb3 Mon Sep 17 00:00:00 2001 From: Bernhard Kaindl Date: Mon, 6 Nov 2023 15:52:39 +0100 Subject: [PATCH 1/2] Use hashlib.md5 (2.7 and py3) as md5_new() Signed-off-by: Bernhard Kaindl --- xen-bugtool | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/xen-bugtool b/xen-bugtool index 5984baae..08e576bc 100755 --- a/xen-bugtool +++ b/xen-bugtool @@ -55,6 +55,7 @@ import traceback import urllib import xml import zipfile +from hashlib import md5 as md5_new from select import select from signal import SIGHUP, SIGTERM, SIGUSR1 from subprocess import PIPE, Popen @@ -64,15 +65,6 @@ from xml.etree.ElementTree import Element import defusedxml.sax -try: - import hashlib - def md5_new(): - return hashlib.md5() -except: - import md5 - def md5_new(): - return md5.new() - # Monkey-patch zipfile's __del__ function to be less stupid # Specifically, it calls close which further writes to the file, which # fails with ENOSPC if the root filesystem is full From 00ff7a992b2761e26629cbdc685dfb25242925d6 Mon Sep 17 00:00:00 2001 From: Bernhard Kaindl Date: Mon, 6 Nov 2023 15:56:31 +0100 Subject: [PATCH 2/2] Skip monkey-patching ZipFile on py3.7+ (has the fix) Signed-off-by: Bernhard Kaindl --- xen-bugtool | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/xen-bugtool b/xen-bugtool index 08e576bc..b1bdbd4a 100755 --- a/xen-bugtool +++ b/xen-bugtool @@ -65,16 +65,18 @@ from xml.etree.ElementTree import Element import defusedxml.sax +# Fixed in 3.7: https://github.com/python/cpython/pull/12628 # Monkey-patch zipfile's __del__ function to be less stupid # Specifically, it calls close which further writes to the file, which # fails with ENOSPC if the root filesystem is full -zipfile_del = zipfile.ZipFile.__del__ -def exceptionless_del(*argl, **kwargs): - try: - zipfile_del(*argl, **kwargs) - except: - pass -zipfile.ZipFile.__del__ = exceptionless_del +if sys.version < "3.7": + zipfile_del = zipfile.ZipFile.__del__ # type: ignore[attr-defined] # mypy,pyright + def exceptionless_del(*argl, **kwargs): + try: + zipfile_del(*argl, **kwargs) + except OSError: + pass + zipfile.ZipFile.__del__ = exceptionless_del # type: ignore[attr-defined] # mypy,pyright def xapi_local_session(): import XenAPI # Import on first use.