Skip to content

Commit 5c25cec

Browse files
authored
[mypyc] Move the shim_template to a file (#7940)
The point of this is that the file can now be used as a bazel template.
1 parent c6c99b9 commit 5c25cec

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ recursive-include extensions *
44
recursive-include docs *
55
recursive-include mypy/typeshed *.py *.pyi
66
recursive-include mypy/xml *.xsd *.xslt *.css
7-
recursive-include mypyc/lib-rt *.c *.h
7+
recursive-include mypyc/lib-rt *.c *.h *.tmpl
88
include mypy_bootstrap.ini
99
include mypy_self_check.ini
1010
include LICENSE

mypyc/build.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,6 @@ def get_mypy_config(mypy_options: List[str],
116116
return mypyc_sources, all_sources, options
117117

118118

119-
shim_template = """\
120-
#include <Python.h>
121-
122-
PyMODINIT_FUNC
123-
PyInit_{modname}(void)
124-
{{
125-
if (!PyImport_ImportModule("{libname}")) return NULL;
126-
void *init_func = PyCapsule_Import("{libname}.init_{full_modname}", 0);
127-
if (!init_func) {{
128-
return NULL;
129-
}}
130-
return ((PyObject *(*)(void))init_func)();
131-
}}
132-
133-
// distutils sometimes spuriously tells cl to export CPyInit___init__,
134-
// so provide that so it chills out
135-
PyMODINIT_FUNC PyInit___init__(void) {{ return PyInit_{modname}(); }}
136-
"""
137-
138-
139119
def generate_c_extension_shim(
140120
full_module_name: str, module_name: str, dir_name: str, group_name: str) -> str:
141121
"""Create a C extension shim with a passthrough PyInit function.
@@ -149,6 +129,11 @@ def generate_c_extension_shim(
149129
cname = '%s.c' % exported_name(full_module_name)
150130
cpath = os.path.join(dir_name, cname)
151131

132+
# We load the C extension shim template from a file.
133+
# (So that the file could be reused as a bazel template also.)
134+
with open(os.path.join(include_dir(), 'module_shim.tmpl')) as f:
135+
shim_template = f.read()
136+
152137
write_file(
153138
cpath,
154139
shim_template.format(modname=module_name,

mypyc/lib-rt/module_shim.tmpl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <Python.h>
2+
3+
PyMODINIT_FUNC
4+
PyInit_{modname}(void)
5+
{{
6+
PyObject *tmp;
7+
if (!(tmp = PyImport_ImportModule("{libname}"))) return NULL;
8+
Py_DECREF(tmp);
9+
void *init_func = PyCapsule_Import("{libname}.init_{full_modname}", 0);
10+
if (!init_func) {{
11+
return NULL;
12+
}}
13+
return ((PyObject *(*)(void))init_func)();
14+
}}
15+
16+
// distutils sometimes spuriously tells cl to export CPyInit___init__,
17+
// so provide that so it chills out
18+
PyMODINIT_FUNC PyInit___init__(void) {{ return PyInit_{modname}(); }}

0 commit comments

Comments
 (0)