Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
d0f5e61a
Commit
d0f5e61a
authored
10 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
py: Implement __file__ attribute for modules.
parent
645582fe
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
py/builtinimport.c
+3
-0
3 additions, 0 deletions
py/builtinimport.c
py/mpconfig.h
+5
-0
5 additions, 0 deletions
py/mpconfig.h
py/qstrdefs.h
+3
-0
3 additions, 0 deletions
py/qstrdefs.h
tests/import/import_file.py
+2
-0
2 additions, 0 deletions
tests/import/import_file.py
unix/main.c
+5
-0
5 additions, 0 deletions
unix/main.c
with
18 additions
and
0 deletions
py/builtinimport.c
+
3
−
0
View file @
d0f5e61a
...
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
py/mpconfig.h
+
5
−
0
View file @
d0f5e61a
...
...
@@ -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.
...
...
This diff is collapsed.
Click to expand it.
py/qstrdefs.h
+
3
−
0
View file @
d0f5e61a
...
...
@@ -43,6 +43,9 @@ Q(__next__)
Q
(
__qualname__
)
Q
(
__path__
)
Q
(
__repl_print__
)
#if MICROPY_PY___FILE__
Q
(
__file__
)
#endif
Q
(
__bool__
)
Q
(
__contains__
)
...
...
This diff is collapsed.
Click to expand it.
tests/import/import_file.py
0 → 100644
+
2
−
0
View file @
d0f5e61a
import
import1b
print
(
import1b
.
__file__
)
This diff is collapsed.
Click to expand it.
unix/main.c
+
5
−
0
View file @
d0f5e61a
...
...
@@ -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
);
/*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment