#!/usr/bin/env python3 # -*- encoding: utf-8 -*- # --------------------------------------------------------------------- # Exception ABC tests # --------------------------------------------------------------------- # Copyright (c) 2015 Medardo Rodriguez # Created on 2015-10-20 '''Problem using ABCs with exceptions in Python 3 and -BTW- in PyPy. When I run the next code in all my installed versions of Python 3 (and the equivalent code for PyPy and CPython 2.7): - Runs OK in CPython 2.7 equivalent code (using `ABCMeta` as meta-class). - Fails in PyPy and all versions of Python 3. .. for: https://bugs.python.org ''' from __future__ import (division as _py3_division, print_function as _py3_print, absolute_import as _py3_abs_import) from abc import ABC class APIError(ABC, Exception): '''Sample Exception for the API layer.''' pass @APIError.register class DriverError(Exception): '''Sample Exception for the drivers (concrete implementations) layer.''' pass def do_test(): '''Should return ``(1000, 100)``.''' one = 0 try: raise APIError('This has no sense, but any way tested.') one += 10 except DriverError: one += 100 except Exception: one += 1000 two = 0 try: raise DriverError('Test driver again API exception.') two += 10 except APIError: two += 100 except Exception: two += 1000 return one, two if __name__ == '__main__': assert issubclass(DriverError, APIError), 'This runs OK in all versions.' assert do_test() == (1000, 100), ('WTF: Only working in CPython 2.7 ' 'equivalent code.') print("What's happening.")