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

Skip to content

Commit 2e5fab6

Browse files
committed
Replace #if DEBUG with conditional attribute
1 parent 01fe979 commit 2e5fab6

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/clrmodule/ClrModule.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// calls are made to indicate what's going on during the load...
2222
//============================================================================
2323
using System;
24+
using System.Diagnostics;
2425
using System.Globalization;
2526
using System.IO;
2627
using System.Reflection;
@@ -37,9 +38,7 @@ public static IntPtr PyInit_clr()
3738
public static void initclr()
3839
#endif
3940
{
40-
#if DEBUG
41-
Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... ");
42-
#endif
41+
debugPrint("Attempting to load 'Python.Runtime' using standard binding rules.");
4342
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
4443
var pythonRuntimePublicKeyTokenData = new byte[] { 0x50, 0x00, 0xfe, 0xa6, 0xcb, 0xa7, 0x02, 0xdd };
4544
#endif
@@ -65,12 +64,11 @@ public static void initclr()
6564
try
6665
{
6766
pythonRuntime = Assembly.Load(pythonRuntimeName);
68-
#if DEBUG
69-
Console.WriteLine("Success!");
70-
#endif
67+
debugPrint("Success loading 'Python.Runtime' using standard binding rules.");
7168
}
7269
catch (IOException)
7370
{
71+
debugPrint("'Python.Runtime' not found using standard binding rules.");
7472
try
7573
{
7674
// If the above fails for any reason, we fallback to attempting to load "Python.Runtime.dll"
@@ -89,16 +87,13 @@ public static void initclr()
8987
throw new InvalidOperationException(executingAssembly.Location);
9088
}
9189
string pythonRuntimeDllPath = Path.Combine(assemblyDirectory, "Python.Runtime.dll");
92-
#if DEBUG
93-
Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath);
94-
#endif
90+
debugPrint($"Attempting to load Python.Runtime from: '{pythonRuntimeDllPath}.'");
9591
pythonRuntime = Assembly.LoadFrom(pythonRuntimeDllPath);
92+
debugPrint($"Success loading 'Python.Runtime' from: '{pythonRuntimeDllPath}'.");
9693
}
9794
catch (InvalidOperationException)
9895
{
99-
#if DEBUG
100-
Console.WriteLine("Could not load Python.Runtime");
101-
#endif
96+
debugPrint("Could not load 'Python.Runtime'.");
10297
#if PYTHON3
10398
return IntPtr.Zero;
10499
#elif PYTHON2
@@ -117,4 +112,14 @@ public static void initclr()
117112
pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
118113
#endif
119114
}
115+
116+
/// <summary>
117+
/// Substitute for Debug.Writeline(...). Ideally we would use Debug.Writeline
118+
/// but haven't been able to configure the TRACE from within Python.
119+
/// </summary>
120+
[Conditional("DEBUG")]
121+
private static void debugPrint(string str)
122+
{
123+
Console.WriteLine(str);
124+
}
120125
}

0 commit comments

Comments
 (0)