This commit is contained in:
blueloveTH 2023-02-01 16:10:34 +08:00
parent 2012e47507
commit e73d8ba5a2
3 changed files with 13 additions and 52 deletions

View File

@ -2553,42 +2553,6 @@ class dict:
a.append(k.__json__()+': '+v.__json__()) a.append(k.__json__()+': '+v.__json__())
return '{'+ ', '.join(a) + '}' return '{'+ ', '.join(a) + '}'
import ffi
def input():
return ffi.input()
class FileIO:
def __init__(self, path, mode):
assert type(path) is str
assert type(mode) is str
assert mode in ['r', 'w', 'rt', 'wt']
self.path = path
self.mode = mode
self.fp = ffi.fopen(path, mode)
def read(self):
assert self.mode in ['r', 'rt']
return ffi.fread(self.fp)
def write(self, s):
assert self.mode in ['w', 'wt']
assert type(s) is str
ffi.fwrite(self.fp, s)
def close(self):
ffi.fclose(self.fp)
def __enter__(self):
pass
def __exit__(self):
self.close()
def open(path, mode='r'):
return FileIO(path, mode)
class set: class set:
def __init__(self, iterable=None): def __init__(self, iterable=None):
iterable = iterable or [] iterable = iterable or []
@ -6674,14 +6638,14 @@ extern "C" {
__EXPORT __EXPORT
/// Get a global variable of a virtual machine. /// Get a global variable of a virtual machine.
/// ///
/// Return a json representing the result. /// Return __repr__ of the result.
/// If the variable is not found, return `nullptr`. /// If the variable is not found, return `nullptr`.
char* pkpy_vm_get_global(VM* vm, const char* name){ char* pkpy_vm_get_global(VM* vm, const char* name){
auto it = vm->_main->attribs.find(name); auto it = vm->_main->attribs.find(name);
if(it == vm->_main->attribs.end()) return nullptr; if(it == vm->_main->attribs.end()) return nullptr;
try{ try{
_Str _json = vm->PyStr_AS_C(vm->asJson(it->second)); _Str _repr = vm->PyStr_AS_C(vm->asRepr(it->second));
return strdup(_json.c_str()); return strdup(_repr.c_str());
}catch(...){ }catch(...){
return nullptr; return nullptr;
} }
@ -6690,14 +6654,14 @@ extern "C" {
__EXPORT __EXPORT
/// Evaluate an expression. /// Evaluate an expression.
/// ///
/// Return a json representing the result. /// Return __repr__ of the result.
/// If there is any error, return `nullptr`. /// If there is any error, return `nullptr`.
char* pkpy_vm_eval(VM* vm, const char* source){ char* pkpy_vm_eval(VM* vm, const char* source){
PyVarOrNull ret = vm->exec(source, "<eval>", EVAL_MODE); PyVarOrNull ret = vm->exec(source, "<eval>", EVAL_MODE);
if(ret == nullptr) return nullptr; if(ret == nullptr) return nullptr;
try{ try{
_Str _json = vm->PyStr_AS_C(vm->asJson(ret)); _Str _repr = vm->PyStr_AS_C(vm->asRepr(ret));
return strdup(_json.c_str()); return strdup(_repr.c_str());
}catch(...){ }catch(...){
return nullptr; return nullptr;
} }
@ -6729,9 +6693,6 @@ extern "C" {
__add_module_math(vm); __add_module_math(vm);
__add_module_re(vm); __add_module_re(vm);
__add_module_dis(vm); __add_module_dis(vm);
vm->new_module("ffi");
// add builtins | no exception handler | must succeed // add builtins | no exception handler | must succeed
_Code code = vm->compile(__BUILTINS_CODE, "<builtins>", EXEC_MODE); _Code code = vm->compile(__BUILTINS_CODE, "<builtins>", EXEC_MODE);

@ -1 +1 @@
Subproject commit 5a12c3df42bb5ed9ddb14aba4b6807021f101f77 Subproject commit de94780fd098c376d61f84a6b1655758f9b74c17

View File

@ -710,14 +710,14 @@ extern "C" {
__EXPORT __EXPORT
/// Get a global variable of a virtual machine. /// Get a global variable of a virtual machine.
/// ///
/// Return a json representing the result. /// Return __repr__ of the result.
/// If the variable is not found, return `nullptr`. /// If the variable is not found, return `nullptr`.
char* pkpy_vm_get_global(VM* vm, const char* name){ char* pkpy_vm_get_global(VM* vm, const char* name){
auto it = vm->_main->attribs.find(name); auto it = vm->_main->attribs.find(name);
if(it == vm->_main->attribs.end()) return nullptr; if(it == vm->_main->attribs.end()) return nullptr;
try{ try{
_Str _json = vm->PyStr_AS_C(vm->asJson(it->second)); _Str _repr = vm->PyStr_AS_C(vm->asRepr(it->second));
return strdup(_json.c_str()); return strdup(_repr.c_str());
}catch(...){ }catch(...){
return nullptr; return nullptr;
} }
@ -726,14 +726,14 @@ extern "C" {
__EXPORT __EXPORT
/// Evaluate an expression. /// Evaluate an expression.
/// ///
/// Return a json representing the result. /// Return __repr__ of the result.
/// If there is any error, return `nullptr`. /// If there is any error, return `nullptr`.
char* pkpy_vm_eval(VM* vm, const char* source){ char* pkpy_vm_eval(VM* vm, const char* source){
PyVarOrNull ret = vm->exec(source, "<eval>", EVAL_MODE); PyVarOrNull ret = vm->exec(source, "<eval>", EVAL_MODE);
if(ret == nullptr) return nullptr; if(ret == nullptr) return nullptr;
try{ try{
_Str _json = vm->PyStr_AS_C(vm->asJson(ret)); _Str _repr = vm->PyStr_AS_C(vm->asRepr(ret));
return strdup(_json.c_str()); return strdup(_repr.c_str());
}catch(...){ }catch(...){
return nullptr; return nullptr;
} }