From 29aa38571925bcebf35e41287173baae8fe4e6e4 Mon Sep 17 00:00:00 2001 From: lusitania Date: Sun, 5 Oct 2014 16:04:09 +0200 Subject: [PATCH 1/5] Py3 changes, Py2 backwards compatibility fix Rationale: Target current Python release and provide *backwards* compatibility if needed. Don't target legacy releases. Tested (in app) with Python 3.4 and loaded with Python 2.7. --- airbrake/utils.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/airbrake/utils.py b/airbrake/utils.py index 57107c3..f828b8d 100644 --- a/airbrake/utils.py +++ b/airbrake/utils.py @@ -1,15 +1,20 @@ """Util functions/classes for airbrake-python.""" -import Queue +try: + from queue import Queue, Full, Empty +except ImportError: + #Py2 legacy fix + from Queue import Queue, Full, Empty + import traceback import types -class CheckableQueue(Queue.Queue): +class CheckableQueue(Queue): """Checkable FIFO Queue which makes room for new items.""" def __init__(self, maxsize=1000): - Queue.Queue.__init__(self, maxsize=maxsize) + Queue.__init__(self, maxsize=maxsize) def __contains__(self, item): try: @@ -20,11 +25,11 @@ def __contains__(self, item): def put(self, item, block=False, timeout=1): try: - Queue.Queue.put(self, item, block=block, timeout=timeout) - except Queue.Full: + Queue.put(self, item, block=block, timeout=timeout) + except Full: try: self.get_nowait() - except Queue.Empty: + except Empty: pass self.put(item) From 2f30619966f193bc4003a3f130f1f47585e079a7 Mon Sep 17 00:00:00 2001 From: lusitania Date: Sun, 5 Oct 2014 16:10:46 +0200 Subject: [PATCH 2/5] Updated setuptools classifiers I have no tox at hand so my integration "tests" should be validated properly for all Python releases (2.7, 3.2, 3.3, 3.4?). `2to3` only really complained the import of Queue so I changed only that but you may want to validate all other issues as well. --- setup.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index da54d7b..245ebb1 100644 --- a/setup.py +++ b/setup.py @@ -35,6 +35,11 @@ test_suite='tests', install_requires=dependencies, packages=find_packages(exclude=['tests']), - classifiers=["Programming Language :: Python"], + classifiers=["Programming Language :: Python", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4" + ], license='Apache License (2.0)' ) From ef6dd5875e37ebec13a8a7fa06f849161b35b2bf Mon Sep 17 00:00:00 2001 From: lusitania Date: Sun, 5 Oct 2014 19:50:24 +0200 Subject: [PATCH 3/5] Py3 exception logging, issue #17 Previous commit didn't verify logging of exceptions. Critical issues: - types.TypeType is now built-in, added legacy switch - filter yields `TypeError: 'filter' object is not subscriptable`, changed to list comprehension (since it was used already the previous line) 2to3 also complained the use of map over list comprehension, changed for similar rationale. --- airbrake/handler.py | 2 +- airbrake/utils.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/airbrake/handler.py b/airbrake/handler.py index 7babe6c..4e3b008 100644 --- a/airbrake/handler.py +++ b/airbrake/handler.py @@ -93,7 +93,7 @@ def airbrake_error_from_logrecord(record): # find params from kwarg 'extra' # See "The second keyword argument is extra" # - https://docs.python.org/2/library/logging.html#logging.Logger.debug - for key, val in vars(record).items(): + for key, val in list(vars(record).items()): if not hasattr(_FAKE_LOGRECORD, key): # handle attribute/field name collisions: # logrecod attrs should not limit or take diff --git a/airbrake/utils.py b/airbrake/utils.py index f828b8d..34a7aef 100644 --- a/airbrake/utils.py +++ b/airbrake/utils.py @@ -7,7 +7,13 @@ import traceback import types +import sys +if sys.version_info < (3,): + #Py2 legacy fix + type_of_type = types.TypeType +else: + type_of_type = type class CheckableQueue(Queue): @@ -42,9 +48,9 @@ def is_exc_info_tuple(exc_info): """ try: errtype, value, tback = exc_info - if all(map(lambda x: x is None, exc_info)): + if all([x is None for x in exc_info]): return True - elif all((isinstance(errtype, types.TypeType), + elif all((isinstance(errtype, type_of_type), isinstance(value, Exception), isinstance(tback, types.TracebackType))): return True @@ -92,6 +98,6 @@ def pytb_lastline(excinfo=None): # strip whitespace, Falsy values, # and the string 'None', sometimes returned by the traceback module lines = [line.strip() for line in lines if line] - lines = filter(lambda line: str(line).lower() != 'none', lines) + lines = [line for line in lines if str(line).lower() != 'none'] if lines: return lines[-1] From 2a11ea0a10ac673258a9392f82b112989a3bfdd6 Mon Sep 17 00:00:00 2001 From: lusitania Date: Sun, 5 Oct 2014 20:03:42 +0200 Subject: [PATCH 4/5] Revert "Py3 exception logging, issue #17" This reverts commit ef6dd5875e37ebec13a8a7fa06f849161b35b2bf. --- airbrake/handler.py | 2 +- airbrake/utils.py | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/airbrake/handler.py b/airbrake/handler.py index 4e3b008..7babe6c 100644 --- a/airbrake/handler.py +++ b/airbrake/handler.py @@ -93,7 +93,7 @@ def airbrake_error_from_logrecord(record): # find params from kwarg 'extra' # See "The second keyword argument is extra" # - https://docs.python.org/2/library/logging.html#logging.Logger.debug - for key, val in list(vars(record).items()): + for key, val in vars(record).items(): if not hasattr(_FAKE_LOGRECORD, key): # handle attribute/field name collisions: # logrecod attrs should not limit or take diff --git a/airbrake/utils.py b/airbrake/utils.py index 34a7aef..f828b8d 100644 --- a/airbrake/utils.py +++ b/airbrake/utils.py @@ -7,13 +7,7 @@ import traceback import types -import sys -if sys.version_info < (3,): - #Py2 legacy fix - type_of_type = types.TypeType -else: - type_of_type = type class CheckableQueue(Queue): @@ -48,9 +42,9 @@ def is_exc_info_tuple(exc_info): """ try: errtype, value, tback = exc_info - if all([x is None for x in exc_info]): + if all(map(lambda x: x is None, exc_info)): return True - elif all((isinstance(errtype, type_of_type), + elif all((isinstance(errtype, types.TypeType), isinstance(value, Exception), isinstance(tback, types.TracebackType))): return True @@ -98,6 +92,6 @@ def pytb_lastline(excinfo=None): # strip whitespace, Falsy values, # and the string 'None', sometimes returned by the traceback module lines = [line.strip() for line in lines if line] - lines = [line for line in lines if str(line).lower() != 'none'] + lines = filter(lambda line: str(line).lower() != 'none', lines) if lines: return lines[-1] From 4d25174766cbc4cc269a2c7fa92e69354f4f257f Mon Sep 17 00:00:00 2001 From: lusitania Date: Sun, 5 Oct 2014 20:14:08 +0200 Subject: [PATCH 5/5] Py3 exception logging, issue #17 Previous commit didn't verify logging of exceptions. Critical issues: - types.TypeType is now built-in, added legacy switch - filter yields `TypeError: 'filter' object is not subscriptable`, changed to list comprehension (since it was used already the previous line) 2to3 also complained the use of map over list comprehension, changed for similar rationale. --- airbrake/handler.py | 2 +- airbrake/utils.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/airbrake/handler.py b/airbrake/handler.py index 7babe6c..4e3b008 100644 --- a/airbrake/handler.py +++ b/airbrake/handler.py @@ -93,7 +93,7 @@ def airbrake_error_from_logrecord(record): # find params from kwarg 'extra' # See "The second keyword argument is extra" # - https://docs.python.org/2/library/logging.html#logging.Logger.debug - for key, val in vars(record).items(): + for key, val in list(vars(record).items()): if not hasattr(_FAKE_LOGRECORD, key): # handle attribute/field name collisions: # logrecod attrs should not limit or take diff --git a/airbrake/utils.py b/airbrake/utils.py index f828b8d..34a7aef 100644 --- a/airbrake/utils.py +++ b/airbrake/utils.py @@ -7,7 +7,13 @@ import traceback import types +import sys +if sys.version_info < (3,): + #Py2 legacy fix + type_of_type = types.TypeType +else: + type_of_type = type class CheckableQueue(Queue): @@ -42,9 +48,9 @@ def is_exc_info_tuple(exc_info): """ try: errtype, value, tback = exc_info - if all(map(lambda x: x is None, exc_info)): + if all([x is None for x in exc_info]): return True - elif all((isinstance(errtype, types.TypeType), + elif all((isinstance(errtype, type_of_type), isinstance(value, Exception), isinstance(tback, types.TracebackType))): return True @@ -92,6 +98,6 @@ def pytb_lastline(excinfo=None): # strip whitespace, Falsy values, # and the string 'None', sometimes returned by the traceback module lines = [line.strip() for line in lines if line] - lines = filter(lambda line: str(line).lower() != 'none', lines) + lines = [line for line in lines if str(line).lower() != 'none'] if lines: return lines[-1]