| 34 | } |
| 35 | |
| 36 | int main(int argc, char** argv) { |
| 37 | if (argc != 6) { |
| 38 | Cerr << "Usage:\n\t" << argv[0] << " --slow-py3cc <slow-py3cc> SRC_PATH_X- SRC OUT" << Endl; |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | PyConfig cfg{}; |
| 43 | PyConfig_InitIsolatedConfig(&cfg); |
| 44 | cfg._install_importlib = 0; |
| 45 | Y_SCOPE_EXIT(&cfg) {PyConfig_Clear(&cfg);}; |
| 46 | |
| 47 | const char* slowPy3cc{argv[2]}; |
| 48 | TString srcpath{argv[3]}; |
| 49 | srcpath.pop_back(); |
| 50 | const TFsPath inPath{argv[4]}; |
| 51 | const char* outPath = argv[5]; |
| 52 | |
| 53 | const auto status = Py_InitializeFromConfig(&cfg); |
| 54 | if (PyStatus_Exception(status)) { |
| 55 | Py_ExitStatusException(status); |
| 56 | } |
| 57 | Y_SCOPE_EXIT() {Py_Finalize();}; |
| 58 | |
| 59 | TPyObject bytecode{Py_CompileString( |
| 60 | TFileInput{inPath}.ReadAll().c_str(), |
| 61 | srcpath.c_str(), |
| 62 | Py_file_input |
| 63 | )}; |
| 64 | if (!bytecode) { |
| 65 | int rc = runSlowPy3cc(slowPy3cc, argv[3], argv[4], argv[5]); |
| 66 | return rc; |
| 67 | } |
| 68 | |
| 69 | if (FILE* out = fopen(outPath, "wb")) { |
| 70 | PyMarshal_WriteObjectToFile(bytecode.Get(), out, Py_MARSHAL_VERSION); |
| 71 | fclose(out); |
| 72 | if (PyErr_Occurred()) { |
| 73 | Cerr << "Failed to marshal " << outPath << Endl; |
| 74 | PyErr_Print(); |
| 75 | return 1; |
| 76 | } |
| 77 | } else { |
| 78 | Cerr << "Failed to write " << outPath << ": " << std::error_code{errno, std::system_category()}.message() << Endl; |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |