本文整理汇总了C++中MP_OBJ_NEW_SMALL_INT函数的典型用法代码示例。如果您正苦于以下问题:C++ MP_OBJ_NEW_SMALL_INT函数的具体用法?C++ MP_OBJ_NEW_SMALL_INT怎么用?C++ MP_OBJ_NEW_SMALL_INT使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MP_OBJ_NEW_SMALL_INT函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mp_obj_int_make_new
// This dispatcher function is expected to be independent of the implementation of long int
STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
(void)type_in;
mp_arg_check_num(n_args, n_kw, 0, 2, false);
switch (n_args) {
case 0:
return MP_OBJ_NEW_SMALL_INT(0);
case 1:
if (MP_OBJ_IS_INT(args[0])) {
// already an int (small or long), just return it
return args[0];
} else if (MP_OBJ_IS_STR_OR_BYTES(args[0])) {
// a string, parse it
mp_uint_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, 0, NULL);
#if MICROPY_PY_BUILTINS_FLOAT
} else if (mp_obj_is_float(args[0])) {
return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
#endif
} else {
// try to convert to small int (eg from bool)
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
}
case 2:
default: {
// should be a string, parse it
// TODO proper error checking of argument types
mp_uint_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL);
}
}
}
示例2: ffimod_var
STATIC mp_obj_t ffimod_var(mp_obj_t self_in, mp_obj_t vartype_in, mp_obj_t symname_in) {
mp_obj_ffimod_t *self = self_in;
const char *rettype = mp_obj_str_get_str(vartype_in);
const char *symname = mp_obj_str_get_str(symname_in);
void *sym = dlsym(self->handle, symname);
if (sym == NULL) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
}
mp_obj_ffivar_t *o = m_new_obj(mp_obj_ffivar_t);
o->base.type = &ffivar_type;
o->var = sym;
o->type = *rettype;
return o;
}
示例3: stream_readall
STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
if (o->type->stream_p == NULL || o->type->stream_p->read == NULL) {
// CPython: io.UnsupportedOperation, OSError subclass
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Operation not supported"));
}
int total_size = 0;
vstr_t *vstr = vstr_new_size(DEFAULT_BUFFER_SIZE);
char *p = vstr_str(vstr);
int error;
int current_read = DEFAULT_BUFFER_SIZE;
while (true) {
mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error);
if (out_sz == MP_STREAM_ERROR) {
if (is_nonblocking_error(error)) {
// With non-blocking streams, we read as much as we can.
// If we read nothing, return None, just like read().
// Otherwise, return data read so far.
if (total_size == 0) {
return mp_const_none;
}
break;
}
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
}
if (out_sz == 0) {
break;
}
total_size += out_sz;
if (out_sz < current_read) {
current_read -= out_sz;
p += out_sz;
} else {
current_read = DEFAULT_BUFFER_SIZE;
p = vstr_extend(vstr, current_read);
if (p == NULL) {
// TODO
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory"));
}
}
}
mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
vstr_free(vstr);
return s;
}
示例4: socket_recv
// method socket.recv(bufsize)
STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
mod_network_socket_obj_t *self = self_in;
mp_int_t len = mp_obj_get_int(len_in);
vstr_t vstr;
vstr_init_len(&vstr, len);
int _errno;
mp_uint_t ret = wlan_socket_recv(self, (byte*)vstr.buf, len, &_errno);
if (ret == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
if (ret == 0) {
return mp_const_empty_bytes;
}
vstr.len = ret;
vstr.buf[vstr.len] = '\0';
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
示例5: pyb_keyboard_get_char
// get_char method : waits until a key is pressed and returns it
mp_obj_t pyb_keyboard_get_char(mp_obj_t self_in) {
pyb_keyboard_obj_t *self = self_in;
// wait key press
uint8_t key=0xFF;
while(key==0xFF)
key = pyb_keyboard_getKey(self);
// wait key relese
uint8_t key2=key;
while(key2!=0xFF)
key2 = pyb_keyboard_getKey(self);
mp_hal_milli_delay(1);
return MP_OBJ_NEW_SMALL_INT(key);
}
示例6: pin_value
STATIC mp_obj_t pin_value(mp_uint_t n_args, const mp_obj_t *args) {
pin_obj_t *self = args[0];
if (n_args == 1) {
// get the value
return MP_OBJ_NEW_SMALL_INT(pin_get_value(self));
} else {
// set the pin value
if (mp_obj_is_true(args[1])) {
self->value = 1;
MAP_GPIOPinWrite(self->port, self->bit, self->bit);
} else {
self->value = 0;
MAP_GPIOPinWrite(self->port, self->bit, 0);
}
return mp_const_none;
}
}
示例7: mp_obj_len_maybe
// may return MP_OBJ_NULL
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
if (
#if !MICROPY_PY_BUILTINS_STR_UNICODE
// It's simple - unicode is slow, non-unicode is fast
MP_OBJ_IS_STR(o_in) ||
#endif
MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
return MP_OBJ_NEW_SMALL_INT(mp_obj_str_get_len(o_in));
} else {
mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->unary_op != NULL) {
return type->unary_op(MP_UNARY_OP_LEN, o_in);
} else {
return MP_OBJ_NULL;
}
}
}
示例8: mp_seq_get_fast_slice_indexes
bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
mp_obj_t ostart, ostop, ostep;
machine_int_t start, stop;
mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
if (ostart == mp_const_none) {
start = 0;
} else {
start = MP_OBJ_SMALL_INT_VALUE(ostart);
}
if (ostop == mp_const_none) {
stop = len;
} else {
stop = MP_OBJ_SMALL_INT_VALUE(ostop);
}
// Unlike subscription, out-of-bounds slice indexes are never error
if (start < 0) {
start = len + start;
if (start < 0) {
start = 0;
}
} else if (start > len) {
start = len;
}
if (stop < 0) {
stop = len + stop;
} else if (stop > len) {
stop = len;
}
// CPython returns empty sequence in such case, or point for assignment is at start
if (start > stop) {
stop = start;
}
indexes->start = start;
indexes->stop = stop;
if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
indexes->step = MP_OBJ_SMALL_INT_VALUE(ostep);
return false;
}
indexes->step = 1;
return true;
}
示例9: exti_load_attr
static void exti_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) {
(void)self_in;
const char *attr = qstr_str(attr_qstr);
if (strcmp(attr, "regs") == 0) {
dest[0] = (mp_obj_t)&exti_regs_obj;
return;
}
const exti_const_t *entry = &exti_const[0];
for (; entry < &exti_const[EXTI_NUM_CONST]; entry++) {
if (strcmp(attr, entry->name) == 0) {
dest[0] = MP_OBJ_NEW_SMALL_INT(entry->val);
dest[1] = MP_OBJ_NULL;
return;
}
}
}
示例10: mp_stream_write
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
int error;
mp_uint_t out_sz = mp_stream_rw(self_in, (void*)buf, len, &error, flags);
if (error != 0) {
if (mp_is_nonblocking_error(error)) {
// http://docs.python.org/3/library/io.html#io.RawIOBase.write
// "None is returned if the raw stream is set not to block and
// no single byte could be readily written to it."
return mp_const_none;
}
mp_raise_OSError(error);
} else {
return MP_OBJ_NEW_SMALL_INT(out_sz);
}
}
示例11: socket_connect
// method socket.connect(address)
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
mod_network_socket_obj_t *self = self_in;
// get address
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_LITTLE);
// connect the socket
int _errno;
if (wlan_socket_connect(self, ip, port, &_errno) != 0) {
if (!self->sock_base.cert_req && _errno == SL_ESECSNOVERIFY) {
return mp_const_none;
}
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
}
return mp_const_none;
}
示例12: mp_obj_id
mp_obj_t mp_obj_id(mp_obj_t o_in) {
mp_int_t id = (mp_int_t)o_in;
if (!MP_OBJ_IS_OBJ(o_in)) {
return mp_obj_new_int(id);
} else if (id >= 0) {
// Many OSes and CPUs have affinity for putting "user" memories
// into low half of address space, and "system" into upper half.
// We're going to take advantage of that and return small int
// (signed) for such "user" addresses.
return MP_OBJ_NEW_SMALL_INT(id);
} else {
// If that didn't work, well, let's return long int, just as
// a (big) positve value, so it will never clash with the range
// of small int returned in previous case.
return mp_obj_new_int_from_uint((mp_uint_t)id);
}
}
示例13: mp_binary_get_val_array
mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
mp_int_t val = 0;
switch (typecode) {
case 'b':
val = ((signed char*)p)[index];
break;
case BYTEARRAY_TYPECODE:
case 'B':
val = ((unsigned char*)p)[index];
break;
case 'h':
val = ((short*)p)[index];
break;
case 'H':
val = ((unsigned short*)p)[index];
break;
case 'i':
return mp_obj_new_int(((int*)p)[index]);
case 'I':
return mp_obj_new_int_from_uint(((unsigned int*)p)[index]);
case 'l':
return mp_obj_new_int(((long*)p)[index]);
case 'L':
return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
case 'q':
return mp_obj_new_int_from_ll(((long long*)p)[index]);
case 'Q':
return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]);
#endif
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
return mp_obj_new_float(((float*)p)[index]);
case 'd':
return mp_obj_new_float(((double*)p)[index]);
#endif
// Extension to CPython: array of objects
case 'O':
return ((mp_obj_t*)p)[index];
// Extension to CPython: array of pointers
case 'P':
return mp_obj_new_int((mp_int_t)((void**)p)[index]);
}
return MP_OBJ_NEW_SMALL_INT(val);
}
示例14: str_subscr
STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_obj_type_t *type = mp_obj_get_type(self_in);
assert(type == &mp_type_str);
GET_STR_DATA_LEN(self_in, self_data, self_len);
if (value == MP_OBJ_SENTINEL) {
// load
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_obj_t ostart, ostop, ostep;
mp_obj_slice_get(index, &ostart, &ostop, &ostep);
if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"only slices with step=1 (aka None) are supported"));
}
const byte *pstart, *pstop;
if (ostart != mp_const_none) {
pstart = str_index_to_ptr(type, self_data, self_len, ostart, true);
} else {
pstart = self_data;
}
if (ostop != mp_const_none) {
// pstop will point just after the stop character. This depends on
// the \0 at the end of the string.
pstop = str_index_to_ptr(type, self_data, self_len, ostop, true);
} else {
pstop = self_data + self_len;
}
if (pstop < pstart) {
return MP_OBJ_NEW_QSTR(MP_QSTR_);
}
return mp_obj_new_str_of_type(type, (const byte *)pstart, pstop - pstart);
}
#endif
const byte *s = str_index_to_ptr(type, self_data, self_len, index, false);
int len = 1;
if (UTF8_IS_NONASCII(*s)) {
// Count the number of 1 bits (after the first)
for (char mask = 0x40; *s & mask; mask >>= 1) {
++len;
}
}
return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string
} else {
return MP_OBJ_NULL; // op not supported
示例15: socket_bind
// method socket.bind(address)
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
mod_network_socket_obj_t *self = self_in;
// get address
uint8_t ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE];
mp_uint_t port = mod_network_parse_inet_addr(addr_in, ip);
// check if we need to select a NIC
socket_select_nic(self, ip);
// call the NIC to bind the socket
int _errno;
if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
return mp_const_none;
}