Skip to content

Commit 8aba5fc

Browse files
committed
Drop PYTHON3 flag (default now)
1 parent 3031e96 commit 8aba5fc

File tree

8 files changed

+70
-175
lines changed

8 files changed

+70
-175
lines changed

Python.Runtime/converter.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
519519
return true;
520520

521521
case TypeCode.Byte:
522-
#if PYTHON3
522+
#if !PYTHON2
523523
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
524524
{
525525
if (Runtime.PyBytes_Size(value) == 1)
@@ -530,7 +530,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
530530
}
531531
goto type_error;
532532
}
533-
#elif PYTHON2
533+
#else
534534
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
535535
{
536536
if (Runtime.PyString_Size(value) == 1)
@@ -564,7 +564,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
564564
return true;
565565

566566
case TypeCode.SByte:
567-
#if PYTHON3
567+
#if !PYTHON2
568568
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
569569
{
570570
if (Runtime.PyBytes_Size(value) == 1)
@@ -575,7 +575,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
575575
}
576576
goto type_error;
577577
}
578-
#elif PYTHON2
578+
#else
579579
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
580580
{
581581
if (Runtime.PyString_Size(value) == 1)
@@ -609,7 +609,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
609609
return true;
610610

611611
case TypeCode.Char:
612-
#if PYTHON3
612+
#if !PYTHON2
613613
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
614614
{
615615
if (Runtime.PyBytes_Size(value) == 1)
@@ -620,7 +620,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
620620
}
621621
goto type_error;
622622
}
623-
#elif PYTHON2
623+
#else
624624
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
625625
{
626626
if (Runtime.PyString_Size(value) == 1)
@@ -728,20 +728,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
728728
}
729729
goto type_error;
730730
}
731-
731+
732732
uint ui;
733-
try
733+
try
734734
{
735735
ui = Convert.ToUInt32(Runtime.PyLong_AsUnsignedLong(op));
736736
} catch (OverflowException)
737737
{
738738
// Probably wasn't an overflow in python but was in C# (e.g. if cpython
739-
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
739+
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
740740
// PyLong_AsUnsignedLong)
741741
Runtime.XDecref(op);
742742
goto overflow;
743743
}
744-
744+
745745

746746
if (Exceptions.ErrorOccurred())
747747
{
@@ -875,7 +875,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
875875

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

@@ -896,7 +896,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
896896

897897
items = Array.CreateInstance(elementType, list.Count);
898898
list.CopyTo(items, 0);
899-
899+
900900
result = items;
901901
return true;
902902
}

Python.Runtime/delegateobject.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
9696
/// <summary>
9797
/// Implements __cmp__ for reflected delegate types.
9898
/// </summary>
99-
#if PYTHON3 // TODO: Doesn't PY2 implement tp_richcompare too?
10099
public new static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
101100
{
102101
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
@@ -126,13 +125,5 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
126125
Runtime.XIncref(pyfalse);
127126
return pyfalse;
128127
}
129-
#elif PYTHON2
130-
public static int tp_compare(IntPtr ob, IntPtr other)
131-
{
132-
Delegate d1 = GetTrueDelegate(ob);
133-
Delegate d2 = GetTrueDelegate(other);
134-
return d1 == d2 ? 0 : -1;
135-
}
136-
#endif
137128
}
138129
}

Python.Runtime/exceptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal static void SetArgsAndCause(IntPtr ob)
180180

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

183-
#if PYTHON3
183+
#if !PYTHON2
184184
if (e.InnerException != null)
185185
{
186186
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);

Python.Runtime/importhook.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ internal class ImportHook
1313
private static MethodWrapper hook;
1414
private static IntPtr py_clr_module;
1515

16-
#if PYTHON3
1716
private static IntPtr module_def = IntPtr.Zero;
1817

1918
internal static void InitializeModuleDef()
@@ -23,7 +22,6 @@ internal static void InitializeModuleDef()
2322
module_def = ModuleDefOffset.AllocModuleDef("clr");
2423
}
2524
}
26-
#endif
2725

2826
/// <summary>
2927
/// Get a <i>New reference</i> to the builtins module.
@@ -86,7 +84,7 @@ internal static void Initialize()
8684
// Initialize the clr module and tell Python about it.
8785
root = new CLRModule();
8886

89-
#if PYTHON3
87+
#if !PYTHON2
9088
// create a python module with the same methods as the clr module-like object
9189
InitializeModuleDef();
9290
py_clr_module = Runtime.PyModule_Create2(module_def, 3);
@@ -97,7 +95,7 @@ internal static void Initialize()
9795
clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr));
9896

9997
Runtime.PyDict_Update(mod_dict, clr_dict);
100-
#elif PYTHON2
98+
#else
10199
Runtime.XIncref(root.pyHandle); // we are using the module two times
102100
py_clr_module = root.pyHandle; // Alias handle for PY2/PY3
103101
#endif

Python.Runtime/interop.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static int Size()
155155
public static int args = 0;
156156
#if PYTHON2
157157
public static int message = 0;
158-
#elif PYTHON3
158+
#else
159159
public static int traceback = 0;
160160
public static int context = 0;
161161
public static int cause = 0;
@@ -168,7 +168,6 @@ public static int Size()
168168
}
169169

170170

171-
#if PYTHON3
172171
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
173172
internal class BytesOffset
174173
{
@@ -259,7 +258,6 @@ public static void FreeModuleDef(IntPtr ptr)
259258

260259
public static int name = 0;
261260
}
262-
#endif // PYTHON3
263261

264262
/// <summary>
265263
/// TypeFlags(): The actual bit values for the Type Flags stored
@@ -269,17 +267,6 @@ public static void FreeModuleDef(IntPtr ptr)
269267
/// </summary>
270268
internal class TypeFlags
271269
{
272-
#if PYTHON2 // these flags were removed in Python 3
273-
public static int HaveGetCharBuffer = (1 << 0);
274-
public static int HaveSequenceIn = (1 << 1);
275-
public static int GC = 0;
276-
public static int HaveInPlaceOps = (1 << 3);
277-
public static int CheckTypes = (1 << 4);
278-
public static int HaveRichCompare = (1 << 5);
279-
public static int HaveWeakRefs = (1 << 6);
280-
public static int HaveIter = (1 << 7);
281-
public static int HaveClass = (1 << 8);
282-
#endif
283270
public static int HeapType = (1 << 9);
284271
public static int BaseType = (1 << 10);
285272
public static int Ready = (1 << 12);
@@ -308,6 +295,15 @@ internal class TypeFlags
308295
public static int TypeSubclass = (1 << 31);
309296

310297
#if PYTHON2 // Default flags for Python 2
298+
public static int HaveGetCharBuffer = (1 << 0);
299+
public static int HaveSequenceIn = (1 << 1);
300+
public static int GC = 0;
301+
public static int HaveInPlaceOps = (1 << 3);
302+
public static int CheckTypes = (1 << 4);
303+
public static int HaveRichCompare = (1 << 5);
304+
public static int HaveWeakRefs = (1 << 6);
305+
public static int HaveIter = (1 << 7);
306+
public static int HaveClass = (1 << 8);
311307
public static int Default = (
312308
HaveGetCharBuffer |
313309
HaveSequenceIn |
@@ -319,7 +315,7 @@ internal class TypeFlags
319315
HaveStacklessExtension |
320316
HaveIndex |
321317
0);
322-
#elif PYTHON3 // Default flags for Python 3
318+
#else // Default flags for Python 3
323319
public static int Default = (
324320
HaveStacklessExtension |
325321
HaveVersionTag);

Python.Runtime/pythonengine.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,7 @@ static void OnDomainUnload(object _, EventArgs __)
254254
/// CPython interpreter process - this bootstraps the managed runtime
255255
/// when it is imported by the CLR extension module.
256256
/// </summary>
257-
#if PYTHON3
258-
public static IntPtr InitExt()
259-
#elif PYTHON2
260-
public static void InitExt()
261-
#endif
257+
public static int InternalInitialize(int size, IntPtr data)
262258
{
263259
try
264260
{
@@ -294,18 +290,13 @@ public static void InitExt()
294290
" break\n";
295291

296292
PythonEngine.Exec(code);
293+
return 0;
297294
}
298295
catch (PythonException e)
299296
{
300297
e.Restore();
301-
#if PYTHON3
302-
return IntPtr.Zero;
303-
#endif
298+
return -1;
304299
}
305-
306-
#if PYTHON3
307-
return Python.Runtime.ImportHook.GetCLRModule();
308-
#endif
309300
}
310301

311302
/// <summary>
@@ -321,7 +312,7 @@ public static void Shutdown()
321312
if (initialized)
322313
{
323314
PyScopeManager.Global.Clear();
324-
315+
325316
// If the shutdown handlers trigger a domain unload,
326317
// don't call shutdown again.
327318
AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
@@ -584,7 +575,7 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
584575
borrowedGlobals = false;
585576
}
586577
}
587-
578+
588579
if (locals == null)
589580
{
590581
locals = globals;
@@ -640,7 +631,7 @@ public static PyScope CreateScope(string name)
640631
var scope = PyScopeManager.Global.Create(name);
641632
return scope;
642633
}
643-
634+
644635
public class GILState : IDisposable
645636
{
646637
private IntPtr state;
@@ -737,7 +728,7 @@ public static void SetArgv(IEnumerable<string> argv)
737728

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

743734
IntPtr type = Runtime.PyNone;

0 commit comments

Comments
 (0)