mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-15 17:12:04 +00:00
config: always use bytes() for default value
This commit is contained in:
parent
13533d9156
commit
be7ee61ddd
@ -32,7 +32,7 @@ STATIC mp_obj_t mod_TrezorConfig_Config_make_new(const mp_obj_type_t *type, size
|
|||||||
|
|
||||||
/// def trezor.config.get(app: int, key: int) -> bytes:
|
/// def trezor.config.get(app: int, key: int) -> bytes:
|
||||||
/// '''
|
/// '''
|
||||||
/// Gets a value of given key for given app (or None if not set).
|
/// Gets a value of given key for given app (or empty bytes if not set).
|
||||||
/// '''
|
/// '''
|
||||||
STATIC mp_obj_t mod_TrezorConfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_t key) {
|
STATIC mp_obj_t mod_TrezorConfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_t key) {
|
||||||
uint8_t a = mp_obj_get_int(app);
|
uint8_t a = mp_obj_get_int(app);
|
||||||
@ -41,8 +41,8 @@ STATIC mp_obj_t mod_TrezorConfig_Config_get(mp_obj_t self, mp_obj_t app, mp_obj_
|
|||||||
const void *val;
|
const void *val;
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
bool r = norcow_get(appkey, &val, &len);
|
bool r = norcow_get(appkey, &val, &len);
|
||||||
if (!r) {
|
if (!r || len == 0) {
|
||||||
return mp_const_none;
|
return mp_const_empty_bytes;
|
||||||
}
|
}
|
||||||
vstr_t vstr;
|
vstr_t vstr;
|
||||||
vstr_init_len(&vstr, len);
|
vstr_init_len(&vstr, len);
|
||||||
|
@ -6,12 +6,14 @@ else:
|
|||||||
from TrezorConfig import Config
|
from TrezorConfig import Config
|
||||||
_config = Config()
|
_config = Config()
|
||||||
|
|
||||||
def get(app, key, default=None):
|
|
||||||
v = _config.get(app, key)
|
|
||||||
return v if v else default
|
|
||||||
|
|
||||||
def set(app, key, value):
|
def get(app: int, key: int) -> bytes:
|
||||||
|
return _config.get(app, key)
|
||||||
|
|
||||||
|
|
||||||
|
def set(app: int, key: int, value: bytes):
|
||||||
return _config.set(app, key, value)
|
return _config.set(app, key, value)
|
||||||
|
|
||||||
|
|
||||||
def wipe():
|
def wipe():
|
||||||
return _config.wipe()
|
return _config.wipe()
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
import ustruct
|
import ustruct
|
||||||
|
|
||||||
def Config():
|
|
||||||
|
class Config:
|
||||||
|
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
self._data = {}
|
self._data = {}
|
||||||
@ -25,8 +26,8 @@ def Config():
|
|||||||
f.write(ustruct.pack('<HH', k, len(v)))
|
f.write(ustruct.pack('<HH', k, len(v)))
|
||||||
f.write(v)
|
f.write(v)
|
||||||
|
|
||||||
def get(self, app_id, key, default=None):
|
def get(self, app_id, key):
|
||||||
return self._data.get((app_id << 8) | key, default)
|
return self._data.get((app_id << 8) | key, bytes())
|
||||||
|
|
||||||
def set(self, app_id, key, value):
|
def set(self, app_id, key, value):
|
||||||
self._data[(app_id << 8) | key] = value
|
self._data[(app_id << 8) | key] = value
|
||||||
|
@ -33,9 +33,8 @@ class TestConfig(unittest.TestCase):
|
|||||||
config.wipe()
|
config.wipe()
|
||||||
for _ in range(64):
|
for _ in range(64):
|
||||||
appid, key = random.uniform(256), random.uniform(256)
|
appid, key = random.uniform(256), random.uniform(256)
|
||||||
value = random.bytes(128)
|
value = config.get(appid, key)
|
||||||
value2 = config.get(appid, key, value)
|
self.assertEqual(value, bytes())
|
||||||
self.assertEqual(value, value2)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user