add py_compilefile and --compile flag

This commit is contained in:
blueloveTH 2026-01-05 19:17:12 +08:00
parent 00c44a9a43
commit 5d3ff2076d
7 changed files with 31 additions and 23 deletions

View File

@ -18,7 +18,6 @@ void pk__add_module_pickle();
void pk__add_module_base64(); void pk__add_module_base64();
void pk__add_module_importlib(); void pk__add_module_importlib();
void pk__add_module_unicodedata(); void pk__add_module_unicodedata();
void pk__add_module_py_compile();
void pk__add_module_stdc(); void pk__add_module_stdc();
void pk__add_module_vmath(); void pk__add_module_vmath();

View File

@ -180,6 +180,9 @@ PK_API bool py_compile(const char* source,
const char* filename, const char* filename,
enum py_CompileMode mode, enum py_CompileMode mode,
bool is_dynamic) PY_RAISE PY_RETURN; bool is_dynamic) PY_RAISE PY_RETURN;
/// Compile a `.py` file into a `.pyc` file.
PK_API bool py_compilefile(const char* src_path,
const char* dst_path) PY_RAISE;
/// Run a source string. /// Run a source string.
/// @param source source string. /// @param source source string.
/// @param filename filename (for error messages). /// @param filename filename (for error messages).

View File

@ -1 +0,0 @@
def compile(file: str, cfile: str) -> None: ...

View File

@ -2,12 +2,7 @@
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
#include <errno.h> #include <errno.h>
static bool py_compile_compile(int argc, py_Ref argv) { bool py_compilefile(const char* src_path, const char* dst_path) {
PY_CHECK_ARGC(2);
PY_CHECK_ARG_TYPE(0, tp_str);
PY_CHECK_ARG_TYPE(1, tp_str);
const char* src_path = py_tostr(py_arg(0));
const char* dst_path = py_tostr(py_arg(1));
// read // read
FILE* fp = fopen(src_path, "rb"); FILE* fp = fopen(src_path, "rb");
if(fp == NULL) { if(fp == NULL) {
@ -40,12 +35,5 @@ static bool py_compile_compile(int argc, py_Ref argv) {
fwrite(bc_data, 1, bc_size, fp); fwrite(bc_data, 1, bc_size, fp);
fclose(fp); fclose(fp);
PK_FREE(bc_data); PK_FREE(bc_data);
py_newnone(py_retval());
return true; return true;
} }
void pk__add_module_py_compile() {
py_Ref mod = py_newmodule("py_compile");
py_bindfunc(mod, "compile", py_compile_compile);
}

View File

@ -260,7 +260,6 @@ void VM__ctor(VM* self) {
pk__add_module_base64(); pk__add_module_base64();
pk__add_module_importlib(); pk__add_module_importlib();
pk__add_module_unicodedata(); pk__add_module_unicodedata();
pk__add_module_py_compile();
pk__add_module_conio(); pk__add_module_conio();
pk__add_module_lz4(); // optional pk__add_module_lz4(); // optional

View File

@ -34,7 +34,9 @@ int main(int argc, char** argv) {
bool profile = false; bool profile = false;
bool debug = false; bool debug = false;
const char* filename = NULL; bool compile = false;
const char* arg1 = NULL;
const char* arg2 = NULL;
for(int i = 1; i < argc; i++) { for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "--profile") == 0) { if(strcmp(argv[i], "--profile") == 0) {
@ -45,11 +47,19 @@ int main(int argc, char** argv) {
debug = true; debug = true;
continue; continue;
} }
if(filename == NULL) { if(strcmp(argv[i], "--compile") == 0) {
filename = argv[i]; compile = true;
continue; continue;
} }
printf("Usage: pocketpy [--profile] [--debug] filename\n"); if(arg1 == NULL) {
arg1 = argv[i];
continue;
}
if(arg2 == NULL) {
arg2 = argv[i];
continue;
}
printf("Usage: pocketpy [--profile] [--debug] [--compile] filename\n");
} }
if(debug && profile) { if(debug && profile) {
@ -57,9 +67,21 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
if(compile && (debug || profile)) {
printf("Error: --compile cannot be used with --debug or --profile.\n");
return 1;
}
py_initialize(); py_initialize();
py_sys_setargv(argc, argv); py_sys_setargv(argc, argv);
if(compile) {
bool ok = py_compilefile(arg1, arg2);
py_finalize();
return ok ? 0 : 1;
}
const char* filename = arg1;
if(filename == NULL) { if(filename == NULL) {
if(profile) printf("Warning: --profile is ignored in REPL mode.\n"); if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
if(debug) printf("Warning: --debug is ignored in REPL mode.\n"); if(debug) printf("Warning: --debug is ignored in REPL mode.\n");

View File

@ -1,12 +1,10 @@
from py_compile import compile
try: try:
import os import os
except ImportError: except ImportError:
print('os is not enabled, skipping test...') print('os is not enabled, skipping test...')
exit(0) exit(0)
compile('python/heapq.py', 'heapq1.pyc') assert os.system('./main --compile python/heapq.py heapq1.pyc') == 0
assert os.path.exists('heapq1.pyc') assert os.path.exists('heapq1.pyc')
import heapq1 import heapq1