| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,8 +45,8 @@ internal static void Initialize() { | |||
| 45 | 45 | IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__"); | |
| 46 | 46 | py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); | |
| 47 | 47 | #endif | |
| 48 | - hook = new MethodWrapper(typeof(ImportHook), "__import__"); | ||
| 49 | - Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr); | ||
| 48 | + hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc"); | ||
| 49 | + Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr); | ||
| 50 | 50 | Runtime.Decref(hook.ptr); | |
| 51 | 51 | ||
| 52 | 52 | root = new CLRModule(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -249,7 +249,7 @@ public static void FreeModuleDef(IntPtr ptr) { | |||
| 249 | 249 | // } PyModuleDef | |
| 250 | 250 | ||
| 251 | 251 | public static int name = 0; | |
| 252 | - } | ||
| 252 | + } | ||
| 253 | 253 | #endif // PYTHON3 | |
| 254 | 254 | ||
| 255 | 255 | /// <summary> | |
@@ -445,17 +445,19 @@ static Interop() { | |||
| 445 | 445 | pmap["bf_getwritebuffer"] = p["IntObjArgFunc"]; | |
| 446 | 446 | pmap["bf_getsegcount"] = p["ObjObjFunc"]; | |
| 447 | 447 | pmap["bf_getcharbuffer"] = p["IntObjArgFunc"]; | |
| 448 | - | ||
| 449 | - pmap["__import__"] = p["TernaryFunc"]; | ||
| 450 | - pmap["__instancecheck__"] = p["BinaryFunc"]; | ||
| 451 | 448 | } | |
| 452 | 449 | ||
| 453 | 450 | internal static Type GetPrototype(string name) { | |
| 454 | 451 | return pmap[name] as Type; | |
| 455 | 452 | } | |
| 456 | 453 | ||
| 457 | - internal static IntPtr GetThunk(MethodInfo method) { | ||
| 458 | - Type dt = Interop.GetPrototype(method.Name); | ||
| 454 | + internal static IntPtr GetThunk(MethodInfo method, string funcType = null) { | ||
| 455 | + Type dt; | ||
| 456 | + if (funcType != null) | ||
| 457 | + dt = typeof(Interop).GetNestedType(funcType) as Type; | ||
| 458 | + else | ||
| 459 | + dt = GetPrototype(method.Name); | ||
| 460 | + | ||
| 459 | 461 | if (dt != null) { | |
| 460 | 462 | IntPtr tmp = Marshal.AllocHGlobal(IntPtr.Size); | |
| 461 | 463 | Delegate d = Delegate.CreateDelegate(dt, method); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -260,20 +260,24 @@ public static void tp_dealloc(IntPtr tp) { | |||
| 260 | 260 | return; | |
| 261 | 261 | } | |
| 262 | 262 | ||
| 263 | - public static IntPtr __instancecheck__(IntPtr tp, IntPtr args) | ||
| 263 | + static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType) | ||
| 264 | 264 | { | |
| 265 | 265 | ClassBase cb = GetManagedObject(tp) as ClassBase; | |
| 266 | - | ||
| 266 | + | ||
| 267 | 267 | if (cb == null) | |
| 268 | 268 | return Runtime.PyFalse; | |
| 269 | - | ||
| 269 | + | ||
| 270 | 270 | using (PyList argsObj = new PyList(args)) | |
| 271 | 271 | { | |
| 272 | 272 | if (argsObj.Length() != 1) | |
| 273 | 273 | return Exceptions.RaiseTypeError("Invalid parameter count"); | |
| 274 | - | ||
| 274 | + | ||
| 275 | 275 | PyObject arg = argsObj[0]; | |
| 276 | - PyObject otherType = arg.GetPythonType(); | ||
| 276 | + PyObject otherType; | ||
| 277 | + if (checkType) | ||
| 278 | + otherType = arg; | ||
| 279 | + else | ||
| 280 | + otherType = arg.GetPythonType(); | ||
| 277 | 281 | ||
| 278 | 282 | if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType) | |
| 279 | 283 | return Runtime.PyFalse; | |
@@ -284,6 +288,16 @@ public static IntPtr __instancecheck__(IntPtr tp, IntPtr args) | |||
| 284 | 288 | ||
| 285 | 289 | return Converter.ToPython(cb.type.IsAssignableFrom(otherCb.type)); | |
| 286 | 290 | } | |
| 291 | + } | ||
| 292 | + | ||
| 293 | + public static IntPtr __instancecheck__(IntPtr tp, IntPtr args) | ||
| 294 | + { | ||
| 295 | + return DoInstanceCheck(tp, args, false); | ||
| 287 | 296 | } | |
| 297 | + | ||
| 298 | + public static IntPtr __subclasscheck__(IntPtr tp, IntPtr args) | ||
| 299 | + { | ||
| 300 | + return DoInstanceCheck(tp, args, true); | ||
| 301 | + } | ||
| 288 | 302 | } | |
| 289 | 303 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,20 +25,17 @@ internal class MethodWrapper { | |||
| 25 | 25 | public IntPtr mdef; | |
| 26 | 26 | public IntPtr ptr; | |
| 27 | 27 | ||
| 28 | - public MethodWrapper(Type type, string name) { | ||
| 28 | + public MethodWrapper(Type type, string name, string funcType = null) { | ||
| 29 | 29 | ||
| 30 | 30 | // Turn the managed method into a function pointer | |
| 31 | 31 | ||
| 32 | - IntPtr fp = Interop.GetThunk(type.GetMethod(name)); | ||
| 32 | + IntPtr fp = Interop.GetThunk(type.GetMethod(name), funcType); | ||
| 33 | 33 | ||
| 34 | 34 | // Allocate and initialize a PyMethodDef structure to represent | |
| 35 | 35 | // the managed method, then create a PyCFunction. | |
| 36 | 36 | ||
| 37 | 37 | mdef = Runtime.PyMem_Malloc(4 * IntPtr.Size); | |
| 38 | - Marshal.WriteIntPtr(mdef, Marshal.StringToHGlobalAnsi(name)); | ||
| 39 | - Marshal.WriteIntPtr(mdef, (1 * IntPtr.Size), fp); | ||
| 40 | - Marshal.WriteInt32(mdef, (2 * IntPtr.Size), 0x0003); // METH_VARARGS | METH_KEYWORDS | ||
| 41 | - Marshal.WriteIntPtr(mdef, (3 * IntPtr.Size), IntPtr.Zero); | ||
| 38 | + TypeManager.WriteMethodDef(mdef, name, fp, 0x0003); | ||
| 42 | 39 | ptr = Runtime.PyCFunction_NewEx(mdef, IntPtr.Zero, IntPtr.Zero); | |
| 43 | 40 | } | |
| 44 | 41 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -267,6 +267,28 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr | |||
| 267 | 267 | } | |
| 268 | 268 | } | |
| 269 | 269 | ||
| 270 | + internal static IntPtr WriteMethodDef(IntPtr mdef, IntPtr name, IntPtr func, int flags, IntPtr doc) | ||
| 271 | + { | ||
| 272 | + Marshal.WriteIntPtr(mdef, name); | ||
| 273 | + Marshal.WriteIntPtr(mdef, (1 * IntPtr.Size), func); | ||
| 274 | + Marshal.WriteInt32(mdef, (2 * IntPtr.Size), flags); | ||
| 275 | + Marshal.WriteIntPtr(mdef, (3 * IntPtr.Size), doc); | ||
| 276 | + return mdef + 4 * IntPtr.Size; | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + internal static IntPtr WriteMethodDef(IntPtr mdef, string name, IntPtr func, int flags = 0x0001, string doc = null) | ||
| 280 | + { | ||
| 281 | + IntPtr namePtr = Marshal.StringToHGlobalAnsi(name); | ||
| 282 | + IntPtr docPtr = doc != null ? Marshal.StringToHGlobalAnsi(doc) : IntPtr.Zero; | ||
| 283 | + | ||
| 284 | + return WriteMethodDef(mdef, namePtr, func, flags, docPtr); | ||
| 285 | + } | ||
| 286 | + | ||
| 287 | + internal static IntPtr WriteMethodDefSentinel(IntPtr mdef) | ||
| 288 | + { | ||
| 289 | + return WriteMethodDef(mdef, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero); | ||
| 290 | + } | ||
| 291 | + | ||
| 270 | 292 | internal static IntPtr CreateMetaType(Type impl) { | |
| 271 | 293 | ||
| 272 | 294 | // The managed metatype is functionally little different than the | |
@@ -302,20 +324,25 @@ internal static IntPtr CreateMetaType(Type impl) { | |||
| 302 | 324 | flags |= TypeFlags.HaveGC; | |
| 303 | 325 | Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags); | |
| 304 | 326 | ||
| 305 | - IntPtr fp = Interop.GetThunk(typeof(MetaType).GetMethod("__instancecheck__")); | ||
| 306 | - IntPtr mdef = Runtime.PyMem_Malloc(5 * IntPtr.Size); | ||
| 307 | - Marshal.WriteIntPtr(mdef, Marshal.StringToHGlobalAnsi("__instancecheck__")); | ||
| 308 | - Marshal.WriteIntPtr(mdef, (1 * IntPtr.Size), fp); | ||
| 309 | - Marshal.WriteInt32(mdef, (2 * IntPtr.Size), 0x0001); | ||
| 310 | - Marshal.WriteIntPtr(mdef, (3 * IntPtr.Size), IntPtr.Zero); | ||
| 311 | - | ||
| 312 | - // Write empty sentinel struct | ||
| 313 | - Marshal.WriteIntPtr(mdef, (4 * IntPtr.Size), IntPtr.Zero); | ||
| 314 | - Marshal.WriteIntPtr(mdef, (5 * IntPtr.Size), IntPtr.Zero); | ||
| 315 | - Marshal.WriteIntPtr(mdef, (6 * IntPtr.Size), IntPtr.Zero); | ||
| 316 | - Marshal.WriteIntPtr(mdef, (7 * IntPtr.Size), IntPtr.Zero); | ||
| 317 | - | ||
| 318 | - Marshal.WriteIntPtr(type, TypeOffset.tp_methods, mdef); | ||
| 327 | + // We need space for 3 PyMethodDef structs, each of them | ||
| 328 | + // 4 int-ptrs in size. | ||
| 329 | + IntPtr mdef = Runtime.PyMem_Malloc(3 * (4 * IntPtr.Size)); | ||
| 330 | + IntPtr mdefStart = mdef; | ||
| 331 | + mdef = WriteMethodDef( | ||
| 332 | + mdef, | ||
| 333 | + "__instancecheck__", | ||
| 334 | + Interop.GetThunk(typeof(MetaType).GetMethod("__instancecheck__"), "BinaryFunc") | ||
| 335 | + ); | ||
| 336 | + | ||
| 337 | + mdef = WriteMethodDef( | ||
| 338 | + mdef, | ||
| 339 | + "__subclasscheck__", | ||
| 340 | + Interop.GetThunk(typeof(MetaType).GetMethod("__subclasscheck__"), "BinaryFunc") | ||
| 341 | + ); | ||
| 342 | + | ||
| 343 | + mdef = WriteMethodDefSentinel(mdef); | ||
| 344 | + | ||
| 345 | + Marshal.WriteIntPtr(type, TypeOffset.tp_methods, mdefStart); | ||
| 319 | 346 | ||
| 320 | 347 | Runtime.PyType_Ready(type); | |
| 321 | 348 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments