msl.loadlib.utils module

Common functions used by the MSL-LoadLib package.

msl.loadlib.utils.is_pythonnet_installed()

Checks if Python for .NET is installed.

Returns:

Whether Python for .NET is installed.

Return type:

bool

Note

For help getting Python for .NET installed on a non-Windows operating system look at the prerequisites, the Mono project and the Python for .NET documentation.

msl.loadlib.utils.is_py4j_installed()

Checks if Py4J is installed.

New in version 0.4.

Returns:

Whether Py4J is installed.

Return type:

bool

msl.loadlib.utils.is_comtypes_installed()

Checks if comtypes is installed.

New in version 0.5.

Returns:

Whether comtypes is installed.

Return type:

bool

msl.loadlib.utils.check_dot_net_config(py_exe_path)

Check if the useLegacyV2RuntimeActivationPolicy property is enabled.

By default, Python for .NET works with .NET 4.0+ and therefore it cannot automatically load a shared library that was compiled with .NET <4.0. This method ensures that the useLegacyV2RuntimeActivationPolicy property exists in the <python-executable>.config file and that it is enabled.

This link provides an overview explaining why the useLegacyV2RuntimeActivationPolicy property is required.

The <python-executable>.config file should look like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" />
        <supportedRuntime version="v2.0.50727" />
    </startup>
</configuration>
Parameters:

py_exe_path (str) – The path to a Python executable.

Returns:

A status flag and a message describing the outcome.

The flag will be one of the following values:

  • -1: if there was a problem

  • 0: if the .NET property was already enabled, or

  • 1: if the property was created successfully.

Return type:

tuple[int, str]

msl.loadlib.utils.is_port_in_use(port)

Checks whether the TCP port is in use.

Changed in version 0.10.0: Only check TCP ports (instead of both TCP and UDP ports). Uses the ss command instead of netstat on Linux.

Changed in version 0.7.0: Renamed from port_in_use and added support for macOS.

Parameters:

port (int) – The port number to test.

Returns:

Whether the TCP port is in use.

Return type:

bool

msl.loadlib.utils.get_available_port()

Returns a port number that is available.

Return type:

int

msl.loadlib.utils.wait_for_server(host, port, timeout)

Wait for the 32-bit server to start.

Parameters:
  • host (str) – The hostname or IP address of the server.

  • port (int) – The port number of the server.

  • timeout (float) – The maximum number of seconds to wait to establish a connection to the server.

Raises:

ConnectionTimeoutError – If a timeout occurred.

Return type:

None

msl.loadlib.utils.get_com_info(*additional_keys)

Reads the registry for the COM libraries that are available.

This function is only supported on Windows.

New in version 0.5.

Parameters:

additional_keys (str) – The Program ID (ProgID) key is returned automatically. You can include additional keys (e.g., Version, InprocHandler32, ToolboxBitmap32, VersionIndependentProgID, …) if you also want this additional information to be returned for each Class ID.

Returns:

The keys are the Class ID’s and each value is a dict of the information that was requested.

Return type:

dict[str, dict[str, str | None]]

Example:

>>> from msl.loadlib import utils
>>> info = utils.get_com_info()
>>> more_info = utils.get_com_info('Version', 'ToolboxBitmap32')
msl.loadlib.utils.generate_com_wrapper(lib, out_dir=None)

Generate a Python wrapper module around a COM library.

For more information see Accessing type libraries.

New in version 0.9.

Parameters:
  • lib (Any) –

    The COM library to create a wrapper of.

    Can be any of the following

    • a LoadLibrary object

    • the ProgID or CLSID of a registered COM library as a str

    • a COM pointer instance (POINTER())

    • an ITypeLib COM pointer instance (POINTER())

    • a path to a library file (.tlb, .exe or .dll) as a str

    • a tuple or list specifying the GUID of a library, a major and a minor version number, plus optionally an LCID number, e.g.,

      ['{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}', 1, 1]

    • an object with _reg_libid_ and _reg_version_ attributes

  • out_dir (str | None) – The output directory to save the wrapper to. If not specified, the module is saved to the ../site-packages/comtypes/gen directory.

Returns:

The wrapper module that was generated.

Return type:

module