mirror of
https://github.com/pocketpy/pocketpy
synced 2026-02-04 06:30:17 +00:00
add py_compilefile and --compile flag
This commit is contained in:
parent
00c44a9a43
commit
5d3ff2076d
@ -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();
|
||||
|
||||
@ -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).
|
||||
|
||||
@ -1 +0,0 @@
|
||||
def compile(file: str, cfile: str) -> None: ...
|
||||
@ -2,12 +2,7 @@
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
#include <errno.h>
|
||||
|
||||
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);
|
||||
}
|
||||
@ -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
|
||||
|
||||
30
src2/main.c
30
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");
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user