Skip to content

Commit

Permalink
workaround for gzip.open() failure in Windows Python 2.7
Browse files Browse the repository at this point in the history
- fix #99
- implement workaround from https://bugs.python.org/issue30012 : for Python 3, use mode="rt",
  for Python 2, just use default ("r")
  • Loading branch information
orbeckst committed Oct 10, 2021
1 parent c8794b2 commit ea46d69
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion gridData/OpenDX.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@

import warnings

# Python 2/3 compatibility (see issue #99)
# and https://bugs.python.org/issue30012
import sys
if sys.version_info >= (3, ):
def _gzip_open(filename, mode="rt"):
return gzip.open(filename, mode)
else:
def _gzip_open(filename, mode="rt"):
return gzip.open(filename)
del sys

class DXclass(object):
"""'class' object as defined by OpenDX"""
def __init__(self,classid):
Expand Down Expand Up @@ -707,7 +718,7 @@ def parse(self, DXfield):
self.tokens = [] # token buffer

if self.filename.endswith('.gz'):
with gzip.open(self.filename, 'rt') as self.dxfile:
with _gzip_open(self.filename, 'rt') as self.dxfile:
self.use_parser('general')
else:
with open(self.filename, 'r') as self.dxfile:
Expand Down

0 comments on commit ea46d69

Please sign in to comment.