Skip to content

Commit c197a64

Browse files
authored
Merge pull request pythonnet#357 from vmuriart/Remove-Build_Directives
Replace some build directives with runtime checks
2 parents edada03 + 587f318 commit c197a64

4 files changed

Lines changed: 65 additions & 54 deletions

File tree

src/runtime/converter.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8282
{
8383
return Runtime.PyUnicodeType;
8484
}
85-
#if PYTHON3
86-
else if ((op == int16Type) ||
87-
(op == int32Type) ||
88-
(op == int64Type))
85+
86+
else if (Runtime.IsPython3 && ((op == int16Type) ||
87+
(op == int32Type) ||
88+
(op == int64Type)))
8989
{
9090
return Runtime.PyIntType;
9191
}
92-
#endif
92+
9393
else if ((op == int16Type) ||
9494
(op == int32Type))
9595
{
@@ -435,9 +435,8 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, bool setEr
435435
return true;
436436

437437
case TypeCode.Int32:
438-
#if PYTHON2 // Trickery to support 64-bit platforms.
439-
440-
if (Runtime.is32bit)
438+
// Trickery to support 64-bit platforms.
439+
if (Runtime.IsPython2 && Runtime.Is32Bit)
441440
{
442441
op = Runtime.PyNumber_Int(value);
443442

@@ -461,11 +460,8 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, bool setEr
461460
result = ival;
462461
return true;
463462
}
464-
else
463+
else // Python3 always use PyLong API
465464
{
466-
#elif PYTHON3 // When using Python3 always use the PyLong API
467-
{
468-
#endif
469465
op = Runtime.PyNumber_Long(value);
470466
if (op == IntPtr.Zero)
471467
{
@@ -613,17 +609,20 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, bool setEr
613609
if (Runtime.PyUnicode_GetSize(value) == 1)
614610
{
615611
op = Runtime.PyUnicode_AS_UNICODE(value);
616-
#if UCS2
617-
// 2011-01-02: Marshal as character array because the cast
618-
// result = (char)Marshal.ReadInt16(op); throws an OverflowException
619-
// on negative numbers with Check Overflow option set on the project
620-
Char[] buff = new Char[1];
621-
Marshal.Copy(op, buff, 0, 1);
622-
result = buff[0];
623-
#elif UCS4
624-
// XXX this is probably NOT correct?
625-
result = (char)Marshal.ReadInt32(op);
626-
#endif
612+
if (Runtime.UCS == 2) // Don't trust linter, statement not always true.
613+
{
614+
// 2011-01-02: Marshal as character array because the cast
615+
// result = (char)Marshal.ReadInt16(op); throws an OverflowException
616+
// on negative numbers with Check Overflow option set on the project
617+
Char[] buff = new Char[1];
618+
Marshal.Copy(op, buff, 0, 1);
619+
result = buff[0];
620+
}
621+
else // UCS4
622+
{
623+
// XXX this is probably NOT correct?
624+
result = (char)Marshal.ReadInt32(op);
625+
}
627626
return true;
628627
}
629628
goto type_error;

src/runtime/exceptions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,14 @@ private Exceptions()
8181
/// </summary>
8282
internal static void Initialize()
8383
{
84-
#if PYTHON3
85-
exceptions_module = Runtime.PyImport_ImportModule("builtins");
86-
#elif PYTHON2
87-
exceptions_module = Runtime.PyImport_ImportModule("exceptions");
88-
#endif
84+
string exceptionsModuleName = Runtime.IsPython3 ? "builtins" : "exceptions";
85+
exceptions_module = Runtime.PyImport_ImportModule(exceptionsModuleName);
86+
8987
Exceptions.ErrorCheck(exceptions_module);
9088
warnings_module = Runtime.PyImport_ImportModule("warnings");
9189
Exceptions.ErrorCheck(warnings_module);
9290
Type type = typeof(Exceptions);
93-
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public |
94-
BindingFlags.Static))
91+
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))
9592
{
9693
IntPtr op = Runtime.PyObject_GetAttrString(exceptions_module, fi.Name);
9794
if (op != IntPtr.Zero)
@@ -116,8 +113,7 @@ internal static void Shutdown()
116113
if (Runtime.Py_IsInitialized() != 0)
117114
{
118115
Type type = typeof(Exceptions);
119-
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public |
120-
BindingFlags.Static))
116+
foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))
121117
{
122118
var op = (IntPtr)fi.GetValue(type);
123119
if (op != IntPtr.Zero)

src/runtime/importhook.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ internal static void Initialize()
3535
// but it provides the most "Pythonic" way of dealing with CLR
3636
// modules (Python doesn't provide a way to emulate packages).
3737
IntPtr dict = Runtime.PyImport_GetModuleDict();
38-
#if PYTHON3
39-
IntPtr mod = Runtime.PyImport_ImportModule("builtins");
40-
#elif PYTHON2
41-
IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__");
42-
#endif
38+
39+
IntPtr mod = Runtime.IsPython3
40+
? Runtime.PyImport_ImportModule("builtins")
41+
: Runtime.PyDict_GetItemString(dict, "__builtin__");
42+
4343
py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
4444
hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
4545
Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr);
@@ -86,7 +86,14 @@ internal static void Shutdown()
8686
public static IntPtr GetCLRModule(IntPtr? fromList = null)
8787
{
8888
root.InitializePreload();
89-
#if PYTHON3
89+
90+
if (Runtime.IsPython2)
91+
{
92+
Runtime.XIncref(py_clr_module);
93+
return py_clr_module;
94+
}
95+
96+
// Python 3
9097
// update the module dictionary with the contents of the root dictionary
9198
root.LoadNames();
9299
IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module);
@@ -135,7 +142,6 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
135142
}
136143
}
137144
}
138-
#endif
139145
Runtime.XIncref(py_clr_module);
140146
return py_clr_module;
141147
}

src/runtime/runtime.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,18 @@ public class Runtime
170170
internal static Object IsFinalizingLock = new Object();
171171
internal static bool IsFinalizing = false;
172172

173-
internal static bool is32bit;
173+
internal static bool Is32Bit;
174+
internal static bool IsPython2;
175+
internal static bool IsPython3;
174176

175177
/// <summary>
176178
/// Initialize the runtime...
177179
/// </summary>
178180
internal static void Initialize()
179181
{
180-
is32bit = IntPtr.Size == 4;
182+
Is32Bit = IntPtr.Size == 4;
183+
IsPython2 = pyversionnumber < 30;
184+
IsPython3 = pyversionnumber >= 30;
181185

182186
if (Runtime.Py_IsInitialized() == 0)
183187
{
@@ -189,13 +193,19 @@ internal static void Initialize()
189193
Runtime.PyEval_InitThreads();
190194
}
191195

192-
#if PYTHON3
193-
IntPtr op = Runtime.PyImport_ImportModule("builtins");
194-
IntPtr dict = Runtime.PyObject_GetAttrString(op, "__dict__");
195-
#elif PYTHON2
196-
IntPtr dict = Runtime.PyImport_GetModuleDict();
197-
IntPtr op = Runtime.PyDict_GetItemString(dict, "__builtin__");
198-
#endif
196+
IntPtr op;
197+
IntPtr dict;
198+
if (IsPython3)
199+
{
200+
op = Runtime.PyImport_ImportModule("builtins");
201+
dict = Runtime.PyObject_GetAttrString(op, "__dict__");
202+
203+
}
204+
else // Python2
205+
{
206+
dict = Runtime.PyImport_GetModuleDict();
207+
op = Runtime.PyDict_GetItemString(dict, "__builtin__");
208+
}
199209
PyNotImplemented = Runtime.PyObject_GetAttrString(op, "NotImplemented");
200210
PyBaseObjectType = Runtime.PyObject_GetAttrString(op, "object");
201211

@@ -501,7 +511,7 @@ internal unsafe static void XIncref(IntPtr op)
501511
void* p = (void*)op;
502512
if ((void*)0 != p)
503513
{
504-
if (is32bit)
514+
if (Is32Bit)
505515
{
506516
(*(int*)p)++;
507517
}
@@ -524,7 +534,7 @@ internal static unsafe void XDecref(IntPtr op)
524534
void* p = (void*)op;
525535
if ((void*)0 != p)
526536
{
527-
if (is32bit)
537+
if (Is32Bit)
528538
{
529539
--(*(int*)p);
530540
}
@@ -535,11 +545,11 @@ internal static unsafe void XDecref(IntPtr op)
535545
if ((*(int*)p) == 0)
536546
{
537547
// PyObject_HEAD: struct _typeobject *ob_type
538-
void* t = is32bit
548+
void* t = Is32Bit
539549
? (void*)(*((uint*)p + 1))
540550
: (void*)(*((ulong*)p + 1));
541551
// PyTypeObject: destructor tp_dealloc
542-
void* f = is32bit
552+
void* f = Is32Bit
543553
? (void*)(*((uint*)t + 6))
544554
: (void*)(*((ulong*)t + 6));
545555
if ((void*)0 == f)
@@ -558,7 +568,7 @@ internal unsafe static long Refcount(IntPtr op)
558568
void* p = (void*)op;
559569
if ((void*)0 != p)
560570
{
561-
if (is32bit)
571+
if (Is32Bit)
562572
{
563573
return (*(int*)p);
564574
}
@@ -887,7 +897,7 @@ internal unsafe static IntPtr
887897
#else
888898
int n = 1;
889899
#endif
890-
if (is32bit)
900+
if (Is32Bit)
891901
{
892902
return new IntPtr((void*)(*((uint*)p + n)));
893903
}

0 commit comments

Comments
 (0)