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

Skip to content

Commit c16bb82

Browse files
committed
Refactor #if by using PYTHON2 and PYTHON3
1 parent 935c653 commit c16bb82

8 files changed

+77
-83
lines changed

src/runtime/converter.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8484
{
8585
return Runtime.PyUnicodeType;
8686
}
87-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
87+
#if PYTHON3
8888
else if ((op == int16Type) ||
8989
(op == int32Type) ||
9090
(op == int64Type)) {
@@ -450,7 +450,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
450450
return true;
451451

452452
case TypeCode.Int32:
453-
#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
453+
#if PYTHON2
454454
// Trickery to support 64-bit platforms.
455455
if (IntPtr.Size == 4)
456456
{
@@ -478,7 +478,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
478478
}
479479
else
480480
{
481-
#else
481+
#elif PYTHON3
482482
// When using Python3 always use the PyLong API
483483
{
484484
#endif
@@ -511,7 +511,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
511511
return true;
512512

513513
case TypeCode.Byte:
514-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
514+
#if PYTHON3
515515
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
516516
{
517517
if (Runtime.PyBytes_Size(value) == 1)
@@ -522,7 +522,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
522522
}
523523
goto type_error;
524524
}
525-
#else
525+
#elif PYTHON2
526526
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
527527
{
528528
if (Runtime.PyString_Size(value) == 1)
@@ -556,7 +556,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
556556
return true;
557557

558558
case TypeCode.SByte:
559-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
559+
#if PYTHON3
560560
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) {
561561
if (Runtime.PyBytes_Size(value) == 1) {
562562
op = Runtime.PyBytes_AS_STRING(value);
@@ -565,7 +565,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
565565
}
566566
goto type_error;
567567
}
568-
#else
568+
#elif PYTHON2
569569
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
570570
{
571571
if (Runtime.PyString_Size(value) == 1)
@@ -599,7 +599,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
599599
return true;
600600

601601
case TypeCode.Char:
602-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
602+
#if PYTHON3
603603
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) {
604604
if (Runtime.PyBytes_Size(value) == 1) {
605605
op = Runtime.PyBytes_AS_STRING(value);
@@ -608,7 +608,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result,
608608
}
609609
goto type_error;
610610
}
611-
#else
611+
#elif PYTHON2
612612
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
613613
{
614614
if (Runtime.PyString_Size(value) == 1)
@@ -935,4 +935,4 @@ public static PyObject ToPython(this object o)
935935
return new PyObject(Converter.ToPython(o, o?.GetType()));
936936
}
937937
}
938-
}
938+
}

src/runtime/delegateobject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
102102
//====================================================================
103103
// Implements __cmp__ for reflected delegate types.
104104
//====================================================================
105-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
105+
#if PYTHON3
106106
public static new IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) {
107107
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
108108
{
@@ -131,7 +131,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
131131
Runtime.XIncref(pyfalse);
132132
return pyfalse;
133133
}
134-
#else
134+
#elif PYTHON2
135135
public static new int tp_compare(IntPtr ob, IntPtr other)
136136
{
137137
Delegate d1 = GetTrueDelegate(ob);
@@ -144,4 +144,4 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
144144
}
145145
#endif
146146
}
147-
}
147+
}

src/runtime/exceptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ private Exceptions()
8484

8585
internal static void Initialize()
8686
{
87-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
87+
#if PYTHON3
8888
exceptions_module = Runtime.PyImport_ImportModule("builtins");
89-
#else
89+
#elif PYTHON2
9090
exceptions_module = Runtime.PyImport_ImportModule("exceptions");
9191
#endif
9292
Exceptions.ErrorCheck(exceptions_module);
@@ -164,7 +164,7 @@ internal static void SetArgsAndCause(IntPtr ob)
164164

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

167-
#if !(PYTHON25 || PYTHON26 || PYTHON27)
167+
#if PYTHON3
168168
if (e.InnerException != null)
169169
{
170170
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
@@ -375,7 +375,7 @@ puplic static variables on the Exceptions class filled in from
375375
public static IntPtr Exception;
376376
public static IntPtr StopIteration;
377377
public static IntPtr GeneratorExit;
378-
#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
378+
#if PYTHON2
379379
public static IntPtr StandardError;
380380
#endif
381381
public static IntPtr ArithmeticError;

src/runtime/importhook.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class ImportHook
1414
static CLRModule root;
1515
static MethodWrapper hook;
1616

17-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
17+
#if PYTHON3
1818
static IntPtr py_clr_module;
1919
static IntPtr module_def;
2020
#endif
@@ -30,10 +30,10 @@ internal static void Initialize()
3030
// but it provides the most "Pythonic" way of dealing with CLR
3131
// modules (Python doesn't provide a way to emulate packages).
3232
IntPtr dict = Runtime.PyImport_GetModuleDict();
33-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
33+
#if PYTHON3
3434
IntPtr mod = Runtime.PyImport_ImportModule("builtins");
3535
py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
36-
#else
36+
#elif PYTHON2
3737
IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__");
3838
py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
3939
#endif
@@ -43,7 +43,7 @@ internal static void Initialize()
4343

4444
root = new CLRModule();
4545

46-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
46+
#if PYTHON3
4747
// create a python module with the same methods as the clr module-like object
4848
module_def = ModuleDefOffset.AllocModuleDef("clr");
4949
py_clr_module = Runtime.PyModule_Create2(module_def, 3);
@@ -56,7 +56,7 @@ internal static void Initialize()
5656
Runtime.PyDict_Update(mod_dict, clr_dict);
5757
Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module);
5858
Runtime.PyDict_SetItemString(dict, "clr", py_clr_module);
59-
#else
59+
#elif PYTHON2
6060
Runtime.XIncref(root.pyHandle); // we are using the module two times
6161
Runtime.PyDict_SetItemString(dict, "CLR", root.pyHandle);
6262
Runtime.PyDict_SetItemString(dict, "clr", root.pyHandle);
@@ -70,13 +70,13 @@ internal static void Initialize()
7070

7171
internal static void Shutdown()
7272
{
73-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
73+
#if PYTHON3
7474
if (0 != Runtime.Py_IsInitialized()) {
7575
Runtime.XDecref(py_clr_module);
7676
Runtime.XDecref(root.pyHandle);
7777
}
7878
ModuleDefOffset.FreeModuleDef(module_def);
79-
#else
79+
#elif PYTHON2
8080
if (0 != Runtime.Py_IsInitialized())
8181
{
8282
Runtime.XDecref(root.pyHandle);
@@ -95,7 +95,7 @@ internal static void Shutdown()
9595
public static IntPtr GetCLRModule(IntPtr? fromList = null)
9696
{
9797
root.InitializePreload();
98-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
98+
#if PYTHON3
9999
// update the module dictionary with the contents of the root dictionary
100100
root.LoadNames();
101101
IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module);
@@ -136,7 +136,7 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
136136

137137
Runtime.XIncref(py_clr_module);
138138
return py_clr_module;
139-
#else
139+
#elif PYTHON2
140140
Runtime.XIncref(root.pyHandle);
141141
return root.pyHandle;
142142
#endif
@@ -368,4 +368,4 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw)
368368
return mod.pyHandle;
369369
}
370370
}
371-
}
371+
}

src/runtime/interop.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,13 @@ public static int Size()
154154
// (start after PyObject_HEAD)
155155
public static int dict = 0;
156156
public static int args = 0;
157-
#if (PYTHON25 || PYTHON26 || PYTHON27)
157+
#if PYTHON2
158158
public static int message = 0;
159-
#elif (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
159+
#elif PYTHON3
160160
public static int traceback = 0;
161161
public static int context = 0;
162162
public static int cause = 0;
163-
#if !PYTHON32
164163
public static int suppress_context = 0;
165-
#endif
166164
#endif
167165

168166
// extra c# data
@@ -171,7 +169,7 @@ public static int Size()
171169
}
172170

173171

174-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
172+
#if PYTHON3
175173
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
176174
internal class BytesOffset
177175
{
@@ -270,7 +268,7 @@ public static void FreeModuleDef(IntPtr ptr) {
270268
/// </summary>
271269
internal class TypeFlags
272270
{
273-
#if (PYTHON23 || PYTHON24 || PYTHON25 || PYTHON26 || PYTHON27)
271+
#if PYTHON2
274272
// these flags were removed in Python 3
275273
public static int HaveGetCharBuffer = (1 << 0);
276274
public static int HaveSequenceIn = (1 << 1);
@@ -309,8 +307,8 @@ internal class TypeFlags
309307
public static int BaseExceptionSubclass = (1 << 30);
310308
public static int TypeSubclass = (1 << 31);
311309

312-
// Default flags for Python 2
313-
#if (PYTHON23 || PYTHON24 || PYTHON25 || PYTHON26 || PYTHON27)
310+
#if PYTHON2
311+
// Default flags for Python 2
314312
public static int Default = (
315313
HaveGetCharBuffer |
316314
HaveSequenceIn |
@@ -320,14 +318,10 @@ internal class TypeFlags
320318
HaveIter |
321319
HaveClass |
322320
HaveStacklessExtension |
323-
#if (PYTHON25 || PYTHON26 || PYTHON27)
324321
HaveIndex |
325-
#endif
326322
0);
327-
#endif
328-
329-
// Default flags for Python 3
330-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
323+
#elif PYTHON3
324+
// Default flags for Python 3
331325
public static int Default = (
332326
HaveStacklessExtension |
333327
HaveVersionTag);
@@ -390,7 +384,7 @@ static Interop()
390384
pmap["nb_add"] = p["BinaryFunc"];
391385
pmap["nb_subtract"] = p["BinaryFunc"];
392386
pmap["nb_multiply"] = p["BinaryFunc"];
393-
#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
387+
#if PYTHON2
394388
pmap["nb_divide"] = p["BinaryFunc"];
395389
#endif
396390
pmap["nb_remainder"] = p["BinaryFunc"];
@@ -415,7 +409,7 @@ static Interop()
415409
pmap["nb_inplace_add"] = p["BinaryFunc"];
416410
pmap["nb_inplace_subtract"] = p["BinaryFunc"];
417411
pmap["nb_inplace_multiply"] = p["BinaryFunc"];
418-
#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
412+
#if PYTHON2
419413
pmap["nb_inplace_divide"] = p["BinaryFunc"];
420414
#endif
421415
pmap["nb_inplace_remainder"] = p["BinaryFunc"];

src/runtime/pythonengine.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ public static void Initialize()
186186
// CPython interpreter process - this bootstraps the managed runtime
187187
// when it is imported by the CLR extension module.
188188
//====================================================================
189-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
189+
#if PYTHON3
190190
public static IntPtr InitExt() {
191-
#else
191+
#elif PYTHON2
192192
public static void InitExt()
193193
{
194194
#endif
@@ -234,12 +234,12 @@ public static void InitExt()
234234
catch (PythonException e)
235235
{
236236
e.Restore();
237-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
237+
#if PYTHON3
238238
return IntPtr.Zero;
239239
#endif
240240
}
241241

242-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
242+
#if PYTHON3
243243
return Python.Runtime.ImportHook.GetCLRModule();
244244
#endif
245245
}
@@ -501,4 +501,4 @@ public static PyObject Import(string name)
501501
return PythonEngine.ImportModule(name);
502502
}
503503
}
504-
}
504+
}

0 commit comments

Comments
 (0)