Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement configuring clr from environment#1817

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 2 commits intopythonnet:masterfromfilmor:env-config
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions.github/workflows/ARM.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,7 +36,7 @@ jobs:

- name: Set Python DLL path (non Windows)
run: |
python -mpythonnet.find_libpython --export >> $GITHUB_ENV
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV

- name: Embedding tests
run: dotnet test --logger "console;verbosity=detailed" src/embed_tests/
Expand All@@ -45,7 +45,7 @@ jobs:
run: python -m pytest --runtime mono

- name: Python Tests (.NET Core)
run: python -m pytest --runtimenetcore
run: python -m pytest --runtimecoreclr

- name: Python tests run from .NET
run: dotnet test src/python_tests_runner/
Expand Down
9 changes: 5 additions & 4 deletions.github/workflows/main.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Main (x64)
name: Main

on:
push:
Expand DownExpand Up@@ -57,12 +57,12 @@ jobs:
- name: Set Python DLL path (non Windows)
if: ${{ matrix.os != 'windows' }}
run: |
python -mpythonnet.find_libpython --export >> $GITHUB_ENV
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV

- name: Set Python DLL path (Windows)
if: ${{ matrix.os == 'windows' }}
run: |
python -m pythonnet.find_libpython --export |Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONNET_PYDLL=$(python -m find_libpython)"

- name: Embedding tests
run: dotnet test --runtime any-${{ matrix.platform }} --logger "console;verbosity=detailed" src/embed_tests/
Expand All@@ -73,9 +73,10 @@ jobs:
if: ${{ matrix.os != 'windows' }}
run: pytest --runtime mono

# TODO: Run these tests on Windows x86
- name: Python Tests (.NET Core)
if: ${{ matrix.platform == 'x64' }}
run: pytest --runtimenetcore
run: pytest --runtimecoreclr

- name: Python Tests (.NET Framework)
if: ${{ matrix.os == 'windows' }}
Expand Down
2 changes: 1 addition & 1 deletion.github/workflows/nuget-preview.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,7 @@ jobs:
-name:Set Python DLL path (non Windows)
if:${{ matrix.os != 'windows' }}
run:|
python -mpythonnet.find_libpython --export >> $GITHUB_ENV
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV
-name:Python Tests
run:pytest
Expand Down
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@ and other `PyObject` derived types when called from Python.
- .NET classes, that have `__call__` method are callable from Python
- `PyIterable` type, that wraps any iterable object in Python
- `PythonEngine` properties for supported Python versions: `MinSupportedVersion`, `MaxSupportedVersion`, and `IsSupportedVersion`
- The runtime that is loaded on `import clr` can now be configured via environment variables


### Changed
Expand Down
116 changes: 96 additions & 20 deletionspythonnet/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,115 @@
import sys
from pathlib import Path
from typing import Dict, Optional, Union
import clr_loader

_RUNTIME = None
_LOADER_ASSEMBLY = None
_FFI = None
_LOADED = False
__all__ = ["set_runtime", "set_default_runtime", "load"]

_RUNTIME: Optional[clr_loader.Runtime] = None
_LOADER_ASSEMBLY: Optional[clr_loader.wrappers.Assembly] = None
_LOADED: bool = False


def set_runtime(runtime: Union[clr_loader.Runtime, str], **params: str) -> None:
"""Set up a clr_loader runtime without loading it

:param runtime: Either an already initialised `clr_loader` runtime, or one
of netfx, coreclr, mono, or default. If a string parameter is given, the
runtime will be created."""

def set_runtime(runtime):
global _RUNTIME
if _LOADED:
raise RuntimeError("The runtime {} has already been loaded".format(_RUNTIME))
raise RuntimeError(f"The runtime {_RUNTIME} has already been loaded")

if isinstance(runtime, str):
runtime = _create_runtime_from_spec(runtime, params)

_RUNTIME = runtime


def set_default_runtime() -> None:
if sys.platform == "win32":
set_runtime(clr_loader.get_netfx())
def _get_params_from_env(prefix: str) -> Dict[str, str]:
from os import environ

full_prefix = f"PYTHONNET_{prefix.upper()}"
len_ = len(full_prefix)

env_vars = {
(k[len_:].lower()): v
for k, v in environ.items()
if k.upper().startswith(full_prefix)
}

return env_vars


def _create_runtime_from_spec(
spec: str, params: Optional[Dict[str, str]] = None
) -> clr_loader.Runtime:
if spec == "default":
if sys.platform == "win32":
spec = "netfx"
else:
spec = "mono"

params = params or _get_params_from_env(spec)

if spec == "netfx":
return clr_loader.get_netfx(**params)
elif spec == "mono":
return clr_loader.get_mono(**params)
elif spec == "coreclr":
return clr_loader.get_coreclr(**params)
else:
set_runtime(clr_loader.get_mono())
raise RuntimeError(f"Invalid runtime name: '{spec}'")


def load():
global _FFI, _LOADED, _LOADER_ASSEMBLY
def set_default_runtime() -> None:
"""Set up the default runtime

This will use the environment variable PYTHONNET_RUNTIME to decide the
runtime to use, which may be one of netfx, coreclr or mono. The parameters
of the respective clr_loader.get_<runtime> functions can also be given as
environment variables, named `PYTHONNET_<RUNTIME>_<PARAM_NAME>`. In
particular, to use `PYTHONNET_RUNTIME=coreclr`, the variable
`PYTHONNET_CORECLR_RUNTIME_CONFIG` has to be set to a valid
`.runtimeconfig.json`.

If no environment variable is specified, a globally installed Mono is used
for all environments but Windows, on Windows the legacy .NET Framework is
used.
"""
from os import environ

print("Set default RUNTIME")
raise RuntimeError("Shouldn't be called here")

spec = environ.get("PYTHONNET_RUNTIME", "default")
runtime = _create_runtime_from_spec(spec)
set_runtime(runtime)


def load(
runtime: Union[clr_loader.Runtime, str] = "default", **params: Dict[str, str]
) -> None:
"""Load Python.NET in the specified runtime

The same parameters as for `set_runtime` can be used. By default,
`set_default_runtime` is called if no environment has been set yet and no
parameters are passed."""
global _LOADED, _LOADER_ASSEMBLY

if _LOADED:
return

from os.path import join, dirname
if _RUNTIME is None:
set_runtime(runtime, **params)

if _RUNTIME is None:
# TODO: Warn, in the future the runtime must be set explicitly, either
# as a config/env variable or via set_runtime
set_default_runtime()
raise RuntimeError("No valid runtime selected")

dll_path =join(dirname(__file__),"runtime","Python.Runtime.dll")
dll_path =Path(__file__).parent /"runtime" /"Python.Runtime.dll"

_LOADER_ASSEMBLY = _RUNTIME.get_assembly(dll_path)
_LOADER_ASSEMBLY = _RUNTIME.get_assembly(str(dll_path))

func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]
if func(b"") != 0:
Expand All@@ -48,13 +120,17 @@ def load():
atexit.register(unload)


def unload():
global _RUNTIME
def unload() -> None:
"""Explicitly unload a laoded runtime and shut down Python.NET"""

global _RUNTIME, _LOADER_ASSEMBLY
if _LOADER_ASSEMBLY is not None:
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Shutdown"]
if func(b"full_shutdown") != 0:
raise RuntimeError("Failed to call Python.NET shutdown")

_LOADER_ASSEMBLY = None

if _RUNTIME is not None:
# TODO: Add explicit `close` to clr_loader
_RUNTIME = None
Loading

[8]ページ先頭

©2009-2025 Movatter.jp