Skip to content

Commit 039d4fc

Browse files
committed
Merge remote-tracking branch 'upstream/master' into OWL_v1.18
2 parents d3c911e + 68ca22b commit 039d4fc

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

py/runtime.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,14 +1531,11 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
15311531
mp_obj_t dest[2];
15321532

15331533
mp_load_method_maybe(module, name, dest);
1534-
15351534
if (dest[1] != MP_OBJ_NULL) {
1536-
// Hopefully we can't import bound method from an object
1537-
import_error:
1538-
mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("can't import name %q"), name);
1539-
}
1540-
1541-
if (dest[0] != MP_OBJ_NULL) {
1535+
// Importing a bound method from a class instance.
1536+
return mp_obj_new_bound_meth(dest[0], dest[1]);
1537+
} else if (dest[0] != MP_OBJ_NULL) {
1538+
// Importing a function or attribute.
15421539
return dest[0];
15431540
}
15441541

@@ -1571,6 +1568,9 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
15711568
goto import_error;
15721569

15731570
#endif
1571+
1572+
import_error:
1573+
mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("can't import name %q"), name);
15741574
}
15751575

15761576
void mp_import_all(mp_obj_t module) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Test importing a method from a class instance.
2+
# This is not a common thing to do, but ensures MicroPython has the same semantics as CPython.
3+
4+
import sys
5+
6+
if not hasattr(sys, "modules"):
7+
print("SKIP")
8+
raise SystemExit
9+
10+
11+
class A:
12+
def __init__(self, value):
13+
self.value = value
14+
15+
def meth(self):
16+
return self.value
17+
18+
def meth_with_arg(self, a):
19+
return [self.value, a]
20+
21+
22+
# Register a class instance as the module "mod".
23+
sys.modules["mod"] = A(1)
24+
25+
# Try importing it as a module.
26+
import mod
27+
28+
print(mod.meth())
29+
print(mod.meth_with_arg(2))
30+
31+
# Change the module.
32+
sys.modules["mod"] = A(3)
33+
34+
# Try importing it using "from ... import".
35+
from mod import meth, meth_with_arg
36+
37+
print(meth())
38+
print(meth_with_arg(4))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test the registerJsModule() public API method.
2+
3+
import(process.argv[2]).then((mp) => {
4+
mp.loadMicroPython().then((py) => {
5+
// Simple module.
6+
py.registerJsModule("js_module", { y: 2 });
7+
py.runPython("import js_module; print(js_module); print(js_module.y)");
8+
9+
// Module with functions.
10+
// In particular test how "this" behaves.
11+
py.registerJsModule("js_module2", {
12+
yes: true,
13+
add1(x) {
14+
return x + 1;
15+
},
16+
getThis() {
17+
return this;
18+
},
19+
});
20+
21+
console.log("====");
22+
23+
// Test using simple import.
24+
py.runPython(`
25+
import js_module2
26+
27+
print(js_module2.yes)
28+
print(js_module2.add1(1))
29+
print(js_module2.getThis())
30+
print(js_module2.getThis().yes)
31+
`);
32+
33+
console.log("====");
34+
35+
// Test using "from ... import".
36+
py.runPython(`
37+
from js_module2 import yes, add1, getThis
38+
39+
print(yes)
40+
print(add1(2))
41+
print(getThis())
42+
print(getThis().yes)
43+
`);
44+
});
45+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<JsProxy 2>
2+
2
3+
====
4+
True
5+
2
6+
<JsProxy 3>
7+
True
8+
====
9+
True
10+
3
11+
<JsProxy 3>
12+
True

0 commit comments

Comments
 (0)