3 Create a C file for embedding one or more Cython source files.
4 Requires Cython 0.11.2 (or perhaps newer).
6 See Demos/freeze/README.txt for more details.
11 usage= '%prog [-o outfile] [-p] module [module ...]'
12 description = 'Create a C file for embedding Cython modules.'
13 p = optparse.OptionParser(usage=usage, description=description)
14 p.add_option('-o', '--output', metavar='FILE',
15 help='write output to FILE instead of standard output')
16 p.add_option('-p', '--pymain', action='store_true', default=False,
17 help='do not automatically run the first module as __main__')
19 options, args = p.parse_args()
27 old_stdout = sys.stdout
28 sys.stdout = open(options.output, 'w')
30 def format_modname(name):
31 if name.endswith('.pyx'):
33 elif name.endswith('.py'):
35 return name.replace('.','_')
37 modules = [format_modname(x) for x in args]
46 #include <floatingpoint.h>
49 #if PY_MAJOR_VERSION < 3
50 # define MODINIT(name) init ## name
52 # define MODINIT(name) PyInit_ ## name
57 print "PyMODINIT_FUNC MODINIT(%s) (void);" % name
60 static struct _inittab inittab[] = {"""
63 print ' {"%(name)s", MODINIT(%(name)s)},' % {'name' : name}
65 print """ {NULL, NULL}
69 if not options.pymain:
70 print "\nextern int __pyx_module_is_main_%s;" % modules[0]
73 #if PY_MAJOR_VERSION < 3
74 int main(int argc, char** argv) {
75 #elif defined(WIN32) || defined(MS_WINDOWS)
76 int wmain(int argc, wchar_t **argv) {
78 static int python_main(int argc, wchar_t **argv) {
81 if not options.pymain:
87 /* 754 requires that FP exceptions run in "no stop" mode by default,
88 * and until C vendors implement C99's ways to control FP exceptions,
89 * Python requires non-stop mode. Alas, some platforms enable FP
90 * exceptions by default. Here we disable them.
96 fpsetmask(m & ~FP_X_OFL);
98 if (PyImport_ExtendInittab(inittab)) {
99 fprintf(stderr, "No memory\\n");
105 return Py_Main(argc, argv);
110 Py_SetProgramName(argv[0]);
112 PySys_SetArgv(argc, argv);
113 __pyx_module_is_main_%(main)s = 1;
114 m = PyImport_ImportModule(inittab[0].name);
117 PyErr_Print(); /* This exits with the right code if SystemExit. */
118 #if PY_MAJOR_VERSION < 3
127 """ % {'main' : modules[0]},
130 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
132 char2wchar(char* arg)
135 #ifdef HAVE_BROKEN_MBSTOWCS
136 /* Some platforms have a broken implementation of
137 * mbstowcs which does not count the characters that
138 * would result from conversion. Use an upper bound.
140 size_t argsize = strlen(arg);
142 size_t argsize = mbstowcs(NULL, arg, 0);
150 if (argsize != (size_t)-1) {
151 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
154 count = mbstowcs(res, arg, argsize+1);
155 if (count != (size_t)-1) {
157 /* Only use the result if it contains no
158 surrogate characters. */
159 for (tmp = res; *tmp != 0 &&
160 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
167 /* Conversion failed. Fall back to escaping with surrogateescape. */
169 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
171 /* Overallocate; as multi-byte characters are in the argument, the
172 actual output could use less memory. */
173 argsize = strlen(arg) + 1;
174 res = malloc(argsize*sizeof(wchar_t));
176 in = (unsigned char*)arg;
178 memset(&mbs, 0, sizeof mbs);
180 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
182 /* Reached end of string; null char stored. */
184 if (converted == (size_t)-2) {
185 /* Incomplete character. This should never happen,
186 since we provide everything that we have -
187 unless there is a bug in the C library, or I
188 misunderstood how mbrtowc works. */
189 fprintf(stderr, "unexpected mbrtowc result -2\n");
192 if (converted == (size_t)-1) {
193 /* Conversion error. Escape as UTF-8b, and start over
194 in the initial shift state. */
195 *out++ = 0xdc00 + *in++;
197 memset(&mbs, 0, sizeof mbs);
200 if (*out >= 0xd800 && *out <= 0xdfff) {
201 /* Surrogate character. Escape the original
202 byte sequence with surrogateescape. */
203 argsize -= converted;
205 *out++ = 0xdc00 + *in++;
208 /* successfully converted some bytes */
210 argsize -= converted;
214 /* Cannot use C locale for escaping; manually escape as if charset
215 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
216 correctly in the locale's charset, which must be an ASCII superset. */
217 res = malloc((strlen(arg)+1)*sizeof(wchar_t));
219 in = (unsigned char*)arg;
225 *out++ = 0xdc00 + *in++;
230 fprintf(stderr, "out of memory\n");
235 main(int argc, char **argv)
237 wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
238 /* We need a second copies, as Python might modify the first one. */
239 wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
242 if (!argv_copy || !argv_copy2) {
243 fprintf(stderr, "out of memory\n");
246 oldloc = strdup(setlocale(LC_ALL, NULL));
247 setlocale(LC_ALL, "");
248 for (i = 0; i < argc; i++) {
249 argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]);
253 setlocale(LC_ALL, oldloc);
255 res = python_main(argc, argv_copy);
256 for (i = 0; i < argc; i++) {