Skip to content

Commit 1dfc17b

Browse files
authored
Update pythonnet (#13)
Merge changes from pythonnet 3.03 - support python 3.12
2 parents f22a272 + cd5e216 commit 1dfc17b

File tree

16 files changed

+277
-85
lines changed

16 files changed

+277
-85
lines changed

.github/workflows/ARM.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ jobs:
2929
3030
- name: Install dependencies
3131
run: |
32-
pip install -r requirements.txt
33-
pip install pytest numpy # for tests
32+
pip3.8 install -r requirements.txt
33+
pip3.8 install pytest numpy # for tests
3434
3535
- name: Build and Install
3636
run: |
37-
pip install -v .
37+
pip3.8 install -v .
3838
3939
- name: Set Python DLL path (non Windows)
4040
run: |
41-
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV
41+
echo PYTHONNET_PYDLL=$(python3.8 -m find_libpython) >> $GITHUB_ENV
4242
4343
- name: Embedding tests
4444
run: dotnet test --logger "console;verbosity=detailed" src/embed_tests/
4545

4646
- name: Python Tests (Mono)
47-
run: python -m pytest --runtime mono
47+
run: python3.8 -m pytest --runtime mono
4848

4949
- name: Python Tests (.NET Core)
50-
run: python -m pytest --runtime coreclr
50+
run: python3.8 -m pytest --runtime coreclr
5151

5252
- name: Python tests run from .NET
5353
run: dotnet test src/python_tests_runner/

.github/workflows/ci_cd.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ jobs:
4848
strategy:
4949
fail-fast: false
5050
matrix:
51-
os: [windows, ubuntu]
52-
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
51+
os: [windows, ubuntu, macos]
52+
python: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
5353
platform: [x64, x86]
5454
exclude:
5555
- os: ubuntu

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
77

88
## [Unreleased][]
99

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

12-
- use enum name in repr
15+
- Support for Python 3.12
1316

1417
### Changed
1518

16-
### Fixed
19+
- Use enum name in `repr`
1720

1821
## [3.0.2](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.2) - 2023-08-29
1922

@@ -833,7 +836,7 @@ This version improves performance on benchmarks significantly compared to 2.3.
833836

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

836-
[unreleased]: ../../compare/v2.3.0...HEAD
839+
[unreleased]: ../../compare/v3.0.1...HEAD
837840

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

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ Ansys fork of `pythonnet <https://github.com/pythonnet/pythonnet>`_.
33
We will try to keep this up-to-date with pythonnet and upstream changes that might benefit the pythonnet community
44

55
Changes relative to pythonnet:
6-
- Revert of `#1240 <https://github.com/pythonnet/pythonnet/pull/1240>`_.
7-
- Enum REPR `#2239 <https://github.com/pythonnet/pythonnet/pull/2239>` is included in this release of version 3.0.2, but is unreleased in pythonnet
6+
7+
- Revert of `#1240 <https://github.com/pythonnet/pythonnet/pull/1240>`_.

doc/source/dotnet.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Code executed from the scope will have access to the variable:
110110
using (Py.GIL())
111111
{
112112
// create a Python scope
113-
using (PyScope scope = Py.CreateScope())
113+
using (PyModule scope = Py.CreateScope())
114114
{
115115
// convert the Person object to a PyObject
116116
PyObject pyPerson = person.ToPython();

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"clr_loader>=0.2.6,<0.3.0"
1414
]
1515

16-
requires-python = ">=3.7, <3.12"
16+
requires-python = ">=3.7, <3.13"
1717

1818
classifiers = [
1919
"Development Status :: 5 - Production/Stable",
@@ -26,6 +26,7 @@ classifiers = [
2626
"Programming Language :: Python :: 3.9",
2727
"Programming Language :: Python :: 3.10",
2828
"Programming Language :: Python :: 3.11",
29+
"Programming Language :: Python :: 3.12",
2930
"Operating System :: Microsoft :: Windows",
3031
"Operating System :: POSIX :: Linux",
3132
"Operating System :: MacOS :: MacOS X",

src/embed_tests/Codecs.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,23 @@ public void FloatDerivedDecoded()
371371
[Test]
372372
public void ExceptionDecodedNoInstance()
373373
{
374-
PyObjectConversions.RegisterDecoder(new InstancelessExceptionDecoder());
375-
using var scope = Py.CreateScope();
376-
var error = Assert.Throws<ValueErrorWrapper>(() => PythonEngine.Exec(
377-
$"[].__iter__().__next__()"));
378-
Assert.AreEqual(TestExceptionMessage, error.Message);
374+
if (Runtime.PyVersion < new Version(3, 12))
375+
{
376+
PyObjectConversions.RegisterDecoder(new InstancelessExceptionDecoder());
377+
using var scope = Py.CreateScope();
378+
379+
var error = Assert.Throws<ValueErrorWrapper>(() =>
380+
PythonEngine.Exec($"[].__iter__().__next__()")
381+
);
382+
Assert.AreEqual(TestExceptionMessage, error.Message);
383+
}
384+
else
385+
{
386+
Assert.Ignore(
387+
"This test does not work for Python 3.12, see " +
388+
"https://github.com/python/cpython/issues/101578"
389+
);
390+
}
379391
}
380392

381393
public static void AcceptsDateTime(DateTime v) {}

src/embed_tests/Modules.cs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void TestEval()
5151
ps.Set("a", 1);
5252
var result = ps.Eval<int>("a + 2");
5353
Assert.AreEqual(3, result);
54-
}
54+
}
5555
}
5656

5757
/// <summary>
@@ -169,6 +169,62 @@ public void TestScopeClass()
169169
}
170170
}
171171

172+
/// <summary>
173+
/// Create a class in the scope, the class can read variables in the scope.
174+
/// Its methods can write the variables with the help of 'global' keyword.
175+
/// </summary>
176+
[Test]
177+
public void TestCreateVirtualPackageStructure()
178+
{
179+
using (Py.GIL())
180+
{
181+
using var _p1 = PyModule.FromString("test", "");
182+
// Sub-module
183+
using var _p2 = PyModule.FromString("test.scope",
184+
"class Class1():\n" +
185+
" def __init__(self, value):\n" +
186+
" self.value = value\n" +
187+
" def call(self, arg):\n" +
188+
" return self.value + bb + arg\n" + // use scope variables
189+
" def update(self, arg):\n" +
190+
" global bb\n" +
191+
" bb = self.value + arg\n", // update scope variable
192+
"test"
193+
);
194+
195+
dynamic ps2 = Py.Import("test.scope");
196+
ps2.bb = 100;
197+
198+
dynamic obj1 = ps2.Class1(20);
199+
var result = obj1.call(10).As<int>();
200+
Assert.AreEqual(130, result);
201+
202+
obj1.update(10);
203+
result = ps2.Get<int>("bb");
204+
Assert.AreEqual(30, result);
205+
}
206+
}
207+
208+
/// <summary>
209+
/// Test setting the file attribute via a FromString parameter
210+
/// </summary>
211+
[Test]
212+
public void TestCreateModuleWithFilename()
213+
{
214+
using var _gil = Py.GIL();
215+
216+
using var mod = PyModule.FromString("mod", "");
217+
using var modWithoutName = PyModule.FromString("mod_without_name", "", " ");
218+
using var modNullName = PyModule.FromString("mod_null_name", "", null);
219+
220+
using var modWithName = PyModule.FromString("mod_with_name", "", "some_filename");
221+
222+
Assert.AreEqual("none", mod.Get<string>("__file__"));
223+
Assert.AreEqual("none", modWithoutName.Get<string>("__file__"));
224+
Assert.AreEqual("none", modNullName.Get<string>("__file__"));
225+
Assert.AreEqual("some_filename", modWithName.Get<string>("__file__"));
226+
}
227+
172228
/// <summary>
173229
/// Import a python module into the session.
174230
/// Equivalent to the Python "import" statement.
@@ -194,7 +250,7 @@ public void TestImportModule()
194250
}
195251

196252
/// <summary>
197-
/// Create a scope and import variables from a scope,
253+
/// Create a scope and import variables from a scope,
198254
/// exec Python statements in the scope then discard it.
199255
/// </summary>
200256
[Test]
@@ -218,7 +274,7 @@ public void TestImportScope()
218274
}
219275

220276
/// <summary>
221-
/// Create a scope and import variables from a scope,
277+
/// Create a scope and import variables from a scope,
222278
/// exec Python statements in the scope then discard it.
223279
/// </summary>
224280
[Test]
@@ -241,7 +297,7 @@ public void TestImportAllFromScope()
241297
}
242298

243299
/// <summary>
244-
/// Create a scope and import variables from a scope,
300+
/// Create a scope and import variables from a scope,
245301
/// call the function imported.
246302
/// </summary>
247303
[Test]
@@ -286,7 +342,7 @@ public void TestImportScopeFunction()
286342
public void TestVariables()
287343
{
288344
using (Py.GIL())
289-
{
345+
{
290346
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
291347
var a0 = ps.Get<int>("ee");
292348
Assert.AreEqual(200, a0);
@@ -326,8 +382,8 @@ public void TestThread()
326382
_ps.res = 0;
327383
_ps.bb = 100;
328384
_ps.th_cnt = 0;
329-
//add function to the scope
330-
//can be call many times, more efficient than ast
385+
//add function to the scope
386+
//can be call many times, more efficient than ast
331387
ps.Exec(
332388
"import threading\n"+
333389
"lock = threading.Lock()\n"+

src/runtime/Converter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,8 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
680680
{
681681
if (Runtime.PyUnicode_GetLength(value) == 1)
682682
{
683-
IntPtr unicodePtr = Runtime.PyUnicode_AsUnicode(value);
684-
Char[] buff = new Char[1];
685-
Marshal.Copy(unicodePtr, buff, 0, 1);
686-
result = buff[0];
683+
int chr = Runtime.PyUnicode_ReadChar(value, 0);
684+
result = (Char)chr;
687685
return true;
688686
}
689687
goto type_error;

0 commit comments

Comments
 (0)