| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 14e8f42 commit 02848fa
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,24 +26,23 @@ namespace Python.Runtime { | |||
| 26 | 26 | ||
| 27 | 27 | internal class CodeGenerator { | |
| 28 | 28 | ||
| 29 | - static AssemblyBuilder aBuilder; | ||
| 30 | - static ModuleBuilder mBuilder; | ||
| 29 | + AssemblyBuilder aBuilder; | ||
| 30 | + ModuleBuilder mBuilder; | ||
| 31 | 31 | ||
| 32 | - static CodeGenerator() { | ||
| 32 | + internal CodeGenerator() { | ||
| 33 | 33 | AssemblyName aname = new AssemblyName(); | |
| 34 | 34 | aname.Name = "__CodeGenerator_Assembly"; | |
| 35 | 35 | AssemblyBuilderAccess aa = AssemblyBuilderAccess.Run; | |
| 36 | 36 | ||
| 37 | 37 | aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa); | |
| 38 | 38 | mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module"); | |
| 39 | - | ||
| 40 | 39 | } | |
| 41 | 40 | ||
| 42 | 41 | //==================================================================== | |
| 43 | 42 | // DefineType is a shortcut utility to get a new TypeBuilder. | |
| 44 | 43 | //==================================================================== | |
| 45 | 44 | ||
| 46 | - internal static TypeBuilder DefineType(string name) { | ||
| 45 | + internal TypeBuilder DefineType(string name) { | ||
| 47 | 46 | TypeAttributes attrs = TypeAttributes.Public; | |
| 48 | 47 | return mBuilder.DefineType(name, attrs); | |
| 49 | 48 | } | |
@@ -52,7 +51,7 @@ internal static TypeBuilder DefineType(string name) { | |||
| 52 | 51 | // DefineType is a shortcut utility to get a new TypeBuilder. | |
| 53 | 52 | //==================================================================== | |
| 54 | 53 | ||
| 55 | - internal static TypeBuilder DefineType(string name, Type basetype) { | ||
| 54 | + internal TypeBuilder DefineType(string name, Type basetype) { | ||
| 56 | 55 | TypeAttributes attrs = TypeAttributes.Public; | |
| 57 | 56 | return mBuilder.DefineType(name, attrs, basetype); | |
| 58 | 57 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,20 +24,22 @@ namespace Python.Runtime { | |||
| 24 | 24 | ||
| 25 | 25 | internal class DelegateManager { | |
| 26 | 26 | ||
| 27 | - static Hashtable cache; | ||
| 28 | - static Type basetype; | ||
| 29 | - static Type listtype; | ||
| 30 | - static Type voidtype; | ||
| 31 | - static Type typetype; | ||
| 32 | - static Type ptrtype; | ||
| 33 | - | ||
| 34 | - static DelegateManager() { | ||
| 27 | + Hashtable cache; | ||
| 28 | + Type basetype; | ||
| 29 | + Type listtype; | ||
| 30 | + Type voidtype; | ||
| 31 | + Type typetype; | ||
| 32 | + Type ptrtype; | ||
| 33 | + CodeGenerator codeGenerator; | ||
| 34 | + | ||
| 35 | + public DelegateManager() { | ||
| 35 | 36 | basetype = typeof(Dispatcher); | |
| 36 | 37 | listtype = typeof(ArrayList); | |
| 37 | 38 | voidtype = typeof(void); | |
| 38 | 39 | typetype = typeof(Type); | |
| 39 | 40 | ptrtype = typeof(IntPtr); | |
| 40 | 41 | cache = new Hashtable(); | |
| 42 | + codeGenerator = new CodeGenerator(); | ||
| 41 | 43 | } | |
| 42 | 44 | ||
| 43 | 45 | //==================================================================== | |
@@ -46,7 +48,7 @@ static DelegateManager() { | |||
| 46 | 48 | // delegate is not implemented in Python code. | |
| 47 | 49 | //==================================================================== | |
| 48 | 50 | ||
| 49 | - public static IntPtr GetPythonHandle(Delegate d) { | ||
| 51 | + public IntPtr GetPythonHandle(Delegate d) { | ||
| 50 | 52 | if ((d != null) && (d.Target is Dispatcher)) { | |
| 51 | 53 | Dispatcher disp = d.Target as Dispatcher; | |
| 52 | 54 | return disp.target; | |
@@ -59,7 +61,7 @@ public static IntPtr GetPythonHandle(Delegate d) { | |||
| 59 | 61 | // an appropriate managed callback method for a given delegate type. | |
| 60 | 62 | //==================================================================== | |
| 61 | 63 | ||
| 62 | - private static Type GetDispatcher(Type dtype) { | ||
| 64 | + private Type GetDispatcher(Type dtype) { | ||
| 63 | 65 | ||
| 64 | 66 | // If a dispatcher type for the given delegate type has already | |
| 65 | 67 | // been generated, get it from the cache. The cache maps delegate | |
@@ -76,7 +78,7 @@ private static Type GetDispatcher(Type dtype) { | |||
| 76 | 78 | string name = "__" + dtype.FullName + "Dispatcher"; | |
| 77 | 79 | name = name.Replace('.', '_'); | |
| 78 | 80 | name = name.Replace('+', '_'); | |
| 79 | - TypeBuilder tb = CodeGenerator.DefineType(name, basetype); | ||
| 81 | + TypeBuilder tb = codeGenerator.DefineType(name, basetype); | ||
| 80 | 82 | ||
| 81 | 83 | // Generate a constructor for the generated type that calls the | |
| 82 | 84 | // appropriate constructor of the Dispatcher base type. | |
@@ -164,7 +166,7 @@ private static Type GetDispatcher(Type dtype) { | |||
| 164 | 166 | // returned will dispatch calls to the given Python object. | |
| 165 | 167 | //==================================================================== | |
| 166 | 168 | ||
| 167 | - internal static Delegate GetDelegate(Type dtype, IntPtr callable) { | ||
| 169 | + internal Delegate GetDelegate(Type dtype, IntPtr callable) { | ||
| 168 | 170 | Type dispatcher = GetDispatcher(dtype); | |
| 169 | 171 | object[] args = {callable, dtype}; | |
| 170 | 172 | object o = Activator.CreateInstance(dispatcher, args); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,7 +71,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) { | |||
| 71 | 71 | return Exceptions.RaiseTypeError("argument must be callable"); | |
| 72 | 72 | } | |
| 73 | 73 | ||
| 74 | - Delegate d = DelegateManager.GetDelegate(self.type, method); | ||
| 74 | + Delegate d = PythonEngine.DelegateManager.GetDelegate(self.type, method); | ||
| 75 | 75 | return CLRObject.GetInstHandle(d, self.pyHandle); | |
| 76 | 76 | } | |
| 77 | 77 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,7 +46,7 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler) { | |||
| 46 | 46 | // always succeeds, though calling the wrapper may fail. | |
| 47 | 47 | ||
| 48 | 48 | Type type = this.info.EventHandlerType; | |
| 49 | - Delegate d = DelegateManager.GetDelegate(type, handler); | ||
| 49 | + Delegate d = PythonEngine.DelegateManager.GetDelegate(type, handler); | ||
| 50 | 50 | ||
| 51 | 51 | // Now register the handler in a mapping from instance to pairs | |
| 52 | 52 | // of (handler hash, delegate) so we can lookup to remove later. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,7 @@ namespace Python.Runtime { | |||
| 18 | 18 | ||
| 19 | 19 | public class PythonEngine { | |
| 20 | 20 | ||
| 21 | + private static DelegateManager delegateManager; | ||
| 21 | 22 | private static bool initialized; | |
| 22 | 23 | ||
| 23 | 24 | #region Properties | |
@@ -28,6 +29,15 @@ public static bool IsInitialized { | |||
| 28 | 29 | } | |
| 29 | 30 | } | |
| 30 | 31 | ||
| 32 | + internal static DelegateManager DelegateManager { | ||
| 33 | + get { | ||
| 34 | + if (delegateManager == null) { | ||
| 35 | + throw new InvalidOperationException("DelegateManager has not yet been initialized using Python.Runtime.PythonEngine.Initialize()."); | ||
| 36 | + } | ||
| 37 | + return delegateManager; | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + | ||
| 31 | 41 | public static string ProgramName { | |
| 32 | 42 | get { | |
| 33 | 43 | string result = Runtime.Py_GetProgramName(); | |
@@ -98,6 +108,12 @@ public static int RunSimpleString(string code) { | |||
| 98 | 108 | ||
| 99 | 109 | public static void Initialize() { | |
| 100 | 110 | if (!initialized) { | |
| 111 | + // Creating the delegateManager MUST happen before Runtime.Initialize | ||
| 112 | + // is called. If it happens afterwards, DelegateManager's CodeGenerator | ||
| 113 | + // throws an exception in its ctor. This exception is eaten somehow | ||
| 114 | + // during an initial "import clr", and the world ends shortly thereafter. | ||
| 115 | + // This is probably masking some bad mojo happening somewhere in Runtime.Initialize(). | ||
| 116 | + delegateManager = new DelegateManager(); | ||
| 101 | 117 | Runtime.Initialize(); | |
| 102 | 118 | initialized = true; | |
| 103 | 119 | Exceptions.Clear(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments