Skip to content

Commit 146e6ac

Browse files
committed
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 ca99b17 commit 146e6ac

File tree

6 files changed

+48
-35
lines changed

6 files changed

+48
-35
lines changed

Python.Runtime/CustomMarshaler.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ public static int GetUnicodeByteLength(IntPtr p)
122122
/// </remarks>
123123
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
124124
{
125-
return Runtime.IsPython3
126-
? Instance.MarshalManagedToNative(s)
127-
: Marshal.StringToHGlobalAnsi(s);
125+
#if PYTHON2
126+
return Marshal.StringToHGlobalAnsi(s);
127+
#else
128+
return Instance.MarshalManagedToNative(s);
129+
#endif
128130
}
129131

130132
/// <summary>
@@ -139,9 +141,11 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
139141
/// </returns>
140142
public static string PtrToPy3UnicodePy2String(IntPtr p)
141143
{
142-
return Runtime.IsPython3
143-
? PtrToStringUni(p)
144-
: Marshal.PtrToStringAnsi(p);
144+
#if PYTHON2
145+
return Marshal.PtrToStringAnsi(p);
146+
#else
147+
return PtrToStringUni(p);
148+
#endif
145149
}
146150
}
147151

Python.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;
@@ -455,8 +457,9 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
455457
return true;
456458

457459
case TypeCode.Int32:
460+
#if PYTHON2
458461
// Trickery to support 64-bit platforms.
459-
if (Runtime.IsPython2 && Runtime.Is32Bit)
462+
if (Runtime.Is32Bit)
460463
{
461464
op = Runtime.PyNumber_Int(value);
462465

@@ -480,7 +483,8 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
480483
result = ival;
481484
return true;
482485
}
483-
else // Python3 always use PyLong API
486+
#else
487+
// Python3 always use PyLong API
484488
{
485489
op = Runtime.PyNumber_Long(value);
486490
if (op == IntPtr.Zero)
@@ -505,6 +509,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
505509
result = (int)ll;
506510
return true;
507511
}
512+
#endif
508513

509514
case TypeCode.Boolean:
510515
result = Runtime.PyObject_IsTrue(value) != 0;

Python.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);

Python.Runtime/importhook.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@ internal static void InitializeModuleDef()
2828
/// </summary>
2929
static IntPtr GetNewRefToBuiltins()
3030
{
31-
if (Runtime.IsPython3)
32-
{
33-
return Runtime.PyImport_ImportModule("builtins");
34-
}
35-
else
36-
{
37-
// dict is a borrowed ref, no need to decref
38-
IntPtr dict = Runtime.PyImport_GetModuleDict();
31+
#if !PYTHON2
32+
return Runtime.PyImport_ImportModule("builtins");
33+
#else
34+
// dict is a borrowed ref, no need to decref
35+
IntPtr dict = Runtime.PyImport_GetModuleDict();
3936

40-
// GetItemString is a borrowed ref; incref to get a new ref
41-
IntPtr builtins = Runtime.PyDict_GetItemString(dict, "__builtin__");
42-
Runtime.XIncref(builtins);
43-
return builtins;
44-
}
37+
// GetItemString is a borrowed ref; incref to get a new ref
38+
IntPtr builtins = Runtime.PyDict_GetItemString(dict, "__builtin__");
39+
Runtime.XIncref(builtins);
40+
return builtins;
41+
#endif
4542
}
4643

4744
/// <summary>
@@ -126,11 +123,10 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
126123
{
127124
root.InitializePreload();
128125

129-
if (Runtime.IsPython2)
130-
{
131-
Runtime.XIncref(py_clr_module);
132-
return py_clr_module;
133-
}
126+
#if PYTHON2
127+
Runtime.XIncref(py_clr_module);
128+
return py_clr_module;
129+
#endif
134130

135131
// Python 3
136132
// update the module dictionary with the contents of the root dictionary

Python.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

Python.Test.Embed/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
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy