diff --git a/HACKING b/HACKING index 16552e6..0dda320 100644 --- a/HACKING +++ b/HACKING @@ -9,6 +9,17 @@ See http://docs.python.org/dev/howto/pyporting.html and http://python3porting.com/differences.html for tips on writing portable Python code. +In particular, you must capture exceptions in a way that's +compatible with both python 2.4 and 3.x. For example: + + try: + raise Exception() + except Exception: + import sys + exc = sys.exc_info()[1] + # Current exception is 'exc'. + pass + Tests ----- diff --git a/src/tito/builder/main.py b/src/tito/builder/main.py index 233d06d..68c3d0e 100644 --- a/src/tito/builder/main.py +++ b/src/tito/builder/main.py @@ -227,13 +227,15 @@ class BuilderBase(object): except (KeyboardInterrupt, SystemExit): print "" exit(1) - except RunCommandException, err: + except RunCommandException: + err = sys.exc_info()[1] msg = str(err) if (re.search('Failed build dependencies', err.output)): msg = "Please run 'yum-builddep %s' as root." % \ find_spec_file(self.relative_project_dir) error_out('%s' % msg) - except Exception, err: + except Exception: + err = sys.exc_info()[1] error_out('%s' % str(err)) print(output) files_written = find_wrote_in_rpmbuild_output(output) diff --git a/src/tito/cli.py b/src/tito/cli.py index a422f0e..c2b06f9 100644 --- a/src/tito/cli.py +++ b/src/tito/cli.py @@ -709,7 +709,8 @@ class TagModule(BaseCliModule): try: return tagger.run(self.options) - except TitoException, e: + except TitoException: + e = sys.exc_info()[1] error_out(e.message) def _validate_options(self): diff --git a/src/tito/release.py b/src/tito/release.py index 822712a..8c49599 100644 --- a/src/tito/release.py +++ b/src/tito/release.py @@ -16,6 +16,7 @@ Code for submitting builds for release. import copy import os +import sys import commands import tempfile import subprocess @@ -454,7 +455,7 @@ class YumRepoReleaser(RsyncReleaser): if artifact.endswith(".rpm") and not artifact.endswith(".src.rpm"): try: header = self._read_rpm_header(rpm_ts, artifact) - except rpm.error, e: + except rpm.error: continue self.new_rpm_dep_sets[header['name']] = header.dsOfHeader() @@ -467,7 +468,8 @@ class YumRepoReleaser(RsyncReleaser): full_path = os.path.join(temp_dir, filename) try: hdr = self._read_rpm_header(rpm_ts, full_path) - except rpm.error, e: + except rpm.error: + e = sys.exc_info()[1] print "error reading rpm header in '%s': %s" % (full_path, e) continue if hdr['name'] in self.new_rpm_dep_sets: diff --git a/test/unit/pep8-tests.py b/test/unit/pep8-tests.py index 7f1050d..8a5ec36 100644 --- a/test/unit/pep8-tests.py +++ b/test/unit/pep8-tests.py @@ -64,3 +64,23 @@ class TestPep8(TitoUnitTestFixture): self.assertEqual(result, 0, "Found PEP8 errors that may break your code in Python 3.") + + +class UglyHackishTest(TitoUnitTestFixture): + def setUp(self): + TitoUnitTestFixture.setUp(self) + os.chdir(REPO_DIR) + + def test_exceptions_2_dot_4(self): + # detect 'except rpm.error as e:' + regex = "'^[[:space:]]*except .* as .*:'" + cmd = "find . -type f -regex '.*\.py$' -exec egrep %s {} + | wc -l" % regex + result = int(getoutput(cmd)) + self.assertEqual(result, 0, "Found except clause not supported in Python 2.4") + + def test_exceptions_3(self): + # detect 'except rpm.error, e:' + regex = "'^[[:space:]]*except [^,]+,[[:space:]]*[[:alpha:]]+:'" + cmd = "find . -type f -regex '.*\.py$' -exec egrep %s {} + | wc -l" % regex + result = int(getoutput(cmd)) + self.assertEqual(result, 0, "Found except clause not supported in Python 3")