13# include <sys/sysctl.h>
17# include <mach-o/dyld.h>
26#if PY_MAJOR_VERSION >= 3
29# if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 5
30# define Py_DecodeLocale _Py_char2wchar
33# include "structmember.h"
38#define MAX_NUM_POINTERS 24
43 F_log_filename_strftime = 2,
44 F_keep_docstrings = 4,
51__attribute__((__visibility__(
"default"), used))
57 uint16_t num_pointers;
61 void *pointers[MAX_NUM_POINTERS];
65} blobinfo = {(uint64_t)-1};
70__declspec(dllexport) DWORD SymbolPlaceholder___________________ = 0x00000001;
71__declspec(dllexport) DWORD SymbolPlaceholder__ = 0x00000001;
75# define WIN32_LEAN_AND_MEAN
78extern void PyWinFreeze_ExeInit(
void);
79extern void PyWinFreeze_ExeTerm(
void);
81static struct _inittab extensions[] = {
85#if PY_MAJOR_VERSION >= 3
91static wchar_t *log_pathw = NULL;
94#if PY_VERSION_HEX >= 0x030b0000
97 const unsigned char *code;
101typedef struct _frozen ModuleDef;
104#if defined(_WIN32) && PY_VERSION_HEX < 0x03060000
105static int supports_code_page(UINT cp) {
112 if (cp != 0 && cp != 1252 && cp != 367 && cp != 437 && cp != 850 && cp != 819) {
113 const struct _frozen *moddef;
118 PyOS_snprintf(codec,
sizeof(codec),
"encodings.cp%u", (
unsigned int)cp);
120 moddef = PyImport_FrozenModules;
121 while (moddef->name) {
122 if (strcmp(moddef->name, codec) == 0) {
138static void set_main_dir(
char *main_dir) {
139 if (blobinfo.num_pointers >= 10) {
140 if (blobinfo.num_pointers == 10) {
141 ++blobinfo.num_pointers;
142 blobinfo.pointers[10] = NULL;
144 if (blobinfo.pointers[10] == NULL) {
145 blobinfo.pointers[10] = main_dir;
154static int mkdir_parent(
const wchar_t *path) {
156 wchar_t buffer[4096];
157 size_t buflen = wcslen(path);
158 if (buflen + 1 >= _countof(buffer)) {
161 wcscpy_s(buffer, _countof(buffer), path);
164 while (buflen-- > 0) {
165 if (buffer[buflen] ==
'/' || buffer[buflen] ==
'\\') {
170 if (buflen == (
size_t)-1 || buflen == 0) {
175 if (CreateDirectoryW(buffer, NULL) != 0) {
181 DWORD last_error = GetLastError();
182 if (last_error == ERROR_ALREADY_EXISTS) {
187 if (last_error == ERROR_PATH_NOT_FOUND) {
189 if (mkdir_parent(buffer)) {
191 if (CreateDirectoryW(buffer, NULL) != 0) {
200static int mkdir_parent(
const char *path) {
203 size_t buflen = strlen(path);
204 if (buflen + 1 >=
sizeof(buffer)) {
207 strcpy(buffer, path);
210 while (buflen-- > 0) {
211 if (buffer[buflen] ==
'/') {
216 if (buflen == (
size_t)-1 || buflen == 0) {
220 if (mkdir(buffer, 0755) == 0) {
226 if (errno == EEXIST) {
231 if (errno == ENOENT || errno == EACCES) {
233 if (mkdir_parent(buffer)) {
235 if (mkdir(buffer, 0755) == 0) {
251static int setup_logging(
const char *path,
int append) {
254 wchar_t *pathw = (
wchar_t *)malloc(
sizeof(
wchar_t) * MAX_PATH);
257 if (path[0] ==
'~' && (path[1] == 0 || path[1] ==
'/' || path[1] ==
'\\')) {
262 if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, pathw))) {
266 offset = wcslen(pathw);
270 if (MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw + offset,
271 (
int)(MAX_PATH - offset)) == 0) {
276 DWORD access = append ? FILE_APPEND_DATA : (GENERIC_READ | GENERIC_WRITE);
277 int creation = append ? OPEN_ALWAYS : CREATE_ALWAYS;
278 HANDLE handle = CreateFileW(pathw, access, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
279 NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
281 if (handle == INVALID_HANDLE_VALUE) {
284 handle = CreateFileW(pathw, access, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
285 NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
288 if (handle == INVALID_HANDLE_VALUE) {
296 SetFilePointer(handle, 0, NULL, FILE_END);
299 SetStdHandle(STD_OUTPUT_HANDLE, handle);
300 SetStdHandle(STD_ERROR_HANDLE, handle);
305 if (_fileno(stdout) < 0) {
307 _wfreopen(L
"\\\\.\\NUL", L
"w", stdout);
310 if (_fileno(stderr) < 0) {
312 _wfreopen(L
"\\\\.\\NUL", L
"w", stderr);
317 int fd = _open_osfhandle((intptr_t)handle, _O_WRONLY | _O_TEXT | _O_APPEND);
318 _dup2(fd, _fileno(stdout));
319 _dup2(fd, _fileno(stderr));
325 char buffer[PATH_MAX * 2];
327 if (path[0] ==
'~' && (path[1] == 0 || path[1] ==
'/')) {
332 const char *home_dir = getenv(
"HOME");
333 if (home_dir == NULL) {
334 home_dir = getpwuid(getuid())->pw_dir;
336 offset = strlen(home_dir);
337 assert(offset <
sizeof(buffer));
338 strncpy(buffer, home_dir,
sizeof(buffer));
342 strcpy(buffer + offset, path);
344 mode_t mode = O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC);
345 int fd = open(buffer, mode, 0644);
348 mkdir_parent(buffer);
349 fd = open(buffer, mode, 0644);
364 perror(
"setup_logging: close");
373#if PY_MAJOR_VERSION >= 3
374static int enable_line_buffering(PyObject *file) {
375#if PY_VERSION_HEX >= 0x03070000
377 PyObject *kwargs = _PyDict_NewPresized(1);
378 PyDict_SetItemString(kwargs,
"line_buffering", Py_True);
379 PyObject *args = PyTuple_New(0);
381 PyObject *method = PyObject_GetAttrString(file,
"reconfigure");
382 if (method != NULL) {
383 PyObject *result = PyObject_Call(method, args, kwargs);
387 if (result != NULL) {
404 PyTypeObject *type = Py_TYPE(file);
405 PyMemberDef *member = type->tp_members;
407 while (member != NULL && member->name != NULL) {
408 if (strcmp(member->name,
"line_buffering") == 0) {
409 *((
char *)file + member->offset) = 1;
423int Py_FrozenMain(
int argc,
wchar_t **argv)
425int Py_FrozenMain(
int argc,
char **argv)
435#if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
438 wchar_t **argv_copy = NULL;
440 wchar_t **argv_copy2 = NULL;
443 argv_copy = (
wchar_t **)alloca(
sizeof(
wchar_t *) * argc);
444 argv_copy2 = (
wchar_t **)alloca(
sizeof(
wchar_t *) * argc);
448#if defined(MS_WINDOWS) && PY_VERSION_HEX >= 0x03040000 && PY_VERSION_HEX < 0x03060000
449 if (!supports_code_page(GetConsoleOutputCP()) ||
450 !supports_code_page(GetConsoleCP())) {
456 SetConsoleOutputCP(acp);
457 Py_SetStandardStreamEncoding(
"mbcs", NULL);
463 Py_NoUserSiteDirectory = 1;
465#if PY_VERSION_HEX >= 0x03020000
466 if (blobinfo.flags & F_keep_docstrings) {
474 if ((p = Py_GETENV(
"PYTHONINSPECT")) && *p !=
'\0')
477 if ((p = Py_GETENV(
"PYTHONUNBUFFERED")) && *p !=
'\0')
481 setbuf(stdin, (
char *)NULL);
482 setbuf(stdout, (
char *)NULL);
483 setbuf(stderr, (
char *)NULL);
486#if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
487 oldloc = setlocale(LC_ALL, NULL);
488 setlocale(LC_ALL,
"");
489 for (i = 0; i < argc; i++) {
490 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
491 argv_copy2[i] = argv_copy[i];
493 fprintf(stderr,
"Unable to decode the command line argument #%i\n",
499 setlocale(LC_ALL, oldloc);
503 PyImport_ExtendInittab(extensions);
507#if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
508 Py_SetProgramName(argv_copy[0]);
510 Py_SetProgramName(argv[0]);
516 PyWinFreeze_ExeInit();
519#if defined(MS_WINDOWS) && PY_VERSION_HEX < 0x03040000
526#if PY_MAJOR_VERSION < 3
527 if (log_pathw != NULL) {
528 PyObject *uniobj = PyUnicode_FromWideChar(log_pathw, (Py_ssize_t)wcslen(log_pathw));
529 PyObject *file = PyObject_CallFunction((PyObject*)&PyFile_Type,
"Nsi", uniobj,
"a", 0);
532 PyFile_SetEncodingAndErrors(file,
"utf-8", NULL);
534 PySys_SetObject(
"stdout", file);
535 PySys_SetObject(
"stderr", file);
536 PySys_SetObject(
"__stdout__", file);
537 PySys_SetObject(
"__stderr__", file);
540 setbuf(stdout, (
char *)NULL);
541 setbuf(stderr, (
char *)NULL);
546 if (!supports_code_page(GetConsoleOutputCP()) ||
547 !supports_code_page(GetConsoleCP())) {
552 PyObject *sys_stream;
555 SetConsoleOutputCP(acp);
557 sys_stream = PySys_GetObject(
"stdin");
558 if (sys_stream && PyFile_Check(sys_stream)) {
559 PyFile_SetEncodingAndErrors(sys_stream,
"mbcs", NULL);
561 sys_stream = PySys_GetObject(
"stdout");
562 if (sys_stream && PyFile_Check(sys_stream)) {
563 PyFile_SetEncodingAndErrors(sys_stream,
"mbcs", NULL);
565 sys_stream = PySys_GetObject(
"stderr");
566 if (sys_stream && PyFile_Check(sys_stream)) {
567 PyFile_SetEncodingAndErrors(sys_stream,
"mbcs", NULL);
572#if defined(MS_WINDOWS) && PY_VERSION_HEX >= 0x03040000
576 PyObject *sys_stream;
577 sys_stream = PySys_GetObject(
"__stdout__");
578 if (sys_stream && !enable_line_buffering(sys_stream)) {
579 fprintf(stderr,
"Failed to enable line buffering on sys.stdout\n");
582 sys_stream = PySys_GetObject(
"__stderr__");
583 if (sys_stream && !enable_line_buffering(sys_stream)) {
584 fprintf(stderr,
"Failed to enable line buffering on sys.stderr\n");
591 fprintf(stderr,
"Python %s\n%s\n",
592 Py_GetVersion(), Py_GetCopyright());
594#if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
595 PySys_SetArgv(argc, argv_copy);
597 PySys_SetArgv(argc, argv);
600#ifdef MACOS_APP_BUNDLE
602 char buffer[PATH_MAX];
603 uint32_t bufsize =
sizeof(buffer);
604 if (_NSGetExecutablePath(buffer, &bufsize) != 0) {
608 char resolved[PATH_MAX];
609 if (!realpath(buffer, resolved)) {
613 const char *dir = dirname(resolved);
614 sprintf(buffer,
"%s/../Frameworks", dir);
616 PyObject *sys_path = PyList_New(1);
617 #if PY_MAJOR_VERSION >= 3
618 PyList_SET_ITEM(sys_path, 0, PyUnicode_FromString(buffer));
620 PyList_SET_ITEM(sys_path, 0, PyString_FromString(buffer));
622 PySys_SetObject(
"path", sys_path);
627 sprintf(buffer,
"%s/../Resources", dir);
628 set_main_dir(buffer);
635 n = PyImport_ImportFrozenModule(
"__main__");
637 Py_FatalError(
"__main__ not frozen");
646 if (inspect && isatty((
int)fileno(stdin)))
647 sts = PyRun_AnyFile(stdin,
"<stdin>") != 0;
651 PyWinFreeze_ExeTerm();
655#if PY_MAJOR_VERSION >= 3 && !defined(WIN_UNICODE)
658 for (i = 0; i < argc; i++) {
659#if PY_MAJOR_VERSION > 3 || PY_MINOR_VERSION >= 4
660 PyMem_RawFree(argv_copy2[i]);
662 PyMem_Free(argv_copy2[i]);
674static void *map_blob(off_t offset,
size_t size) {
679 wchar_t buffer[2048];
680 GetModuleFileNameW(NULL, buffer, 2048);
681 runtime = _wfopen(buffer, L
"rb");
682#elif defined(__FreeBSD__)
683 size_t bufsize = 4096;
685 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
687 if (sysctl(mib, 4, (
void *)buffer, &bufsize, NULL, 0) == -1) {
691 runtime = fopen(buffer,
"rb");
692#elif defined(__APPLE__)
694 uint32_t bufsize =
sizeof(buffer);
695 if (_NSGetExecutablePath(buffer, &bufsize) != 0) {
698 runtime = fopen(buffer,
"rb");
701 ssize_t pathlen = readlink(
"/proc/self/exe", buffer,
sizeof(buffer) - 1);
703 perror(
"readlink(/proc/self/exe)");
706 buffer[pathlen] =
'\0';
707 runtime = fopen(buffer,
"rb");
711 if (blobinfo.version == 0) {
713 fseek(runtime, -8, SEEK_END);
714 end = ftell(runtime);
715 fread(&begin, 8, 1, runtime);
717 offset = (off_t)begin;
718 size = (size_t)(end - begin);
723 blob = (
void *)malloc(size);
724 assert(blob != NULL);
725 fseek(runtime, (
long)offset, SEEK_SET);
726 fread(blob, size, 1, runtime);
728 blob = (
void *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(runtime), offset);
729 assert(blob != MAP_FAILED);
739static void unmap_blob(
void *blob) {
744 munmap(blob, blobinfo.blob_size);
752#if defined(_WIN32) && PY_MAJOR_VERSION >= 3
753int wmain(
int argc,
wchar_t *argv[]) {
755int main(
int argc,
char *argv[]) {
759 const char *log_filename;
765 if (argc > 1 && strncmp(argv[1],
"-psn_", 5) == 0) {
783 if (blobinfo.version == 0 || blobinfo.blob_offset != 0) {
784 void *blob = map_blob((off_t)blobinfo.blob_offset, (
size_t)blobinfo.blob_size);
785 assert(blob != NULL);
788 if (blobinfo.version > 0 && blobinfo.num_pointers > 0) {
790 assert(blobinfo.num_pointers <= MAX_NUM_POINTERS);
791 for (i = 0; i < blobinfo.num_pointers; ++i) {
795 if (i == 0 || blobinfo.pointers[i] != 0) {
796 blobinfo.pointers[i] = (
void *)((uintptr_t)blobinfo.pointers[i] + (uintptr_t)blob);
799 if (blobinfo.num_pointers >= 12) {
800 log_filename = blobinfo.pointers[11];
803 blobinfo.pointers[0] = blob;
807 moddef = blobinfo.pointers[0];
808#if PY_VERSION_HEX < 0x030b0000
809 PyImport_FrozenModules = moddef;
811 while (moddef->name) {
812 moddef->name = (
char *)((uintptr_t)moddef->name + (uintptr_t)blob);
813 if (moddef->code != 0) {
814 moddef->code = (
unsigned char *)((uintptr_t)moddef->code + (uintptr_t)blob);
821#if PY_VERSION_HEX >= 0x030b0000
822 ModuleDef *moddef_end = moddef;
823 ptrdiff_t num_modules = moddef - (ModuleDef *)blobinfo.pointers[0];
824 struct _frozen *new_moddef = (
struct _frozen *)calloc(num_modules + 1,
sizeof(
struct _frozen));
825 PyImport_FrozenModules = new_moddef;
826 for (moddef = blobinfo.pointers[0]; moddef < moddef_end; ++moddef) {
827 new_moddef->name = moddef->name;
828 new_moddef->code = moddef->code;
829 new_moddef->size = moddef->size < 0 ? -(moddef->size) : moddef->size;
830 new_moddef->is_package = moddef->size < 0;
831#if PY_VERSION_HEX < 0x030d0000
832 new_moddef->get_code = NULL;
838 PyImport_FrozenModules = blobinfo.pointers[0];
841 if (log_filename != NULL) {
842 char log_filename_buf[4096];
843 if (blobinfo.flags & F_log_filename_strftime) {
844 log_filename_buf[0] = 0;
845 time_t now = time(NULL);
846 if (strftime(log_filename_buf,
sizeof(log_filename_buf), log_filename, localtime(&now)) > 0) {
847 log_filename = log_filename_buf;
850 setup_logging(log_filename, (blobinfo.flags & F_log_append) != 0);
854 if (blobinfo.codepage != 0) {
855 SetConsoleCP(blobinfo.codepage);
856 SetConsoleOutputCP(blobinfo.codepage);
861 retval = Py_FrozenMain(argc, argv);
866#if PY_VERSION_HEX >= 0x030b0000
867 free((
void *)PyImport_FrozenModules);
868 PyImport_FrozenModules = NULL;
876int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
wchar_t *lpCmdLine,
int nCmdShow) {
877 return wmain(__argc, __wargv);
880int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
char *lpCmdLine,
int nCmdShow) {
881 return main(__argc, __argv);