Writing backwards compatible code

Discussion in 'Python' started by Steven D'Aprano, Apr 14, 2006.

  1. I came across an interesting (as in the Chinese curse) problem today. I
    had to modify a piece of code using generator expressions written with
    Python 2.4 in mind to run under version 2.3, but I wanted the code to
    continue to use the generator expression if possible.

    My first approach was to use a try...except block to test for generator
    expressions:

    try:
    gen = (something for x in blah)
    except SyntaxError:
    def g():
    for x in blah:
    yield something
    gen = g()


    This failed to work under 2.3, because the SyntaxError occurs at compile
    time, and so the try block never happens.

    I've been burnt by making assumptions before, so I tried a second,
    similar, approach:

    if sys.version_info >= (2, 4):
    gen = (something for x in blah)
    else:
    # you know the rest

    As expected, that failed too.

    The solution which worked was to put the generator expression in a second
    module, then import that:

    try:
    import othermodule
    except SyntaxError:
    # fall back code


    What techniques do others use?


    --
    Steven.
    Steven D'Aprano, Apr 14, 2006
    #1
    1. Advertising

  2. Steven D'Aprano

    Robert Kern Guest

    Steven D'Aprano wrote:
    > I came across an interesting (as in the Chinese curse) problem today. I
    > had to modify a piece of code using generator expressions written with
    > Python 2.4 in mind to run under version 2.3, but I wanted the code to
    > continue to use the generator expression if possible.


    Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
    not necessary at all. If you are going to have the ugly, syntactically bitter
    version in your code anyways, why clutter up your code even more trying to do both?

    > What techniques do others use?


    Personally, I write code that only uses the syntactic features of the Python
    version that I am targetting.

    --
    Robert Kern


    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Apr 14, 2006
    #2
    1. Advertising

  3. Steven D'Aprano

    Dave Brueck Guest

    Steven D'Aprano wrote:
    > I came across an interesting (as in the Chinese curse) problem today. I
    > had to modify a piece of code using generator expressions written with
    > Python 2.4 in mind to run under version 2.3, but I wanted the code to
    > continue to use the generator expression if possible.

    [snip]
    > What techniques do others use?


    About the only reliable one I know of is to not use syntax from the newer
    version, period, if the code needs to run on old versions. Your example was
    pretty straightforward, but unless the use case is always that simple it's too
    easy to have two separate versions of the code to maintain, and there's no way
    to enforce that changes/fixes to one will be duplicated in the other.

    So... if you have to take the time to make one that works with the older
    version, you might as well just use it exclusively.

    -Dave
    Dave Brueck, Apr 14, 2006
    #3
  4. Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:
    > Steven D'Aprano wrote:
    > > I came across an interesting (as in the Chinese curse) problem today. I
    > > had to modify a piece of code using generator expressions written with
    > > Python 2.4 in mind to run under version 2.3, but I wanted the code to
    > > continue to use the generator expression if possible.

    >
    > Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
    > not necessary at all. If you are going to have the ugly, syntactically bitter
    > version in your code anyways, why clutter up your code even more trying to do both?


    Right. You can always use classes.

    --
    Felipe.
    Felipe Almeida Lessa, Apr 14, 2006
    #4
  5. Steven D'Aprano

    Paul Rubin Guest

    Steven D'Aprano <> writes:
    > What techniques do others use?


    I'd just write the generator with a yield statement. The generator
    expression does the same thing more concisely, I think.
    Paul Rubin, Apr 14, 2006
    #5
  6. Steven D'Aprano

    Robert Kern Guest

    Felipe Almeida Lessa wrote:
    > Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:
    >
    >>Steven D'Aprano wrote:
    >>
    >>>I came across an interesting (as in the Chinese curse) problem today. I
    >>>had to modify a piece of code using generator expressions written with
    >>>Python 2.4 in mind to run under version 2.3, but I wanted the code to
    >>>continue to use the generator expression if possible.

    >>
    >>Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
    >>not necessary at all. If you are going to have the ugly, syntactically bitter
    >>version in your code anyways, why clutter up your code even more trying to do both?

    >
    > Right. You can always use classes.


    Well, I guess you could, but using actual generators would be much cleaner.

    --
    Robert Kern


    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Apr 14, 2006
    #6
  7. Em Sex, 2006-04-14 às 13:37 -0500, Robert Kern escreveu:
    > Felipe Almeida Lessa wrote:
    > > Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:
    > >
    > >>Steven D'Aprano wrote:
    > >>
    > >>>I came across an interesting (as in the Chinese curse) problem today. I
    > >>>had to modify a piece of code using generator expressions written with
    > >>>Python 2.4 in mind to run under version 2.3, but I wanted the code to
    > >>>continue to use the generator expression if possible.
    > >>
    > >>Why? AFAICT, it really is just syntactic sugar. Very nice syntactic sugar, but
    > >>not necessary at all. If you are going to have the ugly, syntactically bitter
    > >>version in your code anyways, why clutter up your code even more trying to do both?

    > >
    > > Right. You can always use classes.

    >
    > Well, I guess you could, but using actual generators would be much cleaner.


    That's the whole point around syntactic sugar: being cleaner, more
    concise, (sometimes) less error-prone and (less times) faster.

    --
    Felipe.
    Felipe Almeida Lessa, Apr 14, 2006
    #7
  8. On Sat, Apr 15, 2006 at 04:04:44AM +1000, Steven D'Aprano wrote:
    > I came across an interesting (as in the Chinese curse) problem today. I
    > had to modify a piece of code using generator expressions written with
    > Python 2.4 in mind to run under version 2.3, but I wanted the code to
    > continue to use the generator expression if possible.
    >
    > My first approach was to use a try...except block to test for generator
    > expressions:
    >
    > try:
    > gen = (something for x in blah)
    > except SyntaxError:
    > def g():
    > for x in blah:
    > yield something
    > gen = g()
    >
    > The solution which worked was to put the generator expression in a second
    > module, then import that:
    >
    > try:
    > import othermodule
    > except SyntaxError:
    > # fall back code
    >
    > What techniques do others use?
    >


    If you want to support 2.3 just use the yield version. Having
    two versions of the same thing is asking for trouble. Twice
    the tests, twice the room for breakage, twice the places to
    update if you want to change it.

    -Jack
    Jack Diederich, Apr 14, 2006
    #8
  9. Steven D'Aprano

    James Stroud Guest

    Steven D'Aprano wrote:
    > I came across an interesting (as in the Chinese curse) problem today. I
    > had to modify a piece of code using generator expressions written with
    > Python 2.4 in mind to run under version 2.3, but I wanted the code to
    > continue to use the generator expression if possible.
    >
    > My first approach was to use a try...except block to test for generator
    > expressions:
    >
    > try:
    > gen = (something for x in blah)
    > except SyntaxError:
    > def g():
    > for x in blah:
    > yield something
    > gen = g()
    >
    >
    > This failed to work under 2.3, because the SyntaxError occurs at compile
    > time, and so the try block never happens.
    >
    > I've been burnt by making assumptions before, so I tried a second,
    > similar, approach:
    >
    > if sys.version_info >= (2, 4):
    > gen = (something for x in blah)
    > else:
    > # you know the rest
    >
    > As expected, that failed too.
    >
    > The solution which worked was to put the generator expression in a second
    > module, then import that:
    >
    > try:
    > import othermodule
    > except SyntaxError:
    > # fall back code
    >
    >
    > What techniques do others use?
    >
    >


    Here is one every one will have fun lambasting:

    try:
    exec('gen = (something for x in blah)')
    except:
    def g():
    # etc.

    James

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com/
    James Stroud, Apr 15, 2006
    #9
  10. Steven D'Aprano

    Bob Greschke Guest

    Re: Writing backwards compatible code - when?

    Is there a list of all of the Python commands and modules that tell when
    (what version) they were added to Python? I was hoping the new Essential
    Reference would have it, but it doesn't.

    Thanks!

    Bob
    Bob Greschke, Apr 18, 2006
    #10
  11. Re: Writing backwards compatible code - when?

    Bob Greschke wrote:
    > Is there a list of all of the Python commands and modules that tell when
    > (what version) they were added to Python? I was hoping the new Essential
    > Reference would have it, but it doesn't.


    Here's a reference that stops at 2.3:

    http://rgruet.free.fr/PQR2.3.html

    --Scott David Daniels
    Scott David Daniels, Apr 18, 2006
    #11
  12. Steven D'Aprano

    Dan Sommers Guest

    Re: Writing backwards compatible code - when?

    On Tue, 18 Apr 2006 13:40:11 -0600,
    "Bob Greschke" <> wrote:

    > Is there a list of all of the Python commands and modules that tell
    > when (what version) they were added to Python? I was hoping the new
    > Essential Reference would have it, but it doesn't.


    I thought it was more cohesive, but googling for

    python "what's new"

    turns up a collection of such documents in various places.

    Regards,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
    "I wish people would die in alphabetical order." -- My wife, the genealogist
    Dan Sommers, Apr 19, 2006
    #12
  13. Steven D'Aprano

    John Machin Guest

    Re: Writing backwards compatible code - when?

    On 19/04/2006 5:40 AM, Bob Greschke wrote:
    > Is there a list of all of the Python commands and modules that tell when
    > (what version) they were added to Python? I was hoping the new Essential
    > Reference would have it, but it doesn't.
    >


    Possibly because it was deemed to be not essential :)

    For (a) writing from scratch a module that's targeted at a particular
    version of Python or (b) back-porting an existing module to a particular
    version, you need the docs for that version. As a reminder of features
    not to use in case (a) or that need changes in case (b), it's very handy
    to have a list of syntax and module changes made after that version ...
    for this I can't imagine anything better than amk's excellent "What's
    New in Python x.y" series.
    John Machin, Apr 19, 2006
    #13
  14. Steven D'Aprano

    Kent Johnson Guest

    Re: Writing backwards compatible code - when?

    > Bob Greschke wrote:
    >> Is there a list of all of the Python commands and modules that tell when
    >> (what version) they were added to Python? I was hoping the new Essential
    >> Reference would have it, but it doesn't.


    The Library Reference page for a module or built-in often documents the
    version when any change was made. You can also read through the What's
    New documents for each release to see what changed in that release.

    Kent
    Kent Johnson, Apr 19, 2006
    #14
  15. Re: Writing backwards compatible code - when?

    Scott David Daniels a écrit :
    > Bob Greschke wrote:
    >> Is there a list of all of the Python commands and modules that tell
    >> when (what version) they were added to Python? I was hoping the new
    >> Essential Reference would have it, but it doesn't.

    >
    > Here's a reference that stops at 2.3:
    >
    > http://rgruet.free.fr/PQR2.3.html


    Remove the 2.3, and you get the 2.4 :)

    http://rgruet.free.fr/


    A+

    Laurent.
    Laurent Pointal, Apr 24, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Priess
    Replies:
    0
    Views:
    407
    Bill Priess
    Aug 5, 2003
  2. Replies:
    11
    Views:
    628
  3. jacob navia

    Writing C90 compatible code

    jacob navia, Jun 13, 2010, in forum: C Programming
    Replies:
    12
    Views:
    943
    Keith Thompson
    Jun 20, 2010
  4. Replies:
    3
    Views:
    210
    Mike Brind
    Oct 16, 2007
  5. pantagruel
    Replies:
    0
    Views:
    224
    pantagruel
    Feb 17, 2006
Loading...

Share This Page