1from __future__ import print_function
2import os
3import sys
4from textwrap import dedent
5
6__all__ = list("Qt" + body for body in
7    "@all_module_shortnames@"
8    .split(";"))
9__version__ = "@FINAL_PACKAGE_VERSION@"
10__version_info__ = (@BINDING_API_MAJOR_VERSION@, @BINDING_API_MINOR_VERSION@, @BINDING_API_MICRO_VERSION@, "@BINDING_API_PRE_RELEASE_VERSION_TYPE@", "@BINDING_API_PRE_RELEASE_VERSION@")
11
12
13def _additional_dll_directories(package_dir):
14    # Find shiboken2 relative to the package directory.
15    root = os.path.dirname(package_dir)
16    # Check for a flat .zip as deployed by cx_free(PYSIDE-1257)
17    if root.endswith('.zip'):
18        return []
19    shiboken2 = os.path.join(root, 'shiboken2')
20    if os.path.isdir(shiboken2): # Standard case, only shiboken2 is needed
21        return [shiboken2]
22    # The below code is for the build process when generate_pyi.py
23    # is executed in the build directory. We need libpyside and Qt in addition.
24    shiboken2 = os.path.join(os.path.dirname(root), 'shiboken2', 'libshiboken')
25    if not os.path.isdir(shiboken2):
26        raise ImportError(shiboken2 + ' does not exist')
27    result = [shiboken2, os.path.join(root, 'libpyside')]
28    for path in os.environ.get('PATH').split(';'):
29        if path:
30             if os.path.exists(os.path.join(path, 'qmake.exe')):
31                 result.append(path)
32                 break
33    return result
34
35
36def _setupQtDirectories():
37    # On Windows we need to explicitly import the shiboken2 module so
38    # that the libshiboken.dll dependency is loaded by the time a
39    # Qt module is imported. Otherwise due to PATH not containing
40    # the shiboken2 module path, the Qt module import would fail
41    # due to the missing libshiboken dll.
42    # In addition, as of Python 3.8, the shiboken package directory
43    # must be added to the DLL search paths so that shiboken2.dll
44    # is found.
45    # We need to do the same on Linux and macOS, because we do not
46    # embed rpaths into the PySide2 libraries that would point to
47    # the libshiboken library location. Importing the module
48    # loads the libraries into the process memory beforehand, and
49    # thus takes care of it for us.
50
51    pyside_package_dir = os.path.abspath(os.path.dirname(__file__))
52
53    if sys.platform == 'win32' and sys.version_info[0] == 3 and sys.version_info[1] >= 8:
54        for dir in _additional_dll_directories(pyside_package_dir):
55            os.add_dll_directory(dir)
56
57    try:
58        import shiboken2
59    except Exception:
60        paths = ', '.join(sys.path)
61        print('PySide2/__init__.py: Unable to import shiboken2 from {}'.format(paths),
62              file=sys.stderr)
63        raise
64
65    #   Trigger signature initialization.
66    try:
67        # PYSIDE-829: Avoid non-existent attributes in compiled code (Nuitka).
68        # We now use an explicit function instead of touching a signature.
69        _init_pyside_extension()
70    except AttributeError:
71        print(dedent('''\
72            {stars}
73            PySide2/__init__.py: The `signature` module was not initialized.
74            This libshiboken module was loaded from
75
76            "{shiboken2.__file__}".
77
78            Please make sure that this is the real shiboken2 binary and not just a folder.
79            {stars}
80            ''').format(stars=79*"*", **locals()), file=sys.stderr)
81        raise
82
83    if sys.platform == 'win32':
84        # PATH has to contain the package directory, otherwise plugins
85        # won't be able to find their required Qt libraries (e.g. the
86        # svg image plugin won't find Qt5Svg.dll).
87        os.environ['PATH'] = pyside_package_dir + os.pathsep + os.environ['PATH']
88
89        # On Windows, add the PySide2\openssl folder (created by setup.py's
90        # --openssl option) to the PATH so that the SSL DLLs can be found
91        # when Qt tries to dynamically load them. Tell Qt to load them and
92        # then reset the PATH.
93        openssl_dir = os.path.join(pyside_package_dir, 'openssl')
94        if os.path.exists(openssl_dir):
95            path = os.environ['PATH']
96            try:
97                os.environ['PATH'] = openssl_dir + os.pathsep + path
98                try:
99                    from . import QtNetwork
100                except ImportError:
101                    pass
102                else:
103                    QtNetwork.QSslSocket.supportsSsl()
104            finally:
105                os.environ['PATH'] = path
106
107_setupQtDirectories()
108