Skip to content

Commit d89b69e

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents d0f9f6c + e1e4249 commit d89b69e

10 files changed

Lines changed: 140 additions & 64 deletions

File tree

py/objfun.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "qstr.h"
1010
#include "obj.h"
1111
#include "objtuple.h"
12+
#include "objfun.h"
1213
#include "runtime0.h"
1314
#include "runtime.h"
1415
#include "bc.h"
@@ -150,18 +151,6 @@ mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var
150151
/******************************************************************************/
151152
/* byte code functions */
152153

153-
typedef struct _mp_obj_fun_bc_t {
154-
mp_obj_base_t base;
155-
mp_obj_dict_t *globals; // the context within which this function was defined
156-
machine_uint_t n_args : 15; // number of arguments this function takes
157-
machine_uint_t n_def_args : 15; // number of default arguments
158-
machine_uint_t takes_var_args : 1; // set if this function takes variable args
159-
machine_uint_t takes_kw_args : 1; // set if this function takes keyword args
160-
const byte *bytecode; // bytecode for the function
161-
qstr *args; // argument names (needed to resolve positional args passed as keywords)
162-
mp_obj_t extra_args[]; // values of default args (if any), plus a slot at the end for var args and/or kw args (if it takes them)
163-
} mp_obj_fun_bc_t;
164-
165154
#if DEBUG_PRINT
166155
STATIC void dump_args(const mp_obj_t *a, int sz) {
167156
DEBUG_printf("%p: ", a);

py/objfun.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
typedef struct _mp_obj_fun_bc_t {
2+
mp_obj_base_t base;
3+
mp_obj_dict_t *globals; // the context within which this function was defined
4+
machine_uint_t n_args : 15; // number of arguments this function takes
5+
machine_uint_t n_def_args : 15; // number of default arguments
6+
machine_uint_t takes_var_args : 1; // set if this function takes variable args
7+
machine_uint_t takes_kw_args : 1; // set if this function takes keyword args
8+
const byte *bytecode; // bytecode for the function
9+
qstr *args; // argument names (needed to resolve positional args passed as keywords)
10+
// values of default args (if any), plus a slot at the end for var args and/or kw args (if it takes them)
11+
mp_obj_t extra_args[];
12+
} mp_obj_fun_bc_t;

py/objgenerator.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "runtime.h"
1010
#include "bc.h"
1111
#include "objgenerator.h"
12+
#include "objfun.h"
1213

1314
/******************************************************************************/
1415
/* generator wrapper */
@@ -18,11 +19,12 @@ typedef struct _mp_obj_gen_wrap_t {
1819
mp_obj_t *fun;
1920
} mp_obj_gen_wrap_t;
2021

21-
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj_t *args, uint n_args2, const mp_obj_t *args2);
22+
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
23+
uint n_args2, const mp_obj_t *args2);
2224

2325
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
2426
mp_obj_gen_wrap_t *self = self_in;
25-
mp_obj_t self_fun = self->fun;
27+
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
2628
assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
2729
int bc_n_args;
2830
const byte *bc_code;
@@ -34,7 +36,7 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp
3436
assert(0);
3537
}
3638

37-
return mp_obj_new_gen_instance(bc_code, len1, args1, len2, args2);
39+
return mp_obj_new_gen_instance(self_fun->globals, bc_code, len1, args1, len2, args2);
3840
}
3941

4042
const mp_obj_type_t mp_type_gen_wrap = {
@@ -55,6 +57,7 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
5557

5658
typedef struct _mp_obj_gen_instance_t {
5759
mp_obj_base_t base;
60+
mp_obj_dict_t *globals;
5861
const byte *code_info;
5962
const byte *ip;
6063
mp_obj_t *sp;
@@ -89,9 +92,12 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
8992
} else {
9093
*self->sp = send_value;
9194
}
95+
mp_obj_dict_t *old_globals = mp_globals_get();
96+
mp_globals_set(self->globals);
9297
mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
9398
&self->state[self->n_state - 1], &self->sp, (mp_exc_stack_t*)(self->state + self->n_state),
9499
&self->exc_sp, throw_value);
100+
mp_globals_set(old_globals);
95101

96102
switch (ret_kind) {
97103
case MP_VM_RETURN_NORMAL:
@@ -225,7 +231,8 @@ const mp_obj_type_t mp_type_gen_instance = {
225231
.locals_dict = (mp_obj_t)&gen_instance_locals_dict,
226232
};
227233

228-
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj_t *args, uint n_args2, const mp_obj_t *args2) {
234+
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
235+
uint n_args2, const mp_obj_t *args2) {
229236
const byte *code_info = bytecode;
230237
// get code info size, and skip the line number table
231238
machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
@@ -239,6 +246,7 @@ mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj
239246
// allocate the generator object, with room for local stack and exception stack
240247
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte, n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
241248
o->base.type = &mp_type_gen_instance;
249+
o->globals = globals;
242250
o->code_info = code_info;
243251
o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
244252
o->exc_sp = (mp_exc_stack_t*)(o->state + n_state) - 1;

tests/basics/gen_context.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import gen_context2
2+
3+
GLOBAL = "GLOBAL"
4+
5+
def gen():
6+
print(GLOBAL)
7+
yield 1
8+
9+
gen_context2.call(gen())

tests/basics/gen_context2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def call(g):
2+
next(g)

unix/main.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ int usage(char **argv) {
250250

251251
mp_obj_t mem_info(void) {
252252
printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
253+
gc_dump_info();
253254
return mp_const_none;
254255
}
255256

@@ -356,10 +357,6 @@ int main(int argc, char **argv) {
356357
mp_store_name(qstr_from_str("gc"), (mp_obj_t)&pyb_gc_obj);
357358
#endif
358359

359-
microsocket_init();
360-
#if MICROPY_MOD_TIME
361-
time_init();
362-
#endif
363360
#if MICROPY_MOD_FFI
364361
ffi_init();
365362
#endif

unix/modsocket.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,18 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 6, mod
322322

323323
extern mp_obj_type_t sockaddr_in_type;
324324

325-
#define C(name) { #name, name }
325+
STATIC const mp_map_elem_t mp_module_socket_globals_table[] = {
326+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_microsocket) },
327+
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&microsocket_type },
328+
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_socket_getaddrinfo_obj },
329+
#if MICROPY_SOCKET_EXTRA
330+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sockaddr_in), (mp_obj_t)&sockaddr_in_type },
331+
{ MP_OBJ_NEW_QSTR(MP_QSTR_htons), (mp_obj_t)&mod_socket_htons_obj },
332+
{ MP_OBJ_NEW_QSTR(MP_QSTR_inet_aton), (mp_obj_t)&mod_socket_inet_aton_obj },
333+
{ MP_OBJ_NEW_QSTR(MP_QSTR_gethostbyname), (mp_obj_t)&mod_socket_gethostbyname_obj },
334+
#endif
326335

327-
STATIC const struct sym_entry {
328-
const char *sym;
329-
int val;
330-
} constants[] = {
336+
#define C(name) { MP_OBJ_NEW_QSTR(MP_QSTR_ ## name), MP_OBJ_NEW_SMALL_INT(name) }
331337
C(AF_UNIX),
332338
C(AF_INET),
333339
C(AF_INET6),
@@ -344,23 +350,22 @@ STATIC const struct sym_entry {
344350
C(SO_KEEPALIVE),
345351
C(SO_LINGER),
346352
C(SO_REUSEADDR),
347-
348-
{NULL}
353+
#undef C
349354
};
350355

351-
#undef C
356+
STATIC const mp_obj_dict_t mp_module_socket_globals = {
357+
.base = {&mp_type_dict},
358+
.map = {
359+
.all_keys_are_qstrs = 1,
360+
.table_is_fixed_array = 1,
361+
.used = sizeof(mp_module_socket_globals_table) / sizeof(mp_map_elem_t),
362+
.alloc = sizeof(mp_module_socket_globals_table) / sizeof(mp_map_elem_t),
363+
.table = (mp_map_elem_t*)mp_module_socket_globals_table,
364+
},
365+
};
352366

353-
void microsocket_init() {
354-
mp_obj_t m = mp_obj_new_module(MP_QSTR_microsocket);
355-
mp_store_attr(m, MP_QSTR_socket, (mp_obj_t)&microsocket_type);
356-
#if MICROPY_SOCKET_EXTRA
357-
mp_store_attr(m, MP_QSTR_sockaddr_in, (mp_obj_t)&sockaddr_in_type);
358-
mp_store_attr(m, MP_QSTR_htons, (mp_obj_t)&mod_socket_htons_obj);
359-
mp_store_attr(m, MP_QSTR_inet_aton, (mp_obj_t)&mod_socket_inet_aton_obj);
360-
mp_store_attr(m, MP_QSTR_gethostbyname, (mp_obj_t)&mod_socket_gethostbyname_obj);
361-
#endif
362-
mp_store_attr(m, MP_QSTR_getaddrinfo, (mp_obj_t)&mod_socket_getaddrinfo_obj);
363-
for (const struct sym_entry *p = constants; p->sym != NULL; p++) {
364-
mp_store_attr(m, QSTR_FROM_STR_STATIC(p->sym), MP_OBJ_NEW_SMALL_INT((machine_int_t)p->val));
365-
}
366-
}
367+
const mp_obj_module_t mp_module_socket = {
368+
.base = { &mp_type_module },
369+
.name = MP_QSTR_microsocket,
370+
.globals = (mp_obj_dict_t*)&mp_module_socket_globals,
371+
};

unix/modtime.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@
1111
#include "runtime.h"
1212

1313
STATIC mp_obj_t mod_time_time() {
14+
#if MICROPY_ENABLE_FLOAT
15+
struct timeval tv;
16+
gettimeofday(&tv, NULL);
17+
mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000;
18+
return mp_obj_new_float(val);
19+
#else
1420
return mp_obj_new_int((machine_int_t)time(NULL));
21+
#endif
1522
}
1623
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time);
1724

1825
// Note: this is deprecated since CPy3.3, but pystone still uses it.
1926
STATIC mp_obj_t mod_time_clock() {
20-
// return mp_obj_new_int((machine_int_t)clock());
21-
// POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume
27+
#if MICROPY_ENABLE_FLOAT
28+
// POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume.
2229
// float cannot represent full range of int32 precisely, so we pre-divide
2330
// int to reduce resolution, and then actually do float division hoping
2431
// to preserve integer part resolution.
2532
return mp_obj_new_float((float)(clock() / 1000) / 1000.0);
33+
#else
34+
return mp_obj_new_int((machine_int_t)clock());
35+
#endif
2636
}
2737
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
2838

@@ -41,9 +51,26 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
4151
}
4252
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
4353

44-
void time_init() {
45-
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("time"));
46-
mp_store_attr(m, QSTR_FROM_STR_STATIC("time"), (mp_obj_t)&mod_time_time_obj);
47-
mp_store_attr(m, QSTR_FROM_STR_STATIC("clock"), (mp_obj_t)&mod_time_clock_obj);
48-
mp_store_attr(m, QSTR_FROM_STR_STATIC("sleep"), (mp_obj_t)&mod_time_sleep_obj);
49-
}
54+
STATIC const mp_map_elem_t mp_module_time_globals_table[] = {
55+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
56+
{ MP_OBJ_NEW_QSTR(MP_QSTR_clock), (mp_obj_t)&mod_time_clock_obj },
57+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&mod_time_sleep_obj },
58+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mod_time_time_obj },
59+
};
60+
61+
STATIC const mp_obj_dict_t mp_module_time_globals = {
62+
.base = {&mp_type_dict},
63+
.map = {
64+
.all_keys_are_qstrs = 1,
65+
.table_is_fixed_array = 1,
66+
.used = sizeof(mp_module_time_globals_table) / sizeof(mp_map_elem_t),
67+
.alloc = sizeof(mp_module_time_globals_table) / sizeof(mp_map_elem_t),
68+
.table = (mp_map_elem_t*)mp_module_time_globals_table,
69+
},
70+
};
71+
72+
const mp_obj_module_t mp_module_time = {
73+
.base = { &mp_type_module },
74+
.name = MP_QSTR_time,
75+
.globals = (mp_obj_dict_t*)&mp_module_time_globals,
76+
};

unix/mpconfigport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
#define MICROPY_MOD_SYS_STDFILES (1)
1818
#define MICROPY_ENABLE_MOD_CMATH (1)
1919

20+
extern const struct _mp_obj_module_t mp_module_time;
21+
extern const struct _mp_obj_module_t mp_module_socket;
22+
#define MICROPY_EXTRA_BUILTIN_MODULES \
23+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_time }, \
24+
{ MP_OBJ_NEW_QSTR(MP_QSTR_microsocket), (mp_obj_t)&mp_module_socket }, \
25+
2026
// type definitions for the specific machine
2127

2228
#ifdef __LP64__

unix/qstrdefsport.h

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@
22

33
Q(Test)
44

5-
Q(rawsocket)
6-
Q(socket)
7-
Q(sockaddr_in)
8-
Q(htons)
9-
Q(inet_aton)
10-
Q(gethostbyname)
11-
Q(getaddrinfo)
12-
Q(microsocket)
135
Q(fileno)
146
Q(read)
157
Q(readall)
168
Q(readline)
179
Q(write)
1810
Q(makefile)
19-
Q(connect)
20-
Q(bind)
21-
Q(listen)
22-
Q(accept)
23-
Q(recv)
24-
Q(setsockopt)
2511

2612
Q(FileIO)
2713
Q(ffimod)
@@ -30,3 +16,38 @@ Q(fficallback)
3016
Q(ffivar)
3117
Q(func)
3218
Q(var)
19+
20+
Q(time)
21+
Q(clock)
22+
Q(sleep)
23+
24+
Q(socket)
25+
Q(sockaddr_in)
26+
Q(htons)
27+
Q(inet_aton)
28+
Q(gethostbyname)
29+
Q(getaddrinfo)
30+
Q(microsocket)
31+
Q(connect)
32+
Q(bind)
33+
Q(listen)
34+
Q(accept)
35+
Q(recv)
36+
Q(setsockopt)
37+
38+
Q(AF_UNIX)
39+
Q(AF_INET)
40+
Q(AF_INET6)
41+
Q(SOCK_STREAM)
42+
Q(SOCK_DGRAM)
43+
Q(SOCK_RAW)
44+
45+
Q(MSG_DONTROUTE)
46+
Q(MSG_DONTWAIT)
47+
48+
Q(SOL_SOCKET)
49+
Q(SO_BROADCAST)
50+
Q(SO_ERROR)
51+
Q(SO_KEEPALIVE)
52+
Q(SO_LINGER)
53+
Q(SO_REUSEADDR)

0 commit comments

Comments
 (0)