Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
filmor committed Dec 15, 2019
commit 146e6ace5d7872c93014165ae6b40dc6a40f95e2
16 changes: 10 additions & 6 deletions Python.Runtime/CustomMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ public static int GetUnicodeByteLength(IntPtr p)
/// </remarks>
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
{
return Runtime.IsPython3
? Instance.MarshalManagedToNative(s)
: Marshal.StringToHGlobalAnsi(s);
#if PYTHON2
return Marshal.StringToHGlobalAnsi(s);
#else
return Instance.MarshalManagedToNative(s);
#endif
}

/// <summary>
Expand All @@ -139,9 +141,11 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
/// </returns>
public static string PtrToPy3UnicodePy2String(IntPtr p)
{
return Runtime.IsPython3
? PtrToStringUni(p)
: Marshal.PtrToStringAnsi(p);
#if PYTHON2
return Marshal.PtrToStringAnsi(p);
#else
return PtrToStringUni(p);
#endif
}
}

Expand Down
11 changes: 8 additions & 3 deletions Python.Runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
if (op == int32Type)
return Runtime.PyIntType;

if (op == int64Type && Runtime.IsPython2)
#if PYTHON2
if (op == int64Type)
return Runtime.PyLongType;
#endif

if (op == int64Type)
return Runtime.PyIntType;
Expand Down Expand Up @@ -455,8 +457,9 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
return true;

case TypeCode.Int32:
#if PYTHON2
// Trickery to support 64-bit platforms.
if (Runtime.IsPython2 && Runtime.Is32Bit)
if (Runtime.Is32Bit)
{
op = Runtime.PyNumber_Int(value);

Expand All @@ -480,7 +483,8 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
result = ival;
return true;
}
else // Python3 always use PyLong API
#else
// Python3 always use PyLong API
{
op = Runtime.PyNumber_Long(value);
if (op == IntPtr.Zero)
Expand All @@ -505,6 +509,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
result = (int)ll;
return true;
}
#endif

case TypeCode.Boolean:
result = Runtime.PyObject_IsTrue(value) != 0;
Expand Down
7 changes: 6 additions & 1 deletion Python.Runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ private Exceptions()
/// </summary>
internal static void Initialize()
{
string exceptionsModuleName = Runtime.IsPython3 ? "builtins" : "exceptions";
string exceptionsModuleName =
#if PYTHON2
"exceptions";
#else
"builtins";
#endif
exceptions_module = Runtime.PyImport_ImportModule(exceptionsModuleName);

Exceptions.ErrorCheck(exceptions_module);
Expand Down
32 changes: 14 additions & 18 deletions Python.Runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ internal static void InitializeModuleDef()
/// </summary>
static IntPtr GetNewRefToBuiltins()
{
if (Runtime.IsPython3)
{
return Runtime.PyImport_ImportModule("builtins");
}
else
{
// dict is a borrowed ref, no need to decref
IntPtr dict = Runtime.PyImport_GetModuleDict();
#if !PYTHON2
return Runtime.PyImport_ImportModule("builtins");
#else
// dict is a borrowed ref, no need to decref
IntPtr dict = Runtime.PyImport_GetModuleDict();

// GetItemString is a borrowed ref; incref to get a new ref
IntPtr builtins = Runtime.PyDict_GetItemString(dict, "__builtin__");
Runtime.XIncref(builtins);
return builtins;
}
// GetItemString is a borrowed ref; incref to get a new ref
IntPtr builtins = Runtime.PyDict_GetItemString(dict, "__builtin__");
Runtime.XIncref(builtins);
return builtins;
#endif
}

/// <summary>
Expand Down Expand Up @@ -126,11 +123,10 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
{
root.InitializePreload();

if (Runtime.IsPython2)
{
Runtime.XIncref(py_clr_module);
return py_clr_module;
}
#if PYTHON2
Runtime.XIncref(py_clr_module);
return py_clr_module;
#endif

// Python 3
// update the module dictionary with the contents of the root dictionary
Expand Down
8 changes: 4 additions & 4 deletions Python.Runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public static string PythonPath
}
set
{
if (Runtime.IsPython2)
{
throw new NotSupportedException("Set PythonPath not supported on Python 2");
}
#if PYTHON2
throw new NotSupportedException("Set PythonPath not supported on Python 2");
#else
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
Runtime.Py_SetPath(_pythonPath);
#endif
}
}

Expand Down
9 changes: 6 additions & 3 deletions Python.Test.Embed/TestCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ public void TestNoOverloadException() {
dynamic callWith42 = PythonEngine.Eval("lambda f: f([42])");
var error = Assert.Throws<PythonException>(() => callWith42(aFunctionThatCallsIntoPython.ToPython()));
Assert.AreEqual("TypeError", error.PythonTypeName);
string expectedArgTypes = Runtime.IsPython2
? "(<type 'list'>)"
: "(<class 'list'>)";
string expectedArgTypes =
#if PYTHON2
"(<type 'list'>)";
#else
"(<class 'list'>)";
#endif
StringAssert.EndsWith(expectedArgTypes, error.Message);
}
}
Expand Down