change _import_handler

This commit is contained in:
blueloveTH 2024-05-04 20:04:15 +08:00
parent 0a725eecb2
commit 620c1d2882
6 changed files with 9 additions and 12 deletions

View File

@ -36,8 +36,6 @@
#define PK_DEBUG_EXTRA_CHECK 0 #define PK_DEBUG_EXTRA_CHECK 0
// Do not edit the following settings unless you know what you are doing // Do not edit the following settings unless you know what you are doing
#define PK_DEBUG_NO_BUILTINS 0
#define PK_DEBUG_DIS_EXEC 0
#define PK_DEBUG_CEVAL_STEP 0 #define PK_DEBUG_CEVAL_STEP 0
#define PK_DEBUG_MEMORY_POOL 0 #define PK_DEBUG_MEMORY_POOL 0
#define PK_DEBUG_NO_MEMORY_POOL 0 #define PK_DEBUG_NO_MEMORY_POOL 0

View File

@ -3,7 +3,7 @@
#include "bindings.h" #include "bindings.h"
namespace pkpy{ namespace pkpy{
unsigned char* _default_import_handler(const char*, int, int*); unsigned char* _default_import_handler(const char*, int*);
void add_module_os(VM* vm); void add_module_os(VM* vm);
void add_module_io(VM* vm); void add_module_io(VM* vm);
} }

View File

@ -13,7 +13,7 @@ extern "C" {
typedef struct pkpy_vm_handle pkpy_vm; typedef struct pkpy_vm_handle pkpy_vm;
typedef int (*pkpy_CFunction)(pkpy_vm*); typedef int (*pkpy_CFunction)(pkpy_vm*);
typedef void (*pkpy_COutputHandler)(const char*, int); typedef void (*pkpy_COutputHandler)(const char*, int);
typedef unsigned char* (*pkpy_CImportHandler)(const char*, int, int*); typedef unsigned char* (*pkpy_CImportHandler)(const char*, int*);
typedef int pkpy_CName; typedef int pkpy_CName;
typedef int pkpy_CType; typedef int pkpy_CType;
typedef const char* pkpy_CString; typedef const char* pkpy_CString;

View File

@ -167,7 +167,7 @@ public:
void(*_stdout)(const char*, int); void(*_stdout)(const char*, int);
void(*_stderr)(const char*, int); void(*_stderr)(const char*, int);
unsigned char* (*_import_handler)(const char*, int, int*); unsigned char* (*_import_handler)(const char*, int*);
// for quick access // for quick access
static constexpr Type tp_object=Type(0), tp_type=Type(1); static constexpr Type tp_object=Type(0), tp_type=Type(1);

View File

@ -37,11 +37,10 @@ static size_t io_fread(void* buffer, size_t size, size_t count, FILE* fp){
#endif #endif
} }
unsigned char* _default_import_handler(const char* name_p, int name_size, int* out_size){ unsigned char* _default_import_handler(const char* name, int* out_size){
std::string name(name_p, name_size);
bool exists = std::filesystem::exists(std::filesystem::path(name)); bool exists = std::filesystem::exists(std::filesystem::path(name));
if(!exists) return nullptr; if(!exists) return nullptr;
FILE* fp = io_fopen(name.c_str(), "rb"); FILE* fp = io_fopen(name, "rb");
if(!fp) return nullptr; if(!fp) return nullptr;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
int buffer_size = ftell(fp); int buffer_size = ftell(fp);
@ -243,7 +242,7 @@ void add_module_os(VM* vm){
void add_module_io(VM* vm){} void add_module_io(VM* vm){}
void add_module_os(VM* vm){} void add_module_os(VM* vm){}
unsigned char* _default_import_handler(const char* name_p, int name_size, int* out_size){ unsigned char* _default_import_handler(const char* name, int* out_size){
return nullptr; return nullptr;
} }

View File

@ -79,7 +79,7 @@ namespace pkpy{
_stderr = [](const char* buf, int size) { std::cerr.write(buf, size); }; _stderr = [](const char* buf, int size) { std::cerr.write(buf, size); };
_main = nullptr; _main = nullptr;
__last_exception = nullptr; __last_exception = nullptr;
_import_handler = [](const char* name_p, int name_size, int* out_size) -> unsigned char*{ return nullptr; }; _import_handler = [](const char* name, int* out_size) -> unsigned char*{ return nullptr; };
__init_builtin_types(); __init_builtin_types();
} }
@ -358,11 +358,11 @@ namespace pkpy{
auto it = _lazy_modules.find(name); auto it = _lazy_modules.find(name);
if(it == _lazy_modules.end()){ if(it == _lazy_modules.end()){
int out_size; int out_size;
unsigned char* out = _import_handler(filename.data, filename.size, &out_size); unsigned char* out = _import_handler(filename.c_str(), &out_size);
if(out == nullptr){ if(out == nullptr){
filename = path.replace('.', PK_PLATFORM_SEP).str() + PK_PLATFORM_SEP + "__init__.py"; filename = path.replace('.', PK_PLATFORM_SEP).str() + PK_PLATFORM_SEP + "__init__.py";
is_init = true; is_init = true;
out = _import_handler(filename.data, filename.size, &out_size); out = _import_handler(filename.c_str(), &out_size);
} }
if(out == nullptr){ if(out == nullptr){
if(throw_err) ImportError(_S("module ", path.escape(), " not found")); if(throw_err) ImportError(_S("module ", path.escape(), " not found"));