| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,6 +80,10 @@ internal static Type GetTypeByAlias(IntPtr op) { | |||
| 80 | 80 | // This always returns a new reference. Note that the System.Decimal | |
| 81 | 81 | // type has no Python equivalent and converts to a managed instance. | |
| 82 | 82 | //==================================================================== | |
| 83 | + internal static IntPtr ToPython<T>(T value) | ||
| 84 | + { | ||
| 85 | + return ToPython(value, typeof(T)); | ||
| 86 | + } | ||
| 83 | 87 | ||
| 84 | 88 | internal static IntPtr ToPython(Object value, Type type) { | |
| 85 | 89 | IntPtr result = IntPtr.Zero; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -447,6 +447,7 @@ static Interop() { | |||
| 447 | 447 | pmap["bf_getcharbuffer"] = p["IntObjArgFunc"]; | |
| 448 | 448 | ||
| 449 | 449 | pmap["__import__"] = p["TernaryFunc"]; | |
| 450 | + pmap["__instancecheck__"] = p["BinaryFunc"]; | ||
| 450 | 451 | } | |
| 451 | 452 | ||
| 452 | 453 | internal static Type GetPrototype(string name) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -260,10 +260,30 @@ public static void tp_dealloc(IntPtr tp) { | |||
| 260 | 260 | return; | |
| 261 | 261 | } | |
| 262 | 262 | ||
| 263 | - | ||
| 264 | - | ||
| 265 | - | ||
| 263 | + public static IntPtr __instancecheck__(IntPtr tp, IntPtr args) | ||
| 264 | + { | ||
| 265 | + ClassBase cb = GetManagedObject(tp) as ClassBase; | ||
| 266 | + | ||
| 267 | + if (cb == null) | ||
| 268 | + return Runtime.PyFalse; | ||
| 269 | + | ||
| 270 | + using (PyList argsObj = new PyList(args)) | ||
| 271 | + { | ||
| 272 | + if (argsObj.Length() != 1) | ||
| 273 | + return Exceptions.RaiseTypeError("Invalid parameter count"); | ||
| 274 | + | ||
| 275 | + PyObject arg = argsObj[0]; | ||
| 276 | + PyObject otherType = arg.GetPythonType(); | ||
| 277 | + | ||
| 278 | + if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType) | ||
| 279 | + return Runtime.PyFalse; | ||
| 280 | + | ||
| 281 | + ClassBase otherCb = GetManagedObject(otherType.Handle) as ClassBase; | ||
| 282 | + if (otherCb == null) | ||
| 283 | + return Runtime.PyFalse; | ||
| 284 | + | ||
| 285 | + return Converter.ToPython(cb.type.IsAssignableFrom(otherCb.type)); | ||
| 286 | + } | ||
| 287 | + } | ||
| 266 | 288 | } | |
| 267 | - | ||
| 268 | - | ||
| 269 | 289 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -300,7 +300,17 @@ internal static IntPtr CreateMetaType(Type impl) { | |||
| 300 | 300 | flags |= TypeFlags.Managed; | |
| 301 | 301 | flags |= TypeFlags.HeapType; | |
| 302 | 302 | flags |= TypeFlags.HaveGC; | |
| 303 | - Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags); | ||
| 303 | + Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags); | ||
| 304 | + | ||
| 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.WriteIntPtr(mdef, (2 * IntPtr.Size), (IntPtr)0x0001); // METH_VARARGS | ||
| 310 | + Marshal.WriteIntPtr(mdef, (3 * IntPtr.Size), IntPtr.Zero); | ||
| 311 | + Marshal.WriteIntPtr(mdef, (4 * IntPtr.Size), IntPtr.Zero); | ||
| 312 | + | ||
| 313 | + Marshal.WriteIntPtr(type, TypeOffset.tp_methods, mdef); | ||
| 304 | 314 | ||
| 305 | 315 | Runtime.PyType_Ready(type); | |
| 306 | 316 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments