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

Skip to content

Commit 8972b15

Browse files
authored
Merge pull request #324 from vmuriart/refactor_directives
Refactor directives
2 parents 8fa699c + c16bb82 commit 8972b15

12 files changed

+107
-128
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ def build_extension(self, ext):
138138
unicode_width = ctypes.sizeof(ctypes.c_wchar)
139139

140140
defines = [
141-
"PYTHON%d%s" % (sys.version_info[:2]),
141+
"PYTHON%d%d" % (sys.version_info[:2]),
142+
"PYTHON%d" % (sys.version_info[:1]), # Python Major Version
142143
"UCS%d" % unicode_width,
143144
]
144145

src/clrmodule/ClrModule.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// set to Python.Runtime's public key token. (sn -T Python.Runtin.dll)
1818
#define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
1919

20-
// If DEBUG_PRINT is defined in the Build Properties, a few Console.WriteLine
20+
// If DEBUG is defined in the Build Properties, a few Console.WriteLine
2121
// calls are made to indicate what's going on during the load...
2222
//============================================================================
2323
using System;
@@ -29,15 +29,15 @@
2929

3030
public class clrModule
3131
{
32-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
32+
#if PYTHON3
3333
[DllExport("PyInit_clr", CallingConvention.StdCall)]
3434
public static IntPtr PyInit_clr()
35-
#else
35+
#elif PYTHON2
3636
[DllExport("initclr", CallingConvention.StdCall)]
3737
public static void initclr()
3838
#endif
3939
{
40-
#if DEBUG_PRINT
40+
#if DEBUG
4141
Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... ");
4242
#endif
4343
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
@@ -65,7 +65,7 @@ public static void initclr()
6565
try
6666
{
6767
pythonRuntime = Assembly.Load(pythonRuntimeName);
68-
#if DEBUG_PRINT
68+
#if DEBUG
6969
Console.WriteLine("Success!");
7070
#endif
7171
}
@@ -89,19 +89,19 @@ public static void initclr()
8989
throw new InvalidOperationException(executingAssembly.Location);
9090
}
9191
string pythonRuntimeDllPath = Path.Combine(assemblyDirectory, "Python.Runtime.dll");
92-
#if DEBUG_PRINT
92+
#if DEBUG
9393
Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath);
9494
#endif
9595
pythonRuntime = Assembly.LoadFrom(pythonRuntimeDllPath);
9696
}
9797
catch (InvalidOperationException)
9898
{
99-
#if DEBUG_PRINT
99+
#if DEBUG
100100
Console.WriteLine("Could not load Python.Runtime");
101101
#endif
102-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
102+
#if PYTHON3
103103
return IntPtr.Zero;
104-
#else
104+
#elif PYTHON2
105105
return;
106106
#endif
107107
}
@@ -111,9 +111,9 @@ public static void initclr()
111111
// So now we get the PythonEngine and execute the InitExt method on it.
112112
Type pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
113113

114-
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
114+
#if PYTHON3
115115
return (IntPtr)pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
116-
#else
116+
#elif PYTHON2
117117
pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
118118
#endif
119119
}

src/clrmodule/clrmodule.csproj

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|x86'">
1919
<DebugSymbols>true</DebugSymbols>
2020
<OutputPath>bin\x86\DebugMono\</OutputPath>
21-
<DefineConstants Condition="'$(DefineConstants)' == ''">DEBUG;TRACE</DefineConstants>
21+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2</DefineConstants>
2222
<DebugType>full</DebugType>
2323
<PlatformTarget>x86</PlatformTarget>
2424
<ErrorReport>prompt</ErrorReport>
@@ -29,7 +29,7 @@
2929
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono|x64'">
3030
<DebugSymbols>true</DebugSymbols>
3131
<OutputPath>bin\x64\DebugMono\</OutputPath>
32-
<DefineConstants Condition="'$(DefineConstants)' == ''">DEBUG;TRACE</DefineConstants>
32+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2</DefineConstants>
3333
<DebugType>full</DebugType>
3434
<PlatformTarget>x64</PlatformTarget>
3535
<ErrorReport>prompt</ErrorReport>
@@ -39,8 +39,7 @@
3939
</PropertyGroup>
4040
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseMono|x86'">
4141
<OutputPath>bin\x86\ReleaseMono\</OutputPath>
42-
<DefineConstants Condition="'$(DefineConstants)' == ''">
43-
</DefineConstants>
42+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2</DefineConstants>
4443
<Optimize>true</Optimize>
4544
<DebugType>pdbonly</DebugType>
4645
<PlatformTarget>x86</PlatformTarget>
@@ -51,8 +50,7 @@
5150
</PropertyGroup>
5251
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseMono|x64'">
5352
<OutputPath>bin\x64\ReleaseMono\</OutputPath>
54-
<DefineConstants Condition="'$(DefineConstants)' == ''">
55-
</DefineConstants>
53+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2</DefineConstants>
5654
<Optimize>true</Optimize>
5755
<DebugType>pdbonly</DebugType>
5856
<PlatformTarget>x64</PlatformTarget>
@@ -64,7 +62,7 @@
6462
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugWin|x86'">
6563
<DebugSymbols>true</DebugSymbols>
6664
<OutputPath>bin\x86\DebugWin\</OutputPath>
67-
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;DEBUG_PRINT</DefineConstants>
65+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2</DefineConstants>
6866
<DebugType>full</DebugType>
6967
<PlatformTarget>x86</PlatformTarget>
7068
<ErrorReport>prompt</ErrorReport>
@@ -75,7 +73,7 @@
7573
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugWin|x64'">
7674
<DebugSymbols>true</DebugSymbols>
7775
<OutputPath>bin\x64\DebugWin\</OutputPath>
78-
<DefineConstants Condition="'$(DefineConstants)' == ''">DEBUG;TRACE</DefineConstants>
76+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2</DefineConstants>
7977
<DebugType>full</DebugType>
8078
<PlatformTarget>x64</PlatformTarget>
8179
<ErrorReport>prompt</ErrorReport>
@@ -85,8 +83,7 @@
8583
</PropertyGroup>
8684
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseWin|x86'">
8785
<OutputPath>bin\x86\ReleaseWin\</OutputPath>
88-
<DefineConstants Condition="'$(DefineConstants)' == ''">
89-
</DefineConstants>
86+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2</DefineConstants>
9087
<Optimize>true</Optimize>
9188
<DebugType>pdbonly</DebugType>
9289
<PlatformTarget>x86</PlatformTarget>
@@ -97,8 +94,7 @@
9794
</PropertyGroup>
9895
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseWin|x64'">
9996
<OutputPath>bin\x64\ReleaseWin\</OutputPath>
100-
<DefineConstants Condition="'$(DefineConstants)' == ''">
101-
</DefineConstants>
97+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2</DefineConstants>
10298
<Optimize>true</Optimize>
10399
<DebugType>pdbonly</DebugType>
104100
<PlatformTarget>x64</PlatformTarget>

src/runtime/Python.Runtime.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|x86'">
1515
<OutputPath>bin\x86\ReleaseMono\</OutputPath>
16-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS4</DefineConstants>
16+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4</DefineConstants>
1717
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1818
<Optimize>true</Optimize>
1919
<DebugType>pdbonly</DebugType>
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|x64'">
2525
<OutputPath>bin\x64\ReleaseMono\</OutputPath>
26-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS4</DefineConstants>
26+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4</DefineConstants>
2727
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2828
<Optimize>true</Optimize>
2929
<DebugType>pdbonly</DebugType>
@@ -33,7 +33,7 @@
3333
</PropertyGroup>
3434
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseWin|x86'">
3535
<OutputPath>bin\x86\ReleaseWin\</OutputPath>
36-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS2</DefineConstants>
36+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS2</DefineConstants>
3737
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3838
<Optimize>true</Optimize>
3939
<DebugType>pdbonly</DebugType>
@@ -43,7 +43,7 @@
4343
</PropertyGroup>
4444
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseWin|x64'">
4545
<OutputPath>bin\x64\ReleaseWin\</OutputPath>
46-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON27, UCS2</DefineConstants>
46+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS2</DefineConstants>
4747
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4848
<Optimize>true</Optimize>
4949
<DebugType>pdbonly</DebugType>
@@ -54,7 +54,7 @@
5454
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|x86'">
5555
<DebugSymbols>true</DebugSymbols>
5656
<OutputPath>bin\x86\DebugMono\</OutputPath>
57-
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
57+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2;PYTHON27;UCS4</DefineConstants>
5858
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5959
<Optimize>false</Optimize>
6060
<DebugType>full</DebugType>
@@ -66,7 +66,7 @@
6666
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|x64'">
6767
<DebugSymbols>true</DebugSymbols>
6868
<OutputPath>bin\x64\DebugMono\</OutputPath>
69-
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
69+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2;PYTHON27;UCS4</DefineConstants>
7070
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7171
<Optimize>false</Optimize>
7272
<DebugType>full</DebugType>
@@ -75,7 +75,7 @@
7575
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugWin|x86'">
7676
<DebugSymbols>true</DebugSymbols>
7777
<OutputPath>bin\x86\DebugWin\</OutputPath>
78-
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
78+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2;PYTHON27;UCS2</DefineConstants>
7979
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8080
<Optimize>false</Optimize>
8181
<DebugType>full</DebugType>
@@ -87,7 +87,7 @@
8787
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugWin|x64'">
8888
<DebugSymbols>true</DebugSymbols>
8989
<OutputPath>bin\x64\DebugWin\</OutputPath>
90-
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
90+
<DefineConstants Condition="'$(DefineConstants)' == ''">TRACE;DEBUG;PYTHON2;PYTHON27;UCS2</DefineConstants>
9191
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9292
<Optimize>false</Optimize>
9393
<DebugType>full</DebugType>

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 & 10 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);
@@ -371,15 +371,11 @@ internal static IntPtr RaiseTypeError(string message)
371371
puplic static variables on the Exceptions class filled in from
372372
the python class using reflection in Initialize() looked up by
373373
name, not posistion. */
374-
#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
375374
public static IntPtr BaseException;
376-
#endif
377375
public static IntPtr Exception;
378376
public static IntPtr StopIteration;
379-
#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
380377
public static IntPtr GeneratorExit;
381-
#endif
382-
#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
378+
#if PYTHON2
383379
public static IntPtr StandardError;
384380
#endif
385381
public static IntPtr ArithmeticError;
@@ -436,10 +432,8 @@ puplic static variables on the Exceptions class filled in from
436432
public static IntPtr SyntaxWarning;
437433
public static IntPtr RuntimeWarning;
438434
public static IntPtr FutureWarning;
439-
#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
440435
public static IntPtr ImportWarning;
441436
public static IntPtr UnicodeWarning;
442437
//PyAPI_DATA(PyObject *) PyExc_BytesWarning;
443-
#endif
444438
}
445439
}

0 commit comments

Comments
 (0)