Ejemplo n.º 1
0
 public ConstructorBinding(Type type, PyType typeToCreate, ConstructorBinder ctorBinder)
 {
     this.type         = type;
     this.typeToCreate = typeToCreate;
     this.ctorBinder   = ctorBinder;
     repr = IntPtr.Zero;
 }
Ejemplo n.º 2
0
 public PythonException(PyType type, PyObject?value, PyObject?traceback,
                        string message, Exception?innerException)
     : base(message, innerException)
 {
     Type      = type ?? throw new ArgumentNullException(nameof(type));
     Value     = value;
     Traceback = traceback;
 }
Ejemplo n.º 3
0
 public BoundContructor(Type type, PyType typeToCreate, ConstructorBinder ctorBinder, ConstructorInfo ci)
 {
     this.type         = type;
     this.typeToCreate = typeToCreate;
     this.ctorBinder   = ctorBinder;
     ctorInfo          = ci;
     repr = IntPtr.Zero;
 }
Ejemplo n.º 4
0
 internal static BorrowedReference GetUnmanagedBaseType(BorrowedReference managedType)
 {
     Debug.Assert(managedType != null && IsManagedType(managedType));
     do
     {
         managedType = PyType.GetBase(managedType);
     } while (IsManagedType(managedType));
     return(managedType);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Given a Python object, return the associated managed object or null.
 /// </summary>
 internal static ManagedType?GetManagedObject(BorrowedReference ob)
 {
     if (ob != null)
     {
         BorrowedReference tp = Runtime.PyObject_TYPE(ob);
         var flags            = PyType.GetFlags(tp);
         if ((flags & TypeFlags.HasClrInstance) != 0)
         {
             var gc = TryGetGCHandle(ob, tp);
             return((ManagedType?)gc?.Target);
         }
     }
     return(null);
 }
Ejemplo n.º 6
0
        private static Exception FromPyErr(BorrowedReference typeRef, BorrowedReference valRef, BorrowedReference tbRef,
                                           out ExceptionDispatchInfo?exceptionDispatchInfo)
        {
            if (valRef == null)
            {
                throw new ArgumentNullException(nameof(valRef));
            }

            var type      = PyType.FromReference(typeRef);
            var value     = new PyObject(valRef);
            var traceback = PyObject.FromNullableReference(tbRef);

            exceptionDispatchInfo = TryGetDispatchInfo(valRef);
            if (exceptionDispatchInfo != null)
            {
                return(exceptionDispatchInfo.SourceException);
            }

            if (ManagedType.GetManagedObject(valRef) is CLRObject {
                inst : Exception e
            })
Ejemplo n.º 7
0
        static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType)
        {
            IPyObjectDecoder decoder;
            var sourceTypeRef = new BorrowedReference(sourceType);

            Debug.Assert(PyType.IsType(sourceTypeRef));
            using (var pyType = new PyType(sourceTypeRef, prevalidated: true))
            {
                lock (decoders)
                {
                    decoder = decoders.GetDecoder(pyType, targetType);
                    if (decoder == null)
                    {
                        return(null);
                    }
                }
            }

            var decode = genericDecode.MakeGenericMethod(targetType);

            bool TryDecode(IntPtr pyHandle, out object result)
            {
                var  pyObj   = new PyObject(Runtime.SelfIncRef(pyHandle));
                var  @params = new object[] { pyObj, null };
                bool success = (bool)decode.Invoke(decoder, @params);

                if (!success)
                {
                    pyObj.Dispose();
                }

                result = @params[1];
                return(success);
            }

            return(TryDecode);
        }
Ejemplo n.º 8
0
 internal PyType(PyType o)
     : base(o is not null ? o.Reference : throw new ArgumentNullException(nameof(o)))
Ejemplo n.º 9
0
 /// <summary>
 /// Metatype initialization. This bootstraps the CLR metatype to life.
 /// </summary>
 public static PyType Initialize()
 {
     PyCLRMetaType = TypeManager.CreateMetaType(typeof(MetaType), out _metaSlotsHodler);
     return(PyCLRMetaType);
 }
Ejemplo n.º 10
0
 public PythonException(PyType type, PyObject?value, PyObject?traceback)
     : this(type, value, traceback, innerException : null)
 {
 }
Ejemplo n.º 11
0
 public PythonException(PyType type, PyObject?value, PyObject?traceback,
                        Exception?innerException)
     : this(type, value, traceback, GetMessage(value, type), innerException)
 {
 }
Ejemplo n.º 12
0
        private static void InitClassBase(Type type, ClassBase impl, PyType pyType)
        {
            // First, we introspect the managed type and build some class
            // information, including generating the member descriptors
            // that we'll be putting in the Python class __dict__.

            ClassInfo info = GetClassInfo(type);

            impl.indexer     = info.indexer;
            impl.richcompare = new Dictionary <int, MethodObject>();

            // Now we force initialize the Python type object to reflect the given
            // managed type, filling the Python type slots with thunks that
            // point to the managed methods providing the implementation.


            TypeManager.GetOrInitializeClass(impl, type);

            // Finally, initialize the class __dict__ and return the object.
            using var dict = Runtime.PyObject_GenericGetDict(pyType.Reference);


            if (impl.dotNetMembers == null)
            {
                impl.dotNetMembers = new List <string>();
            }
            IDictionaryEnumerator iter = info.members.GetEnumerator();

            while (iter.MoveNext())
            {
                var item = (ManagedType)iter.Value;
                var name = (string)iter.Key;
                impl.dotNetMembers.Add(name);
                Runtime.PyDict_SetItemString(dict, name, item.ObjectReference);
                // Decref the item now that it's been used.
                item.DecrRefCount();
                if (ClassBase.CilToPyOpMap.TryGetValue(name, out var pyOp))
                {
                    impl.richcompare.Add(pyOp, (MethodObject)item);
                }
            }

            // If class has constructors, generate an __doc__ attribute.
            NewReference doc    = default;
            Type         marker = typeof(DocStringAttribute);
            var          attrs  = (Attribute[])type.GetCustomAttributes(marker, false);

            if (attrs.Length != 0)
            {
                var    attr   = (DocStringAttribute)attrs[0];
                string docStr = attr.DocString;
                doc = NewReference.DangerousFromPointer(Runtime.PyString_FromString(docStr));
                Runtime.PyDict_SetItem(dict, PyIdentifier.__doc__, doc);
            }

            var co = impl as ClassObject;

            // If this is a ClassObject AND it has constructors, generate a __doc__ attribute.
            // required that the ClassObject.ctors be changed to internal
            if (co != null)
            {
                if (co.NumCtors > 0)
                {
                    // Implement Overloads on the class object
                    if (!CLRModule._SuppressOverloads)
                    {
                        var ctors = new ConstructorBinding(type, pyType, co.binder);
                        // ExtensionType types are untracked, so don't Incref() them.
                        // TODO: deprecate __overloads__ soon...
                        Runtime.PyDict_SetItem(dict, PyIdentifier.__overloads__, ctors.ObjectReference);
                        Runtime.PyDict_SetItem(dict, PyIdentifier.Overloads, ctors.ObjectReference);
                        ctors.DecrRefCount();
                    }

                    // don't generate the docstring if one was already set from a DocStringAttribute.
                    if (!CLRModule._SuppressDocs && doc.IsNull())
                    {
                        doc = co.GetDocString();
                        Runtime.PyDict_SetItem(dict, PyIdentifier.__doc__, doc);
                    }
                }
            }
            doc.Dispose();

            // The type has been modified after PyType_Ready has been called
            // Refresh the type
            Runtime.PyType_Modified(pyType.Reference);
        }
Ejemplo n.º 13
0
        internal static bool IsManagedType(BorrowedReference type)
        {
            var flags = PyType.GetFlags(type);

            return((flags & TypeFlags.HasClrInstance) != 0);
        }