mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 20:10:17 +00:00
...
This commit is contained in:
parent
ddd841f0c6
commit
a484879b14
@ -43,7 +43,7 @@ As you can see, direct access does not take care of derived attributes or method
|
||||
In most cases, what you need is `getattr` and `setattr`.
|
||||
These two methods handle all possible cases.
|
||||
|
||||
#### `PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err=true)`
|
||||
#### `PyObject* getattr(PyObject* obj, StrName name, bool throw_err=true)`
|
||||
|
||||
This method is equivalent to `getattr` in python.
|
||||
If the attribute is not found, it will return `nullptr`
|
||||
@ -64,7 +64,7 @@ int result = CAST(int, ret);
|
||||
std::cout << result << std::endl; // 3
|
||||
```
|
||||
|
||||
#### `void VM::setattr(PyObject*, StrName, PyObject*)`
|
||||
#### `void setattr(PyObject*, StrName, PyObject*)`
|
||||
|
||||
This method is equivalent to `setattr` in python.
|
||||
It raises `TypeError` if the object does not support attribute assignment.
|
@ -10,8 +10,8 @@ You can use `call` to invoke any python callable object,
|
||||
including functions, methods, classes, etc.
|
||||
For methods, `call_method` can be used.
|
||||
|
||||
+ `PyObject* VM::call(PyObject* obj, ...)`
|
||||
+ `PyObject* VM::call_method(PyObject* obj, StrName name, ...)`
|
||||
+ `PyObject* call(PyObject* obj, ...)`
|
||||
+ `PyObject* call_method(PyObject* obj, StrName name, ...)`
|
||||
|
||||
### Exmaple
|
||||
|
||||
|
@ -79,7 +79,7 @@ List& list = CAST(List&, obj);
|
||||
### Check type of `PyObject*`
|
||||
|
||||
Each `PyObject*` has a `Type` field to indicate its type.
|
||||
`Type` is just an integer which is the global index in `VM::_all_types`.
|
||||
`Type` is just an integer which is the global index in `vm->_all_types`.
|
||||
|
||||
`VM` class has a set of predefined `Type` constants for quick access.
|
||||
They are prefixed by `tp_`. For example, `tp_object`(object),
|
||||
@ -115,5 +115,5 @@ Other variants are designed for specific types and are faster.
|
||||
You can also use `check_` prefix functions assert the type of a `PyObject*`,
|
||||
which will throw `TypeError` on failure.
|
||||
|
||||
+ `void VM::check_type(PyObject* obj, Type type)`
|
||||
+ `void VM::check_non_tagged_type(PyObject* obj, Type type)`
|
||||
+ `void check_type(PyObject* obj, Type type)`
|
||||
+ `void check_non_tagged_type(PyObject* obj, Type type)`
|
@ -53,7 +53,7 @@ When you do `import` a module, the VM will try to find it in the following order
|
||||
|
||||
1. Search `vm->_modules`, if found, return it.
|
||||
2. Search `vm->_lazy_modules`, if found, compile and execute it, then return it.
|
||||
3. Search the working directory and try to load it from file system via `vm->_import_handler`.
|
||||
3. Try `vm->_import_handler`.
|
||||
|
||||
|
||||
### Customized import handler
|
||||
@ -77,3 +77,9 @@ inline Bytes _default_import_handler(const Str& name){
|
||||
return Bytes(std::move(buffer));
|
||||
};
|
||||
```
|
||||
|
||||
### Import module via cpp
|
||||
|
||||
You can use `vm->py_import` to import a module.
|
||||
This is equivalent to `import` in python.
|
||||
Return the module object if success.
|
Loading…
x
Reference in New Issue
Block a user