Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f394cdc

Browse files
authored
Merge branch 'master' into PR/ObjectAcceptsAll
2 parents 4ea1c40 + 4d1442d commit f394cdc

28 files changed

+665
-53
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ install:
4444

4545
script:
4646
- python -m pytest
47-
- $RUN_TESTS src/embed_tests/bin/$EMBED_TESTS_PATH/Python.EmbeddingTest.dll
47+
- $RUN_TESTS src/embed_tests/bin/$EMBED_TESTS_PATH/Python.EmbeddingTest.dll --labels=All
4848
# does not work on Linux, because NuGet package for 2.3 is Windows only
4949
# - "if [[ $TRAVIS_PYTHON_VERSION == '3.5' && $PERF_TESTS_PATH != '' ]]; then mono $NUNIT_PATH src/perf_tests/bin/$PERF_TESTS_PATH/Python.PerformanceTests.dll; fi"
5050

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Alex Earl ([@slide](https://github.com/slide))
1616
- Alex Helms ([@alexhelms](https://github.com/alexhelms))
1717
- Alexandre Catarino([@AlexCatarino](https://github.com/AlexCatarino))
18+
- Andrey Sant'Anna ([@andreydani](https://github.com/andreydani))
1819
- Arvid JB ([@ArvidJB](https://github.com/ArvidJB))
1920
- Benoît Hudson ([@benoithudson](https://github.com/benoithudson))
2021
- Bradley Friedman ([@leith-bartrich](https://github.com/leith-bartrich))

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2929
together with Nuitka
3030
- Fixes bug where delegates get casts (dotnetcore)
3131
- Determine size of interpreter longs at runtime
32+
- Handling exceptions ocurred in ModuleObject's getattribute
3233

3334
## [2.4.0][]
3435

ci/appveyor_run_tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if ($PYTHON_STATUS -ne 0) {
5555
# Run Embedded tests with C# coverage
5656
Write-Host ("Starting embedded tests") -ForegroundColor "Green"
5757
.$OPENCOVER -register:user -searchdirs:"$RUNTIME_DIR" -output:cs.coverage `
58-
-target:"$CS_RUNNER" -targetargs:"$CS_TESTS" `
58+
-target:"$CS_RUNNER" -targetargs:"$CS_TESTS --labels=All" `
5959
-filter:"+[*]Python.Runtime*" `
6060
-returntargetcode
6161
$CS_STATUS = $LastExitCode

src/embed_tests/Codecs.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace Python.EmbeddingTest {
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using NUnit.Framework;
6+
using Python.Runtime;
7+
using Python.Runtime.Codecs;
8+
9+
public class Codecs {
10+
[SetUp]
11+
public void SetUp() {
12+
PythonEngine.Initialize();
13+
}
14+
15+
[TearDown]
16+
public void Dispose() {
17+
PythonEngine.Shutdown();
18+
}
19+
20+
[Test]
21+
public void ConversionsGeneric() {
22+
ConversionsGeneric<ValueTuple<int, string, object>, ValueTuple>();
23+
}
24+
25+
static void ConversionsGeneric<T, TTuple>() {
26+
TupleCodec<TTuple>.Register();
27+
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
28+
T restored = default;
29+
using (Py.GIL())
30+
using (var scope = Py.CreateScope()) {
31+
void Accept(T value) => restored = value;
32+
var accept = new Action<T>(Accept).ToPython();
33+
scope.Set(nameof(tuple), tuple);
34+
scope.Set(nameof(accept), accept);
35+
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
36+
Assert.AreEqual(expected: tuple, actual: restored);
37+
}
38+
}
39+
40+
[Test]
41+
public void ConversionsObject() {
42+
ConversionsObject<ValueTuple<int, string, object>, ValueTuple>();
43+
}
44+
static void ConversionsObject<T, TTuple>() {
45+
TupleCodec<TTuple>.Register();
46+
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
47+
T restored = default;
48+
using (Py.GIL())
49+
using (var scope = Py.CreateScope()) {
50+
void Accept(object value) => restored = (T)value;
51+
var accept = new Action<object>(Accept).ToPython();
52+
scope.Set(nameof(tuple), tuple);
53+
scope.Set(nameof(accept), accept);
54+
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
55+
Assert.AreEqual(expected: tuple, actual: restored);
56+
}
57+
}
58+
59+
[Test]
60+
public void TupleRoundtripObject() {
61+
TupleRoundtripObject<ValueTuple<int, string, object>, ValueTuple>();
62+
}
63+
static void TupleRoundtripObject<T, TTuple>() {
64+
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
65+
using (Py.GIL()) {
66+
var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
67+
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out object restored));
68+
Assert.AreEqual(expected: tuple, actual: restored);
69+
}
70+
}
71+
72+
[Test]
73+
public void TupleRoundtripGeneric() {
74+
TupleRoundtripGeneric<ValueTuple<int, string, object>, ValueTuple>();
75+
}
76+
77+
static void TupleRoundtripGeneric<T, TTuple>() {
78+
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
79+
using (Py.GIL()) {
80+
var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
81+
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out T restored));
82+
Assert.AreEqual(expected: tuple, actual: restored);
83+
}
84+
}
85+
}
86+
}

src/embed_tests/Python.EmbeddingTest.15.csproj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
2424
<PythonBuildDir Condition="'$(TargetFramework)'=='net40' AND '$(PythonBuildDir)' == ''">$(SolutionDir)\bin\</PythonBuildDir>
2525
<PublishDir Condition="'$(TargetFramework)'!='net40'">$(OutputPath)\$(TargetFramework)_publish</PublishDir>
26-
<LangVersion>6</LangVersion>
26+
<LangVersion>7.3</LangVersion>
2727
<ErrorReport>prompt</ErrorReport>
2828
<CustomDefineConstants Condition="'$(CustomDefineConstants)' == ''">$(PYTHONNET_DEFINE_CONSTANTS)</CustomDefineConstants>
2929
<BaseDefineConstants>XPLAT</BaseDefineConstants>
@@ -77,13 +77,17 @@
7777

7878

7979
<ItemGroup>
80-
<PackageReference Include="NUnit" Version="3.7.1" />
81-
<PackageReference Include="NUnit.ConsoleRunner" Version="3.7.0" />
82-
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
83-
<PackageReference Include="NUnitLite" Version="3.7.2" />
80+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
81+
<PackageReference Include="NUnit" Version="3.12.0" />
82+
<PackageReference Include="NUnit.ConsoleRunner" Version="3.11.1" />
83+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
84+
<PrivateAssets>all</PrivateAssets>
85+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
86+
</PackageReference>
87+
<PackageReference Include="NUnitLite" Version="3.12.0" />
8488
</ItemGroup>
8589
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
86-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
90+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
8791
</ItemGroup>
8892
<ItemGroup>
8993
<ProjectReference Include="..\runtime\Python.Runtime.15.csproj" />

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<NoWarn>1591</NoWarn>
1515
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
1616
<PythonBuildDir Condition=" '$(PythonBuildDir)' == '' ">$(SolutionDir)\bin\</PythonBuildDir>
17-
<LangVersion>6</LangVersion>
17+
<LangVersion>7.3</LangVersion>
1818
<RestorePackages>true</RestorePackages>
1919
<ErrorReport>prompt</ErrorReport>
2020
</PropertyGroup>
@@ -70,8 +70,11 @@
7070
</PropertyGroup>
7171
<ItemGroup>
7272
<Reference Include="Microsoft.CSharp" />
73-
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
74-
<HintPath>..\..\packages\NUnit.3.7.1\lib\net40\nunit.framework.dll</HintPath>
73+
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
74+
<HintPath>..\..\packages\NUnit.3.12.0\lib\net40\nunit.framework.dll</HintPath>
75+
</Reference>
76+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
77+
<HintPath>..\..\packages\System.ValueTuple.4.5.0\lib\portable-net40+sl4+win8+wp8\System.ValueTuple.dll</HintPath>
7578
</Reference>
7679
<Reference Include="System" />
7780
</ItemGroup>
@@ -80,6 +83,7 @@
8083
<None Include="packages.config" />
8184
</ItemGroup>
8285
<ItemGroup>
86+
<Compile Include="Codecs.cs" />
8387
<Compile Include="dynamic.cs" />
8488
<Compile Include="pyimport.cs" />
8589
<Compile Include="pyinitialize.cs" />
@@ -127,4 +131,10 @@
127131
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
128132
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
129133
</Target>
134+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
135+
<PropertyGroup>
136+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
137+
</PropertyGroup>
138+
<Error Condition="!Exists('..\..\packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\NUnit.3.12.0\build\NUnit.props'))" />
139+
</Target>
130140
</Project>

src/embed_tests/TestPyScope.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using NUnit.Framework;
34
using Python.Runtime;
45

@@ -337,9 +338,12 @@ public void TestThread()
337338
//add function to the scope
338339
//can be call many times, more efficient than ast
339340
ps.Exec(
341+
"import clr\n" +
342+
"from System.Threading import Thread\n" +
340343
"def update():\n" +
341344
" global res, th_cnt\n" +
342345
" res += bb + 1\n" +
346+
" Thread.MemoryBarrier()\n" +
343347
" th_cnt += 1\n"
344348
);
345349
}
@@ -364,8 +368,9 @@ public void TestThread()
364368
{
365369
cnt = ps.Get<int>("th_cnt");
366370
}
367-
System.Threading.Thread.Sleep(10);
371+
Thread.Sleep(10);
368372
}
373+
Thread.MemoryBarrier();
369374
using (Py.GIL())
370375
{
371376
var result = ps.Get<int>("res");

src/embed_tests/TestRuntime.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using NUnit.Framework;
34
using Python.Runtime;
45
using Python.Runtime.Platform;
@@ -110,9 +111,15 @@ public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test()
110111
// Create an instance of threading.Lock, which is one of the very few types that does not have the
111112
// TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check.
112113
var threading = Runtime.Runtime.PyImport_ImportModule("threading");
114+
Exceptions.ErrorCheck(threading);
113115
var threadingDict = Runtime.Runtime.PyModule_GetDict(threading);
116+
Exceptions.ErrorCheck(threadingDict);
114117
var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock");
118+
if (lockType == IntPtr.Zero)
119+
throw new KeyNotFoundException("class 'Lock' was not found in 'threading'");
120+
115121
var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, Runtime.Runtime.PyTuple_New(0));
122+
Exceptions.ErrorCheck(lockInstance);
116123

117124
Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance));
118125
Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance));

src/embed_tests/packages.config

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="NUnit" version="3.7.1" targetFramework="net40" />
4-
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net40" />
3+
<package id="NUnit" version="3.12.0" targetFramework="net40" />
4+
<package id="NUnit.ConsoleRunner" version="3.11.1" targetFramework="net40" />
5+
<package id="System.ValueTuple" version="4.5.0" targetFramework="net40" />
56
</packages>

src/perf_tests/BaselineComparisonConfig.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using BenchmarkDotNet.Configs;
77
using BenchmarkDotNet.Jobs;
8+
using BenchmarkDotNet.Horology;
89

910
namespace Python.PerformanceTests
1011
{
@@ -18,7 +19,11 @@ public BaselineComparisonConfig()
1819

1920
string deploymentRoot = BenchmarkTests.DeploymentRoot;
2021

21-
var baseJob = Job.Default;
22+
var baseJob = Job.Default
23+
.WithLaunchCount(1)
24+
.WithWarmupCount(3)
25+
.WithMaxIterationCount(100)
26+
.WithIterationTime(TimeInterval.FromMilliseconds(100));
2227
this.Add(baseJob
2328
.WithId("baseline")
2429
.WithEnvironmentVariable(EnvironmentVariableName,

src/perf_tests/Python.PerformanceTests.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
14+
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
1515
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
1616
<PrivateAssets>all</PrivateAssets>
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
</PackageReference>
1919
<PackageReference Include="nunit" Version="3.12.0" />
20-
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
21-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
20+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
24+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
2225
<PackageReference Include="pythonnet" Version="2.3.0" GeneratePathProperty="true">
2326
<IncludeAssets>compile</IncludeAssets>
2427
</PackageReference>

src/runtime/BorrowedReference.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Python.Runtime
2+
{
3+
using System;
4+
/// <summary>
5+
/// Represents a reference to a Python object, that is being lent, and
6+
/// can only be safely used until execution returns to the caller.
7+
/// </summary>
8+
readonly ref struct BorrowedReference
9+
{
10+
readonly IntPtr pointer;
11+
public bool IsNull => this.pointer == IntPtr.Zero;
12+
13+
/// <summary>Gets a raw pointer to the Python object</summary>
14+
public IntPtr DangerousGetAddress()
15+
=> this.IsNull ? throw new NullReferenceException() : this.pointer;
16+
17+
BorrowedReference(IntPtr pointer)
18+
{
19+
this.pointer = pointer;
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)