diff --git a/include/pocketpy/interpreter/modules.h b/include/pocketpy/interpreter/modules.h index 6054cce3..ed7f33d5 100644 --- a/include/pocketpy/interpreter/modules.h +++ b/include/pocketpy/interpreter/modules.h @@ -18,7 +18,6 @@ void pk__add_module_pickle(); void pk__add_module_base64(); void pk__add_module_importlib(); void pk__add_module_unicodedata(); -void pk__add_module_py_compile(); void pk__add_module_stdc(); void pk__add_module_vmath(); diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 87051c0a..ebf3a0b0 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -180,6 +180,9 @@ PK_API bool py_compile(const char* source, const char* filename, enum py_CompileMode mode, 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. /// @param source source string. /// @param filename filename (for error messages). diff --git a/include/typings/py_compile.pyi b/include/typings/py_compile.pyi deleted file mode 100644 index 85d5abba..00000000 --- a/include/typings/py_compile.pyi +++ /dev/null @@ -1 +0,0 @@ -def compile(file: str, cfile: str) -> None: ... diff --git a/src/modules/py_compile.c b/src/interpreter/py_compile.c similarity index 72% rename from src/modules/py_compile.c rename to src/interpreter/py_compile.c index 56d97621..bfecedb9 100644 --- a/src/modules/py_compile.c +++ b/src/interpreter/py_compile.c @@ -2,12 +2,7 @@ #include "pocketpy/interpreter/vm.h" #include -static bool py_compile_compile(int argc, py_Ref argv) { - 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)); +bool py_compilefile(const char* src_path, const char* dst_path) { // read FILE* fp = fopen(src_path, "rb"); if(fp == NULL) { @@ -40,12 +35,5 @@ static bool py_compile_compile(int argc, py_Ref argv) { fwrite(bc_data, 1, bc_size, fp); fclose(fp); PK_FREE(bc_data); - py_newnone(py_retval()); return true; } - -void pk__add_module_py_compile() { - py_Ref mod = py_newmodule("py_compile"); - - py_bindfunc(mod, "compile", py_compile_compile); -} \ No newline at end of file diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index e64e3edd..89e38ea8 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -260,7 +260,6 @@ void VM__ctor(VM* self) { pk__add_module_base64(); pk__add_module_importlib(); pk__add_module_unicodedata(); - pk__add_module_py_compile(); pk__add_module_conio(); pk__add_module_lz4(); // optional diff --git a/src2/main.c b/src2/main.c index d78cdcb5..76daaaa9 100644 --- a/src2/main.c +++ b/src2/main.c @@ -34,7 +34,9 @@ int main(int argc, char** argv) { bool profile = 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++) { if(strcmp(argv[i], "--profile") == 0) { @@ -45,11 +47,19 @@ int main(int argc, char** argv) { debug = true; continue; } - if(filename == NULL) { - filename = argv[i]; + if(strcmp(argv[i], "--compile") == 0) { + compile = true; 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) { @@ -57,9 +67,21 @@ int main(int argc, char** argv) { return 1; } + if(compile && (debug || profile)) { + printf("Error: --compile cannot be used with --debug or --profile.\n"); + return 1; + } + py_initialize(); 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(profile) printf("Warning: --profile is ignored in REPL mode.\n"); if(debug) printf("Warning: --debug is ignored in REPL mode.\n"); diff --git a/tests/922_py_compile.py b/tests/922_py_compile.py index 2dd76616..c83fddc8 100644 --- a/tests/922_py_compile.py +++ b/tests/922_py_compile.py @@ -1,12 +1,10 @@ -from py_compile import compile - try: import os except ImportError: print('os is not enabled, skipping test...') 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') import heapq1