Skip to content

merge changes to get npython working again on windows #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ install:
- ps: (new-object net.webclient).DownloadFile('https://raw.github.com/pypa/pip/master/contrib/get-pip.py', 'C:\get-pip.py')
# appveyor has python 2.7.6 x86 preinstalled, but in the wrong directory, this works around this
- ps: if ($env:pythonurl -eq "http://www.python.org/ftp/python/2.7.6/python-2.7.6.msi") {mi c:\python27 c:\python}
- set PATH=C:\Python;%PATH%
- C:\Python\python.exe c:\get-pip.py
- C:\Python\Scripts\pip.exe install wheel

Expand Down
19 changes: 17 additions & 2 deletions pythonnet/src/console/pythonconsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System;
using System.Reflection;
using System.Collections.Generic;
using Python.Runtime;

namespace Python.Runtime {
Expand All @@ -19,6 +20,9 @@ private PythonConsole() {}

[STAThread]
public static int Main(string[] args) {
// reference the static assemblyLoader to stop it being optimized away
AssemblyLoader a = assemblyLoader;

string [] cmd = Environment.GetCommandLineArgs();
PythonEngine.Initialize();

Expand All @@ -31,16 +35,27 @@ public static int Main(string[] args) {
// Register a callback function to load embedded assmeblies.
// (Python.Runtime.dll is included as a resource)
private sealed class AssemblyLoader {
Dictionary<string, Assembly> loadedAssemblies;

public AssemblyLoader() {
loadedAssemblies = new Dictionary<string, Assembly>();

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
String resourceName = new AssemblyName(args.Name).Name + ".dll";
string shortName = args.Name.Split(',')[0];
String resourceName = shortName + ".dll";

if (loadedAssemblies.ContainsKey(resourceName)) {
return loadedAssemblies[resourceName];
}

// looks for the assembly from the resources and load it
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
if (stream != null) {
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
Assembly assembly = Assembly.Load(assemblyData);
loadedAssemblies[resourceName] = assembly;
return assembly;
}
}

Expand Down
16 changes: 15 additions & 1 deletion pythonnet/src/runtime/assemblymanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal class AssemblyManager {
static ResolveEventHandler rhandler;
static Dictionary<string, int> probed;
static List<Assembly> assemblies;
static Dictionary<string, Assembly> loadedAssemblies;
internal static List<string> pypath;

private AssemblyManager() {}
Expand All @@ -46,6 +47,7 @@ internal static void Initialize() {
probed = new Dictionary<string, int>(32);
//generics = new Dictionary<string, Dictionary<string, string>>();
assemblies = new List<Assembly>(16);
loadedAssemblies = new Dictionary<string, Assembly>();
pypath = new List<string>(16);

AppDomain domain = AppDomain.CurrentDomain;
Expand Down Expand Up @@ -202,7 +204,19 @@ public static Assembly LoadAssemblyPath(string name) {
string path = FindAssembly(name);
Assembly assembly = null;
if (path != null) {
try { assembly = Assembly.LoadFrom(path); }
if (loadedAssemblies.ContainsKey(path)) {
return loadedAssemblies[path];
}
// Avoid using Assembly.LoadFrom as referenced assemblies that exist
// in the same path will be loaded directly from there, rather than
// using other versions already loaded. This is a problem if there
// is a Python.Runtime.dll in the same folder as the assembly being
// loaded, as that will result in two instances being loaded.
try {
byte[] bytes = System.IO.File.ReadAllBytes(path);
assembly = Assembly.Load(bytes);
loadedAssemblies[path] = assembly;
}
catch {}
}
return assembly;
Expand Down
54 changes: 31 additions & 23 deletions pythonnet/src/testing/threadtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,43 @@ public class ThreadTest {

public static string CallEchoString(string arg) {
IntPtr gs = PythonEngine.AcquireLock();
if (module == null) {
module = PythonEngine.ModuleFromString("tt", testmod);
try {
if (module == null) {
module = PythonEngine.ModuleFromString("tt", testmod);
}
PyObject func = module.GetAttr("echostring");
PyString parg = new PyString(arg);
PyObject temp = func.Invoke(parg);
string result = (string)temp.AsManagedObject(typeof(String));
func.Dispose();
parg.Dispose();
temp.Dispose();
return result;
}
finally {
PythonEngine.ReleaseLock(gs);
}
PyObject func = module.GetAttr("echostring");
PyString parg = new PyString(arg);
PyObject temp = func.Invoke(parg);
string result = (string)temp.AsManagedObject(typeof(String));
func.Dispose();
parg.Dispose();
temp.Dispose();
PythonEngine.ReleaseLock(gs);
return result;
}

public static string CallEchoString2(string arg) {
IntPtr gs = PythonEngine.AcquireLock();
if (module == null) {
module = PythonEngine.ModuleFromString("tt", testmod);
}
try {
if (module == null) {
module = PythonEngine.ModuleFromString("tt", testmod);
}

PyObject func = module.GetAttr("echostring2");
PyString parg = new PyString(arg);
PyObject temp = func.Invoke(parg);
string result = (string)temp.AsManagedObject(typeof(String));
func.Dispose();
parg.Dispose();
temp.Dispose();
PythonEngine.ReleaseLock(gs);
return result;
PyObject func = module.GetAttr("echostring2");
PyString parg = new PyString(arg);
PyObject temp = func.Invoke(parg);
string result = (string)temp.AsManagedObject(typeof(String));
func.Dispose();
parg.Dispose();
temp.Dispose();
return result;
}
finally {
PythonEngine.ReleaseLock(gs);
}
}


Expand Down