py: Add builtin functions bin and oct, and some tests for them.
This commit is contained in:
parent
d5323f07ff
commit
897fe0c0d0
14
py/builtin.c
14
py/builtin.c
@ -6,6 +6,7 @@
|
|||||||
#include "mpconfig.h"
|
#include "mpconfig.h"
|
||||||
#include "qstr.h"
|
#include "qstr.h"
|
||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
|
#include "objstr.h"
|
||||||
#include "runtime0.h"
|
#include "runtime0.h"
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
#include "builtin.h"
|
#include "builtin.h"
|
||||||
@ -125,6 +126,13 @@ STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
|
|||||||
|
|
||||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
|
||||||
|
|
||||||
|
STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
|
||||||
|
mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
|
||||||
|
return mp_obj_str_format(sizeof(args) / sizeof(args[0]), args);
|
||||||
|
}
|
||||||
|
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
|
||||||
|
|
||||||
STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
|
STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
|
||||||
if (mp_obj_is_callable(o_in)) {
|
if (mp_obj_is_callable(o_in)) {
|
||||||
return mp_const_true;
|
return mp_const_true;
|
||||||
@ -299,6 +307,12 @@ STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
|
|||||||
|
|
||||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
|
||||||
|
|
||||||
|
STATIC mp_obj_t mp_builtin_oct(mp_obj_t o_in) {
|
||||||
|
return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent__hash_o), o_in);
|
||||||
|
}
|
||||||
|
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
|
||||||
|
|
||||||
STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
|
STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
|
||||||
uint len;
|
uint len;
|
||||||
const char *str = mp_obj_str_get_data(o_in, &len);
|
const char *str = mp_obj_str_get_data(o_in, &len);
|
||||||
|
@ -6,6 +6,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj);
|
|||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_bin_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_dir_obj);
|
||||||
@ -26,6 +27,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_locals_obj);
|
|||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj);
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_oct_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
|
||||||
|
@ -51,6 +51,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
|
|||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_abs), (mp_obj_t)&mp_builtin_abs_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&mp_builtin_all_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_all), (mp_obj_t)&mp_builtin_all_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&mp_builtin_any_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&mp_builtin_any_obj },
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_bin), (mp_obj_t)&mp_builtin_bin_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_callable), (mp_obj_t)&mp_builtin_callable_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_callable), (mp_obj_t)&mp_builtin_callable_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_chr), (mp_obj_t)&mp_builtin_chr_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_chr), (mp_obj_t)&mp_builtin_chr_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_dir), (mp_obj_t)&mp_builtin_dir_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_dir), (mp_obj_t)&mp_builtin_dir_obj },
|
||||||
@ -70,6 +71,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
|
|||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&mp_builtin_max_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&mp_builtin_max_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&mp_builtin_min_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&mp_builtin_min_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_next), (mp_obj_t)&mp_builtin_next_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_next), (mp_obj_t)&mp_builtin_next_obj },
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_oct), (mp_obj_t)&mp_builtin_oct_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ord), (mp_obj_t)&mp_builtin_ord_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ord), (mp_obj_t)&mp_builtin_ord_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
|
||||||
|
@ -18,6 +18,8 @@ codepoint2name[ord(':')] = 'colon'
|
|||||||
codepoint2name[ord('/')] = 'slash'
|
codepoint2name[ord('/')] = 'slash'
|
||||||
codepoint2name[ord('%')] = 'percent'
|
codepoint2name[ord('%')] = 'percent'
|
||||||
codepoint2name[ord('#')] = 'hash'
|
codepoint2name[ord('#')] = 'hash'
|
||||||
|
codepoint2name[ord('{')] = 'brace_open'
|
||||||
|
codepoint2name[ord('}')] = 'brace_close'
|
||||||
|
|
||||||
# this must match the equivalent function in qstr.c
|
# this must match the equivalent function in qstr.c
|
||||||
def compute_hash(qstr):
|
def compute_hash(qstr):
|
||||||
|
@ -579,7 +579,7 @@ static mp_obj_t arg_as_int(mp_obj_t arg) {
|
|||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
|
mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
|
||||||
assert(MP_OBJ_IS_STR(args[0]));
|
assert(MP_OBJ_IS_STR(args[0]));
|
||||||
|
|
||||||
GET_STR_DATA_LEN(args[0], str, len);
|
GET_STR_DATA_LEN(args[0], str, len);
|
||||||
@ -1346,7 +1346,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
|
|||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, str_split);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_startswith_obj, str_startswith);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, mp_obj_str_format);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
|
||||||
|
@ -8,3 +8,5 @@ typedef struct _mp_obj_str_t {
|
|||||||
} mp_obj_str_t;
|
} 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};
|
#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str};
|
||||||
|
|
||||||
|
mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args);
|
||||||
|
@ -83,6 +83,8 @@ Q(all)
|
|||||||
Q(any)
|
Q(any)
|
||||||
Q(args)
|
Q(args)
|
||||||
Q(array)
|
Q(array)
|
||||||
|
Q(bin)
|
||||||
|
Q({:#b})
|
||||||
Q(bool)
|
Q(bool)
|
||||||
Q(bytearray)
|
Q(bytearray)
|
||||||
Q(bytes)
|
Q(bytes)
|
||||||
@ -122,6 +124,8 @@ Q(max)
|
|||||||
Q(min)
|
Q(min)
|
||||||
Q(namedtuple)
|
Q(namedtuple)
|
||||||
Q(next)
|
Q(next)
|
||||||
|
Q(oct)
|
||||||
|
Q(%#o)
|
||||||
Q(open)
|
Q(open)
|
||||||
Q(ord)
|
Q(ord)
|
||||||
Q(path)
|
Q(path)
|
||||||
|
12
tests/basics/builtin-bin.py
Normal file
12
tests/basics/builtin-bin.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# test builtin bin function
|
||||||
|
|
||||||
|
print(bin(1))
|
||||||
|
print(bin(-1))
|
||||||
|
print(bin(15))
|
||||||
|
print(bin(-15))
|
||||||
|
|
||||||
|
print(bin(12345))
|
||||||
|
print(bin(0b10101))
|
||||||
|
|
||||||
|
print(bin(12345678901234567890))
|
||||||
|
print(bin(0b10101010101010101010))
|
12
tests/basics/builtin-hex.py
Normal file
12
tests/basics/builtin-hex.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# test builtin hex function
|
||||||
|
|
||||||
|
print(hex(1))
|
||||||
|
print(hex(-1))
|
||||||
|
print(hex(15))
|
||||||
|
print(hex(-15))
|
||||||
|
|
||||||
|
print(hex(12345))
|
||||||
|
print(hex(0x12345))
|
||||||
|
|
||||||
|
print(hex(12345678901234567890))
|
||||||
|
print(hex(0x12345678901234567890))
|
12
tests/basics/builtin-oct.py
Normal file
12
tests/basics/builtin-oct.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# test builtin oct function
|
||||||
|
|
||||||
|
print(oct(1))
|
||||||
|
print(oct(-1))
|
||||||
|
print(oct(15))
|
||||||
|
print(oct(-15))
|
||||||
|
|
||||||
|
print(oct(12345))
|
||||||
|
print(oct(0o12345))
|
||||||
|
|
||||||
|
print(oct(12345678901234567890))
|
||||||
|
print(oct(0o12345670123456701234))
|
Loading…
Reference in New Issue
Block a user