Skip to content

Commit

Permalink
Removed hack for py3 version of np.genfromtxt
Browse files Browse the repository at this point in the history
  • Loading branch information
langmm committed Oct 2, 2017
1 parent 0e84ae4 commit a894041
Showing 1 changed file with 1 addition and 31 deletions.
32 changes: 1 addition & 31 deletions cis_interface/backwards.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
r"""This module allows for backward compatibility."""
import sys
import functools
import numpy as np
PY2 = (sys.version_info[0] == 2)
PY34 = ((sys.version_info[0] == 3) and (sys.version_info[1] == 4))
if PY2: # pragma: Python 2
Expand Down Expand Up @@ -223,36 +221,8 @@ def decode_escape(s):
return out


# Python 3 version of np.genfromtxt
# https://github.com/numpy/numpy/issues/3184
genfromtxt_old = np.genfromtxt


@functools.wraps(genfromtxt_old)
def genfromtxt_py3_fixed(f, encoding="utf-8", *args, **kwargs): # pragma: Python 3
if isinstance(f, sio.TextIOBase):
if hasattr(f, "buffer") and hasattr(f.buffer, "raw") and \
isinstance(f.buffer.raw, sio.FileIO):
# Best case: get underlying FileIO stream (binary!) and use that
fb = f.buffer.raw
# Reset cursor on the underlying object to match that on wrapper
fb.seek(f.tell())
result = genfromtxt_old(fb, *args, **kwargs)
# Reset cursor on wrapper to match that of the underlying object
f.seek(fb.tell())
else:
# Not very good but works: Put entire contents into BytesIO object,
# otherwise same ideas as above
old_cursor_pos = f.tell()
fb = sio.BytesIO(bytes(f.read(), encoding=encoding))
result = genfromtxt_old(fb, *args, **kwargs)
f.seek(old_cursor_pos + fb.tell())
else:
result = genfromtxt_old(f, *args, **kwargs)
return result


if sys.version_info >= (3,): # pragma: Python 3
np.genfromtxt = genfromtxt_py3_fixed


__all__ = ['pickle', 'configparser', 'sio',
Expand Down

0 comments on commit a894041

Please sign in to comment.