Valid syntax in both Python 2.x and 3.x for raising exception?

speendo

How can I port this code to Python 3 so that it would run in both, Python 2 and Python3?

raise BarException, BarException(e), sys.exc_info()[2]

(copied from http://blog.ionelmc.ro/2014/08/03/the-most-underrated-feature-in-python-3/)

Bonus question
Does it make sense to do something like

IS_PYTHON2 = sys.version_info < (3, 0)

if IS_PYTHON2:
    raise BarException, BarException(e), sys.exc_info()[2]
    # replace with the code that would run in Python 2 and Python 3 respectively
else:
    raise BarException("Bar is closed on Christmas")
Martijn Pieters

You'll have to resort to using exec() because you cannot use the 3-argument syntax in Python 3; it'll raise a syntax error.

As always the six library already has you covered, ported to not depend on other six definitions their version looks like this:

import sys

if sys.version_info[0] == 3:
    def reraise(tp, value, tb=None):
        if value is None:
            value = tp()
        if value.__traceback__ is not tb:
            raise value.with_traceback(tb)
        raise value

else:    
    exec("def reraise(tp, value, tb=None):\n    raise tp, value, tb\n")

Now you can use:

reraise(BarException, BarException(e), sys.exc_info()[2])

without further testing for a Python version.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using both Python 2.x and Python 3.x in IPython Notebook

From Dev

the meaning of python3.x syntax

From Dev

Python exception handling and raising

From Dev

how to convert Python 2 unicode() function into correct Python 3.x syntax

From Dev

how to convert Python 2 unicode() function into correct Python 3.x syntax

From Dev

I want to use both python 2.7 and 3.x in jupyter

From Dev

Python: Eval with undefined variables (2*x+x = 3*x)

From Dev

More concise way to do something a maximum of X times, raising an exception if it takes X times to run the command

From Java

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Java

Manually raising (throwing) an exception in Python

From Dev

Python - Parameter checking with Exception Raising

From Dev

Weird syntax error in python 3.x logic

From Dev

Python 3.x multi line comment throws syntax error

From Dev

Weird syntax error in python 3.x logic

From Dev

Script incompatibility for Python 2.x and Python 3.x

From Dev

Geocoding Strategies - Python 2.x to Python 3.x

From Dev

R - Is for(x in x) valid?

From Dev

R - Is for(x in x) valid?

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Regex unicode in python 2.x vs 3.x

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Accessing Python2.x and 3.x in windows

From Dev

The statement d = ('x': 1, 'y': 2, 'z': 3) is showing errors in Python 3.6. What would be the correct syntax for the new version ?

From Dev

int[] table = new int[10],x; is valid syntax?

From Java

Why is `2 + x = 7` valid Haskell?

From Dev

Possible to use both OpenCV 2.x and OpenCV3 in the same project?

From Dev

Possible to use both OpenCV 2.x and OpenCV3 in the same project?

From Dev

raising an exception in else part of a for loop in Python

Related Related

  1. 1

    Using both Python 2.x and Python 3.x in IPython Notebook

  2. 2

    the meaning of python3.x syntax

  3. 3

    Python exception handling and raising

  4. 4

    how to convert Python 2 unicode() function into correct Python 3.x syntax

  5. 5

    how to convert Python 2 unicode() function into correct Python 3.x syntax

  6. 6

    I want to use both python 2.7 and 3.x in jupyter

  7. 7

    Python: Eval with undefined variables (2*x+x = 3*x)

  8. 8

    More concise way to do something a maximum of X times, raising an exception if it takes X times to run the command

  9. 9

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  10. 10

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  11. 11

    Manually raising (throwing) an exception in Python

  12. 12

    Python - Parameter checking with Exception Raising

  13. 13

    Weird syntax error in python 3.x logic

  14. 14

    Python 3.x multi line comment throws syntax error

  15. 15

    Weird syntax error in python 3.x logic

  16. 16

    Script incompatibility for Python 2.x and Python 3.x

  17. 17

    Geocoding Strategies - Python 2.x to Python 3.x

  18. 18

    R - Is for(x in x) valid?

  19. 19

    R - Is for(x in x) valid?

  20. 20

    Deal with buffer in Python 2.x and 3.x

  21. 21

    Regex unicode in python 2.x vs 3.x

  22. 22

    Deal with buffer in Python 2.x and 3.x

  23. 23

    Accessing Python2.x and 3.x in windows

  24. 24

    The statement d = ('x': 1, 'y': 2, 'z': 3) is showing errors in Python 3.6. What would be the correct syntax for the new version ?

  25. 25

    int[] table = new int[10],x; is valid syntax?

  26. 26

    Why is `2 + x = 7` valid Haskell?

  27. 27

    Possible to use both OpenCV 2.x and OpenCV3 in the same project?

  28. 28

    Possible to use both OpenCV 2.x and OpenCV3 in the same project?

  29. 29

    raising an exception in else part of a for loop in Python

HotTag

Archive