Merge branch 'master' of github.com:micropython/micropython

This commit is contained in:
Damien George 2014-04-14 19:33:23 +01:00
commit ad8abd1a95
9 changed files with 42 additions and 11 deletions

View File

@ -37,6 +37,8 @@ MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_contains_obj); MP_DECLARE_CONST_FUN_OBJ(mp_op_contains_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_getitem_obj); MP_DECLARE_CONST_FUN_OBJ(mp_op_getitem_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_setitem_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_delitem_obj);
extern const mp_obj_module_t mp_module___main__; extern const mp_obj_module_t mp_module___main__;
extern const mp_obj_module_t mp_module_array; extern const mp_obj_module_t mp_module_array;

View File

@ -59,7 +59,7 @@ $(BUILD)/%.pp: %.c
# object directories (but only for existance), and the object directories # object directories (but only for existance), and the object directories
# will be created if they don't exist. # will be created if they don't exist.
OBJ_DIRS = $(sort $(dir $(OBJ))) OBJ_DIRS = $(sort $(dir $(OBJ)))
$(OBJ): | $(OBJ_DIRS) $(OBJ): $(PY_BUILD)/qstrdefs.generated.h | $(OBJ_DIRS)
$(OBJ_DIRS): $(OBJ_DIRS):
$(MKDIR) -p $@ $(MKDIR) -p $@

View File

@ -6,6 +6,7 @@
#include "runtime.h" #include "runtime.h"
#include "objlist.h" #include "objlist.h"
#include "objtuple.h" #include "objtuple.h"
#include "objstr.h"
#if MICROPY_ENABLE_MOD_SYS #if MICROPY_ENABLE_MOD_SYS
@ -20,13 +21,15 @@ mp_obj_list_t mp_sys_path_obj;
mp_obj_list_t mp_sys_argv_obj; mp_obj_list_t mp_sys_argv_obj;
#define I(n) MP_OBJ_NEW_SMALL_INT(n) #define I(n) MP_OBJ_NEW_SMALL_INT(n)
// TODO: CPython is now at 5-element array, but save 2 els so far... // TODO: CPython is now at 5-element array, but save 2 els so far...
STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(3), I(5)}}; STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
#undef I #undef I
STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = { STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) }, { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj },
#if MP_ENDIANNESS_LITTLE #if MP_ENDIANNESS_LITTLE
{ MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_little) }, { MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_little) },

View File

@ -497,6 +497,8 @@ STATIC const mp_map_elem_t dict_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___setitem__), (mp_obj_t)&mp_op_setitem_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___delitem__), (mp_obj_t)&mp_op_delitem_obj },
}; };
STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);

View File

@ -10,13 +10,7 @@
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
#include "pfenv.h" #include "pfenv.h"
#include "objstr.h"
typedef struct _mp_obj_str_t {
mp_obj_base_t base;
machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
machine_uint_t len : 16; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
const byte *data;
} mp_obj_str_t;
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args); STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args);
const mp_obj_t mp_const_empty_bytes; const mp_obj_t mp_const_empty_bytes;
@ -1463,7 +1457,8 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
} else { } else {
GET_STR_HASH(s1, h1); GET_STR_HASH(s1, h1);
GET_STR_HASH(s2, h2); GET_STR_HASH(s2, h2);
if (h1 != h2) { // If any of hashes is 0, it means it's not valid
if (h1 != 0 && h2 != 0 && h1 != h2) {
return false; return false;
} }
GET_STR_DATA_LEN(s1, d1, l1); GET_STR_DATA_LEN(s1, d1, l1);

10
py/objstr.h Normal file
View File

@ -0,0 +1,10 @@
typedef struct _mp_obj_str_t {
mp_obj_base_t base;
// XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
machine_uint_t hash : 16;
// len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
machine_uint_t len : 16;
const byte *data;
} mp_obj_str_t;
#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str};

View File

@ -13,6 +13,18 @@ STATIC mp_obj_t op_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
} }
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem); MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem);
STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) {
mp_store_subscr(self_in, key_in, value_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem);
STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) {
mp_store_subscr(self_in, key_in, MP_OBJ_NULL);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem);
STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_type_t *type = mp_obj_get_type(lhs_in); mp_obj_type_t *type = mp_obj_get_type(lhs_in);
return type->binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in); return type->binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in);

View File

@ -35,7 +35,12 @@ machine_uint_t qstr_compute_hash(const byte *data, uint len) {
for (const byte *top = data + len; data < top; data++) { for (const byte *top = data + len; data < top; data++) {
hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
} }
return hash & 0xffff; hash &= 0xffff;
// Make sure that valid hash is never zero, zero means "hash not computed"
if (hash == 0) {
hash++;
}
return hash;
} }
typedef struct _qstr_pool_t { typedef struct _qstr_pool_t {

View File

@ -24,6 +24,7 @@ Q(__len__)
Q(__iter__) Q(__iter__)
Q(__getitem__) Q(__getitem__)
Q(__setitem__) Q(__setitem__)
Q(__delitem__)
Q(__add__) Q(__add__)
Q(__sub__) Q(__sub__)
Q(__repr__) Q(__repr__)
@ -279,6 +280,7 @@ Q(little)
Q(stdin) Q(stdin)
Q(stdout) Q(stdout)
Q(stderr) Q(stderr)
Q(version)
Q(version_info) Q(version_info)
#endif #endif