-
Notifications
You must be signed in to change notification settings - Fork 6
Add context manager for adding DLL directories to the search path #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+101
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| # rpyutils | ||
| Various utility types and functions for Python | ||
|
|
||
| ## API | ||
|
|
||
| - Context manager for adding DLLs to the Windows search path. | ||
| Only applies to Python 3.8 or newer: | ||
| - `add_dll_directories_from_env` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from contextlib import contextmanager | ||
| import os | ||
|
|
||
|
|
||
| @contextmanager | ||
| def add_dll_directories_from_env(env_name: str): | ||
| """ | ||
| Add a list of directories from an environment variable to the DLL search path on Windows. | ||
|
|
||
| Each directory in the environment variable that exists is passed to | ||
| :func:`os.add_dll_directory`. | ||
|
|
||
| If this function is called on a system other than Windows, then nothing happens and | ||
| an empty list is returned. | ||
| If this function is called with a version of Python less than 3.8, then nothing happens and | ||
| an empty list is returned. | ||
|
|
||
| Example usage:: | ||
|
|
||
| with add_dll_directories_from_env('PATH'): | ||
| importlib.import_module('foo', package='bar') | ||
|
|
||
| :param env_name: The name of the environment variable with DLL search paths. | ||
| :return: A list of handles to directories. | ||
| """ | ||
| dll_dir_handles = [] | ||
| # This function only makes sense on Windows and if the function 'add_dll_directory' exists | ||
| if os.name == 'nt' and hasattr(os, 'add_dll_directory'): | ||
| env_values = os.environ[env_name].split(';') | ||
|
jacobperron marked this conversation as resolved.
Outdated
|
||
| for prefix_path in env_values: | ||
| # Only add directories that exist | ||
| if os.path.isdir(prefix_path): | ||
| dll_dir_handles.append(os.add_dll_directory(prefix_path)) | ||
|
|
||
| try: | ||
| yield dll_dir_handles | ||
| finally: | ||
| for handle in dll_dir_handles: | ||
| handle.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from rpyutils import add_dll_directories_from_env | ||
|
|
||
|
|
||
| def test_add_dll_direcotires_from_env(monkeypatch, tmp_path): | ||
| # Test with empty value | ||
| monkeypatch.delenv('TEST_ENV', raising=False) | ||
| with add_dll_directories_from_env('TEST_ENV'): | ||
| pass | ||
|
|
||
| # Test with one path | ||
| monkeypatch.setenv('TEST_ENV', tmp_path.name) | ||
| with add_dll_directories_from_env('TEST_ENV'): | ||
| pass | ||
|
|
||
| # Test with multiple paths | ||
| dir1 = tmp_path / 'subdir1' | ||
| dir2 = tmp_path / 'subdir2' | ||
| dir1.mkdir() | ||
| dir2.mkdir() | ||
| monkeypatch.setenv('TEST_ENV', f'{dir1.name};{dir2.name}') | ||
| with add_dll_directories_from_env('TEST_ENV'): | ||
| pass |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.