| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
7 files changed
| 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 | |
|---|---|---|---|
@@ -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,16 +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 | 448 | } | |
| 451 | 449 | ||
| 452 | 450 | internal static Type GetPrototype(string name) { | |
| 453 | 451 | return pmap[name] as Type; | |
| 454 | 452 | } | |
| 455 | 453 | ||
| 456 | - internal static IntPtr GetThunk(MethodInfo method) { | ||
| 457 | - 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 | + | ||
| 458 | 461 | if (dt != null) { | |
| 459 | 462 | IntPtr tmp = Marshal.AllocHGlobal(IntPtr.Size); | |
| 460 | 463 | Delegate d = Delegate.CreateDelegate(dt, method); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -260,10 +260,44 @@ public static void tp_dealloc(IntPtr tp) { | |||
| 260 | 260 | return; | |
| 261 | 261 | } | |
| 262 | 262 | ||
| 263 | + static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType) | ||
| 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; | ||
| 277 | + if (checkType) | ||
| 278 | + otherType = arg; | ||
| 279 | + else | ||
| 280 | + otherType = arg.GetPythonType(); | ||
| 281 | + | ||
| 282 | + if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType) | ||
| 283 | + return Runtime.PyFalse; | ||
| 284 | + | ||
| 285 | + ClassBase otherCb = GetManagedObject(otherType.Handle) as ClassBase; | ||
| 286 | + if (otherCb == null) | ||
| 287 | + return Runtime.PyFalse; | ||
| 288 | + | ||
| 289 | + return Converter.ToPython(cb.type.IsAssignableFrom(otherCb.type)); | ||
| 290 | + } | ||
| 291 | + } | ||
| 263 | 292 | ||
| 264 | - | ||
| 265 | - | ||
| 293 | + public static IntPtr __instancecheck__(IntPtr tp, IntPtr args) | ||
| 294 | + { | ||
| 295 | + return DoInstanceCheck(tp, args, false); | ||
| 296 | + } | ||
| 297 | + | ||
| 298 | + public static IntPtr __subclasscheck__(IntPtr tp, IntPtr args) | ||
| 299 | + { | ||
| 300 | + return DoInstanceCheck(tp, args, true); | ||
| 301 | + } | ||
| 266 | 302 | } | |
| 267 | - | ||
| 268 | - | ||
| 269 | 303 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,41 +25,22 @@ 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)); | ||
| 33 | - | ||
| 34 | - // XXX - here we create a Python string object, then take the | ||
| 35 | - // char * of the internal string to pass to our methoddef | ||
| 36 | - // structure. Its a hack, and the name is leaked! | ||
| 37 | - #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35) | ||
| 38 | - IntPtr ps = Runtime.PyBytes_FromString(name); | ||
| 39 | - IntPtr sp = Runtime.PyBytes_AS_STRING(ps); | ||
| 40 | - #else | ||
| 41 | - IntPtr ps = Runtime.PyString_FromString(name); | ||
| 42 | - IntPtr sp = Runtime.PyString_AS_STRING(ps); | ||
| 43 | - #endif | ||
| 32 | + IntPtr fp = Interop.GetThunk(type.GetMethod(name), funcType); | ||
| 44 | 33 | ||
| 45 | 34 | // Allocate and initialize a PyMethodDef structure to represent | |
| 46 | 35 | // the managed method, then create a PyCFunction. | |
| 47 | 36 | ||
| 48 | 37 | mdef = Runtime.PyMem_Malloc(4 * IntPtr.Size); | |
| 49 | - Marshal.WriteIntPtr(mdef, sp); | ||
| 50 | - Marshal.WriteIntPtr(mdef, (1 * IntPtr.Size), fp); | ||
| 51 | - Marshal.WriteIntPtr(mdef, (2 * IntPtr.Size), (IntPtr)0x0003); // METH_VARARGS | METH_KEYWORDS | ||
| 52 | - Marshal.WriteIntPtr(mdef, (3 * IntPtr.Size), IntPtr.Zero); | ||
| 38 | + TypeManager.WriteMethodDef(mdef, name, fp, 0x0003); | ||
| 53 | 39 | ptr = Runtime.PyCFunction_NewEx(mdef, IntPtr.Zero, IntPtr.Zero); | |
| 54 | 40 | } | |
| 55 | 41 | ||
| 56 | 42 | public IntPtr Call(IntPtr args, IntPtr kw) { | |
| 57 | 43 | return Runtime.PyCFunction_Call(ptr, args, kw); | |
| 58 | 44 | } | |
| 59 | - | ||
| 60 | - | ||
| 61 | 45 | } | |
| 62 | - | ||
| 63 | - | ||
| 64 | - } | ||
| 65 | - | ||
| 46 | + } | ||
| 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 | |
@@ -300,7 +322,27 @@ internal static IntPtr CreateMetaType(Type impl) { | |||
| 300 | 322 | flags |= TypeFlags.Managed; | |
| 301 | 323 | flags |= TypeFlags.HeapType; | |
| 302 | 324 | flags |= TypeFlags.HaveGC; | |
| 303 | - Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags); | ||
| 325 | + Marshal.WriteIntPtr(type, TypeOffset.tp_flags, (IntPtr)flags); | ||
| 326 | + | ||
| 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); | ||
| 304 | 346 | ||
| 305 | 347 | Runtime.PyType_Ready(type); | |
| 306 | 348 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -348,12 +348,20 @@ def testExceptionIsInstanceOfSystemObject(self): | |||
| 348 | 348 | # without causing a crash in the CPython interpreter). This test is | |
| 349 | 349 | # here mainly to remind me to update the caveat in the documentation | |
| 350 | 350 | # one day when when exceptions can be new-style classes. | |
| 351 | + | ||
| 352 | + # This behaviour is now over-shadowed by the implementation of | ||
| 353 | + # __instancecheck__ (i.e., overloading isinstance), so for all Python | ||
| 354 | + # version >= 2.6 we expect isinstance(<managed exception>, Object) to | ||
| 355 | + # be true, even though it does not really subclass Object. | ||
| 351 | 356 | from System import OverflowException | |
| 352 | 357 | from System import Object | |
| 353 | 358 | ||
| 354 | 359 | o = OverflowException('error') | |
| 355 | - self.assertFalse(isinstance(o, Object)) | ||
| 356 | - | ||
| 360 | + | ||
| 361 | + if sys.version_info >= (2, 6): | ||
| 362 | + self.assertTrue(isinstance(o, Object)) | ||
| 363 | + else: | ||
| 364 | + self.assertFalse(isinstance(o, Object)) | ||
| 357 | 365 | ||
| 358 | 366 | ||
| 359 | 367 | def test_suite(): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments