Writing backwards compatible code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven D'Aprano

    #1

    Writing backwards compatible code

    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_inf o >= (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.

  • Robert Kern

    #2
    Re: Writing backwards compatible code

    Steven D'Aprano wrote:[color=blue]
    > 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.[/color]

    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?
    [color=blue]
    > What techniques do others use?[/color]

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

    --
    Robert Kern
    robert.kern@gma il.com

    "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

    Comment

    • Paul Rubin

      #3
      Re: Writing backwards compatible code

      Steven D'Aprano <steve@REMOVETH IScyber.com.au> writes:[color=blue]
      > What techniques do others use?[/color]

      I'd just write the generator with a yield statement. The generator
      expression does the same thing more concisely, I think.

      Comment

      • Felipe Almeida Lessa

        #4
        Re: Writing backwards compatible code

        Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:[color=blue]
        > Steven D'Aprano wrote:[color=green]
        > > 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.[/color]
        >
        > 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?[/color]

        Right. You can always use classes.

        --
        Felipe.

        Comment

        • Robert Kern

          #5
          Re: Writing backwards compatible code

          Felipe Almeida Lessa wrote:[color=blue]
          > Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:
          >[color=green]
          >>Steven D'Aprano wrote:
          >>[color=darkred]
          >>>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.[/color]
          >>
          >>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?[/color]
          >
          > Right. You can always use classes.[/color]

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

          --
          Robert Kern
          robert.kern@gma il.com

          "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

          Comment

          • Felipe Almeida Lessa

            #6
            Re: Writing backwards compatible code

            Em Sex, 2006-04-14 às 13:37 -0500, Robert Kern escreveu:[color=blue]
            > Felipe Almeida Lessa wrote:[color=green]
            > > Em Sex, 2006-04-14 às 13:28 -0500, Robert Kern escreveu:
            > >[color=darkred]
            > >>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?[/color]
            > >
            > > Right. You can always use classes.[/color]
            >
            > Well, I guess you could, but using actual generators would be much cleaner.[/color]

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

            --
            Felipe.

            Comment

            • Jack Diederich

              #7
              Re: Writing backwards compatible code

              On Sat, Apr 15, 2006 at 04:04:44AM +1000, Steven D'Aprano wrote:[color=blue]
              > 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?
              >[/color]

              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

              Comment

              • James Stroud

                #8
                Re: Writing backwards compatible code

                Steven D'Aprano wrote:[color=blue]
                > 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_inf o >= (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?
                >
                >[/color]

                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


                Comment

                • Dave Brueck

                  #9
                  Re: Writing backwards compatible code

                  Steven D'Aprano wrote:[color=blue]
                  > 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.[/color]
                  [snip][color=blue]
                  > What techniques do others use?[/color]

                  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

                  Comment

                  • Bob Greschke

                    #10
                    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


                    Comment

                    • Scott David Daniels

                      #11
                      Re: Writing backwards compatible code - when?

                      Bob Greschke wrote:[color=blue]
                      > 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.[/color]

                      Here's a reference that stops at 2.3:

                      Python 2.3 language Quick Reference, freely available in HTML (4 styles) and PDF versions.


                      --Scott David Daniels
                      scott.daniels@a cm.org

                      Comment

                      • Dan Sommers

                        #12
                        Re: Writing backwards compatible code - when?

                        On Tue, 18 Apr 2006 13:40:11 -0600,
                        "Bob Greschke" <bob@greschke.c om> wrote:
                        [color=blue]
                        > 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.[/color]

                        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.tombstoneze ro.net/dan/>
                        "I wish people would die in alphabetical order." -- My wife, the genealogist

                        Comment

                        • John Machin

                          #13
                          Re: Writing backwards compatible code - when?

                          On 19/04/2006 5:40 AM, Bob Greschke wrote:[color=blue]
                          > 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.
                          >[/color]

                          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.

                          Comment

                          • Kent Johnson

                            #14
                            Re: Writing backwards compatible code - when?

                            > Bob Greschke wrote:[color=blue][color=green]
                            >> 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.[/color][/color]

                            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

                            Comment

                            • Laurent Pointal

                              #15
                              Re: Writing backwards compatible code - when?

                              Scott David Daniels a écrit :[color=blue]
                              > Bob Greschke wrote:[color=green]
                              >> 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.[/color]
                              >
                              > Here's a reference that stops at 2.3:
                              >
                              > http://rgruet.free.fr/PQR2.3.html[/color]

                              Remove the 2.3, and you get the 2.4 :-)




                              A+

                              Laurent.

                              Comment

                              Working...