Skip to content

Commit

Permalink
Merge branch 'master' into pr/tested/1-newtypes
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/runtime/MethodBinder.cs
#	src/runtime/Types/ReflectedClrType.cs
  • Loading branch information
eirannejad committed Nov 14, 2024
2 parents cdc8f58 + 6690310 commit 6170c71
Show file tree
Hide file tree
Showing 44 changed files with 1,143 additions and 122 deletions.
48 changes: 29 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,35 @@ on:
jobs:
build-test:
name: Build and Test
runs-on: ${{ matrix.os }}-latest
runs-on: ${{ matrix.os.instance }}
timeout-minutes: 15

strategy:
fail-fast: false
matrix:
os: [windows, ubuntu, macos]
python: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
platform: [x64, x86]
exclude:
- os: ubuntu
platform: x86
- os: macos
os:
- category: windows
platform: x86
instance: windows-latest

- category: windows
platform: x64
instance: windows-latest

- category: ubuntu
platform: x64
instance: ubuntu-latest

- category: macos
platform: x64
instance: macos-13

python: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: Set Environment on macOS
uses: maxim-lobanov/setup-xamarin@v1
if: ${{ matrix.os == 'macos' }}
if: ${{ matrix.os.category == 'macos' }}
with:
mono-version: latest

Expand All @@ -43,7 +53,7 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
architecture: ${{ matrix.platform }}
architecture: ${{ matrix.os.platform }}

- name: Install dependencies
run: |
Expand All @@ -55,42 +65,42 @@ jobs:
pip install -v .
- name: Set Python DLL path and PYTHONHOME (non Windows)
if: ${{ matrix.os != 'windows' }}
if: ${{ matrix.os.category != 'windows' }}
run: |
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV
echo PYTHONHOME=$(python -c 'import sys; print(sys.prefix)') >> $GITHUB_ENV
- name: Set Python DLL path and PYTHONHOME (Windows)
if: ${{ matrix.os == 'windows' }}
if: ${{ matrix.os.category == 'windows' }}
run: |
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONNET_PYDLL=$(python -m find_libpython)"
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONHOME=$(python -c 'import sys; print(sys.prefix)')"
- name: Embedding tests
run: dotnet test --runtime any-${{ matrix.platform }} --logger "console;verbosity=detailed" src/embed_tests/
run: dotnet test --runtime any-${{ matrix.os.platform }} --logger "console;verbosity=detailed" src/embed_tests/
env:
MONO_THREADS_SUSPEND: preemptive # https://github.com/mono/mono/issues/21466

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

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

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

- name: Python tests run from .NET
run: dotnet test --runtime any-${{ matrix.platform }} src/python_tests_runner/
run: dotnet test --runtime any-${{ matrix.os.platform }} src/python_tests_runner/

- name: Perf tests
if: ${{ (matrix.python == '3.8') && (matrix.platform == 'x64') }}
if: ${{ (matrix.python == '3.8') && (matrix.os.platform == 'x64') }}
run: |
pip install --force --no-deps --target src/perf_tests/baseline/ pythonnet==2.5.2
dotnet test --configuration Release --runtime any-${{ matrix.platform }} --logger "console;verbosity=detailed" src/perf_tests/
dotnet test --configuration Release --runtime any-${{ matrix.os.platform }} --logger "console;verbosity=detailed" src/perf_tests/
# TODO: Run mono tests on Windows?
2 changes: 2 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Dmitriy Se ([@dmitriyse](https://github.com/dmitriyse))
- Félix Bourbonnais ([@BadSingleton](https://github.com/BadSingleton))
- Florian Treurniet ([@ftreurni](https://github.com/ftreurni))
- Frank Witscher ([@Frawak](https://github.com/Frawak))
- He-chien Tsai ([@t3476](https://github.com/t3476))
- Inna Wiesel ([@inna-w](https://github.com/inna-w))
- Ivan Cronyn ([@cronan](https://github.com/cronan))
Expand Down Expand Up @@ -86,3 +87,4 @@
- ([@gpetrou](https://github.com/gpetrou))
- Ehsan Iran-Nejad ([@eirannejad](https://github.com/eirannejad))
- ([@legomanww](https://github.com/legomanww))
- ([@gertdreyer](https://github.com/gertdreyer))
31 changes: 29 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ project adheres to [Semantic Versioning][].

This document follows the conventions laid out in [Keep a CHANGELOG][].

## [3.0.4](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.4) - 2024-09-19

### Added

- Added `ToPythonAs<T>()` extension method to allow for explicit conversion
using a specific type. ([#2311][i2311])
- Added `IComparable` and `IEquatable` implementations to `PyInt`, `PyFloat`,
and `PyString` to compare with primitive .NET types like `long`.

### Changed
- Added a `FormatterFactory` member in RuntimeData to create formatters with
parameters. For compatibility, the `FormatterType` member is still present
and has precedence when defining both `FormatterFactory` and `FormatterType`
- Added a post-serialization and a pre-deserialization step callbacks to
extend (de)serialization process
- Added an API to stash serialized data on Python capsules

### Fixed

- Fixed RecursionError for reverse operators on C# operable types from python. See #2240
- Fixed crash when .NET event has no `AddMethod`
- Fixed probing for assemblies in `sys.path` failing when a path in `sys.path`
has invalid characters. See #2376
- Fixed possible access violation exception on shutdown. See ([#1977][i1977])

## [3.0.3](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.3) - 2023-10-11

### Added
Expand Down Expand Up @@ -834,7 +859,7 @@ This version improves performance on benchmarks significantly compared to 2.3.

[semantic versioning]: http://semver.org/

[unreleased]: ../../compare/v2.3.0...HEAD
[unreleased]: ../../compare/v3.0.1...HEAD

[2.3.0]: ../../compare/v2.2.2...v2.3.0

Expand Down Expand Up @@ -951,4 +976,6 @@ This version improves performance on benchmarks significantly compared to 2.3.
[i238]: https://github.com/pythonnet/pythonnet/issues/238
[i1481]: https://github.com/pythonnet/pythonnet/issues/1481
[i1672]: https://github.com/pythonnet/pythonnet/pull/1672
[p2055]: https://github.com/pythonnet/pythonnet/pull/2055
[i2311]: https://github.com/pythonnet/pythonnet/issues/2311
[i1977]: https://github.com/pythonnet/pythonnet/issues/1977
[p2055]: https://github.com/pythonnet/pythonnet/pull/2055
70 changes: 63 additions & 7 deletions src/embed_tests/Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void TestEval()
ps.Set("a", 1);
var result = ps.Eval<int>("a + 2");
Assert.AreEqual(3, result);
}
}
}

/// <summary>
Expand Down Expand Up @@ -169,6 +169,62 @@ public void TestScopeClass()
}
}

/// <summary>
/// Create a class in the scope, the class can read variables in the scope.
/// Its methods can write the variables with the help of 'global' keyword.
/// </summary>
[Test]
public void TestCreateVirtualPackageStructure()
{
using (Py.GIL())
{
using var _p1 = PyModule.FromString("test", "");
// Sub-module
using var _p2 = PyModule.FromString("test.scope",
"class Class1():\n" +
" def __init__(self, value):\n" +
" self.value = value\n" +
" def call(self, arg):\n" +
" return self.value + bb + arg\n" + // use scope variables
" def update(self, arg):\n" +
" global bb\n" +
" bb = self.value + arg\n", // update scope variable
"test"
);

dynamic ps2 = Py.Import("test.scope");
ps2.bb = 100;

dynamic obj1 = ps2.Class1(20);
var result = obj1.call(10).As<int>();
Assert.AreEqual(130, result);

obj1.update(10);
result = ps2.Get<int>("bb");
Assert.AreEqual(30, result);
}
}

/// <summary>
/// Test setting the file attribute via a FromString parameter
/// </summary>
[Test]
public void TestCreateModuleWithFilename()
{
using var _gil = Py.GIL();

using var mod = PyModule.FromString("mod", "");
using var modWithoutName = PyModule.FromString("mod_without_name", "", " ");
using var modNullName = PyModule.FromString("mod_null_name", "", null);

using var modWithName = PyModule.FromString("mod_with_name", "", "some_filename");

Assert.AreEqual("none", mod.Get<string>("__file__"));
Assert.AreEqual("none", modWithoutName.Get<string>("__file__"));
Assert.AreEqual("none", modNullName.Get<string>("__file__"));
Assert.AreEqual("some_filename", modWithName.Get<string>("__file__"));
}

/// <summary>
/// Import a python module into the session.
/// Equivalent to the Python "import" statement.
Expand All @@ -194,7 +250,7 @@ public void TestImportModule()
}

/// <summary>
/// Create a scope and import variables from a scope,
/// Create a scope and import variables from a scope,
/// exec Python statements in the scope then discard it.
/// </summary>
[Test]
Expand All @@ -218,7 +274,7 @@ public void TestImportScope()
}

/// <summary>
/// Create a scope and import variables from a scope,
/// Create a scope and import variables from a scope,
/// exec Python statements in the scope then discard it.
/// </summary>
[Test]
Expand All @@ -241,7 +297,7 @@ public void TestImportAllFromScope()
}

/// <summary>
/// Create a scope and import variables from a scope,
/// Create a scope and import variables from a scope,
/// call the function imported.
/// </summary>
[Test]
Expand Down Expand Up @@ -286,7 +342,7 @@ public void TestImportScopeFunction()
public void TestVariables()
{
using (Py.GIL())
{
{
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
var a0 = ps.Get<int>("ee");
Assert.AreEqual(200, a0);
Expand Down Expand Up @@ -326,8 +382,8 @@ public void TestThread()
_ps.res = 0;
_ps.bb = 100;
_ps.th_cnt = 0;
//add function to the scope
//can be call many times, more efficient than ast
//add function to the scope
//can be call many times, more efficient than ast
ps.Exec(
"import threading\n"+
"lock = threading.Lock()\n"+
Expand Down
9 changes: 9 additions & 0 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ public void RawPyObjectProxy()
Assert.AreEqual(pyObject.DangerousGetAddressOrNull(), proxiedHandle);
}

[Test]
public void GenericToPython()
{
int i = 42;
var pyObject = i.ToPythonAs<IConvertible>();
var type = pyObject.GetPythonType();
Assert.AreEqual(nameof(IConvertible), type.Name);
}

// regression for https://github.com/pythonnet/pythonnet/issues/451
[Test]
public void CanGetListFromDerivedClass()
Expand Down
Loading

0 comments on commit 6170c71

Please sign in to comment.