From 06c1bde586fbb564b45ecc8ef09f1bbd990400f6 Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Thu, 16 Aug 2018 22:05:28 -0500 Subject: [PATCH] patchutils.py: fix crash when a patch deletes a file --- staging/patchutils.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/staging/patchutils.py b/staging/patchutils.py index 5c31438d..d7d7e299 100644 --- a/staging/patchutils.py +++ b/staging/patchutils.py @@ -387,8 +387,11 @@ def read_patch(filename, fp=None): def apply_patch(original, patchfile, reverse=False, fuzz=2): """Apply a patch with optional fuzz - uses the commandline 'patch' utility.""" + if original is None: + raise PatchApplyError("Failed to apply patch - file was deleted (exitcode %d)." % exitcode) + + result = tempfile.NamedTemporaryFile(prefix="apply_patch", mode='w+', delete=False) - result = tempfile.NamedTemporaryFile(mode='w+', delete=False) try: # We open the file again to avoid race-conditions with multithreaded reads with open(original.name) as fp: @@ -404,6 +407,9 @@ def apply_patch(original, patchfile, reverse=False, fuzz=2): if exitcode != 0: raise PatchApplyError("Failed to apply patch (exitcode %d)." % exitcode) + if not os.path.exists(result.name): + return None + # Hack - we can't keep the file open while patching ('patch' might rename/replace # the file), so create a new _TemporaryFileWrapper object for the existing path. return tempfile._TemporaryFileWrapper(file=open(result.name, 'r+'), \