-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.

Community
  • 1
  • 1
Nathan Basanese
  • 8,475
  • 10
  • 37
  • 66
  • // , Please comment before downvoting. Also, I have, indeed, had a look at https://docs.python.org/3/howto/pyporting.html#use-same-source. – Nathan Basanese Aug 31 '15 at 20:22
  • 1
    Do you really need to support Python 2.5? – Morgan Thrapp Aug 31 '15 at 20:29
  • // , Don't get me started on how annoying this use case is, man. Yes. Yes I do. – Nathan Basanese Aug 31 '15 at 20:30
  • 1
    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? – Two-Bit Alchemist Aug 31 '15 at 20:30
  • 1
    Why are you opposed to checking the version? Also, why do you start everything with "// , "? – Morgan Thrapp Aug 31 '15 at 20:30
  • // , "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. – Nathan Basanese Aug 31 '15 at 20:32
  • 2
    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`](https://docs.python.org/2/library/sys.html#sys.version_info) is good down through 2.0. Use that. – Two-Bit Alchemist Aug 31 '15 at 20:34
  • 5
    // , 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. – jonrsharpe Aug 31 '15 at 20:36
  • // , 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. – Nathan Basanese Aug 31 '15 at 20:45
  • 2
    @NathanBasanese: could you please not do that, the `//` thing? There is no need to and is *hugely* distracting. – Martijn Pieters Aug 31 '15 at 20:51
  • // , @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. https://en.wikipedia.org/wiki/De_gustibus_non_est_disputandum – Nathan Basanese Aug 31 '15 at 20:56
  • ~<#%>~} ; You're on -2, hardly *"insta-buried"*, it would still be helpful to edit the question to provide more useful information. – jonrsharpe Aug 31 '15 at 21:01
  • 5
    @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? – Martijn Pieters Aug 31 '15 at 21:04
  • // , Perhaps you could refer me to the appropriate section of the StackOverflow style guide. – Nathan Basanese Dec 01 '15 at 22:51

1 Answers1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343