Skip to content

Commit

Permalink
sac: Fix seeking on native Python 2 file objects.
Browse files Browse the repository at this point in the history
Also, use os.SEEK_END and os.SEEK_SET for better code readability.
  • Loading branch information
claudiodsf authored and QuLogic committed Apr 27, 2015
1 parent 1683405 commit f56a4e8
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions obspy/sac/sacio.py
Expand Up @@ -25,6 +25,7 @@
from future.builtins import * # NOQA
from future.utils import native_str

import os
import time
import warnings

Expand Down Expand Up @@ -467,8 +468,9 @@ def IsSACfile(self, fh, fsize=True, lenchk=False):
Test for a valid SAC file using arrays.
"""
cur_pos = fh.tell()
length = fh.seek(0, 2)
fh.seek(cur_pos, 0)
fh.seek(0, os.SEEK_END)
length = fh.tell()
fh.seek(cur_pos, os.SEEK_SET)
try:
npts = self.GetHvalue('npts')
except:
Expand Down Expand Up @@ -526,7 +528,7 @@ def ReadSacHeader(self, fh):
try:
# if it is not a valid SAC-file try with big endian
# byte order
fh.seek(0, 0)
fh.seek(0, os.SEEK_SET)
self.hf = frombuffer(fh.read(4 * 70), dtype=native_str('>f4'))
self.hi = frombuffer(fh.read(4 * 40), dtype=native_str('>i4'))
# read in the char values
Expand Down Expand Up @@ -566,7 +568,7 @@ def WriteSacHeader(self, fh):
>>> u.GetHvalueFromFile('test2.sac',"kevnm") # doctest: +SKIP
'hullahulla '
"""
fh.seek(0, 0) # set pointer to the file beginning
fh.seek(0, os.SEEK_SET)
try:
# write the header
fh.write(self.hf.data)
Expand Down Expand Up @@ -616,7 +618,7 @@ def ReadSacFile(self, fh, fsize=True):
try:
# if it is not a valid SAC-file try with big endian
# byte order
fh.seek(0, 0)
fh.seek(0, os.SEEK_SET)
self.hf = frombuffer(fh.read(4 * 70), dtype=native_str('>f4'))
self.hi = frombuffer(fh.read(4 * 40), dtype=native_str('>i4'))
# read in the char values
Expand Down

0 comments on commit f56a4e8

Please sign in to comment.