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

Skip to content

Commit 7d6c4ac

Browse files
filmorkoubaa
authored andcommitted
Stop using Runtime.IsPython2/3
Contrary to what I said before, I don't think trying to provide a single DLL for Python 2 and 3. We will in the future default to Python 3, and if someone really wants Python 2, for a while we will support a separate compile configuration for that.
1 parent 3b88515 commit 7d6c4ac

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

src/embed_tests/TestCallbacks.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ public void TestNoOverloadException() {
2525
dynamic callWith42 = PythonEngine.Eval("lambda f: f([42])");
2626
var error = Assert.Throws<PythonException>(() => callWith42(aFunctionThatCallsIntoPython.ToPython()));
2727
Assert.AreEqual("TypeError", error.PythonTypeName);
28-
string expectedArgTypes = Runtime.IsPython2
29-
? "(<type 'list'>)"
30-
: "(<class 'list'>)";
28+
string expectedArgTypes =
29+
#if PYTHON2
30+
"(<type 'list'>)";
31+
#else
32+
"(<class 'list'>)";
33+
#endif
3134
StringAssert.EndsWith(expectedArgTypes, error.Message);
3235
}
3336
}

src/runtime/CustomMarshaler.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ public static int GetUnicodeByteLength(IntPtr p)
120120
/// </remarks>
121121
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
122122
{
123-
return Runtime.IsPython3
124-
? Instance.MarshalManagedToNative(s)
125-
: Marshal.StringToHGlobalAnsi(s);
123+
#if PYTHON2
124+
return Marshal.StringToHGlobalAnsi(s);
125+
#else
126+
return Instance.MarshalManagedToNative(s);
127+
#endif
126128
}
127129

128130
/// <summary>
@@ -137,9 +139,11 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
137139
/// </returns>
138140
public static string PtrToPy3UnicodePy2String(IntPtr p)
139141
{
140-
return Runtime.IsPython3
141-
? PtrToStringUni(p)
142-
: Marshal.PtrToStringAnsi(p);
142+
#if PYTHON2
143+
return Marshal.PtrToStringAnsi(p);
144+
#else
145+
return PtrToStringUni(p);
146+
#endif
143147
}
144148
}
145149

src/runtime/converter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8686
if (op == int32Type)
8787
return Runtime.PyIntType;
8888

89-
if (op == int64Type && Runtime.IsPython2)
89+
#if PYTHON2
90+
if (op == int64Type)
9091
return Runtime.PyLongType;
92+
#endif
9193

9294
if (op == int64Type)
9395
return Runtime.PyIntType;
@@ -488,8 +490,9 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
488490
return true;
489491

490492
case TypeCode.Int32:
493+
#if PYTHON2
491494
// Trickery to support 64-bit platforms.
492-
if (Runtime.IsPython2 && Runtime.Is32Bit)
495+
if (Runtime.Is32Bit)
493496
{
494497
op = Runtime.PyNumber_Int(value);
495498

@@ -513,7 +516,8 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
513516
result = ival;
514517
return true;
515518
}
516-
else // Python3 always use PyLong API
519+
#else
520+
// Python3 always use PyLong API
517521
{
518522
op = Runtime.PyNumber_Long(value);
519523
if (op == IntPtr.Zero)
@@ -538,6 +542,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
538542
result = (int)ll;
539543
return true;
540544
}
545+
#endif
541546

542547
case TypeCode.Boolean:
543548
result = Runtime.PyObject_IsTrue(value) != 0;

src/runtime/exceptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ private Exceptions()
103103
/// </summary>
104104
internal static void Initialize()
105105
{
106-
string exceptionsModuleName = Runtime.IsPython3 ? "builtins" : "exceptions";
106+
string exceptionsModuleName =
107+
#if PYTHON2
108+
"exceptions";
109+
#else
110+
"builtins";
111+
#endif
107112
exceptions_module = Runtime.PyImport_ImportModule(exceptionsModuleName);
108113

109114
Exceptions.ErrorCheck(exceptions_module);

src/runtime/importhook.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ internal static void ReleaseModuleDef()
3232
ModuleDefOffset.FreeModuleDef(module_def);
3333
module_def = IntPtr.Zero;
3434
}
35-
#endif
3635

3736
/// <summary>
3837
/// Initialize just the __import__ hook itself.
@@ -136,11 +135,10 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
136135
{
137136
root.InitializePreload();
138137

139-
if (Runtime.IsPython2)
140-
{
141-
Runtime.XIncref(py_clr_module);
142-
return py_clr_module;
143-
}
138+
#if PYTHON2
139+
Runtime.XIncref(py_clr_module);
140+
return py_clr_module;
141+
#endif
144142

145143
// Python 3
146144
// update the module dictionary with the contents of the root dictionary

src/runtime/pythonengine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ public static string PythonPath
9595
}
9696
set
9797
{
98-
if (Runtime.IsPython2)
99-
{
100-
throw new NotSupportedException("Set PythonPath not supported on Python 2");
101-
}
98+
#if PYTHON2
99+
throw new NotSupportedException("Set PythonPath not supported on Python 2");
100+
#else
102101
Marshal.FreeHGlobal(_pythonPath);
103102
_pythonPath = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
104103
Runtime.Py_SetPath(_pythonPath);
104+
#endif
105105
}
106106
}
107107

0 commit comments

Comments
 (0)