8000 Add IsPython2 and IsPython3 · pythonnet/pythonnet@5d1b5a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d1b5a8

Browse files
committed
Add IsPython2 and IsPython3
1 parent fe584d2 commit 5d1b5a8

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

src/runtime/converter.cs

Lines changed: 8 additions & 12 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
{

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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,17 @@ public class Runtime
181181
internal static bool IsFinalizing = false;
182182

183183
internal static bool Is32Bit;
184+
internal static bool IsPython2;
185+
internal static bool IsPython3;
184186

185187
/// <summary>
186188
/// Initialize the runtime...
187189
/// </summary>
188190
internal static void Initialize()
189191
{
190192
Is32Bit = IntPtr.Size == 4;
193+
IsPython2 = pyversionnumber < 30;
194+
IsPython3 = pyversionnumber >= 30;
191195

192196
if (0 == Runtime.Py_IsInitialized())
193197
{
@@ -199,13 +203,19 @@ internal static void Initialize()
199203
Runtime.PyEval_InitThreads();
200204
}
201205

202-
#if PYTHON3
203-
IntPtr op = Runtime.PyImport_ImportModule("builtins");
204-
IntPtr dict = Runtime.PyObject_GetAttrString(op, "__dict__");
205-
#elif PYTHON2
206-
IntPtr dict = Runtime.PyImport_GetModuleDict();
207-
IntPtr op = Runtime.PyDict_GetItemString(dict, "__builtin__");
208-
#endif
206+
IntPtr op;
207+
IntPtr dict;
208+
if (IsPython3)
209+
{
210+
op = Runtime.PyImport_ImportModule("builtins");
211+
dict = Runtime.PyObject_GetAttrString(op, "__dict__");
212+
213+
}
214+
else // Python2
215+
{
216+
dict = Runtime.PyImport_GetModuleDict();
217+
op = Runtime.PyDict_GetItemString(dict, "__builtin__");
218+
}
209219
PyNotImplemented = Runtime.PyObject_GetAttrString(op, "NotImplemented");
210220
PyBaseObjectType = Runtime.PyObject_GetAttrString(op, "object");
211221

0 commit comments

Comments
 (0)
0