From 45709611085ba5f8c9670ce9877fed5031416fc5 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sat, 24 Jul 2010 13:52:44 -0700 Subject: Handle UnicodeDecodeError from subprocess.getstatusoutput() calls, reported with python3.1. --- pym/portage/dispatch_conf.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'pym') diff --git a/pym/portage/dispatch_conf.py b/pym/portage/dispatch_conf.py index 956f52373..b4cded266 100644 --- a/pym/portage/dispatch_conf.py +++ b/pym/portage/dispatch_conf.py @@ -10,9 +10,9 @@ from __future__ import print_function import os, sys, shutil try: - from subprocess import getoutput as subprocess_getoutput + from subprocess import getstatusoutput as subprocess_getstatusoutput except ImportError: - from commands import getoutput as subprocess_getoutput + from commands import getstatusoutput as subprocess_getstatusoutput import portage from portage.localization import _ @@ -25,6 +25,21 @@ RCS_MERGE = "rcsmerge -p -r" + RCS_BRANCH + " '%s' > '%s'" DIFF3_MERGE = "diff3 -mE '%s' '%s' '%s' > '%s'" +def diffstatusoutput_len(cmd): + """ + Execute the string cmd in a shell with getstatusoutput() and return a + 2-tuple (status, output_length). If getstatusoutput() raises + UnicodeDecodeError (known to happen with python3.1), return a + 2-tuple (1, 1). This provides a simple way to check for non-zero + output length of diff commands, while providing simple handling of + UnicodeDecodeError when necessary. + """ + try: + status, output = subprocess_getstatusoutput(cmd) + return (status, len(output)) + except UnicodeDecodeError: + return (1, 1) + def read_config(mandatory_opts): loader = portage.env.loaders.KeyValuePairFileLoader( '/etc/dispatch-conf.conf', None) @@ -115,7 +130,7 @@ def file_archive(archive, curconf, newconf, mrgconf): # Archive the current config file if it isn't already saved if os.path.exists(archive) \ - and len(subprocess_getoutput("diff -aq '%s' '%s'" % (curconf,archive))) != 0: + and diffstatusoutput_len("diff -aq '%s' '%s'" % (curconf,archive))[1] != 0: suf = 1 while suf < 9 and os.path.exists(archive + '.' + str(suf)): suf += 1 -- cgit v1.2.3-1-g7c22