From: Arun Babu Neelicattu abn@redhat.com
--- bin/bugzilla | 46 ++++++++++++++++++++++++---------------------- bugzilla/bug.py | 2 +- setup.py | 12 +++++++----- tests/__init__.py | 6 ++++-- tests/misc.py | 6 ++++-- tests/rw_functional.py | 10 ++++++---- 6 files changed, 46 insertions(+), 36 deletions(-)
diff --git a/bin/bugzilla b/bin/bugzilla index fa5314b..980970d 100755 --- a/bin/bugzilla +++ b/bin/bugzilla @@ -11,6 +11,8 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license.
+from __future__ import print_function + import getpass import locale import logging @@ -515,7 +517,7 @@ https://fedorahosted.org/mailman/listinfo/python-bugzilla http://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/Bug.html https://bugzilla.redhat.com/docs/en/html/api/Bugzilla/WebService/Bug.html'''
- print manpage + print(manpage)
def _do_query(bz, opt, parser): @@ -653,11 +655,11 @@ def _do_info(bz, opt):
if opt.products: for name in sorted([p["name"] for p in products]): - print name + print(name)
if opt.components: for name in sorted(bz.getcomponents(opt.components)): - print name + print(name)
if opt.component_owners: # Looking up this info for rhbz 'Fedora' product is sloooow @@ -672,8 +674,8 @@ def _do_info(bz, opt):
component_details = bz.getcomponentsdetails(opt.component_owners) for c in sorted(component_details): - print to_encoding(u"%s: %s" % - (c, component_details[c]['initialowner'])) + print(to_encoding(u"%s: %s" % + (c, component_details[c]['initialowner'])))
if opt.versions: for p in products: @@ -681,7 +683,7 @@ def _do_info(bz, opt): continue if "versions" in p: for v in p['versions']: - print to_encoding(v["name"]) + print(to_encoding(v["name"])) break
@@ -723,12 +725,12 @@ def _format_output(bz, opt, buglist): if opt.output == 'raw': buglist = bz.getbugs([b.bug_id for b in buglist]) for b in buglist: - print "Bugzilla %s: " % b.bug_id + print("Bugzilla %s: " % b.bug_id) for a in dir(b): if a.startswith("__") and a.endswith("__"): continue - print to_encoding(u"ATTRIBUTE[%s]: %s" % (a, getattr(b, a))) - print "\n\n" + print(to_encoding(u"ATTRIBUTE[%s]: %s" % (a, getattr(b, a)))) + print("\n\n") return
def bug_field(matchobj): @@ -779,7 +781,7 @@ def _format_output(bz, opt, buglist): return to_encoding(val)
for b in buglist: - print format_field_re.sub(bug_field, opt.outputformat) + print(format_field_re.sub(bug_field, opt.outputformat))
def _parse_triset(vallist, checkplus=True, checkminus=True, checkequal=True, @@ -969,7 +971,7 @@ def _do_get_attach(bz, opt, parser, args): while data: outfile.write(data) data = att.read(4096) - print "Wrote %s" % outfile.name + print("Wrote %s" % outfile.name)
return
@@ -1008,7 +1010,7 @@ def _do_set_attach(bz, opt, parser, args): # Upload attachments for bugid in args: attid = bz.attachfile(bugid, fileobj, desc, **kwargs) - print "Created attachment %i on bug %s" % (attid, bugid) + print("Created attachment %i on bug %s" % (attid, bugid))
################# @@ -1080,10 +1082,10 @@ def main(bzinstance=None): sys.stdout.write('Logging in... ') sys.stdout.flush() if bz.login(global_opt.user, global_opt.password): - print 'Authorization cookie received.' + print('Authorization cookie received.') sys.exit(0) else: - print 'failed.' + print('failed.') sys.exit(1)
# Set up authentication @@ -1162,32 +1164,32 @@ if __name__ == '__main__': main() except KeyboardInterrupt: log.debug("", exc_info=True) - print "\nExited at user request." + print("\nExited at user request.") sys.exit(1) except socket.error, e: log.debug("", exc_info=True) - print "\nConnection lost/failed: %s" % str(e) + print("\nConnection lost/failed: %s" % str(e)) sys.exit(2) except (xmlrpclib.Fault, urllib2.HTTPError), e: log.debug("", exc_info=True) - print "\nServer error: %s" % str(e) + print("\nServer error: %s" % str(e)) sys.exit(3) except xmlrpclib.ProtocolError, e: log.debug("", exc_info=True) - print "\nInvalid server response: %d %s" % (e.errcode, e.errmsg) + print("\nInvalid server response: %d %s" % (e.errcode, e.errmsg))
# Give SSL recommendations import pycurl sslerrcodes = [getattr(pycurl, ename) for ename in dir(pycurl) if ename.startswith("E_SSL")] if e.errcode in sslerrcodes: - print ("\nIf you trust the remote server, you can work " - "around this error with:\n" + print("\nIf you trust the remote server, you can work " + "around this error with:\n" " bugzilla --nosslverify ...")
# Detect redirect redir = (e.headers and e.headers.getheader("location", 0) or None) if redir: - print ("\nServer was attempting a redirect. Try: " - " bugzilla --bugzilla %s ..." % redir) + print("\nServer was attempting a redirect. Try: " + " bugzilla --bugzilla %s ..." % redir) sys.exit(4) diff --git a/bugzilla/bug.py b/bugzilla/bug.py index dbff5cb..4a068d3 100644 --- a/bugzilla/bug.py +++ b/bugzilla/bug.py @@ -48,7 +48,7 @@ class _Bug(object): '''Return a simple string representation of this bug
This is available only for compatibility. Using 'str(bug)' and - 'print bug' is not recommended because of potential encoding issues. + 'print(bug)' is not recommended because of potential encoding issues. Please use unicode(bug) where possible. ''' return unicode(self).encode(locale.getpreferredencoding(), 'replace') diff --git a/setup.py b/setup.py index 27761c4..415eee0 100755 --- a/setup.py +++ b/setup.py @@ -1,5 +1,7 @@ #!/usr/bin/python
+from __future__ import print_function + import glob import os import sys @@ -70,7 +72,7 @@ class TestCommand(Command): try: unittest.installHandler() except: - print "installHandler hack failed" + print("installHandler hack failed")
tests = unittest.TestLoader().loadTestsFromNames(testfiles) if self.only: @@ -82,14 +84,14 @@ class TestCommand(Command): newtests.append(testcase)
if not newtests: - print "--only didn't find any tests" + print("--only didn't find any tests") sys.exit(1)
tests = unittest.TestSuite(newtests) - print "Running only:" + print("Running only:") for test in newtests: - print "%s" % test - print + print("%s" % test) + print()
t = unittest.TextTestRunner(verbosity=1) diff --git a/tests/__init__.py b/tests/__init__.py index 5122cf0..8d32b39 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,6 @@
+from __future__ import print_function + import atexit import commands import difflib @@ -71,8 +73,8 @@ def clicomm(argv, bzinstance, returnmain=False, printcliout=False, ret = 0 mainout = None try: - print " ".join(argv) - print + print(" ".join(argv)) + print()
mainout = bugzillascript.main(bzinstance) except SystemExit, sys_e: diff --git a/tests/misc.py b/tests/misc.py index bb48bcc..865dd8e 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -9,6 +9,8 @@ Unit tests for building query strings with bin/bugzilla '''
+from __future__ import print_function + import atexit import os import shutil @@ -35,7 +37,7 @@ class MiscCLI(unittest.TestCase): from logilab.common.optik_ext import ManHelpFormatter ignore = ManHelpFormatter except Exception, e: - print "Skipping man page test: %s" % e + print("Skipping man page test: %s" % e) return
out = tests.clicomm("bugzilla --generate-man", None) @@ -68,7 +70,7 @@ class MiscAPI(unittest.TestCase): def testCookies(self): if (sys.version_info[0] < 2 or (sys.version_info[0] == 2 and sys.version_info[1] < 6)): - print "\npython too old, skipping cookie test" + print("\npython too old, skipping cookie test") return
cookiesbad = os.path.join(os.getcwd(), "tests/data/cookies-bad.txt") diff --git a/tests/rw_functional.py b/tests/rw_functional.py index 8bc9b93..a50f736 100644 --- a/tests/rw_functional.py +++ b/tests/rw_functional.py @@ -9,6 +9,8 @@ Unit tests that do permanent functional against a real bugzilla instances. '''
+from __future__ import print_function + import datetime import os import random @@ -63,13 +65,13 @@ class RHPartnerTest(BaseTest): # Check a known account that likely won't ever go away ret = bool(bz.getuser("anaconda-maint-list@redhat.com").groupnames) if not ret: - print "\nNo admin privs, skipping %s" % funcname + print("\nNo admin privs, skipping %s" % funcname) return ret
def _check_rh_privs(self, bz, funcname, quiet=False): noprivs = bool(bz.getbugs([184858]) == [None]) if noprivs and not quiet: - print "\nNo RH privs, skipping %s" % funcname + print("\nNo RH privs, skipping %s" % funcname) return not noprivs
@@ -97,7 +99,7 @@ class RHPartnerTest(BaseTest):
bugid = int(newout.splitlines()[2]) bug = bz.getbug(bugid) - print "\nCreated bugid: %s" % bugid + print("\nCreated bugid: %s" % bugid)
# Verify hasattr works self.assertTrue(hasattr(bug, "id")) @@ -142,7 +144,7 @@ class RHPartnerTest(BaseTest):
bugid = int(newout.splitlines()[2]) bug = bz.getbug(bugid) - print "\nCreated bugid: %s" % bugid + print("\nCreated bugid: %s" % bugid)
self.assertEquals(bug.summary, summary) self.assertEquals(bug.bug_file_loc, url)
From: Arun Babu Neelicattu abn@redhat.com
--- bin/bugzilla | 16 ++++++++++------ bugzilla/base.py | 10 +++++++--- tests/__init__.py | 3 ++- tests/misc.py | 3 ++- tests/rw_functional.py | 3 ++- 5 files changed, 23 insertions(+), 12 deletions(-)
diff --git a/bin/bugzilla b/bin/bugzilla index 980970d..ec9ba77 100755 --- a/bin/bugzilla +++ b/bin/bugzilla @@ -64,12 +64,13 @@ def open_without_clobber(name, *args): while fd is None: try: fd = os.open(name, os.O_CREAT | os.O_EXCL, 0666) - except OSError, e: - if e.errno == os.errno.EEXIST: + except OSError: + err = sys.exc_info()[1] + if err.errno == os.errno.EEXIST: name = "%s.%i" % (orig_name, count) count += 1 else: - raise IOError(e.errno, e.strerror, e.filename) + raise IOError(err.errno, err.strerror, err.filename) fobj = open(name, *args) if fd != fobj.fileno(): os.close(fd) @@ -1166,15 +1167,18 @@ if __name__ == '__main__': log.debug("", exc_info=True) print("\nExited at user request.") sys.exit(1) - except socket.error, e: + except socket.error: + e = sys.exc_info()[1] log.debug("", exc_info=True) print("\nConnection lost/failed: %s" % str(e)) sys.exit(2) - except (xmlrpclib.Fault, urllib2.HTTPError), e: + except (xmlrpclib.Fault, urllib2.HTTPError): + e = sys.exc_info()[1] log.debug("", exc_info=True) print("\nServer error: %s" % str(e)) sys.exit(3) - except xmlrpclib.ProtocolError, e: + except xmlrpclib.ProtocolError: + e = sys.exc_info()[1] log.debug("", exc_info=True) print("\nInvalid server response: %d %s" % (e.errcode, e.errmsg))
diff --git a/bugzilla/base.py b/bugzilla/base.py index 9c25fbb..878edaf 100644 --- a/bugzilla/base.py +++ b/bugzilla/base.py @@ -12,6 +12,7 @@ import cookielib import os import StringIO +import sys import urllib2 import urlparse import xmlrpclib @@ -36,7 +37,8 @@ def _detect_filetype(fname): import magic mimemagic = magic.open(magic.MAGIC_MIME_TYPE) mimemagic.load() - except ImportError, e: + except ImportError: + e = sys.exc_info()[1] log.debug("Could not load python-magic: %s", e) mimemagic = False if mimemagic is False: @@ -47,7 +49,8 @@ def _detect_filetype(fname):
try: return mimemagic.file(fname) - except Exception, e: + except Exception: + e = sys.exc_info()[1] log.debug("Could not detect content_type: %s", e) return None
@@ -206,7 +209,8 @@ class _CURLTransport(xmlrpclib.Transport): "SIGINT, raising") m.remove_handle(self.c) raise KeyboardInterrupt - except pycurl.error, e: + except pycurl.error: + e = sys.exc_info()[1] raise xmlrpclib.ProtocolError(url, e[0], e[1], None)
b.seek(0) diff --git a/tests/__init__.py b/tests/__init__.py index 8d32b39..4bf7541 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -77,7 +77,8 @@ def clicomm(argv, bzinstance, returnmain=False, printcliout=False, print()
mainout = bugzillascript.main(bzinstance) - except SystemExit, sys_e: + except SystemExit: + sys_e = sys.exc_info()[1] ret = sys_e.code
outt = "" diff --git a/tests/misc.py b/tests/misc.py index 865dd8e..6f800ec 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -36,7 +36,8 @@ class MiscCLI(unittest.TestCase):
from logilab.common.optik_ext import ManHelpFormatter ignore = ManHelpFormatter - except Exception, e: + except Exception: + e = sys.exc_info()[1] print("Skipping man page test: %s" % e) return
diff --git a/tests/rw_functional.py b/tests/rw_functional.py index a50f736..ce06265 100644 --- a/tests/rw_functional.py +++ b/tests/rw_functional.py @@ -698,7 +698,8 @@ class RHPartnerTest(BaseTest): bz.cookiefile = None raise AssertionError("Setting cookiefile for active connection " "should fail.") - except RuntimeError, e: + except RuntimeError: + e = sys.exc_info()[1] self.assertTrue("disconnect()" in str(e))
bz.disconnect()
From: Arun Babu Neelicattu abn@redhat.com
--- bugzilla/bug.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/bugzilla/bug.py b/bugzilla/bug.py index 4a068d3..580aaf4 100644 --- a/bugzilla/bug.py +++ b/bugzilla/bug.py @@ -51,7 +51,8 @@ class _Bug(object): 'print(bug)' is not recommended because of potential encoding issues. Please use unicode(bug) where possible. ''' - return unicode(self).encode(locale.getpreferredencoding(), 'replace') + return self.__unicode__().encode( + locale.getpreferredencoding(), 'replace')
def __unicode__(self): '''Return a simple unicode string representation of this bug'''
From: Arun Babu Neelicattu abn@redhat.com
--- tests/__init__.py | 4 ++-- tests/misc.py | 2 +- tests/rw_functional.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/tests/__init__.py b/tests/__init__.py index 4bf7541..10df4b4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -43,8 +43,8 @@ def diff(orig, new): def difffile(expect, filename): expect += '\n' if not os.path.exists(filename) or os.getenv("__BUGZILLA_UNITTEST_REGEN"): - file(filename, "w").write(expect) - ret = diff(file(filename).read(), expect) + open(filename, "w").write(expect) + ret = diff(open(filename).read(), expect) if ret: raise AssertionError("Output was different:\n%s" % ret)
diff --git a/tests/misc.py b/tests/misc.py index 6f800ec..0fa69a2 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -87,7 +87,7 @@ class MiscAPI(unittest.TestCase):
# Mozilla should be converted inplace to LWP bugzilla.Bugzilla3(url=None, cookiefile=cookiesnew) - self.assertEquals(file(cookiesmoz).read(), file(cookiesnew).read()) + self.assertEquals(open(cookiesmoz).read(), open(cookiesnew).read())
# Make sure bad cookies raise an error try: diff --git a/tests/rw_functional.py b/tests/rw_functional.py index ce06265..e4b0296 100644 --- a/tests/rw_functional.py +++ b/tests/rw_functional.py @@ -42,7 +42,7 @@ class BaseTest(unittest.TestCase): cookiefile = cf domain = urllib2.urlparse.urlparse(self.url)[1] if os.path.exists(cookiefile): - out = file(cookiefile).read(1024) + out = open(cookiefile).read(1024) if domain in out: return
@@ -509,8 +509,8 @@ class RHPartnerTest(BaseTest):
self.assertEquals(len(out), 3) self.assertEquals(fname, "bz-attach-get1.txt") - self.assertEquals(file(fname).read(), - file(testfile).read()) + self.assertEquals(open(fname).read(), + open(testfile).read()) os.unlink(fname)
# Get all attachments
On 10/17/2013 10:50 AM, abn@redhat.com wrote:
From: Arun Babu Neelicattu abn@redhat.com
bin/bugzilla | 46 ++++++++++++++++++++++++---------------------- bugzilla/bug.py | 2 +- setup.py | 12 +++++++----- tests/__init__.py | 6 ++++-- tests/misc.py | 6 ++++-- tests/rw_functional.py | 10 ++++++---- 6 files changed, 46 insertions(+), 36 deletions(-)
Thanks for splitting these out, I've pushed all 4 now.
- Cole
python-bugzilla@lists.stg.fedorahosted.org