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
Next Next commit
Drop PYTHON3 flag (default now)
  • Loading branch information
filmor authored and koubaa committed Apr 17, 2020
commit 3b88515a572cef9b9932721918eeccfc05d972ca
24 changes: 12 additions & 12 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
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 +555,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 +589,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 +600,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 +634,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 +645,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 +753,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 +900,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 +921,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
}
}
2 changes: 1 addition & 1 deletion src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,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
7 changes: 3 additions & 4 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 Down Expand Up @@ -82,7 +81,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 +92,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 +117,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 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
23 changes: 7 additions & 16 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,7 @@ static void OnDomainUnload(object _, EventArgs __)
/// CPython interpreter process - this bootstraps the managed runtime
/// when it is imported by the CLR extension module.
/// </summary>
#if PYTHON3
public static IntPtr InitExt()
#elif PYTHON2
public static void InitExt()
#endif
public static int InternalInitialize(int size, IntPtr data)
{
try
{
Expand Down Expand Up @@ -294,18 +290,13 @@ public static void InitExt()
" break\n";

PythonEngine.Exec(code);
return 0;
}
catch (PythonException e)
{
e.Restore();
#if PYTHON3
return IntPtr.Zero;
#endif
return -1;
}

#if PYTHON3
return Python.Runtime.ImportHook.GetCLRModule();
#endif
}

/// <summary>
Expand All @@ -321,7 +312,7 @@ public static void Shutdown()
if (initialized)
{
PyScopeManager.Global.Clear();

// If the shutdown handlers trigger a domain unload,
// don't call shutdown again.
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
Expand Down Expand Up @@ -587,7 +578,7 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
borrowedGlobals = false;
}
}

if (locals == null)
{
locals = globals;
Expand Down Expand Up @@ -650,7 +641,7 @@ public static PyScope CreateScope(string name)
var scope = PyScopeManager.Global.Create(name);
return scope;
}

public class GILState : IDisposable
{
private readonly IntPtr state;
Expand Down Expand Up @@ -751,7 +742,7 @@ public static void SetArgv(IEnumerable<string> argv)

public static void With(PyObject obj, Action<dynamic> Body)
{
// Behavior described here:
// Behavior described here:
// https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers

IntPtr type = Runtime.PyNone;
Expand Down
Loading