<|start_filename|>bootloader/src/pyi_python.c<|end_filename|>
/*
 * ****************************************************************************
 * Copyright (c) 2013-2021, PyInstaller Development Team.
 *
 * Distributed under the terms of the GNU General Public License (version 2
 * or later) with exception for distributing the bootloader.
 *
 * The full license is in the file COPYING.txt, distributed with this software.
 *
 * SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 * ****************************************************************************
 */

/*
 * Python.h replacements.
 */

#ifdef _WIN32
    #include <windows.h>  /* HMODULE */
#else
    #include <dlfcn.h>  /* dlsym */
#endif
#include <stddef.h>  /* ptrdiff_t */
#include <stdlib.h>

/* PyInstaller headers. */
#include "pyi_global.h"
#include "pyi_python.h"

/*
 * Python Entry point declarations (see macros in pyi_python.h).
 */
/* external variables */
DECLVAR(Py_DontWriteBytecodeFlag);
DECLVAR(Py_FileSystemDefaultEncoding);
DECLVAR(Py_FrozenFlag);
DECLVAR(Py_IgnoreEnvironmentFlag);
DECLVAR(Py_NoSiteFlag);
DECLVAR(Py_NoUserSiteDirectory);
DECLVAR(Py_OptimizeFlag);
DECLVAR(Py_VerboseFlag);
DECLVAR(Py_UnbufferedStdioFlag);
DECLVAR(Py_UTF8Mode);

/* functions with prefix `Py_` */
DECLPROC(Py_BuildValue);
DECLPROC(Py_DecRef);
DECLPROC(Py_Finalize);
DECLPROC(Py_IncRef);
DECLPROC(Py_Initialize);
DECLPROC(Py_SetPath);
DECLPROC(Py_GetPath);
DECLPROC(Py_SetProgramName);
DECLPROC(Py_SetPythonHome);

/* other functions */
DECLPROC(PyDict_GetItemString);
DECLPROC(PyErr_Clear);
DECLPROC(PyErr_Occurred);
DECLPROC(PyErr_Print);
DECLPROC(PyErr_Fetch);
DECLPROC(PyErr_Restore);
DECLPROC(PyErr_NormalizeException);

DECLPROC(PyImport_AddModule);
DECLPROC(PyImport_ExecCodeModule);
DECLPROC(PyImport_ImportModule);
DECLPROC(PyList_Append);
DECLPROC(PyList_New);
DECLPROC(PyLong_AsLong);
DECLPROC(PyModule_GetDict);
DECLPROC(PyObject_CallFunction);
DECLPROC(PyObject_CallFunctionObjArgs);
DECLPROC(PyObject_SetAttrString);
DECLPROC(PyObject_GetAttrString);
DECLPROC(PyObject_Str);
DECLPROC(PyRun_SimpleStringFlags);
DECLPROC(PySys_AddWarnOption);
DECLPROC(PySys_SetArgvEx);
DECLPROC(PySys_GetObject);
DECLPROC(PySys_SetObject);
DECLPROC(PySys_SetPath);
DECLPROC(PyUnicode_FromString);

DECLPROC(Py_DecodeLocale);
DECLPROC(PyMem_RawFree);
DECLPROC(PyUnicode_FromFormat);
DECLPROC(PyUnicode_DecodeFSDefault);
DECLPROC(PyUnicode_Decode);
DECLPROC(PyUnicode_AsUTF8);
DECLPROC(PyUnicode_Join);
DECLPROC(PyUnicode_Replace);

DECLPROC(PyEval_EvalCode);
DECLPROC(PyMarshal_ReadObjectFromString);

/*
 * Get all of the entry points from libpython
 * that we are interested in.
 */
int
pyi_python_map_names(HMODULE dll, int pyvers)
{
    GETVAR(dll, Py_DontWriteBytecodeFlag);
    GETVAR(dll, Py_FileSystemDefaultEncoding);
    GETVAR(dll, Py_FrozenFlag);
    GETVAR(dll, Py_IgnoreEnvironmentFlag);
    GETVAR(dll, Py_NoSiteFlag);
    GETVAR(dll, Py_NoUserSiteDirectory);
    GETVAR(dll, Py_OptimizeFlag);
    GETVAR(dll, Py_VerboseFlag);
    GETVAR(dll, Py_UnbufferedStdioFlag);
    if (pyvers >= 307) {
        GETVAR(dll, Py_UTF8Mode);
    }

    /* functions with prefix `Py_` */
    GETPROC(dll, Py_BuildValue);
    GETPROC(dll, Py_DecRef);
    GETPROC(dll, Py_Finalize);
    GETPROC(dll, Py_IncRef);
    GETPROC(dll, Py_Initialize);

    GETPROC(dll, Py_SetPath);
    GETPROC(dll, Py_GetPath);
    GETPROC(dll, Py_SetProgramName);
    GETPROC(dll, Py_SetPythonHome);

    /* other functions */
    GETPROC(dll, PyDict_GetItemString);
    GETPROC(dll, PyErr_Clear);
    GETPROC(dll, PyErr_Occurred);
    GETPROC(dll, PyErr_Print);
    GETPROC(dll, PyErr_Fetch);
    GETPROC(dll, PyErr_Restore);
    GETPROC(dll, PyErr_NormalizeException);
    GETPROC(dll, PyImport_AddModule);
    GETPROC(dll, PyImport_ExecCodeModule);
    GETPROC(dll, PyImport_ImportModule);
    GETPROC(dll, PyList_Append);
    GETPROC(dll, PyList_New);
    GETPROC(dll, PyLong_AsLong);
    GETPROC(dll, PyModule_GetDict);
    GETPROC(dll, PyObject_CallFunction);
    GETPROC(dll, PyObject_CallFunctionObjArgs);
    GETPROC(dll, PyObject_SetAttrString);
    GETPROC(dll, PyObject_GetAttrString);
    GETPROC(dll, PyObject_Str);

    GETPROC(dll, PyRun_SimpleStringFlags);

    GETPROC(dll, PySys_AddWarnOption);
    GETPROC(dll, PySys_SetArgvEx);
    GETPROC(dll, PySys_GetObject);
    GETPROC(dll, PySys_SetObject);
    GETPROC(dll, PySys_SetPath);
    GETPROC(dll, PyEval_EvalCode);
    GETPROC(dll, PyMarshal_ReadObjectFromString);

    GETPROC(dll, PyUnicode_FromString);

    GETPROC(dll, Py_DecodeLocale);
    GETPROC(dll, PyMem_RawFree);

    GETPROC(dll, PyUnicode_FromFormat);
    GETPROC(dll, PyUnicode_Decode);
    GETPROC(dll, PyUnicode_DecodeFSDefault);
    GETPROC(dll, PyUnicode_AsUTF8);
    GETPROC(dll, PyUnicode_Join);
    GETPROC(dll, PyUnicode_Replace);

    VS("LOADER: Loaded functions from Python library.\n");

    return 0;
}


<|start_filename|>bootloader/src/pyi_path.h<|end_filename|>
/*
 * ****************************************************************************
 * Copyright (c) 2013-2021, PyInstaller Development Team.
 *
 * Distributed under the terms of the GNU General Public License (version 2
 * or later) with exception for distributing the bootloader.
 *
 * The full license is in the file COPYING.txt, distributed with this software.
 *
 * SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 * ****************************************************************************
 */

/*
 * Path manipulation utilities.
 */

#ifndef PYI_PATH_H
#define PYI_PATH_H

#include "pyi_global.h"

/* Path manipulation. Result is added to the supplied buffer. */
bool pyi_path_basename(char *result, const char *path);
bool pyi_path_dirname(char *result, const char *path);
char *pyi_path_join(char *result, const char *path1, const char *path2);
int pyi_path_fullpath(char *abs, size_t abs_size, const char *rel);
/* TODO implement. */
/* void *pyi_path_abspath(char *result, const char *path); */
int pyi_path_exists(char *path);

bool pyi_path_executable(char *execfile, const char *appname);
bool pyi_path_homepath(char *homepath, const char *executable);
bool pyi_path_archivefile(char *archivefile, const char *executable);

#ifdef _WIN32
FILE *pyi_path_fopen(const char *filename, const char *mode);
#else
    #define pyi_path_fopen(x, y)    fopen(x, y)
#endif
#define pyi_path_fclose(x)    fclose(x)

#endif  /* PYI_PATH_H */


<|start_filename|>scripts/clean.bat<|end_filename|>
:: Delete python development related files.
py.cleanup -a --quiet


<|start_filename|>alpine.dockerfile<|end_filename|>
# Setup an Alpine test environment with Python 3.9, PyInstaller and its test dependencies.
#
# To build, boot into and invoke pytest inside this image run:
#
#   docker build -f alpine.dockerfile -t some-arbitrary-name .
#   docker run -it some-arbitrary-name
#   pytest
#
# Or if you prefer a one-liner:
#
#   docker run -it $(docker build -q -f alpine.dockerfile .) pytest
#
# This docker file should be used for testing only. The bootloaders it compiles internally are not suitable for PyPI.
#
# ---
# This dockerfile is 2-part. The first half builds and the second tests. The test half should only contain PyInstaller's
# runtime and test dependencies - no C compiler or dev packages. Once other packages start shipping musl compatible
# wheels, most or possibly all of the build half will be safely removable.

FROM python:3.9-alpine AS wheel-factory

# Install a C compiler.
RUN apk add musl-dev gcc
# With zlib headers to compiler the bootloader,
RUN apk add zlib-dev
# Development packages to build lxml from source,
RUN apk add libxml2-dev libxslt-dev
# Linux headers to build psutil from source.
RUN apk add linux-headers

# Build/download wheels for all test requirements.
RUN mkdir -p /io/tests
WORKDIR /io
COPY tests/requirements-tools.txt tests/
COPY requirements.txt .
RUN pip wheel -r tests/requirements-tools.txt -w wheels

# Build a wheel for PyInstaller. Do this last and use as few files as possible to maximize cache-ability.
COPY COPYING.txt .
COPY setup.* ./
COPY bootloader bootloader
COPY PyInstaller PyInstaller
RUN python setup.py -qqq bdist_wheel -d wheels


FROM python:3.9-alpine

CMD ash
WORKDIR /io

# Runtime libraries required by lxml.
RUN apk add libxml2 libxslt
# Required by tkinter.
RUN apk add tk
# Used as a test library for some ctypes finding tests.
RUN apk add libpng
# And by PyInstaller itself.
RUN apk add binutils

COPY setup.cfg .

# Import and the precompiled wheels from the `build` image.
COPY --from=wheel-factory /io/wheels /wheels
RUN pip install /wheels/*.whl

COPY tests /io/tests


<|start_filename|>tests/scripts/Dockerfile<|end_filename|>
# Debian-based container for running PyInstaller builds/tests
#
# This serves primarily to make it easier for e.g. macOS-based
# developers to run the tests in a Linux environment *before*
# opening a pull request. See test-docker.sh for usage.
#
# Note: this functionality is contributed and not maintained
# by the PyInstaller maintainers. If you find it useful,
# please consider contributing to its maintenance and improvement.

FROM python:3.7

RUN mkdir -p /src

ADD requirements.txt tests/requirements-*.txt /src/
RUN pip install \
    -r /src/requirements.txt \
    -r /src/requirements-tools.txt \
    -r /src/requirements-libraries.txt

ADD *.py setup.cfg README.rst MANIFEST.in .pylintrc /src/
ADD PyInstaller /src/PyInstaller
ADD bootloader /src/bootloader
ADD doc /src/doc

RUN rm -rf /src/PyInstaller/bootloader/*

WORKDIR /src/bootloader
RUN python waf distclean all

WORKDIR /src
RUN python setup.py install

ADD tests /src/tests


<|start_filename|>tests/scripts/printShortLongCWD.cmd<|end_filename|>
@ECHO OFF
if '%1'=='' (%0 %cd%)
echo CWD - short: %~s1
echo CWD - long: %cd%


<|start_filename|>bootloader/src/pyi_pythonlib.h<|end_filename|>
/*
 * ****************************************************************************
 * Copyright (c) 2013-2021, PyInstaller Development Team.
 *
 * Distributed under the terms of the GNU General Public License (version 2
 * or later) with exception for distributing the bootloader.
 *
 * The full license is in the file COPYING.txt, distributed with this software.
 *
 * SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 * ****************************************************************************
 */

/*
 * Functions to load, initialize and launch Python.
 */

#ifndef PYI_PYTHONLIB_H
#define PYI_PYTHONLIB_H

#include "pyi_archive.h"

int pyi_pylib_attach(ARCHIVE_STATUS *status, int *loadedNew);
int pyi_pylib_load(ARCHIVE_STATUS *status);  /* note - pyi_pylib_attach will call this if not already loaded */
int pyi_pylib_start_python(ARCHIVE_STATUS *status);
int pyi_pylib_import_modules(ARCHIVE_STATUS *status);
int pyi_pylib_install_zlibs(ARCHIVE_STATUS *status);
int pyi_pylib_run_scripts(ARCHIVE_STATUS *status);

void pyi_pylib_finalize(ARCHIVE_STATUS *status);

#endif  /* PYI_PYTHONLIB_H */


<|start_filename|>tests/functional/data/ctypes_dylib/ctypes_dylib.c<|end_filename|>
/*
 * ****************************************************************************
 * Copyright (c) 2005-2021, PyInstaller Development Team.
 *
 * Distributed under the terms of the GNU General Public License (version 2
 * or later) with exception for distributing the bootloader.
 *
 * The full license is in the file COPYING.txt, distributed with this software.
 *
 * SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 * ****************************************************************************
 */

#ifdef _WIN32

// Windows code
int __declspec(dllexport) dummy(int arg)
{
    return arg + 12;
}

#else

// Unix code
int dummy(int arg)
{
    return arg + 12;
}

#endif


<|start_filename|>bootloader/src/pyi_apple_events.h<|end_filename|>
/*
 * ****************************************************************************
 * Copyright (c) 2013-2021, PyInstaller Development Team.
 *
 * Distributed under the terms of the GNU General Public License (version 2
 * or later) with exception for distributing the bootloader.
 *
 * The full license is in the file COPYING.txt, distributed with this software.
 *
 * SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 * ****************************************************************************
 */

/*
 * Handling of Apple Events in macOS windowed (app bundle) mode:
 *  - argv emulation
 *  - event forwarding to child process
 *
 * This allows the app bundle to open file by dragging and dropping it
 * onto app's icon in the macOS dock.
 */

#ifndef PYI_APPLE_EVENTS_H
#define PYI_APPLE_EVENTS_H

#if defined(__APPLE__) && defined(WINDOWED)

/* Install Apple Event handlers */
int pyi_apple_install_event_handlers();

/* Uninstall Apple Event handlers */
int pyi_apple_uninstall_event_handlers();

/*
 * Process Apple Events, either appending them to sys.argv (if argv-emu
 * is enabled and child process is not (yet) running, or forwarding
 * them to the child process.
 */
void pyi_apple_process_events(float timeout);

/*
 * Attempt to submit oapp event to ourselves in order to mitigate
 * issues with UI frameworks when argv-emu is used in onedir mode.
 */
void pyi_apple_submit_oapp_event();

/* Check if we have a pending event that we need to forward. */
int pyi_apple_has_pending_event();

/* Attempt to re-send the pending event after the specified delay. */
int pyi_apple_send_pending_event(float delay);

/* Clean-up the pending event data and status. */
void pyi_apple_cleanup_pending_event();

#endif  /* defined(__APPLE__) && defined(WINDOWED) */

#endif  /* PYI_APPLE_EVENTS_H */
