From 4ae50b3087091e1eccd444a99fe79d8dcdc52a87 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Mon, 25 Aug 2014 23:02:13 +0400 Subject: [PATCH] Updated README --- README.rst | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/README.rst b/README.rst index a51671c..3deed3f 100644 --- a/README.rst +++ b/README.rst @@ -87,37 +87,21 @@ So if we will rewrite given example with PEP3134 import sys import pep3134 - + def example(): + error = -1 try: - pep3134.raise_KeyError("WOW SUCH ERROR") + pep3134.raise_(KeyError("WOW SUCH ERROR")) except KeyError as err: + error = err first = sys.exc_info() - assert err.__traceback__ is first[2] - + assert error.__traceback__ is first[2] + second = sys.exc_info() - assert err.__traceback__ is second[2] # works in Python2 only - return first, second - -See? In Python 3 assert will not work but in Python 2 it will. Anyway, there is the trick. It called -``with_traceback`` method (added in Python 3 also) - -.. code-block:: python - - import sys - import pep3134 + assert error.__traceback__ is not second[2] # works in Python 2 only + + example() - def example(): - try: - pep3134.raise_KeyError("WOW SUCH ERROR") - except KeyError as err: - first = sys.exc_info() - assert err.__traceback__ is first[2] - err = err.with_traceback(err.__traceback__) - - second = sys.exc_info() - assert err.__traceback__ is second[2] # works in in both pythons - return first, second This is the only pitfall. Causes, as I mentioned, work well.