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

New loading based on clr_loader#1373

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 9 commits intopythonnet:masterfromfilmor:clr-loader
Feb 14, 2021
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
65 changes: 22 additions & 43 deletions.github/workflows/main.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,33 +12,9 @@ jobs:
fail-fast: false
matrix:
os: [windows, ubuntu, macos]
pyver_minor: [6, 7, 8, 9]
python: ["3.6", "3.7", "3.8", "3.9"]
platform: [x64]
shutdown_mode: [Normal, Soft]
include:
- os: ubuntu
pyver_minor: 6
dll_suffix: m
- os: ubuntu
pyver_minor: 7
dll_suffix: m

- os: macos
dll_prefix: lib
dll_pyver_major: '3.'
dll_suffix: m
- os: ubuntu
dll_prefix: lib
dll_pyver_major: '3.'
- os: windows
dll_pyver_major: '3'

- os: ubuntu
dll_ext: .so
- os: windows
dll_ext: .dll
- os: macos
dll_ext: .dylib

env:
PYTHONNET_SHUTDOWN_MODE: ${{ matrix.SHUTDOWN_MODE }}
Expand All@@ -56,10 +32,10 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1

- name: Set up Python3.${{ matrix.pyver_minor }}
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v2
with:
python-version:3.${{ matrix.pyver_minor }}
python-version: ${{ matrix.python }}
architecture: ${{ matrix.platform }}

- name: Install dependencies
Expand All@@ -68,31 +44,34 @@ jobs:

- name: Build and Install
run: |
python setup.py configure
pip install -v .

# TODO this should be gone once clr module sets PythonDLL or preloads it
- name: Python Tests
run: pytest
if: ${{ matrix.os != 'macos' }}
env:
PYTHONNET_PYDLL: ${{ matrix.DLL_PREFIX }}python${{matrix.DLL_PYVER_MAJOR}}${{matrix.PYVER_MINOR}}${{matrix.DLL_SUFFIX}}${{matrix.DLL_EXT}}
- name: Set Python DLL path (non Windows)
if: ${{ matrix.os != 'windows' }}
run: |
python -m pythonnet.find_libpython --export >> $GITHUB_ENV

- name: Python Tests
run: pytest
if: ${{ matrix.os == 'macos' }}
- 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

- name: Python Tests (Mono)
if: ${{ matrix.os != 'windows' }}
run: pytest --runtime mono

- name: Python Tests (.NET Core)
run: pytest --runtime netcore

- name: Python Tests (.NET Framework)
if: ${{ matrix.os == 'windows' }}
run: pytest --runtime netfx

- name: Embedding tests
run: dotnet test --runtime any-${{ matrix.platform }} src/embed_tests/
if: ${{ matrix.os != 'macos' }} # Not working right now, doesn't find libpython
env:
PYTHONNET_PYDLL: ${{ matrix.DLL_PREFIX }}python${{matrix.DLL_PYVER_MAJOR}}${{matrix.PYVER_MINOR}}${{matrix.DLL_SUFFIX}}${{matrix.DLL_EXT}}

- name: Python tests run from .NET
run: dotnet test --runtime any-${{ matrix.platform }} src/python_tests_runner/
if: ${{ matrix.os == 'windows' }} # Not working for others right now
env:
PYTHONNET_PYDLL: ${{ matrix.DLL_PREFIX }}python${{matrix.DLL_PYVER_MAJOR}}${{matrix.PYVER_MINOR}}${{matrix.DLL_SUFFIX}}${{matrix.DLL_EXT}}

# TODO: Run perf tests
# TODO: Run mono tests on Windows?
3 changes: 0 additions & 3 deletions.gitignore
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
/src/runtime/interopNative.cs

# Configuration data
configured.props

# General binaries and Build results
*.dll
*.exe
Expand Down
2 changes: 2 additions & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,6 +37,8 @@ when .NET expects an integer [#1342][i1342]
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
- Sign Runtime DLL with a strong name
- Implement loading through `clr_loader` instead of the included `ClrModule`, enables
support for .NET Core

### Fixed

Expand Down
1 change: 0 additions & 1 deletionDirectory.Build.props
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,5 +18,4 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)configured.props" Condition="Exists('$(MSBuildThisFileDirectory)configured.props')" />
</Project>
3 changes: 1 addition & 2 deletionsappveyor.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,9 +30,9 @@ environment:
init:
# Update Environment Variables based on matrix/platform
- set PY_VER=%PYTHON_VERSION:.=%
- set PYTHONNET_PYDLL=python%PY_VER%.dll
- set PYTHON=C:\PYTHON%PY_VER%
- if %PLATFORM%==x64 (set PYTHON=%PYTHON%-x64)
- set PYTHONNET_PYDLL=%PYTHON%\python%PY_VER%.dll

# Put desired Python version first in PATH
- set PATH=%PYTHON%;%PYTHON%\Scripts;%PATH%
Expand All@@ -42,7 +42,6 @@ install:
- pip install --upgrade -r requirements.txt --quiet

build_script:
- python setup.py configure
# Create clean `sdist`. Only used for releases
- python setup.py --quiet sdist
- python setup.py bdist_wheel
Expand Down
39 changes: 2 additions & 37 deletionsclr.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,40 +2,5 @@
Legacy Python.NET loader for backwards compatibility
"""

def _get_netfx_path():
import os, sys

if sys.maxsize > 2 ** 32:
arch = "amd64"
else:
arch = "x86"

return os.path.join(os.path.dirname(__file__), "pythonnet", "netfx", arch, "clr.pyd")


def _get_mono_path():
import os, glob

paths = glob.glob(os.path.join(os.path.dirname(__file__), "pythonnet", "mono", "clr.*so"))
return paths[0]


def _load_clr():
import sys
from importlib import util

if sys.platform == "win32":
path = _get_netfx_path()
else:
path = _get_mono_path()

del sys.modules[__name__]

spec = util.spec_from_file_location("clr", path)
clr = util.module_from_spec(spec)
spec.loader.exec_module(clr)

sys.modules[__name__] = clr


_load_clr()
from pythonnet import load
load()
6 changes: 6 additions & 0 deletionspyproject.toml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
[build-system]
requires = ["setuptools>=42", "wheel", "pycparser"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
xfail_strict = true
testpaths = [
"tests",
]
3 changes: 0 additions & 3 deletionspythonnet.sln
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,8 +6,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Runtime", "src\runti
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Console", "src\console\Console.csproj", "{E6B01706-00BA-4144-9029-186AC42FBE9A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "clrmodule", "src\clrmodule\clrmodule.csproj", "{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.EmbeddingTest", "src\embed_tests\Python.EmbeddingTest.csproj", "{819E089B-4770-400E-93C6-4F7A35F0EA12}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Test", "src\testing\Python.Test.csproj", "{14EF9518-5BB7-4F83-8686-015BD2CC788E}"
Expand DownExpand Up@@ -55,7 +53,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.PythonTestsRunner",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{142A6752-C2C2-4F95-B982-193418001B65}"
ProjectSection(SolutionItems) = preProject
configured.props = configured.props
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Expand Down
74 changes: 71 additions & 3 deletionspythonnet/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
def get_assembly_path():
import os
return os.path.dirname(__file__) + "/runtime/Python.Runtime.dll"
import os
import sys
import clr_loader

_RUNTIME = None
_LOADER_ASSEMBLY = None
_FFI = None
_LOADED = False


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

_RUNTIME = runtime


def set_default_runtime() -> None:
if sys.platform == 'win32':
set_runtime(clr_loader.get_netfx())
else:
set_runtime(clr_loader.get_mono())


def load():
global _FFI, _LOADED, _LOADER_ASSEMBLY

if _LOADED:
return

from .find_libpython import linked_libpython
from os.path import join, dirname

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()

dll_path = join(dirname(__file__), "runtime", "Python.Runtime.dll")
libpython = linked_libpython()

if libpython and _FFI is None and sys.platform != "win32":
# Load and leak libpython handle s.t. the .NET runtime doesn't dlcloses
# it
import posix

import cffi
_FFI = cffi.FFI()
_FFI.dlopen(libpython, posix.RTLD_NODELETE | posix.RTLD_LOCAL)

_LOADER_ASSEMBLY = _RUNTIME.get_assembly(dll_path)

func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]
if func(f"{libpython or ''}".encode("utf8")) != 0:
raise RuntimeError("Failed to initialize Python.Runtime.dll")

import atexit
atexit.register(unload)


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

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

[8]ページ先頭

©2009-2025 Movatter.jp