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

Skip to content

Commit 62ae107

Browse files
committed
Code review fixes
1 parent 833e836 commit 62ae107

File tree

9 files changed

+91
-70
lines changed

9 files changed

+91
-70
lines changed

src/domain_tests/Python.DomainReloadTests.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,4 @@
2323
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" />
2424
</ItemGroup>
2525

26-
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
27-
<Reference Include="Microsoft.CSharp" />
28-
</ItemGroup>
29-
3026
</Project>

src/domain_tests/TestRunner.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,7 @@ public static int Main()
913913
PythonEngine.Initialize(mode:{0});
914914
using (Py.GIL())
915915
{{
916-
// Because the generated assemblies are in the $TEMP folder, add it to the path
917-
var temp = Path.GetTempPath();
916+
var temp = AppDomain.CurrentDomain.BaseDirectory;
918917
dynamic sys = Py.Import(""sys"");
919918
sys.path.append(new PyString(temp));
920919
dynamic test_mod = Py.Import(""domain_test_module.mod"");
@@ -938,6 +937,8 @@ public static int Main()
938937
";
939938
readonly static string PythonDllLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Python.Runtime.dll");
940939

940+
static string TestPath = null;
941+
941942
public static int Main(string[] args)
942943
{
943944
TestCase testCase;
@@ -951,16 +952,11 @@ public static int Main(string[] args)
951952
Console.WriteLine($"-- Looking for domain reload test case {testName}");
952953
testCase = Cases.First(c => c.Name == testName);
953954
}
955+
954956
Console.WriteLine($"-- Running domain reload test case: {testCase.Name}");
955957

956-
var tempFolderPython = Path.Combine(Path.GetTempPath(), "Python.Runtime.dll");
957-
if (File.Exists(tempFolderPython))
958-
{
959-
File.Delete(tempFolderPython);
960-
}
958+
SetupTestFolder(testCase.Name);
961959

962-
File.Copy(PythonDllLocation, tempFolderPython);
963-
964960
CreatePythonModule(testCase);
965961
{
966962
var runnerAssembly = CreateCaseRunnerAssembly(verb:"before");
@@ -989,9 +985,32 @@ public static int Main(string[] args)
989985
RunAndUnload(runnerDomain, runnerAssembly);
990986
}
991987
}
988+
989+
// Don't delete unconditionally. It's sometimes useful to leave the
990+
// folder behind to debug failing tests.
991+
TeardownTestFolder();
992+
992993
return 0;
993994
}
994-
#if !NETCOREAPP
995+
996+
static void SetupTestFolder(string testCaseName)
997+
{
998+
TestPath = Path.Combine(Path.GetTempPath(), $"Python.TestRunner.{testCaseName}");
999+
if (Directory.Exists(TestPath))
1000+
{
1001+
Directory.Delete(TestPath, recursive: true);
1002+
}
1003+
Directory.CreateDirectory(TestPath);
1004+
File.Copy(PythonDllLocation, Path.Combine(TestPath, "Python.Runtime.dll"));
1005+
}
1006+
1007+
static void TeardownTestFolder()
1008+
{
1009+
if (Directory.Exists(TestPath))
1010+
{
1011+
Directory.Delete(TestPath, recursive: true);
1012+
}
1013+
}
9951014

9961015
static void RunAndUnload(AppDomain domain, string assemblyPath)
9971016
{
@@ -1027,7 +1046,7 @@ static string CreateAssembly(string name, string code, bool exe = false)
10271046
CompilerParameters parameters = new CompilerParameters();
10281047
parameters.GenerateExecutable = exe;
10291048
var assemblyName = name;
1030-
var assemblyFullPath = Path.Combine(Path.GetTempPath(), assemblyName);
1049+
var assemblyFullPath = Path.Combine(TestPath, assemblyName);
10311050
parameters.OutputAssembly = assemblyFullPath;
10321051
parameters.ReferencedAssemblies.Add("System.dll");
10331052
parameters.ReferencedAssemblies.Add("System.Core.dll");
@@ -1062,7 +1081,7 @@ static AppDomain CreateDomain(string name)
10621081
var currentDomain = AppDomain.CurrentDomain;
10631082
var domainsetup = new AppDomainSetup()
10641083
{
1065-
ApplicationBase = Path.GetTempPath(),
1084+
ApplicationBase = TestPath,
10661085
ConfigurationFile = currentDomain.SetupInformation.ConfigurationFile,
10671086
LoaderOptimization = LoaderOptimization.SingleDomain,
10681087
PrivateBinPath = "."
@@ -1077,7 +1096,7 @@ static AppDomain CreateDomain(string name)
10771096

10781097
static string CreatePythonModule(TestCase testCase)
10791098
{
1080-
var modulePath = Path.Combine(Path.GetTempPath(), "domain_test_module");
1099+
var modulePath = Path.Combine(TestPath, "domain_test_module");
10811100
if (Directory.Exists(modulePath))
10821101
{
10831102
Directory.Delete(modulePath, recursive: true);
@@ -1092,7 +1111,5 @@ static string CreatePythonModule(TestCase testCase)
10921111

10931112
return null;
10941113
}
1095-
#endif
1096-
10971114
}
10981115
}

src/runtime/Python.Runtime.csproj

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<PropertyGroup>
3-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
4-
<Platforms>AnyCPU</Platforms>
5-
<RootNamespace>Python.Runtime</RootNamespace>
6-
<AssemblyName>Python.Runtime</AssemblyName>
7-
<PackageId>pythonnet</PackageId>
8-
<PackageLicenseUrl>https://github.com/pythonnet/pythonnet/blob/master/LICENSE</PackageLicenseUrl>
9-
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
10-
<RepositoryType>git</RepositoryType>
11-
<PackageTags>python interop dynamic dlr Mono pinvoke</PackageTags>
12-
<PackageIconUrl>https://raw.githubusercontent.com/pythonnet/pythonnet/master/src/console/python-clear.ico</PackageIconUrl>
13-
<PackageProjectUrl>https://pythonnet.github.io/</PackageProjectUrl>
14-
<NoWarn>1591;NU1701</NoWarn>
15-
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
16-
</PropertyGroup>
17-
18-
<PropertyGroup>
19-
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
20-
</PropertyGroup>
21-
22-
<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
23-
<Compile Remove="interop*.cs" />
24-
<Compile Include="interop.cs" />
25-
<Compile Include="$(PythonInteropFile)" />
26-
</ItemGroup>
27-
28-
<ItemGroup>
29-
<None Remove="resources\clr.py" />
30-
<EmbeddedResource Include="resources\clr.py">
31-
<LogicalName>clr.py</LogicalName>
32-
</EmbeddedResource>
33-
</ItemGroup>
34-
35-
<ItemGroup>
36-
<PackageReference Include="System.Security.Permissions" Version="4.4.0" />
37-
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
38-
</ItemGroup>
39-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
4+
<Platforms>AnyCPU</Platforms>
5+
<RootNamespace>Python.Runtime</RootNamespace>
6+
<AssemblyName>Python.Runtime</AssemblyName>
7+
<PackageId>pythonnet</PackageId>
8+
<PackageLicenseUrl>https://github.com/pythonnet/pythonnet/blob/master/LICENSE</PackageLicenseUrl>
9+
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
10+
<RepositoryType>git</RepositoryType>
11+
<PackageTags>python interop dynamic dlr Mono pinvoke</PackageTags>
12+
<PackageIconUrl>https://raw.githubusercontent.com/pythonnet/pythonnet/master/src/console/python-clear.ico</PackageIconUrl>
13+
<PackageProjectUrl>https://pythonnet.github.io/</PackageProjectUrl>
14+
<NoWarn>1591;NU1701</NoWarn>
15+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
16+
</PropertyGroup>
17+
18+
<PropertyGroup>
19+
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
20+
</PropertyGroup>
21+
22+
<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
23+
<Compile Remove="interop*.cs" />
24+
<Compile Include="interop.cs" />
25+
<Compile Include="$(PythonInteropFile)" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<None Remove="resources\clr.py" />
30+
<EmbeddedResource Include="resources\clr.py">
31+
<LogicalName>clr.py</LogicalName>
32+
</EmbeddedResource>
33+
</ItemGroup>
34+
35+
<ItemGroup>
36+
<PackageReference Include="System.Security.Permissions" Version="4.4.0" />
37+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
38+
</ItemGroup>
39+
</Project>

src/runtime/StateSerialization/MaybeMemberInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
namespace Python.Runtime
88
{
9-
[Serializable]
10-
internal struct MaybeMemberInfo<T> : ISerializable where T: MemberInfo
9+
[Serializable]
10+
internal struct MaybeMemberInfo<T> : ISerializable where T : MemberInfo
1111
{
12-
public static implicit operator MaybeMemberInfo<T> (T ob) => new MaybeMemberInfo<T>(ob);
12+
public static implicit operator MaybeMemberInfo<T>(T ob) => new MaybeMemberInfo<T>(ob);
1313

1414
// .ToString() of the serialized object
1515
const string SerializationName = "s";
@@ -22,7 +22,7 @@ internal struct MaybeMemberInfo<T> : ISerializable where T: MemberInfo
2222
[NonSerialized]
2323
Exception deserializationException;
2424

25-
public string DeletedMessage
25+
public string DeletedMessage
2626
{
2727
get
2828
{
@@ -42,7 +42,7 @@ public T Value
4242
}
4343
}
4444

45-
public string Name {get{return name;}}
45+
public string Name => name;
4646
public bool Valid => info != null;
4747

4848
public override string ToString()

src/runtime/StateSerialization/MaybeType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Type Value
3636
}
3737
}
3838

39-
public string Name {get{return name;}}
39+
public string Name => name;
4040
public bool Valid => type != null;
4141

4242
public override string ToString()

src/runtime/classmanager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ internal static void SaveRuntimeData(RuntimeDataStorage storage)
129129
// raises an error. We don't care about it.
130130
Runtime.PyErr_Clear();
131131
}
132+
else if (Exceptions.ErrorOccurred())
133+
{
134+
throw new PythonException();
135+
}
132136
}
133137
// We modified the Type object, notify it we did.
134138
Runtime.PyType_Modified(cls.Value.tpHandle);

src/runtime/methodbinder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ internal class MethodBinder
2424

2525
[NonSerialized]
2626
public bool init = false;
27-
public bool allow_threads = true;
27+
public const bool DefaultAllowThreads = true;
28+
public bool allow_threads = DefaultAllowThreads;
2829

2930
internal MethodBinder()
3031
{
@@ -187,7 +188,7 @@ internal static int GetPrecedence(MethodBase mi)
187188
{
188189
if (mi == null)
189190
{
190-
return -1;
191+
return int.MaxValue;
191192
}
192193

193194
ParameterInfo[] pi = mi.GetParameters();
@@ -864,8 +865,8 @@ internal class MethodSorter : IComparer<MaybeMethodBase>
864865
{
865866
int IComparer<MaybeMethodBase>.Compare(MaybeMethodBase m1, MaybeMethodBase m2)
866867
{
867-
MethodBase me1 = m1.Valid ? m1.Value : null;
868-
MethodBase me2 = m2.Valid ? m2.Value : null;
868+
MethodBase me1 = m1.UnsafeValue;
869+
MethodBase me2 = m2.UnsafeValue;
869870
if (me1 == null && me2 == null)
870871
{
871872
return 0;

src/runtime/methodobject.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ internal class MethodObject : ExtensionType
2828
internal IntPtr doc;
2929
internal Type type;
3030

31-
// `allow_threads = true`: True being the default value of MethodBinder.allow_threads
32-
public MethodObject(Type type, string name, MethodInfo[] info, bool allow_threads = true)
31+
public MethodObject(Type type, string name, MethodInfo[] info, bool allow_threads = MethodBinder.DefaultAllowThreads)
3332
{
3433
this.type = type;
3534
this.name = name;

src/runtime/moduleobject.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ protected override void OnSave(InterDomainContext context)
351351
// raises an error. We don't care about it.
352352
Runtime.PyErr_Clear();
353353
}
354+
else if (Exceptions.ErrorOccurred())
355+
{
356+
throw new PythonException();
357+
}
354358
pair.Value.DecrRefCount();
355359
}
356360

0 commit comments

Comments
 (0)