Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
EAFP when reading schunk and use ujson if avaible
Browse files Browse the repository at this point in the history
  • Loading branch information
bordingj committed May 7, 2017
1 parent 8df64b4 commit ba25bb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
5 changes: 4 additions & 1 deletion bcolz/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import os
import os.path
import json
try:
import ujson as json
except:
import json
from .py2help import dict_iteritems


Expand Down
29 changes: 17 additions & 12 deletions bcolz/carray_ext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -763,18 +763,23 @@ cdef class chunks(object):
cdef read_chunk(self, nchunk):
"""Read a chunk and return it in compressed form."""
schunkfile = self._chunk_file_name(nchunk)
if not os.path.exists(schunkfile):
raise ValueError("chunkfile %s not found" % schunkfile)
with open(schunkfile, 'rb') as schunk:
bloscpack_header = schunk.read(BLOSCPACK_HEADER_LENGTH)
blosc_header_raw = schunk.read(BLOSC_HEADER_LENGTH)
blosc_header = decode_blosc_header(blosc_header_raw)
ctbytes = blosc_header['ctbytes']
nbytes = blosc_header['nbytes']
# seek back BLOSC_HEADER_LENGTH bytes in file relative to current
# position
schunk.seek(-BLOSC_HEADER_LENGTH, 1)
scomp = schunk.read(ctbytes)
try:
with open(schunkfile, 'rb') as schunk:
bloscpack_header = schunk.read(BLOSCPACK_HEADER_LENGTH)
blosc_header_raw = schunk.read(BLOSC_HEADER_LENGTH)
blosc_header = decode_blosc_header(blosc_header_raw)
ctbytes = blosc_header['ctbytes']
nbytes = blosc_header['nbytes']
# seek back BLOSC_HEADER_LENGTH bytes in file relative to current
# position
schunk.seek(-BLOSC_HEADER_LENGTH, 1)
scomp = schunk.read(ctbytes)
except:
if not os.path.exists(schunkfile):
raise ValueError("chunkfile %s not found" % schunkfile)
else:
print("Unexpected error:", sys.exc_info()[0])
raise
return scomp
def __iter__(self):
Expand Down

0 comments on commit ba25bb3

Please sign in to comment.