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

Skip to content

Commit 76e9f7e

Browse files
committed
Refactor clrmodule/console
Reorder Using and use it to clean up calls Replace var were type isn't fully obvious
1 parent f733ae7 commit 76e9f7e

File tree

5 files changed

+51
-47
lines changed

5 files changed

+51
-47
lines changed

src/clrmodule/AssemblyInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following

src/clrmodule/ClrModule.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//============================================================================
2-
// This file replaces the hand-maintained stub that used to implement clr.dll.
2+
// This file replaces the hand-maintained stub that used to implement clr.dll.
33
// This is a line-by-line port from IL back to C#.
44
// We now use RGiesecke.DllExport on the required static init method so it can be
55
// loaded by a standard CPython interpreter as an extension module. When it
@@ -11,31 +11,34 @@
1111

1212
// If defined, the "pythonRuntimeVersionString" variable must be set to
1313
// Python.Runtime's current version.
14-
1514
#define USE_PYTHON_RUNTIME_VERSION
1615

1716
// If defined, the "PythonRuntimePublicKeyTokenData" data array must be
1817
// set to Python.Runtime's public key token. (sn -T Python.Runtin.dll)
1918
#define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
2019

21-
// If DEBUG_PRINT is defined in the Build Properties, a few System.Console.WriteLine
20+
// If DEBUG_PRINT is defined in the Build Properties, a few Console.WriteLine
2221
// calls are made to indicate what's going on during the load...
2322
//============================================================================
2423
using System;
25-
24+
using System.Globalization;
25+
using System.IO;
26+
using System.Reflection;
27+
using System.Runtime.InteropServices;
28+
using RGiesecke.DllExport;
2629

2730
public class clrModule
2831
{
2932
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
30-
[RGiesecke.DllExport.DllExport("PyInit_clr", System.Runtime.InteropServices.CallingConvention.StdCall)]
33+
[DllExport("PyInit_clr", CallingConvention.StdCall)]
3134
public static IntPtr PyInit_clr()
3235
#else
33-
[RGiesecke.DllExport.DllExport("initclr", System.Runtime.InteropServices.CallingConvention.StdCall)]
36+
[DllExport("initclr", CallingConvention.StdCall)]
3437
public static void initclr()
3538
#endif
3639
{
3740
#if DEBUG_PRINT
38-
System.Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... ");
41+
Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... ");
3942
#endif
4043
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
4144
var pythonRuntimePublicKeyTokenData = new byte[] { 0x50, 0x00, 0xfe, 0xa6, 0xcb, 0xa7, 0x02, 0xdd };
@@ -47,26 +50,26 @@ public static void initclr()
4750
// - ApplicationBase
4851
// - A PrivateBinPath under ApplicationBase
4952
// With an unsigned assembly, the GAC is skipped.
50-
var pythonRuntimeName = new System.Reflection.AssemblyName("Python.Runtime")
53+
var pythonRuntimeName = new AssemblyName("Python.Runtime")
5154
{
5255
#if USE_PYTHON_RUNTIME_VERSION
53-
Version = new System.Version("4.0.0.1"),
56+
Version = new Version("4.0.0.1"),
5457
#endif
55-
CultureInfo = System.Globalization.CultureInfo.InvariantCulture,
58+
CultureInfo = CultureInfo.InvariantCulture
5659
};
5760
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
5861
pythonRuntimeName.SetPublicKeyToken(pythonRuntimePublicKeyTokenData);
5962
#endif
6063
// We've got the AssemblyName with optional features; try to load it.
61-
System.Reflection.Assembly pythonRuntime;
64+
Assembly pythonRuntime;
6265
try
6366
{
64-
pythonRuntime = System.Reflection.Assembly.Load(pythonRuntimeName);
67+
pythonRuntime = Assembly.Load(pythonRuntimeName);
6568
#if DEBUG_PRINT
66-
System.Console.WriteLine("Success!");
69+
Console.WriteLine("Success!");
6770
#endif
6871
}
69-
catch (System.IO.IOException)
72+
catch (IOException)
7073
{
7174
try
7275
{
@@ -79,20 +82,22 @@ public static void initclr()
7982
// http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
8083
// http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx
8184

82-
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
83-
var assemblyDirectory = System.IO.Path.GetDirectoryName(executingAssembly.Location);
85+
Assembly executingAssembly = Assembly.GetExecutingAssembly();
86+
string assemblyDirectory = Path.GetDirectoryName(executingAssembly.Location);
8487
if (assemblyDirectory == null)
85-
throw new System.InvalidOperationException(executingAssembly.Location);
86-
var pythonRuntimeDllPath = System.IO.Path.Combine(assemblyDirectory, "Python.Runtime.dll");
88+
{
89+
throw new InvalidOperationException(executingAssembly.Location);
90+
}
91+
string pythonRuntimeDllPath = Path.Combine(assemblyDirectory, "Python.Runtime.dll");
8792
#if DEBUG_PRINT
88-
System.Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath);
93+
Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath);
8994
#endif
90-
pythonRuntime = System.Reflection.Assembly.LoadFrom(pythonRuntimeDllPath);
95+
pythonRuntime = Assembly.LoadFrom(pythonRuntimeDllPath);
9196
}
92-
catch (System.InvalidOperationException)
97+
catch (InvalidOperationException)
9398
{
9499
#if DEBUG_PRINT
95-
System.Console.WriteLine("Could not load Python.Runtime, so sad.");
100+
Console.WriteLine("Could not load Python.Runtime");
96101
#endif
97102
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
98103
return IntPtr.Zero;
@@ -104,12 +109,12 @@ public static void initclr()
104109

105110
// Once here, we've successfully loaded SOME version of Python.Runtime
106111
// So now we get the PythonEngine and execute the InitExt method on it.
107-
var pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
112+
Type pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
108113

109114
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
110-
return (IntPtr)pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
115+
return (IntPtr)pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
111116
#else
112-
pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
117+
pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
113118
#endif
114119
}
115120
}

src/console/Console.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
<ItemGroup>
197197
<Content Include="python-clear.ico" />
198198
<EmbeddedResource Include="$(PythonBuildDir)\Python.Runtime.dll">
199-
<LogicalName>Python.Runtime.dll</LogicalName>
199+
<LogicalName>Python.Runtime.dll</LogicalName>
200200
</EmbeddedResource>
201201
</ItemGroup>
202202
<ItemGroup>

src/console/assemblyinfo.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
using System;
22
using System.Reflection;
3-
using System.Security.Permissions;
4-
using System.Runtime.InteropServices;
53
using System.Resources;
4+
using System.Runtime.InteropServices;
5+
using System.Security.Permissions;
66

7-
[assembly: System.Reflection.AssemblyProduct("Python for .NET")]
8-
[assembly: System.Reflection.AssemblyVersion("2.4.2.7")]
9-
[assembly: AssemblyTitleAttribute("Python Console")]
10-
[assembly: AssemblyDefaultAliasAttribute("python.exe")]
7+
[assembly: AssemblyProduct("Python for .NET")]
8+
[assembly: AssemblyVersion("2.4.2.7")]
9+
[assembly: AssemblyTitle("Python Console")]
10+
[assembly: AssemblyDefaultAlias("python.exe")]
1111
[assembly: CLSCompliant(true)]
1212
[assembly: ComVisible(false)]
13-
[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum,
13+
[assembly: PermissionSet(SecurityAction.RequestMinimum,
1414
Name = "FullTrust")]
15-
[assembly: AssemblyDescriptionAttribute("")]
16-
[assembly: AssemblyCopyrightAttribute("MIT License")]
17-
[assembly: AssemblyFileVersionAttribute("2.0.0.4")]
18-
[assembly: NeutralResourcesLanguageAttribute("en")]
15+
[assembly: AssemblyDescription("")]
16+
[assembly: AssemblyCopyright("MIT License")]
17+
[assembly: AssemblyFileVersion("2.0.0.4")]
18+
[assembly: NeutralResourcesLanguage("en")]

src/console/pythonconsole.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
2-
using System.Reflection;
32
using System.Collections.Generic;
3+
using System.Reflection;
44
using Python.Runtime;
55

66
namespace Python.Runtime
77
{
88
public sealed class PythonConsole
99
{
10+
private static AssemblyLoader assemblyLoader = new AssemblyLoader();
11+
1012
private PythonConsole()
1113
{
1214
}
@@ -26,7 +28,7 @@ public static int Main(string[] args)
2628
return i;
2729
}
2830

29-
// Register a callback function to load embedded assmeblies.
31+
// Register a callback function to load embedded assemblies.
3032
// (Python.Runtime.dll is included as a resource)
3133
private sealed class AssemblyLoader
3234
{
@@ -39,7 +41,7 @@ public AssemblyLoader()
3941
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
4042
{
4143
string shortName = args.Name.Split(',')[0];
42-
String resourceName = shortName + ".dll";
44+
string resourceName = string.Format("{0}.dll", shortName);
4345

4446
if (loadedAssemblies.ContainsKey(resourceName))
4547
{
@@ -51,7 +53,7 @@ public AssemblyLoader()
5153
{
5254
if (stream != null)
5355
{
54-
Byte[] assemblyData = new Byte[stream.Length];
56+
var assemblyData = new byte[stream.Length];
5557
stream.Read(assemblyData, 0, assemblyData.Length);
5658
Assembly assembly = Assembly.Load(assemblyData);
5759
loadedAssemblies[resourceName] = assembly;
@@ -62,8 +64,6 @@ public AssemblyLoader()
6264
return null;
6365
};
6466
}
65-
};
66-
67-
private static AssemblyLoader assemblyLoader = new AssemblyLoader();
68-
};
69-
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)