Fix deserialization in PersistentState

This commit is contained in:
simon987 2022-05-07 09:34:50 -04:00
parent 78c04ef6f3
commit 372abb0076
3 changed files with 32 additions and 3 deletions

View File

@ -118,9 +118,13 @@ class Table:
with sqlite3.connect(self._state.dbfile, **self._state.dbargs) as conn:
conn.row_factory = sqlite3.Row
try:
col_types = conn.execute("PRAGMA table_info(%s)" % self._table).fetchall()
cur = conn.execute("SELECT * FROM %s %s" % (self._table, where_clause), params)
for row in cur:
yield dict(row)
yield dict(
(col[0], _deserialize(row[col[0]], col_types[i]["type"]))
for i, col in enumerate(cur.description)
)
except:
return None
@ -128,9 +132,13 @@ class Table:
with sqlite3.connect(self._state.dbfile, **self._state.dbargs) as conn:
conn.row_factory = sqlite3.Row
try:
col_types = conn.execute("PRAGMA table_info(%s)" % self._table).fetchall()
cur = conn.execute("SELECT * FROM %s" % (self._table,))
for row in cur:
yield dict(row)
yield dict(
(col[0], _deserialize(row[col[0]], col_types[i]["type"]))
for i, col in enumerate(cur.description)
)
except:
return None

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup(
name="hexlib",
version="1.79",
version="1.80",
description="Misc utility methods",
author="simon987",
author_email="me@simon987.net",

View File

@ -110,3 +110,24 @@ class TestPersistentState(TestCase):
del s["a"][456]
except Exception as e:
self.fail(e)
def test_deserialize_get_set(self):
s = PersistentState()
s["a"][0] = {"x": b'abc'}
self.assertEqual(s["a"][0]["x"], b'abc')
def test_deserialize_sql(self):
s = PersistentState()
s["a"][0] = {"x": b'abc'}
self.assertEqual(list(s["a"].sql("WHERE 1=1"))[0]["x"], b'abc')
def test_deserialize_iter(self):
s = PersistentState()
s["a"][0] = {"x": b'abc'}
self.assertEqual(list(s["a"])[0]["x"], b'abc')