PyMapping_HasKey() private méthode

private PyMapping_HasKey ( IntPtr pointer, IntPtr key ) : int
pointer IntPtr
key IntPtr
Résultat int
 /// <summary>
 /// TryGet Method
 /// </summary>
 /// <remarks>
 /// Returns the value of the variable, local variable first.
 /// If the variable does not exist, return null.
 /// </remarks>
 public bool TryGet(string name, out PyObject value)
 {
     Check();
     using (var pyKey = new PyString(name))
     {
         if (Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0)
         {
             IntPtr op = Runtime.PyObject_GetItem(variables, pyKey.obj);
             if (op == IntPtr.Zero)
             {
                 throw new PythonException();
             }
             if (op == Runtime.PyNone)
             {
                 Runtime.XDecref(op);
                 value = null;
                 return(true);
             }
             value = new PyObject(op);
             return(true);
         }
         else
         {
             value = null;
             return(false);
         }
     }
 }
Exemple #2
0
 /// <summary>
 /// Contains Method
 /// </summary>
 /// <remarks>
 /// Returns true if the variable exists in the scope.
 /// </remarks>
 public bool Contains(string name)
 {
     Check();
     using (var pyKey = new PyString(name))
     {
         return(Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0);
     }
 }
Exemple #3
0
        internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr py_dict)
        {
            // Utility to create a subtype of a managed type with the ability for the
            // a python subtype able to override the managed implementation
            string name = Runtime.GetManagedString(py_name);

            // the derived class can have class attributes __assembly__ and __module__ which
            // control the name of the assembly and module the new type is created in.
            object assembly     = null;
            object namespaceStr = null;

            var disposeList = new List <PyObject>();

            try
            {
                var assemblyKey = new PyObject(Converter.ToPython("__assembly__", typeof(string)));
                disposeList.Add(assemblyKey);
                if (0 != Runtime.PyMapping_HasKey(py_dict, assemblyKey.Handle))
                {
                    var pyAssembly = new PyObject(Runtime.PyDict_GetItem(py_dict, assemblyKey.Handle));
                    Runtime.XIncref(pyAssembly.Handle);
                    disposeList.Add(pyAssembly);
                    if (!Converter.ToManagedValue(pyAssembly.Handle, typeof(string), out assembly, false))
                    {
                        throw new InvalidCastException("Couldn't convert __assembly__ value to string");
                    }
                }

                var namespaceKey = new PyObject(Converter.ToPythonImplicit("__namespace__"));
                disposeList.Add(namespaceKey);
                if (0 != Runtime.PyMapping_HasKey(py_dict, namespaceKey.Handle))
                {
                    var pyNamespace = new PyObject(Runtime.PyDict_GetItem(py_dict, namespaceKey.Handle));
                    Runtime.XIncref(pyNamespace.Handle);
                    disposeList.Add(pyNamespace);
                    if (!Converter.ToManagedValue(pyNamespace.Handle, typeof(string), out namespaceStr, false))
                    {
                        throw new InvalidCastException("Couldn't convert __namespace__ value to string");
                    }
                }
            }
            finally
            {
                foreach (PyObject o in disposeList)
                {
                    o.Dispose();
                }
            }

            // create the new managed type subclassing the base managed type
            var baseClass = ManagedType.GetManagedObject(py_base_type) as ClassBase;

            if (null == baseClass)
            {
                return(Exceptions.RaiseTypeError("invalid base class, expected CLR class type"));
            }

            try
            {
                Type subType = ClassDerivedObject.CreateDerivedType(name,
                                                                    baseClass.type,
                                                                    py_dict,
                                                                    (string)namespaceStr,
                                                                    (string)assembly);

                // create the new ManagedType and python type
                ClassBase subClass = ClassManager.GetClass(subType);
                IntPtr    py_type  = GetTypeHandle(subClass, subType);

                // by default the class dict will have all the C# methods in it, but as this is a
                // derived class we want the python overrides in there instead if they exist.
                IntPtr cls_dict = Marshal.ReadIntPtr(py_type, TypeOffset.tp_dict);
                Runtime.PyDict_Update(cls_dict, py_dict);

                return(py_type);
            }
            catch (Exception e)
            {
                return(Exceptions.RaiseTypeError(e.Message));
            }
        }
Exemple #4
0
 /// <summary>
 /// HasKey Method
 /// </summary>
 ///
 /// <remarks>
 /// Returns true if the object key appears in the dictionary.
 /// </remarks>
 public bool HasKey(PyObject key)
 {
     return(Runtime.PyMapping_HasKey(obj, key.obj) != 0);
 }