py/builtinimport: Populate __file__ when importing frozen or mpy files.
Note that bytecode already includes the source filename as a qstr so there is no additional memory used by the interning operation here.
This commit is contained in:
parent
7c15e50eb8
commit
a8e3201b37
@ -145,11 +145,11 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
|
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_MODULE_FROZEN_MPY
|
||||||
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
|
STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code, const char* source_name) {
|
||||||
|
(void)source_name;
|
||||||
|
|
||||||
#if MICROPY_PY___FILE__
|
#if MICROPY_PY___FILE__
|
||||||
// TODO
|
mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(qstr_from_str(source_name)));
|
||||||
//qstr source_name = lex->source_name;
|
|
||||||
//mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// execute the module in its context
|
// execute the module in its context
|
||||||
@ -206,7 +206,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
|
|||||||
// its data) in the list of frozen files, execute it.
|
// its data) in the list of frozen files, execute it.
|
||||||
#if MICROPY_MODULE_FROZEN_MPY
|
#if MICROPY_MODULE_FROZEN_MPY
|
||||||
if (frozen_type == MP_FROZEN_MPY) {
|
if (frozen_type == MP_FROZEN_MPY) {
|
||||||
do_execute_raw_code(module_obj, modref);
|
do_execute_raw_code(module_obj, modref, file_str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -216,7 +216,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
|
|||||||
#if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD
|
#if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD
|
||||||
if (file_str[file->len - 3] == 'm') {
|
if (file_str[file->len - 3] == 'm') {
|
||||||
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
|
mp_raw_code_t *raw_code = mp_raw_code_load_file(file_str);
|
||||||
do_execute_raw_code(module_obj, raw_code);
|
do_execute_raw_code(module_obj, raw_code, file_str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -48,13 +48,15 @@ print(buf.write(bytearray(16)))
|
|||||||
|
|
||||||
# test basic import of frozen scripts
|
# test basic import of frozen scripts
|
||||||
import frzstr1
|
import frzstr1
|
||||||
|
print(frzstr1.__file__)
|
||||||
import frzmpy1
|
import frzmpy1
|
||||||
|
print(frzmpy1.__file__)
|
||||||
|
|
||||||
# test import of frozen packages with __init__.py
|
# test import of frozen packages with __init__.py
|
||||||
import frzstr_pkg1
|
import frzstr_pkg1
|
||||||
print(frzstr_pkg1.x)
|
print(frzstr_pkg1.__file__, frzstr_pkg1.x)
|
||||||
import frzmpy_pkg1
|
import frzmpy_pkg1
|
||||||
print(frzmpy_pkg1.x)
|
print(frzmpy_pkg1.__file__, frzmpy_pkg1.x)
|
||||||
|
|
||||||
# test import of frozen packages without __init__.py
|
# test import of frozen packages without __init__.py
|
||||||
from frzstr_pkg2.mod import Foo
|
from frzstr_pkg2.mod import Foo
|
||||||
|
@ -94,11 +94,13 @@ OSError
|
|||||||
None
|
None
|
||||||
None
|
None
|
||||||
frzstr1
|
frzstr1
|
||||||
|
frzstr1.py
|
||||||
frzmpy1
|
frzmpy1
|
||||||
|
frzmpy1.py
|
||||||
frzstr_pkg1.__init__
|
frzstr_pkg1.__init__
|
||||||
1
|
frzstr_pkg1/__init__.py 1
|
||||||
frzmpy_pkg1.__init__
|
frzmpy_pkg1.__init__
|
||||||
1
|
frzmpy_pkg1/__init__.py 1
|
||||||
frzstr_pkg2.mod
|
frzstr_pkg2.mod
|
||||||
1
|
1
|
||||||
frzmpy_pkg2.mod
|
frzmpy_pkg2.mod
|
||||||
|
Loading…
Reference in New Issue
Block a user