Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfba7586

Browse files
committed
Merge pull request#41 from tonyroberts/develop
merge changes to get npython working again on windowsfixes#39
2 parents0ac66b3 +56bebfa commitfba7586

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

‎appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ install:
1616
-ps:(new-object net.webclient).DownloadFile('https://raw.github.com/pypa/pip/master/contrib/get-pip.py', 'C:\get-pip.py')
1717
# appveyor has python 2.7.6 x86 preinstalled, but in the wrong directory, this works around this
1818
-ps:if ($env:pythonurl -eq "http://www.python.org/ftp/python/2.7.6/python-2.7.6.msi") {mi c:\python27 c:\python}
19+
-set PATH=C:\Python;%PATH%
1920
-C:\Python\python.exe c:\get-pip.py
2021
-C:\Python\Scripts\pip.exe install wheel
2122

‎pythonnet/src/console/pythonconsole.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
usingSystem;
1111
usingSystem.Reflection;
12+
usingSystem.Collections.Generic;
1213
usingPython.Runtime;
1314

1415
namespacePython.Runtime{
@@ -19,6 +20,9 @@ private PythonConsole() {}
1920

2021
[STAThread]
2122
publicstaticintMain(string[]args){
23+
// reference the static assemblyLoader to stop it being optimized away
24+
AssemblyLoadera=assemblyLoader;
25+
2226
string[]cmd=Environment.GetCommandLineArgs();
2327
PythonEngine.Initialize();
2428

@@ -31,16 +35,27 @@ public static int Main(string[] args) {
3135
// Register a callback function to load embedded assmeblies.
3236
// (Python.Runtime.dll is included as a resource)
3337
privatesealedclassAssemblyLoader{
38+
Dictionary<string,Assembly>loadedAssemblies;
39+
3440
publicAssemblyLoader(){
41+
loadedAssemblies=newDictionary<string,Assembly>();
42+
3543
AppDomain.CurrentDomain.AssemblyResolve+=(sender,args)=>{
36-
StringresourceName=newAssemblyName(args.Name).Name+".dll";
44+
stringshortName=args.Name.Split(',')[0];
45+
StringresourceName=shortName+".dll";
46+
47+
if(loadedAssemblies.ContainsKey(resourceName)){
48+
returnloadedAssemblies[resourceName];
49+
}
3750

3851
// looks for the assembly from the resources and load it
3952
using(varstream=Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)){
4053
if(stream!=null){
4154
Byte[]assemblyData=newByte[stream.Length];
4255
stream.Read(assemblyData,0,assemblyData.Length);
43-
returnAssembly.Load(assemblyData);
56+
Assemblyassembly=Assembly.Load(assemblyData);
57+
loadedAssemblies[resourceName]=assembly;
58+
returnassembly;
4459
}
4560
}
4661

‎pythonnet/src/runtime/assemblymanager.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ internal class AssemblyManager {
3030
staticResolveEventHandlerrhandler;
3131
staticDictionary<string,int>probed;
3232
staticList<Assembly>assemblies;
33+
staticDictionary<string,Assembly>loadedAssemblies;
3334
internalstaticList<string>pypath;
3435

3536
privateAssemblyManager(){}
@@ -46,6 +47,7 @@ internal static void Initialize() {
4647
probed=newDictionary<string,int>(32);
4748
//generics = new Dictionary<string, Dictionary<string, string>>();
4849
assemblies=newList<Assembly>(16);
50+
loadedAssemblies=newDictionary<string,Assembly>();
4951
pypath=newList<string>(16);
5052

5153
AppDomaindomain=AppDomain.CurrentDomain;
@@ -202,7 +204,19 @@ public static Assembly LoadAssemblyPath(string name) {
202204
stringpath=FindAssembly(name);
203205
Assemblyassembly=null;
204206
if(path!=null){
205-
try{assembly=Assembly.LoadFrom(path);}
207+
if(loadedAssemblies.ContainsKey(path)){
208+
returnloadedAssemblies[path];
209+
}
210+
// Avoid using Assembly.LoadFrom as referenced assemblies that exist
211+
// in the same path will be loaded directly from there, rather than
212+
// using other versions already loaded. This is a problem if there
213+
// is a Python.Runtime.dll in the same folder as the assembly being
214+
// loaded, as that will result in two instances being loaded.
215+
try{
216+
byte[]bytes=System.IO.File.ReadAllBytes(path);
217+
assembly=Assembly.Load(bytes);
218+
loadedAssemblies[path]=assembly;
219+
}
206220
catch{}
207221
}
208222
returnassembly;

‎pythonnet/src/testing/threadtest.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,35 +39,43 @@ public class ThreadTest {
3939

4040
publicstaticstringCallEchoString(stringarg){
4141
IntPtrgs=PythonEngine.AcquireLock();
42-
if(module==null){
43-
module=PythonEngine.ModuleFromString("tt",testmod);
42+
try{
43+
if(module==null){
44+
module=PythonEngine.ModuleFromString("tt",testmod);
45+
}
46+
PyObjectfunc=module.GetAttr("echostring");
47+
PyStringparg=newPyString(arg);
48+
PyObjecttemp=func.Invoke(parg);
49+
stringresult=(string)temp.AsManagedObject(typeof(String));
50+
func.Dispose();
51+
parg.Dispose();
52+
temp.Dispose();
53+
returnresult;
54+
}
55+
finally{
56+
PythonEngine.ReleaseLock(gs);
4457
}
45-
PyObjectfunc=module.GetAttr("echostring");
46-
PyStringparg=newPyString(arg);
47-
PyObjecttemp=func.Invoke(parg);
48-
stringresult=(string)temp.AsManagedObject(typeof(String));
49-
func.Dispose();
50-
parg.Dispose();
51-
temp.Dispose();
52-
PythonEngine.ReleaseLock(gs);
53-
returnresult;
5458
}
5559

5660
publicstaticstringCallEchoString2(stringarg){
5761
IntPtrgs=PythonEngine.AcquireLock();
58-
if(module==null){
59-
module=PythonEngine.ModuleFromString("tt",testmod);
60-
}
62+
try{
63+
if(module==null){
64+
module=PythonEngine.ModuleFromString("tt",testmod);
65+
}
6166

62-
PyObjectfunc=module.GetAttr("echostring2");
63-
PyStringparg=newPyString(arg);
64-
PyObjecttemp=func.Invoke(parg);
65-
stringresult=(string)temp.AsManagedObject(typeof(String));
66-
func.Dispose();
67-
parg.Dispose();
68-
temp.Dispose();
69-
PythonEngine.ReleaseLock(gs);
70-
returnresult;
67+
PyObjectfunc=module.GetAttr("echostring2");
68+
PyStringparg=newPyString(arg);
69+
PyObjecttemp=func.Invoke(parg);
70+
stringresult=(string)temp.AsManagedObject(typeof(String));
71+
func.Dispose();
72+
parg.Dispose();
73+
temp.Dispose();
74+
returnresult;
75+
}
76+
finally{
77+
PythonEngine.ReleaseLock(gs);
78+
}
7179
}
7280

7381

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp