From 620c1d28820291532351c9ddaf1da236a7e6a99a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 4 May 2024 20:04:15 +0800 Subject: [PATCH] change `_import_handler` --- include/pocketpy/config.h | 2 -- include/pocketpy/io.h | 2 +- include/pocketpy/pocketpy_c.h | 2 +- include/pocketpy/vm.h | 2 +- src/io.cpp | 7 +++---- src/vm.cpp | 6 +++--- 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 9a3570bd..44b1c388 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -36,8 +36,6 @@ #define PK_DEBUG_EXTRA_CHECK 0 // 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_MEMORY_POOL 0 #define PK_DEBUG_NO_MEMORY_POOL 0 diff --git a/include/pocketpy/io.h b/include/pocketpy/io.h index 2ef3d74f..675c6cbc 100644 --- a/include/pocketpy/io.h +++ b/include/pocketpy/io.h @@ -3,7 +3,7 @@ #include "bindings.h" 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_io(VM* vm); } diff --git a/include/pocketpy/pocketpy_c.h b/include/pocketpy/pocketpy_c.h index f5c8a000..a089b525 100644 --- a/include/pocketpy/pocketpy_c.h +++ b/include/pocketpy/pocketpy_c.h @@ -13,7 +13,7 @@ extern "C" { typedef struct pkpy_vm_handle pkpy_vm; typedef int (*pkpy_CFunction)(pkpy_vm*); 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_CType; typedef const char* pkpy_CString; diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 85e32946..56f959b1 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -167,7 +167,7 @@ public: void(*_stdout)(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 static constexpr Type tp_object=Type(0), tp_type=Type(1); diff --git a/src/io.cpp b/src/io.cpp index a35829ae..f29d9135 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -37,11 +37,10 @@ static size_t io_fread(void* buffer, size_t size, size_t count, FILE* fp){ #endif } -unsigned char* _default_import_handler(const char* name_p, int name_size, int* out_size){ - std::string name(name_p, name_size); +unsigned char* _default_import_handler(const char* name, int* out_size){ bool exists = std::filesystem::exists(std::filesystem::path(name)); if(!exists) return nullptr; - FILE* fp = io_fopen(name.c_str(), "rb"); + FILE* fp = io_fopen(name, "rb"); if(!fp) return nullptr; fseek(fp, 0, SEEK_END); int buffer_size = ftell(fp); @@ -243,7 +242,7 @@ void add_module_os(VM* vm){ void add_module_io(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; } diff --git a/src/vm.cpp b/src/vm.cpp index 9d1c4801..7150c758 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -79,7 +79,7 @@ namespace pkpy{ _stderr = [](const char* buf, int size) { std::cerr.write(buf, size); }; _main = 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(); } @@ -358,11 +358,11 @@ namespace pkpy{ auto it = _lazy_modules.find(name); if(it == _lazy_modules.end()){ 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){ filename = path.replace('.', PK_PLATFORM_SEP).str() + PK_PLATFORM_SEP + "__init__.py"; is_init = true; - out = _import_handler(filename.data, filename.size, &out_size); + out = _import_handler(filename.c_str(), &out_size); } if(out == nullptr){ if(throw_err) ImportError(_S("module ", path.escape(), " not found"));