8000 Merge branch 'master' into fix-enum-codec · pythonnet/pythonnet@734e549 · GitHub
[go: up one dir, main page]

Skip to content

Commit 734e549

Browse files
authored
Merge branch 'master' into fix-enum-codec
2 parents 8c52c73 + ac74383 commit 734e549

File tree

148 files changed

+4462
-5267
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+4462
-5267
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131

3232
- name: Setup .NET
3333
uses: actions/setup-dotnet@v1
34+
with:
35+
dotnet-version: '6.0.x'
3436

3537
- name: Set up Python ${{ matrix.python }}
3638
uses: actions/setup-python@v2

.github/workflows/nuget-preview.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525

2626
- name: Setup .NET
2727
uses: actions/setup-dotnet@v1
28+
with:
29+
dotnet-version: '6.0.x'
2830

2931
- name: Set up Python 3.8
3032
uses: actions/setup-python@v2

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ One must now either use enum members (e.g. `MyEnum.Option`), or use enum constru
6565
- BREAKING: Names of .NET types (e.g. `str(__class__)`) changed to better support generic types
6666
- BREAKING: overload resolution will no longer prefer basic types. Instead, first matching overload will
6767
be chosen.
68+
- BREAKING: `Exec` and `Eval` from `PythonEngine` no longer accept raw pointers.
6869
- BREAKING: .NET collections and arrays are no longer automatically converted to
6970
Python collections. Instead, they implement standard Python
7071
collection interfaces from `collections.abc`.
@@ -74,6 +75,7 @@ be of type `PyInt` instead of `System.Int32` due to possible loss of information
7475
Python `float` will continue to be converted to `System.Double`.
7576
- BREAKING: Python.NET will no longer implicitly convert types like `numpy.float64`, that implement `__float__` to
7677
`System.Single` and `System.Double`. An explicit conversion is required on Python or .NET side.
78+
- BREAKING: `PyObject.GetHashCode` can fail.
7779
- BREAKING: Python.NET will no longer implicitly convert any Python object to `System.Boolean`.
7880
- BREAKING: `PyObject.GetAttr(name, default)` now only ignores `AttributeError` (previously ignored all exceptions).
7981
- BREAKING: `PyObject` no longer implements `IEnumerable<PyObject>`.

Directory.Build.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<Project>
22
<PropertyGroup>
33
<VersionPrefix>3.0.0</VersionPrefix>
4-
<AssemblyCopyright>Copyright (c) 2006-2020 The Contributors of the Python.NET Project</AssemblyCopyright>
4+
<AssemblyCopyright>Copyright (c) 2006-2021 The Contributors of the Python.NET Project</AssemblyCopyright>
55
<AssemblyCompany>pythonnet</AssemblyCompany>
66
<AssemblyProduct>Python.NET</AssemblyProduct>
7-
<LangVersion>9.0</LangVersion>
7+
<LangVersion>10.0</LangVersion>
88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
12-
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0-3.final">
12+
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.0.1">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1515
</PackageReference>
16-
<PackageReference Include="Lost.NonCopyableAnalyzer" Version="0.7.0-m04">
16+
<PackageReference Include="NonCopyableAnalyzer" Version="0.7.0">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
recursive-include src/ *
2+
include Directory.Build.*
3+
include pythonnet.sln
4+
global-exclude **/obj/* **/bin/*

pythonnet.sln

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.30717.126
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.31912.275
44
MinimumVisualStudioVersion = 15.0.26124.0
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Runtime", "src\runtime\Python.Runtime.csproj", "{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}"
66
EndProject
@@ -54,6 +54,9 @@ Global
5454
Release|Any CPU = Release|Any CPU
5555
Release|x64 = Release|x64
5656
Release|x86 = Release|x86
57+
TraceAlloc|Any CPU = TraceAlloc|Any CPU
58+
TraceAlloc|x64 = TraceAlloc|x64
59+
TraceAlloc|x86 = TraceAlloc|x86
5760
EndGlobalSection
5861
GlobalSection(ProjectConfigurationPlatforms) = postSolution
5962
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -68,6 +71,12 @@ Global
6871
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.Release|x64.Build.0 = Release|Any CPU
6972
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.Release|x86.ActiveCfg = Release|Any CPU
7073
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.Release|x86.Build.0 = Release|Any CPU
74+
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.TraceAlloc|Any CPU.ActiveCfg = TraceAlloc|Any CPU
75+
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.TraceAlloc|Any CPU.Build.0 = TraceAlloc|Any CPU
76+
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.TraceAlloc|x64.ActiveCfg = Debug|Any CPU
77+
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.TraceAlloc|x64.Build.0 = Debug|Any CPU
78+
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.TraceAlloc|x86.ActiveCfg = Debug|Any CPU
79+
{4E8C8FE2-0FB8-4517-B2D9-5FB2D5FC849B}.TraceAlloc|x86.Build.0 = Debug|Any CPU
7180
{E6B01706-00BA-4144-9029-186AC42FBE9A}.Debug|Any CPU.ActiveCfg = Debug|x64
7281
{E6B01706-00BA-4144-9029-186AC42FBE9A}.Debug|Any CPU.Build.0 = Debug|x64
7382
{E6B01706-00BA-4144-9029-186AC42FBE9A}.Debug|x64.ActiveCfg = Debug|x64
@@ -80,18 +89,12 @@ Global
8089
{E6B01706-00BA-4144-9029-186AC42FBE9A}.Release|x64.Build.0 = Release|x64
8190
{E6B01706-00BA-4144-9029-186AC42FBE9A}.Release|x86.ActiveCfg = Release|x86
8291
{E6B01706-00BA-4144-9029-186AC42FBE9A}.Release|x86.Build.0 = Release|x86
83-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
84-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
85-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Debug|x64.ActiveCfg = Debug|Any CPU
86-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Debug|x64.Build.0 = Debug|Any CPU
87-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Debug|x86.ActiveCfg = Debug|Any CPU
88-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Debug|x86.Build.0 = Debug|Any CPU
89-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
90-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Release|Any CPU.Build.0 = Release|Any CPU
91-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Release|x64.ActiveCfg = Release|Any CPU
92-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Release|x64.Build.0 = Release|Any CPU
93-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Release|x86.ActiveCfg = Release|Any CPU
94-
{F9F5FA13-BC52-4C0B-BC1C-FE3C0B8FCCDD}.Release|x86.Build.0 = Release|Any CPU
92+
{E6B01706-00BA-4144-9029-186AC42FBE9A}.TraceAlloc|Any CPU.ActiveCfg = Debug|x64
93+
{E6B01706-00BA-4144-9029-186AC42FBE9A}.TraceAlloc|Any CPU.Build.0 = Debug|x64
94+
{E6B01706-00BA-4144-9029-186AC42FBE9A}.TraceAlloc|x64.ActiveCfg = Debug|x64
95+
{E6B01706-00BA-4144-9029-186AC42FBE9A}.TraceAlloc|x64.Build.0 = Debug|x64
96+
{E6B01706-00BA-4144-9029-186AC42FBE9A}.TraceAlloc|x86.ActiveCfg = Debug|x86
97+
{E6B01706-00BA-4144-9029-186AC42FBE9A}.TraceAlloc|x86.Build.0 = Debug|x86
9598
{819E089B-4770-400E-93C6-4F7A35F0EA12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
9699
{819E089B-4770-400E-93C6-4F7A35F0EA12}.Debug|Any CPU.Build.0 = Debug|Any CPU
97100
{819E089B-4770-400E-93C6-4F7A35F0EA12}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -104,6 +107,12 @@ Global
104107
{819E089B-4770-400E-93C6-4F7A35F0EA12}.Release|x64.Build.0 = Release|Any CPU
105108
{819E089B-4770-400E-93C6-4F7A35F0EA12}.Release|x86.ActiveCfg = Release|Any CPU
106109
{819E089B-4770-400E-93C6-4F7A35F0EA12}.Release|x86.Build.0 = Release|Any CPU
110+
{819E089B-4770-400E-93C6-4F7A35F0EA12}.TraceAlloc|Any CPU.ActiveCfg = Debug|Any CPU
111+
{819E089B-4770-400E-93C6-4F7A35F0EA12}.TraceAlloc|Any CPU.Build.0 = Debug|Any CPU
112+
{819E089B-4770-400E-93C6-4F7A35F0EA12}.TraceAlloc|x64.ActiveCfg = Debug|Any CPU
113+
{819E089B-4770-400E-93C6-4F7A35F0EA12}.TraceAlloc|x64.Build.0 = Debug|Any CPU
114+
{819E089B-4770-400E-93C6-4F7A35F0EA12}.TraceAlloc|x86.ActiveCfg = Debug|Any CPU
115+
{819E089B-4770-400E-93C6-4F7A35F0EA12}.TraceAlloc|x86.Build.0 = Debug|Any CPU
107116
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
108117
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.Debug|Any CPU.Build.0 = Debug|Any CPU
109118
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -116,6 +125,12 @@ Global
116125
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.Release|x64.Build.0 = Release|Any CPU
117126
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.Release|x86.ActiveCfg = Release|Any CPU
118127
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.Release|x86.Build.0 = Release|Any CPU
128+
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.TraceAlloc|Any CPU.ActiveCfg = Debug|Any CPU
129+
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.TraceAlloc|Any CPU.Build.0 = Debug|Any CPU
130+
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.TraceAlloc|x64.ActiveCfg = Debug|Any CPU
131+
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.TraceAlloc|x64.Build.0 = Debug|Any CPU
132+
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.TraceAlloc|x86.ActiveCfg = Debug|Any CPU
133+
{14EF9518-5BB7-4F83-8686-015BD2CC788E}.TraceAlloc|x86.Build.0 = Debug|Any CPU
119134
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.Debug|Any CPU.ActiveCfg = Debug|x64
120135
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.Debug|Any CPU.Build.0 = Debug|x64
121136
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.Debug|x64.ActiveCfg = Debug|x64
@@ -128,6 +143,12 @@ Global
128143
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.Release|x64.Build.0 = Release|x64
129144
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.Release|x86.ActiveCfg = Release|x86
130145
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.Release|x86.Build.0 = Release|x86
146+
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.TraceAlloc|Any CPU.ActiveCfg = Debug|x64
147+
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.TraceAlloc|Any CPU.Build.0 = Debug|x64
148+
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.TraceAlloc|x64.ActiveCfg = Debug|x64
149+
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.TraceAlloc|x64.Build.0 = Debug|x64
150+
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.TraceAlloc|x86.ActiveCfg = Debug|x86
151+
{4F2EA4A1-7ECA-48B5-8077-7A3C366F9931}.TraceAlloc|x86.Build.0 = Debug|x86
131152
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
132153
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
133154
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -140,6 +161,12 @@ Global
140161
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.Release|x64.Build.0 = Release|Any CPU
141162
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.Release|x86.ActiveCfg = Release|Any CPU
142163
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.Release|x86.Build.0 = Release|Any CPU
164+
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.TraceAlloc|Any CPU.ActiveCfg = Debug|Any CPU
165+
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.TraceAlloc|Any CPU.Build.0 = Debug|Any CPU
166+
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.TraceAlloc|x64.ActiveCfg = Debug|Any CPU
167+
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.TraceAlloc|x64.Build.0 = Debug|Any CPU
168+
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.TraceAlloc|x86.ActiveCfg = Debug|Any CPU
169+
{F2FB6DA3-318E-4F30-9A1F-932C667E38C5}.TraceAlloc|x86.Build.0 = Debug|Any CPU
143170
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
144171
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.Debug|Any CPU.Build.0 = Debug|Any CPU
145172
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -152,6 +179,12 @@ Global
152179
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.Release|x64.Build.0 = Release|Any CPU
153180
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.Release|x86.ActiveCfg = Release|Any CPU
154181
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.Release|x86.Build.0 = Release|Any CPU
182+
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.TraceAlloc|Any CPU.ActiveCfg = Debug|Any CPU
183+
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.TraceAlloc|Any CPU.Build.0 = Debug|Any CPU
184+
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.TraceAlloc|x64.ActiveCfg = Debug|Any CPU
185+
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.TraceAlloc|x64.Build.0 = Debug|Any CPU
186+
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.TraceAlloc|x86.ActiveCfg = Debug|Any CPU
187+
{35CBBDEB-FC07-4D04-9D3E-F88FC180110B}.TraceAlloc|x86.Build.0 = Debug|Any CPU
155188
EndGlobalSection
156189
GlobalSection(SolutionProperties) = preSolution
157190
HideSolutionNode = FALSE

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class build_dotnet(Command):
2626

2727
description = "Build DLLs with dotnet-cli"
2828
user_options = [
29-
("dotnet-config", None, "dotnet build configuration"),
29+
("dotnet-config=", None, "dotnet build configuration"),
3030
(
3131
"inplace",
3232
"i",

src/console/Console.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
3+
<TargetFrameworks>net472;net6.0</TargetFrameworks>
44
<Platforms>x64;x86</Platforms>
55
<OutputType>Exe</OutputType>
66
<AssemblyName>nPython</AssemblyName>

src/embed_tests/CallableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void SetUp()
1414
{
1515
PythonEngine.Initialize();
1616
using var locals = new PyDict();
17-
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals.Handle);
17+
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals);
1818
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
1919
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider());
2020
}

src/embed_tests/CodecGroups.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public void Encodes()
4949
};
5050

5151
var uri = group.TryEncode(new Uri("data:"));
52-
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri.Handle);
52+
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri);
5353
Assert.AreSame(encoder1, clrObject.inst);
5454
Assert.AreNotSame(encoder2, clrObject.inst);
5555

5656
var tuple = group.TryEncode(Tuple.Create(1));
57-
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple.Handle);
57+
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple);
5858
Assert.AreSame(encoder0, clrObject.inst);
5959
}
6060

src/embed_tests/Inheritance.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class Inheritance
1313
public void SetUp()
1414
{
1515
PythonEngine.Initialize();
16-
var locals = new PyDict();
17-
PythonEngine.Exec(InheritanceTestBaseClassWrapper.ClassSo 10000 urceCode, locals: locals.Handle);
16+
using var locals = new PyDict();
17+
PythonEngine.Exec(InheritanceTestBaseClassWrapper.ClassSourceCode, locals: locals);
1818
ExtraBaseTypeProvider.ExtraBase = new PyType(locals[InheritanceTestBaseClassWrapper.ClassName]);
1919
var baseTypeProviders = PythonEngine.InteropConfiguration.PythonBaseTypeProviders;
2020
baseTypeProviders.Add(new ExtraBaseTypeProvider());
@@ -172,7 +172,7 @@ public int XProp
172172
{
173173
return scope.Eval<int>($"super(this.__class__, this).{nameof(XProp)}");
174174
}
175-
catch (PythonException ex) when (ex.Type.Handle == Exceptions.AttributeError)
175+
catch (PythonException ex) when (PythonReferenceComparer.Instance.Equals(ex.Type, Exceptions.AttributeError))
176176
{
177177
if (this.extras.TryGetValue(nameof(this.XProp), out object value))
178178
return (int)value;

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>net472;net6.0</TargetFrameworks>
55
<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
66
<SignAssembly>true</SignAssembly>
77
</PropertyGroup>

src/embed_tests/References.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@ public void MoveToPyObject_SetsNull()
3939
public void CanBorrowFromNewReference()
4040
{
4141
var dict = new PyDict();
42-
NewReference reference = Runtime.PyDict_Items(dict.Reference);
43-
try
44-
{
45-
PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(reference));
46-
}
47-
finally
48-
{
49-
reference.Dispose();
50-
}
42+
using NewReference reference = Runtime.PyDict_Items(dict.Reference);
43+
BorrowedReference borrowed = reference.BorrowOrThrow();
44+
PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(borrowed));
5145
}
5246
}
5347
}

src/embed_tests/TestConverter.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void TestConvertSingleToManaged(
4242
var pyFloat = new PyFloat(testValue);
4343

4444
object convertedValue;
45-
var converted = Converter.ToManaged(pyFloat.Handle, typeof(float), out convertedValue, false);
45+
var converted = Converter.ToManaged(pyFloat, typeof(float), out convertedValue, false);
4646

4747
Assert.IsTrue(converted);
4848
Assert.IsTrue(((float) convertedValue).Equals(testValue));
@@ -56,7 +56,7 @@ public void TestConvertDoubleToManaged(
5656
var pyFloat = new PyFloat(testValue);
5757

5858
object convertedValue;
59-
var converted = Converter.ToManaged(pyFloat.Handle, typeof(double), out convertedValue, false);
59+
var converted = Converter.ToManaged(pyFloat, typeof(double), out convertedValue, false);
6060

6161
Assert.IsTrue(converted);
6262
Assert.IsTrue(((double) convertedValue).Equals(testValue));
@@ -77,7 +77,7 @@ public void CovertTypeError()
7777
object value;
7878
try
7979
{
80-
bool res = Converter.ToManaged(s.Handle, type, out value, true);
80+
bool res = Converter.ToManaged(s, type, out value, true);
8181
Assert.IsFalse(res);
8282
var bo = Exceptions.ExceptionMatches(Exceptions.TypeError);
8383
Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.TypeError)
@@ -96,13 +96,13 @@ public void ConvertOverflow()
9696
{
9797
using (var num = new PyInt(ulong.MaxValue))
9898
{
99-
IntPtr largeNum = PyRuntime.PyNumber_Add(num.Handle, num.Handle);
99+
using var largeNum = PyRuntime.PyNumber_Add(num, num);
100100
try
101101
{
102102
object value;
103103
foreach (var type in _numTypes)
104104
{
105-
bool res = Converter.ToManaged(largeNum, type, out value, true);
105+
bool res = Converter.ToManaged(largeNum.BorrowOrThrow(), type, out value, true);
106106
Assert.IsFalse(res);
107107
Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.OverflowError));
108108
Exceptions.Clear();
@@ -111,7 +111,6 @@ public void ConvertOverflow()
111111
finally
112112
{
113113
Exceptions.Clear();
114-
PyRuntime.XDecref(largeNum);
115114
}
116115
}
117116
}
@@ -147,7 +146,7 @@ public void RawListProxy()
147146
{
148147
var list = new List<string> {"hello", "world"};
149148
var listProxy = PyObject.FromManagedObject(list);
150-
var clrObject = (CLRObject)ManagedType.GetManagedObject(listProxy.Handle);
149+
var clrObject = (CLRObject)ManagedType.GetManagedObject(listProxy);
151150
Assert.AreSame(list, clrObject.inst);
152151
}
153152

@@ -156,7 +155,7 @@ public void RawPyObjectProxy()
156155
{
157156
var pyObject = "hello world!".ToPython();
158157
var pyObjectProxy = PyObject.FromManagedObject(pyObject);
159-
var clrObject = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy.Handle);
158+
var clrObject = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy);
160159
Assert.AreSame(pyObject, clrObject.inst);
161160

162161
var proxiedHandle = pyObjectProxy.GetAttr("Handle").As<IntPtr>();

src/embed_tests/TestCustomMarshal.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public static void GetManagedStringTwice()
2323
{
2424
const string expected = "FooBar";
2525

26-
IntPtr op = Runtime.Runtime.PyString_FromString(expected);
27-
string s1 = Runtime.Runtime.GetManagedString(op);
28-
string s2 = Runtime.Runtime.GetManagedString(op);
26+
using var op = Runtime.Runtime.PyString_FromString(expected);
27+
string s1 = Runtime.Runtime.GetManagedString(op.BorrowOrThrow());
28+
string s2 = Runtime.Runtime.GetManagedString(op.Borrow());
2929

30-
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
30+
Assert.AreEqual(1, Runtime.Runtime.Refcount32(op.Borrow()));
3131
Assert.AreEqual(expected, s1);
3232
Assert.AreEqual(expected, s2);
3333
}

0 commit comments

Comments
 (0)
0