Without version checking or `six`, how can I use `except MyError, e:` vs `except MyError as e` to work with Python 2&3?
Asked Answered
O

1

-5

I'm looking for a way to do this without checking for the Python version used.

Please refer to How to write exception reraising code that's compatible with both Python 2 and Python 3? for details about this, since this question extends that one.

Basically, I can generalize this as, "What if the method-based exception raises a language-based exception?"

According to Python try...except comma vs 'as' in except, the following show the right syntax for Python 3 and Python 2:

Python 3:

except MyError as e

Python 2, for versions 2.6+:

except MyError as e
    #OR#
except MyError, e

Python 2.5-:

except MyError, e

A little background:

I have a sticky situation in which a script will need to be run on many an ancient Linux machine, in which a variety of different Python versions, including Python 2.5, will be used.

Unfortunately, I have to distribute this as a single, size limited file, which puts some constraints on how much importing I can do.

Also, I'm interested in the case in which one of these may misreport its version, or in code that can be used without necessarily checking for a version. This could be worked around, though, of course.

Oma answered 31/8, 2015 at 20:18 Comment(14)
// , Please comment before downvoting. Also, I have, indeed, had a look at docs.python.org/3/howto/pyporting.html#use-same-source.Oma
Do you really need to support Python 2.5?Biddle
// , Don't get me started on how annoying this use case is, man. Yes. Yes I do.Oma
Well you just write it the new way if you don't need to support Python 2.5 -- wait, no. What about checking what version of Python is runn-- no, you don't want to do that. Well you could use a library like six -- no, not that either. I mean, you know a bunch of good solutions but you "don't want" to do any of them. What do you want us to say?Banket
Why are you opposed to checking the version? Also, why do you start everything with "// , "?Biddle
// , "Want" has nothing to do with it. This solution needs to use the same source, support legacy systems, AND not import libs. Lightest possible touch on the system. As to solutions, I may be able to find my own, soon, but I thought I would check with the community. Maybe my answer will be useful to someone in the future.Oma
Then check the source code of six and see what they do. Nothing about checking which version of Python is running (which usually involves a sample line and catching an exception) implies using two different sets of source code. Also, sys.version_info is good down through 2.0. Use that.Banket
// , Why do you do this? Also, you might find you get less pushback on your requirements if you explain why none of the obvious things work for you.Cotta
// , Hm. Good point, @Two-BitAlchemist. I'm sure that works in most cases, normally. Is the problem that the question is not specific enough, or that it shows a lack of research effort? jonrsharpe: Personal convention.Oma
@NathanBasanese: could you please not do that, the // thing? There is no need to and is hugely distracting.Tymothy
// , @jonrsharpe, As to pushback, the question's done. Going into the details of one weird project's individually horrible virtualenv hacks is pointless if it's been insta-buried. Martijn Pieters, I would respect an objective reason, of course, but it seems like a matter of personal preference. en.wikipedia.org/wiki/De_gustibus_non_est_disputandumOma
~<#%>~} ; You're on -2, hardly "insta-buried", it would still be helpful to edit the question to provide more useful information.Cotta
@NathanBasanese: because it is distracting from the conversation. Everyone you encounter here on Stack is going to ask you about it. Again and again, because it stands out like a sore thumb. Don't you want people to focus on your question instead?Tymothy
// , Perhaps you could refer me to the appropriate section of the StackOverflow style guide.Oma
T
4

Your only option is to avoid the exception assignment and pull it out of the result for the sys.exc_info() function instead:

try:
    # ...
except Exception:  # note, no ", e" or "as e"
    import sys
    e = sys.exc_info()[1]

This'll work on Python 1.5 and up.

However, you'll likely to encounter other incompatibilities and difficulties; writing polyglot Python code (code that works on both Python 2.x and 3.x) is only really workable on Python 2.6 and up.

Tymothy answered 31/8, 2015 at 21:1 Comment(1)
// , This is perfect. And the situation I'm in is actually pretty common in "Enterprise" development, I fear.Oma

© 2022 - 2024 — McMap. All rights reserved.