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

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 authored and koubaa committed Apr 17, 2020
commit 7d6c4ace590bddb290e51dcb5059ab4cb9d69203
9 changes: 6 additions & 3 deletions src/embed_tests/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
16 changes: 10 additions & 6 deletions src/runtime/CustomMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,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 @@ -137,9 +139,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 src/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 @@ -488,8 +490,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 @@ -513,7 +516,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 @@ -538,6 +542,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 src/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
10 changes: 4 additions & 6 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ internal static void ReleaseModuleDef()
ModuleDefOffset.FreeModuleDef(module_def);
module_def = IntPtr.Zero;
}
#endif

/// <summary>
/// Initialize just the __import__ hook itself.
Expand Down Expand Up @@ -136,11 +135,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 src/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