Skip to content
This repository
Browse code

Nest one-lined with statements

Evidently, the comma syntax for two with statements is a Python 2.7
  • Loading branch information...
commit a25c849f91a3ac2ae800aab1acc58fb227388bb1 1 parent 1fb098f
David Cain authored January 09, 2013 etal committed January 18, 2013

Showing 1 changed file with 3 additions and 2 deletions. Show diff stats Hide diff stats

  1. 5  Bio/PDB/
5  Bio/PDB/
@@ -213,8 +213,9 @@ def retrieve_pdb_file(self, pdb_code, obsolete=False, pdir=None):
213 213
         urllib.urlretrieve(url, filename)
214 214
215 215
         # Uncompress the archive, delete when done
-        with, 'rb') as gz, open(final_file, 'wb') as out:
-            out.writelines(gz)
+        with, 'rb') as gz:
+            with open(final_file, 'wb') as out:
+                out.writelines(gz)
218 219
219 220
220 221
         return final_file

4 notes on commit a25c849

Bryan Lunt

Line 216, while good style, breaks support for python 2.6, which I believe is not being phased out quite yet.

This demo program shows the issue when run under python2.6 vs python2.7, of course the problem lies in the python2.6 libraries...

import gzip
with,"rb") as thefile:
for line in thefile:
print line

Peter Cock

Yes, well spotted. I wonder how easy it would be to have a unit test for this code...

Update: Hang on, this commit is already on the trunk - are you just annotating the old fix Bryan?

Note at this point we still support Python 2.5 which is why there is the line from __future__ import with_statement at the start of the module

Bryan Lunt

No, I'm not annotating the fix, I mean that the gzip library in python 2.6 doesn't allow you to do "with ... as ...":
I was getting an exception using biopython-1.61 under python2.6, and I traced it to this line, even adding "from future import with_statement" to my demo script above gives the same error under python2.6 (well, at least on Apples):

Traceback (most recent call last):
File "", line 6, in
with"foo.gz","rb") as thefile:
AttributeError: GzipFile instance has no attribute 'exit'

The problem isn't the with statement, it7s that GzipFile doesn't support the with statement.

Peter Cock

Ah - I see what you mean now, and yes, you are right:

That should be fixed under Python 2.5 and 2.6 on the trunk as of this commit 7ca95d3 - if you could test the latest code from git that would be great, thanks!

Please sign in to comment.
Something went wrong with that request. Please try again.