Skip to content

Commit 34ca201

Browse files
committed
moved try catch into read_value
1 parent 301d890 commit 34ca201

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

datajoint/blob.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(self, squeeze=False, is_32_bit = False):
6868
self._squeeze = squeeze
6969
self._blob = None
7070
self._pos = 0
71+
self._pos_prev = 0
7172
self.protocol = None
7273
self.is_32_bit = is_32_bit
7374

@@ -435,7 +436,13 @@ def read_zero_terminated_string(self):
435436
def read_value(self, dtype=None, count=1):
436437
if dtype is None:
437438
dtype = 'uint32' if self.is_32_bit else 'uint64'
438-
data = np.frombuffer(self._blob, dtype=dtype, count=count, offset=self._pos)
439+
try:
440+
data = np.frombuffer(self._blob, dtype=dtype, count=count, offset=self._pos)
441+
except ValueError:
442+
self.is_32_bit = True
443+
self._pos = self._pos_prev
444+
data = np.frombuffer(self._blob, dtype='uint32', count=self.read_value(), offset=self._pos)
445+
self._pos_prev = self._pos
439446
self._pos += data.dtype.itemsize * data.size
440447
return data[0] if count == 1 else data
441448

@@ -468,7 +475,4 @@ def unpack(blob, squeeze=False):
468475
assert isinstance(blob, bytes) and blob.startswith((b'ZL123\0', b'mYm\0', b'dj0\0'))
469476
return blob
470477
if blob is not None:
471-
try:
472-
return Blob(squeeze=squeeze).unpack(blob)
473-
except:
474-
return Blob(squeeze=squeeze, is_32_bit=True).unpack(blob)
478+
return Blob(squeeze=squeeze).unpack(blob)

tests/test_blob.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,19 @@ def test_complex():
134134

135135

136136
def test_insert_longblob():
137-
import numpy as np
138-
# schema.Testmym.insert1({'id': 1, 'data': np.recarray(np.array([[(np.array([[np.nan, 1., 1., 0., 1., 0., np.nan]]), np.array(['llllrrl'], dtype='<U7'), np.array(['ddddddd'], dtype='<U7'), np.array(['Stage 10'], dtype='<U8'))]]), dtype=[('hits', 'O'), ('sides', 'O'), ('tasks', 'O'), ('stage', 'O')])})
139-
dj.conn().query("INSERT INTO djtest_test1.testmym (id, data) VALUES (1, X'6D596D00530200000001000000010000000400000068697473007369646573007461736B73007374616765004D000000410200000001000000070000000600000000000000000000000000F8FF000000000000F03F000000000000F03F0000000000000000000000000000F03F0000000000000000000000000000F8FF230000004102000000010000000700000004000000000000006C006C006C006C00720072006C002300000041020000000100000007000000040000000000000064006400640064006400640064002500000041020000000100000008000000040000000000000053007400610067006500200031003000')").fetchall()
140-
# dj.conn().query("INSERT INTO djtest_test1.testmym (id, data) VALUES (1, X'646A300002060000000000000004000000000000000A01000104000000000000000A01000204000000000000000A01000304000000000000000A01000404000000000000000A01000504000000000000000A010006')").fetchall()
141-
print('\n',dj.conn().query("SELECT hex(data) FROM djtest_test1.testmym").fetchall())
142-
print((schema.Testmym & 'id=1').fetch1())
143-
schema.Testmym.drop()
144-
assert True
137+
query = ("INSERT INTO djtest_test1.testmym (id, data) VALUES (1, "
138+
"X'6D596D00530200000001000000010000000400000068697473007369646573007461736B73007374"
139+
"616765004D000000410200000001000000070000000600000000000000000000000000F8FF00000000"
140+
"0000F03F000000000000F03F0000000000000000000000000000F03F00000000000000000000000000"
141+
"00F8FF230000004102000000010000000700000004000000000000006C006C006C006C00720072006C"
142+
"0023000000410200000001000000070000000400000000000000640064006400640064006400640025"
143+
"00000041020000000100000008000000040000000000000053007400610067006500200031003000')")
144+
dj.conn().query(query).fetchall()
145+
data_32 = {'id': 1, 'data':np.rec.array([[(
146+
np.array([[
147+
np.nan, 1., 1., 0., 1., 0., np.nan]]),
148+
np.array(['llllrrl'], dtype='<U7'),
149+
np.array(['ddddddd'], dtype='<U7'),
150+
np.array(['Stage 10'], dtype='<U8'))]],
151+
dtype=[('hits', 'O'), ('sides', 'O'), ('tasks', 'O'), ('stage', 'O')])}
152+
assert (schema.Testmym & 'id=1').fetch1() == data_32

0 commit comments

Comments
 (0)