Skip to content

Commit 8134b33

Browse files
committed
add Python 3 support
1 parent 0e0068a commit 8134b33

20 files changed

+778
-96
lines changed

pythonnet/setupegg.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
from setuptools import setup
3+
4+
if sys.version_info[0] >= 3:
5+
import imp
6+
setupfile = imp.load_source('setupfile', 'setupwin.py')
7+
else:
8+
execfile('setupwin.py')

pythonnet/setupwin.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Setup for packaging clr into an egg.
3+
"""
4+
from distutils.core import setup, Extension
5+
from distutils.command.build_ext import build_ext
6+
from platform import architecture
7+
import subprocess
8+
import shutil
9+
import sys
10+
import os
11+
12+
from distutils import msvc9compiler
13+
msvc9compiler.VERSION = 11
14+
15+
class PythonNET_BuildExt(build_ext):
16+
17+
def build_extension(self, ext):
18+
"""
19+
Builds the .pyd file using msbuild.
20+
"""
21+
if ext.name != "clr":
22+
return super(PythonNET_BuildExt, self).build_extension(ext)
23+
24+
cc = msvc9compiler.MSVCCompiler()
25+
cc.initialize()
26+
msbuild = cc.find_exe("msbuild.exe")
27+
platform = "x64" if architecture()[0] == "64bit" else "x86"
28+
defines = [
29+
"PYTHON%d%s" % (sys.version_info[:2]),
30+
"UCS2"
31+
]
32+
33+
cmd = [
34+
msbuild,
35+
"pythonnet.sln",
36+
"/p:Configuration=ReleaseWin",
37+
"/p:Platform=%s" % platform,
38+
"/p:DefineConstants=\"%s\"" % ";".join(defines),
39+
"/t:clrmodule",
40+
]
41+
self.announce("Building: %s" % " ".join(cmd))
42+
subprocess.check_call(" ".join(cmd))
43+
44+
dest_file = self.get_ext_fullpath(ext.name)
45+
dest_dir = os.path.dirname(dest_file)
46+
if not os.path.exists(dest_dir):
47+
os.makedirs(dest_dir)
48+
49+
src_file = os.path.join("src", "clrmodule", "bin", platform, "Release", "clr.pyd")
50+
self.announce("Copying %s to %s" % (src_file, dest_file))
51+
shutil.copyfile(src_file, dest_file)
52+
53+
dest_file = os.path.join(dest_dir, "Python.Runtime.dll")
54+
src_file = os.path.join("src", "runtime", "bin", platform, "Release", "Python.Runtime.dll")
55+
self.announce("Copying %s to %s" % (src_file, dest_file))
56+
shutil.copyfile(src_file, dest_file)
57+
58+
setup(name="pythonnet",
59+
ext_modules=[
60+
Extension("clr", sources=[])
61+
],
62+
cmdclass = {
63+
"build_ext" : PythonNET_BuildExt
64+
}
65+
)

pythonnet/src/clrmodule/ClrModule.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@
3131
// to indicate what's going on during the load...
3232
#define DEBUG_PRINT
3333
//============================================================================
34-
34+
using System;
3535

3636
// ReSharper disable CheckNamespace
3737
// ReSharper disable InconsistentNaming
3838
public class clrModule
3939
// ReSharper restore InconsistentNaming
4040
// ReSharper restore CheckNamespace
4141
{
42-
43-
[RGiesecke.DllExport.DllExport("initclr", System.Runtime.InteropServices.CallingConvention.StdCall)]
4442
// ReSharper disable InconsistentNaming
43+
#if (PYTHON32 || PYTHON33 || PYTHON34)
44+
[RGiesecke.DllExport.DllExport("PyInit_clr", System.Runtime.InteropServices.CallingConvention.StdCall)]
45+
public static IntPtr PyInit_clr()
46+
#else
47+
[RGiesecke.DllExport.DllExport("initclr", System.Runtime.InteropServices.CallingConvention.StdCall)]
4548
public static void initclr()
49+
#endif
4650
// ReSharper restore InconsistentNaming
4751
{
4852
#if DEBUG_PRINT
@@ -77,7 +81,7 @@ public static void initclr()
7781
System.Console.WriteLine("Success!");
7882
#endif
7983
}
80-
catch (System.IO.FileNotFoundException)
84+
catch (System.IO.IOException)
8185
{
8286
try
8387
{
@@ -104,13 +108,22 @@ public static void initclr()
104108
#if DEBUG_PRINT
105109
System.Console.WriteLine("Could not load Python.Runtime, so sad.");
106110
#endif
111+
#if (PYTHON32 || PYTHON33 || PYTHON34)
112+
return IntPtr.Zero;
113+
#else
107114
return;
115+
#endif
108116
}
109117
}
110118

111119
// Once here, we've successfully loaded SOME version of Python.Runtime
112120
// So now we get the PythonEngine and execute the InitExt method on it.
113121
var pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
122+
123+
#if (PYTHON32 || PYTHON33 || PYTHON34)
124+
return (IntPtr)pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
125+
#else
114126
pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
127+
#endif
115128
}
116129
}

pythonnet/src/clrmodule/clrmodule.csproj

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<DebugType>full</DebugType>
1919
<Optimize>false</Optimize>
2020
<OutputPath>bin\Debug\</OutputPath>
21-
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
2424
<PlatformTarget>x86</PlatformTarget>
@@ -27,7 +27,7 @@
2727
<DebugType>pdbonly</DebugType>
2828
<Optimize>true</Optimize>
2929
<OutputPath>bin\Release\</OutputPath>
30-
<DefineConstants>TRACE</DefineConstants>
30+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE</DefineConstants>
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
3333
<PlatformTarget>x86</PlatformTarget>
@@ -42,7 +42,7 @@
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
4343
<DebugSymbols>true</DebugSymbols>
4444
<OutputPath>bin\x86\Debug\</OutputPath>
45-
<DefineConstants>DEBUG;TRACE</DefineConstants>
45+
<DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
4646
<DebugType>full</DebugType>
4747
<PlatformTarget>x86</PlatformTarget>
4848
<ErrorReport>prompt</ErrorReport>
@@ -52,7 +52,7 @@
5252
</PropertyGroup>
5353
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
5454
<OutputPath>bin\x86\Release\</OutputPath>
55-
<DefineConstants>TRACE</DefineConstants>
55+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE</DefineConstants>
5656
<Optimize>true</Optimize>
5757
<DebugType>pdbonly</DebugType>
5858
<PlatformTarget>x86</PlatformTarget>
@@ -64,7 +64,7 @@
6464
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|AnyCPU'">
6565
<DebugSymbols>true</DebugSymbols>
6666
<OutputPath>bin\DebugMono_x86\</OutputPath>
67-
<DefineConstants>DEBUG;TRACE</DefineConstants>
67+
<DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
6868
<DebugType>full</DebugType>
6969
<PlatformTarget>x86</PlatformTarget>
7070
<ErrorReport>prompt</ErrorReport>
@@ -74,7 +74,7 @@
7474
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|x86'">
7575
<DebugSymbols>true</DebugSymbols>
7676
<OutputPath>bin\x86\DebugMono_x86\</OutputPath>
77-
<DefineConstants>DEBUG;TRACE</DefineConstants>
77+
<DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
7878
<DebugType>full</DebugType>
7979
<PlatformTarget>x86</PlatformTarget>
8080
<ErrorReport>prompt</ErrorReport>
@@ -85,7 +85,7 @@
8585
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
8686
<DebugSymbols>true</DebugSymbols>
8787
<OutputPath>bin\x64\Debug\</OutputPath>
88-
<DefineConstants>DEBUG;TRACE</DefineConstants>
88+
<DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
8989
<DebugType>full</DebugType>
9090
<PlatformTarget>x64</PlatformTarget>
9191
<ErrorReport>prompt</ErrorReport>
@@ -94,7 +94,7 @@
9494
</PropertyGroup>
9595
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
9696
<OutputPath>bin\x64\Release\</OutputPath>
97-
<DefineConstants>TRACE</DefineConstants>
97+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE</DefineConstants>
9898
<Optimize>true</Optimize>
9999
<DebugType>pdbonly</DebugType>
100100
<PlatformTarget>x64</PlatformTarget>
@@ -105,7 +105,7 @@
105105
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|x64'">
106106
<DebugSymbols>true</DebugSymbols>
107107
<OutputPath>bin\x64\DebugMono_x86\</OutputPath>
108-
<DefineConstants>DEBUG;TRACE</DefineConstants>
108+
<DefineConstants Condition=" '$(DefineConstants)'==''" >DEBUG;TRACE</DefineConstants>
109109
<DebugType>full</DebugType>
110110
<PlatformTarget>x64</PlatformTarget>
111111
<ErrorReport>prompt</ErrorReport>

pythonnet/src/runtime/Python.Runtime.csproj

+17-17
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
<DebugType>full</DebugType>
1616
<Optimize>true</Optimize>
1717
<OutputPath>.\bin\Debug\</OutputPath>
18-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
18+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
1919
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2020
</PropertyGroup>
2121
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2222
<DebugType>pdbonly</DebugType>
2323
<Optimize>true</Optimize>
2424
<OutputPath>.\bin\Release\</OutputPath>
25-
<DefineConstants>TRACE;PYTHON27, UCS2</DefineConstants>
25+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;PYTHON27, UCS2</DefineConstants>
2626
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2727
</PropertyGroup>
2828
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'EmbeddingTest|AnyCPU' ">
2929
<DebugSymbols>true</DebugSymbols>
3030
<OutputPath>bin\EmbeddingTest\</OutputPath>
31-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
31+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
3232
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3333
<Optimize>true</Optimize>
3434
<DebugType>full</DebugType>
@@ -37,30 +37,30 @@
3737
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'UnitTests|AnyCPU' ">
3838
<DebugSymbols>true</DebugSymbols>
3939
<OutputPath>bin\UnitTests\</OutputPath>
40-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
40+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
4141
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4242
<Optimize>true</Optimize>
4343
<DebugType>full</DebugType>
4444
<PlatformTarget>AnyCPU</PlatformTarget>
4545
</PropertyGroup>
4646
<PropertyGroup>
47-
<SignAssembly>true</SignAssembly>
47+
<SignAssembly>false</SignAssembly>
4848
</PropertyGroup>
4949
<PropertyGroup>
5050
<AssemblyOriginatorKeyFile>pythonnet.snk</AssemblyOriginatorKeyFile>
5151
</PropertyGroup>
5252
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
5353
<DebugSymbols>true</DebugSymbols>
5454
<OutputPath>bin\x86\Debug\</OutputPath>
55-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
55+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
5656
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5757
<Optimize>true</Optimize>
5858
<DebugType>full</DebugType>
5959
<PlatformTarget>x86</PlatformTarget>
6060
</PropertyGroup>
6161
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
6262
<OutputPath>bin\x86\Release\</OutputPath>
63-
<DefineConstants>PYTHON27, UCS4</DefineConstants>
63+
<DefineConstants Condition=" '$(DefineConstants)'==''" >PYTHON27, UCS4</DefineConstants>
6464
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6565
<Optimize>true</Optimize>
6666
<DebugType>pdbonly</DebugType>
@@ -70,7 +70,7 @@
7070
</PropertyGroup>
7171
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'EmbeddingTest|x86'">
7272
<DebugSymbols>true</DebugSymbols>
73-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
73+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
7474
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7575
<Optimize>true</Optimize>
7676
<DebugType>full</DebugType>
@@ -82,7 +82,7 @@
8282
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'UnitTests|x86'">
8383
<DebugSymbols>true</DebugSymbols>
8484
<OutputPath>bin\x86\UnitTests\</OutputPath>
85-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
85+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
8686
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8787
<Optimize>true</Optimize>
8888
<DebugType>full</DebugType>
@@ -91,7 +91,7 @@
9191
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|AnyCPU'">
9292
<DebugSymbols>true</DebugSymbols>
9393
<OutputPath>bin\DebugMono_x86\</OutputPath>
94-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
94+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
9595
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9696
<Optimize>true</Optimize>
9797
<DebugType>full</DebugType>
@@ -103,7 +103,7 @@
103103
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|x86'">
104104
<DebugSymbols>true</DebugSymbols>
105105
<OutputPath>bin\x86\DebugMono_x86\</OutputPath>
106-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
106+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
107107
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
108108
<Optimize>true</Optimize>
109109
<DebugType>full</DebugType>
@@ -112,7 +112,7 @@
112112
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
113113
<DebugSymbols>true</DebugSymbols>
114114
<OutputPath>bin\x64\Debug\</OutputPath>
115-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
115+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
116116
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
117117
<Optimize>true</Optimize>
118118
<DebugType>full</DebugType>
@@ -121,7 +121,7 @@
121121
</PropertyGroup>
122122
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
123123
<OutputPath>bin\x64\Release\</OutputPath>
124-
<DefineConstants>PYTHON27, UCS4</DefineConstants>
124+
<DefineConstants Condition=" '$(DefineConstants)'==''" >PYTHON32, UCS2</DefineConstants>
125125
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
126126
<Optimize>true</Optimize>
127127
<DebugType>pdbonly</DebugType>
@@ -132,7 +132,7 @@
132132
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'EmbeddingTest|x64'">
133133
<DebugSymbols>true</DebugSymbols>
134134
<OutputPath>bin\x64\EmbeddingTest\</OutputPath>
135-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
135+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
136136
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
137137
<Optimize>true</Optimize>
138138
<DebugType>full</DebugType>
@@ -144,7 +144,7 @@
144144
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'UnitTests|x64'">
145145
<DebugSymbols>true</DebugSymbols>
146146
<OutputPath>bin\x64\UnitTests\</OutputPath>
147-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
147+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS2</DefineConstants>
148148
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
149149
<Optimize>true</Optimize>
150150
<DebugType>full</DebugType>
@@ -156,7 +156,7 @@
156156
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugMono_x86|x64'">
157157
<DebugSymbols>true</DebugSymbols>
158158
<OutputPath>bin\x64\DebugMono_x86\</OutputPath>
159-
<DefineConstants>TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
159+
<DefineConstants Condition=" '$(DefineConstants)'==''" >TRACE;DEBUG;PYTHON27,UCS4</DefineConstants>
160160
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
161161
<Optimize>true</Optimize>
162162
<DebugType>full</DebugType>
@@ -244,4 +244,4 @@ copy "$(TargetDir)clr.pyd" "$(SolutionDir)"
244244
<PreBuildEvent>del "$(TargetDir)clr.pyd"</PreBuildEvent>
245245
</PropertyGroup>
246246
<Import Project="../../packages/UnmanagedExports.1.2.3-Beta/tools/RGiesecke.DllExport.targets" />
247-
</Project>
247+
</Project>

pythonnet/src/runtime/buildclrmodule.bat

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set INPUT_PATH="%INPUT_DIRECTORY%\clrmodule.il"
88
set OUTPUT_PATH=%3
99

1010
if %TARGET_PLATFORM%==AnyCPU goto SETUP32
11+
if %TARGET_PLATFORM%==x86 goto SETUP32
1112
if %TARGET_PLATFORM%==x64 goto SETUP64
1213
goto ERROR_BAD_PLATFORM
1314

0 commit comments

Comments
 (0)