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

EAFP when reading schunk and use ujson if available #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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