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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881])
- Added support for kwarg parameters when calling .NET methods from Python
- Changed method for finding MSBuild using vswhere
- Removed .NET remoting support

### Fixed

Expand Down
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
43 changes: 20 additions & 23 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 @@ -165,15 +167,7 @@ internal static IntPtr ToPython(object value, Type type)
var pyderived = value as IPythonDerivedType;
if (null != pyderived)
{
#if NETSTANDARD
return ClassDerivedObject.ToPython(pyderived);
#else
// if object is remote don't do this
if (!System.Runtime.Remoting.RemotingServices.IsTransparentProxy(pyderived))
{
return ClassDerivedObject.ToPython(pyderived);
}
#endif
}

// hmm - from Python, we almost never care what the declared
Expand Down Expand Up @@ -488,8 +482,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 +508,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,13 +534,14 @@ 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;
return true;

case TypeCode.Byte:
#if PYTHON3
#if !PYTHON2
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
{
if (Runtime.PyBytes_Size(value) == 1)
Expand All @@ -555,7 +552,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
}
goto type_error;
}
#elif PYTHON2
#else
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
{
if (Runtime.PyString_Size(value) == 1)
Expand Down Expand Up @@ -589,7 +586,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
return true;

case TypeCode.SByte:
#if PYTHON3
#if !PYTHON2
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
{
if (Runtime.PyBytes_Size(value) == 1)
Expand All @@ -600,7 +597,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
}
goto type_error;
}
#elif PYTHON2
#else
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
{
if (Runtime.PyString_Size(value) == 1)
Expand Down Expand Up @@ -634,7 +631,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
return true;

case TypeCode.Char:
#if PYTHON3
#if !PYTHON2
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
{
if (Runtime.PyBytes_Size(value) == 1)
Expand All @@ -645,7 +642,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
}
goto type_error;
}
#elif PYTHON2
#else
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
{
if (Runtime.PyString_Size(value) == 1)
Expand Down Expand Up @@ -753,20 +750,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
}
goto type_error;
}

uint ui;
try
try
{
ui = Convert.ToUInt32(Runtime.PyLong_AsUnsignedLong(op));
} catch (OverflowException)
{
// Probably wasn't an overflow in python but was in C# (e.g. if cpython
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
// PyLong_AsUnsignedLong)
Runtime.XDecref(op);
goto overflow;
}


if (Exceptions.ErrorOccurred())
{
Expand Down Expand Up @@ -900,7 +897,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s

var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(elementType);
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
(IList) Activator.CreateInstance(constructedListType);
IntPtr item;

Expand All @@ -921,7 +918,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s

items = Array.CreateInstance(elementType, list.Count);
list.CopyTo(items, 0);

result = items;
return true;
}
Expand Down
9 changes: 0 additions & 9 deletions src/runtime/delegateobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
/// <summary>
/// Implements __cmp__ for reflected delegate types.
/// </summary>
#if PYTHON3 // TODO: Doesn't PY2 implement tp_richcompare too?
public new static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
{
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
Expand Down Expand Up @@ -126,13 +125,5 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
Runtime.XIncref(pyfalse);
return pyfalse;
}
#elif PYTHON2
public static int tp_compare(IntPtr ob, IntPtr other)
{
Delegate d1 = GetTrueDelegate(ob);
Delegate d2 = GetTrueDelegate(other);
return d1 == d2 ? 0 : -1;
}
#endif
}
}
9 changes: 7 additions & 2 deletions 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 Expand Up @@ -180,7 +185,7 @@ internal static void SetArgsAndCause(IntPtr ob)

Marshal.WriteIntPtr(ob, ExceptionOffset.args, args);

#if PYTHON3
#if !PYTHON2
if (e.InnerException != null)
{
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
Expand Down
17 changes: 7 additions & 10 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal class ImportHook
private static MethodWrapper hook;
private static IntPtr py_clr_module;

#if PYTHON3
private static IntPtr module_def = IntPtr.Zero;

internal static void InitializeModuleDef()
Expand All @@ -33,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 @@ -82,7 +80,7 @@ internal static void Initialize()
// Initialize the clr module and tell Python about it.
root = new CLRModule();

#if PYTHON3
#if !PYTHON2
// create a python module with the same methods as the clr module-like object
InitializeModuleDef();
py_clr_module = Runtime.PyModule_Create2(module_def, 3);
Expand All @@ -93,7 +91,7 @@ internal static void Initialize()
clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr));

Runtime.PyDict_Update(mod_dict, clr_dict);
#elif PYTHON2
#else
Runtime.XIncref(root.pyHandle); // we are using the module two times
py_clr_module = root.pyHandle; // Alias handle for PY2/PY3
#endif
Expand All @@ -118,7 +116,7 @@ internal static void Shutdown()
bool shouldFreeDef = Runtime.Refcount(py_clr_module) == 1;
Runtime.XDecref(py_clr_module);
py_clr_module = IntPtr.Zero;
#if PYTHON3
#if !PYTHON2
if (shouldFreeDef)
{
ReleaseModuleDef();
Expand All @@ -137,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
26 changes: 11 additions & 15 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static int Size()
public static int args = 0;
#if PYTHON2
public static int message = 0;
#elif PYTHON3
#else
public static int traceback = 0;
public static int context = 0;
public static int cause = 0;
Expand All @@ -169,7 +169,6 @@ public static int Size()
}


#if PYTHON3
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class BytesOffset
{
Expand Down Expand Up @@ -260,7 +259,6 @@ public static void FreeModuleDef(IntPtr ptr)

public static int name = 0;
}
#endif // PYTHON3

/// <summary>
/// TypeFlags(): The actual bit values for the Type Flags stored
Expand All @@ -270,17 +268,6 @@ public static void FreeModuleDef(IntPtr ptr)
/// </summary>
internal class TypeFlags
{
#if PYTHON2 // these flags were removed in Python 3
public static int HaveGetCharBuffer = (1 << 0);
public static int HaveSequenceIn = (1 << 1);
public static int GC = 0;
public static int HaveInPlaceOps = (1 << 3);
public static int CheckTypes = (1 << 4);
public static int HaveRichCompare = (1 << 5);
public static int HaveWeakRefs = (1 << 6);
public static int HaveIter = (1 << 7);
public static int HaveClass = (1 << 8);
#endif
public static int HeapType = (1 << 9);
public static int BaseType = (1 << 10);
public static int Ready = (1 << 12);
Expand Down Expand Up @@ -309,6 +296,15 @@ internal class TypeFlags
public static int TypeSubclass = (1 << 31);

#if PYTHON2 // Default flags for Python 2
public static int HaveGetCharBuffer = (1 << 0);
public static int HaveSequenceIn = (1 << 1);
public static int GC = 0;
public static int HaveInPlaceOps = (1 << 3);
public static int CheckTypes = (1 << 4);
public static int HaveRichCompare = (1 << 5);
public static int HaveWeakRefs = (1 << 6);
public static int HaveIter = (1 << 7);
public static int HaveClass = (1 << 8);
public static int Default = (
HaveGetCharBuffer |
HaveSequenceIn |
Expand All @@ -320,7 +316,7 @@ internal class TypeFlags
HaveStacklessExtension |
HaveIndex |
0);
#elif PYTHON3 // Default flags for Python 3
#else // Default flags for Python 3
public static int Default = (
HaveStacklessExtension |
HaveVersionTag);
Expand Down
Loading