py: Implement __file__ attribute for modules.

nlr-macros
Paul Sokolovsky 2014-07-25 11:00:15 +03:00
parent 645582fe14
commit d0f5e61ab5
5 changed files with 18 additions and 0 deletions

View File

@ -120,6 +120,9 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// set the new context
mp_locals_set(mp_obj_module_get_globals(module_obj));
mp_globals_set(mp_obj_module_get_globals(module_obj));
#if MICROPY_PY___FILE__
mp_store_attr(module_obj, MP_QSTR___file__, mp_obj_new_str(vstr_str(file), vstr_len(file), false));
#endif
// parse the imported script
mp_parse_error_kind_t parse_error_kind;

View File

@ -279,6 +279,11 @@ typedef double mp_float_t;
#define MICROPY_PY_BUILTINS_PROPERTY (1)
#endif
// Whether to set __file__ for imported modules
#ifndef MICROPY_PY___FILE__
#define MICROPY_PY___FILE__ (1)
#endif
// Whether to provide "array" module. Note that large chunk of the
// underlying code is shared with "bytearray" builtin type, so to
// get real savings, it should be disabled too.

View File

@ -43,6 +43,9 @@ Q(__next__)
Q(__qualname__)
Q(__path__)
Q(__repl_print__)
#if MICROPY_PY___FILE__
Q(__file__)
#endif
Q(__bool__)
Q(__contains__)

View File

@ -0,0 +1,2 @@
import import1b
print(import1b.__file__)

View File

@ -95,6 +95,11 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
}
qstr source_name = mp_lexer_source_name(lex);
#if MICROPY_PY___FILE__
if (input_kind == MP_PARSE_FILE_INPUT) {
mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
}
#endif
mp_lexer_free(lex);
/*