Skip to content

Commit 1ee3ce7

Browse files
committed
Support PY_MAIN(:main) in python3
DEVTOOLSSUPPORT-1753 ref:5b3e2df0023ddfd79b969261f07fca8670c22b11
1 parent 4b2d43e commit 1ee3ce7

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <library/cpp/resource/resource.h>
2+
3+
#include <stdlib.h>
4+
5+
extern "C" char* GetPyMain() {
6+
TString res = NResource::Find("PY_MAIN");
7+
return strdup(res.c_str());
8+
}

library/python/runtime_py3/main/main.c

+20-35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <locale.h>
66

77
void Py_InitArgcArgv(int argc, wchar_t **argv);
8+
char* GetPyMain();
89

910
static const char* env_entry_point = "Y_PYTHON_ENTRY_POINT";
1011

@@ -109,9 +110,26 @@ static int pymain(int argc, char** argv) {
109110
oldloc = NULL;
110111

111112
const char* entry_point = getenv(env_entry_point);
112-
if (entry_point && !strcmp(entry_point, ":main")) {
113+
if (entry_point) {
114+
entry_point_copy = strdup(entry_point);
115+
if (!entry_point_copy) {
116+
fprintf(stderr, "out of memory\n");
117+
goto error;
118+
}
119+
} else {
120+
entry_point_copy = GetPyMain();
121+
}
122+
123+
if (entry_point_copy == NULL) {
124+
fprintf(stderr, "No entry point, did you forget PY_MAIN?\n");
125+
goto error;
126+
}
127+
128+
if (entry_point_copy && !strcmp(entry_point_copy, ":main")) {
113129
unsetenv(env_entry_point);
114-
return Py_Main(argc, argv_copy);
130+
sts = Py_Main(argc, argv_copy);
131+
free(entry_point_copy);
132+
return sts;
115133
}
116134

117135
Py_InitArgcArgv(argc, argv_copy);
@@ -121,8 +139,6 @@ static int pymain(int argc, char** argv) {
121139

122140
PySys_SetArgv(argc, argv_copy);
123141

124-
PyObject* py_main = NULL;
125-
126142
{
127143
PyObject* module = PyImport_ImportModule("library.python.runtime_py3.entry_points");
128144
if (module == NULL) {
@@ -138,37 +154,6 @@ static int pymain(int argc, char** argv) {
138154
}
139155
}
140156

141-
if (entry_point == NULL) {
142-
PyObject* res = PyImport_ImportModule("__res");
143-
if (res == NULL) {
144-
PyErr_Clear();
145-
} else {
146-
py_main = PyObject_CallMethod(res, "find", "y", "PY_MAIN");
147-
148-
if (py_main == NULL) {
149-
PyErr_Clear();
150-
} else {
151-
if (PyBytes_Check(py_main)) {
152-
entry_point = PyBytes_AsString(py_main);
153-
}
154-
}
155-
156-
Py_DECREF(res);
157-
}
158-
}
159-
160-
if (entry_point == NULL) {
161-
fprintf(stderr, "No entry point, did you forget PY_MAIN?\n");
162-
goto error;
163-
}
164-
165-
entry_point_copy = strdup(entry_point);
166-
Py_XDECREF(py_main);
167-
if (!entry_point_copy) {
168-
fprintf(stderr, "out of memory\n");
169-
goto error;
170-
}
171-
172157
const char* module_name = entry_point_copy;
173158
const char* func_name = NULL;
174159

library/python/runtime_py3/main/ya.make

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ LIBRARY()
44

55
PEERDIR(
66
contrib/tools/python3/src
7+
library/cpp/resource
78
)
89

910
ADDINCL(
@@ -12,6 +13,7 @@ ADDINCL(
1213

1314
SRCS(
1415
main.c
16+
get_py_main.cpp
1517
)
1618

1719
END()

0 commit comments

Comments
 (0)