Skip to content

Commit

Permalink
Move generated .so/.pyd file to visp folder: _visp becomes visp._visp…
Browse files Browse the repository at this point in the history
… and importing directly means that the bindings __init__.py should always be executed, even when generating stubs
  • Loading branch information
SamFlt committed Mar 15, 2024
1 parent 3f12f69 commit aea0103
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 1 addition & 3 deletions modules/python/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ set_target_properties(_visp PROPERTIES
set(build_configs "NONE" "RELEASE" "DEBUG" "RELEASEWITHDEBINFO" "RELWITHDEBINFO")
foreach(imp_config ${build_configs})
set_target_properties(_visp PROPERTIES
LIBRARY_OUTPUT_DIRECTORY_${imp_config} "${CMAKE_CURRENT_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY_${imp_config} "${CMAKE_CURRENT_BINARY_DIR}/visp"
)
endforeach()

foreach(visp_lib ${VISP_LIBRARIES})
get_target_property(dir ${visp_lib} LIBRARY_OUTPUT_DIRECTORY)
get_target_property(n ${visp_lib} OUTPUT_NAME)

message("${dir}/${n}")
endforeach()

set_target_properties(_visp PROPERTIES EXCLUDE_FROM_ALL TRUE)
Expand Down
12 changes: 6 additions & 6 deletions modules/python/bindings/visp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@


try:
import _visp
from _visp import *
from ._visp import *
from ._visp import __dict__ as cpp_extension_dict
except ImportError:
import platform
if platform.system() == "Windows": # On windows import can fail because DLLs are not found in the default search paths
Expand All @@ -51,13 +51,13 @@
with build_directory_manager() as dll_dir_manager:
for p in get_dll_paths():
dll_dir_manager.add_dll_directory(p)
import _visp
from _visp import *
from ._visp import *
from ._visp import __dict__ as cpp_extension_dict
else:
raise
raise ImportError('Could not import ViSP python bindings')

# Fake module names
for k in _visp.__dict__:
for k in cpp_extension_dict:
from types import ModuleType
if isinstance(_visp.__dict__[k], ModuleType):
sys.modules[f'{__name__}.{k}'] = _visp.__dict__[k]
2 changes: 2 additions & 0 deletions modules/python/bindings/visp/windows_dll_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def get_dll_paths():
# For the choice of defaults: see https://peps.python.org/pep-0250/#implementation
DEFAULT_DLL_PATHS = [
'..\\..\\..\\..\\bin', # when current folder is lib/python-version/site-packages/package
'..\\..\\..\\..\\Library\\bin',
'..\\..\\..\\bin', # when current folder is lib/site-packages/package
'..\\..\\..\\Library\\bin',
]
# If we have a different setup, the user should specify their own paths
visp_user_defined_dll_paths = os.getenv("VISP_WINDOWS_DLL_PATH")
Expand Down
1 change: 1 addition & 0 deletions modules/python/doc/conf.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ object_description_options = [

python_type_aliases = {
"_visp.": "visp.",
"visp._visp.": "visp."
}


Expand Down
10 changes: 7 additions & 3 deletions modules/python/stubs/run_stub_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
import sys
import argparse

import pybind11_stubgen
from pybind11_stubgen import __main__



if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--output-root', type=str, help='Path where to save the stubs')
Expand All @@ -47,11 +52,10 @@
output_root = Path(args.output_root)
assert output_root.exists()
bin_folder = Path(sys.executable).parent

subprocess.run([sys.executable, '-m', 'pybind11_stubgen', '-o', str(output_root.absolute()), '--ignore-all-errors', '_visp'], check=True)
subprocess.run([sys.executable, '-m', 'pybind11_stubgen', '-o', str(output_root.absolute()), '--ignore-all-errors', 'visp._visp'], check=True)

# Generate stubs for the bindings (C++ side) and mock it so that they appear in the true 'visp' package
p = Path('./_visp')
p = Path('./visp/_visp')
target_path = Path('./visp-stubs')
target_path.mkdir(exist_ok=True)
for pyi_file in p.iterdir():
Expand Down

0 comments on commit aea0103

Please sign in to comment.