Exemplo n.º 1
0
        public static NameType TryGetName(ReflectedType dt, FieldInfo fi, out string name)
        {
            Debug.Assert(dt.IsSubclassOf(DynamicType.GetDeclaringType(fi)));

            NameType nt = NameType.PythonField;
            name = null;

            // hide MinValue & MaxValue on int, Empty on string, Epsilon, Min/Max, etc.. on double
            if (fi.DeclaringType == typeof(string) ||
                fi.DeclaringType == typeof(int) ||
                fi.DeclaringType == typeof(double) ||
                fi.IsDefined(typeof(PythonHiddenFieldAttribute), false)) nt = NameType.Field;

            string namePrefix = "";
            if (fi.IsPrivate || (fi.IsAssembly && !fi.IsFamilyOrAssembly)) {
                if (!Options.PrivateBinding) {
                    return NameType.None;
                } else {
                    // mangle protectes to private
                    namePrefix = "_" + dt.Name + "__";
                    nt = NameType.Field;
                }
            }

            name = namePrefix + fi.Name;
            return nt;
        }
Exemplo n.º 2
0
 public NameEnvironment(PythonModule globals, object locals)
 {
     this.globals = globals;
     if (locals == null) locals = globals.__dict__;
     this.locals = locals;
     this.builtin = TypeCache.Builtin;
 }
Exemplo n.º 3
0
 internal ModuleScope(PythonModule mod, IAttributesDictionary globals, object locals)
 {
     __module__ = mod;
     f_globals = globals;
     f_locals = locals;
     __builtin__ = TypeCache.Builtin;
 }
Exemplo n.º 4
0
        public static NameType TryGetName(ReflectedType dt, EventInfo ei, MethodInfo eventMethod, out string name)
        {
            Debug.Assert(dt.IsSubclassOf(DynamicType.GetDeclaringType(ei)));

            name = ei.Name;
            NameType res = dt.IsClsType ? NameType.PythonEvent : NameType.Event;

            return GetNameFromMethod(dt, eventMethod, res, ref name);
        }
Exemplo n.º 5
0
        public static NameType TryGetName(ReflectedType dt, MethodInfo mi, out string name)
        {
            Debug.Assert(dt.IsSubclassOf(DynamicType.GetDeclaringType(mi)));

            NameType res = dt.IsClsType ? NameType.PythonMethod : NameType.Method;
            name = mi.Name;

            return GetNameFromMethod(dt, mi, res, ref name);
        }
Exemplo n.º 6
0
 internal bool TryConvertTo(object value, ReflectedType to, out object result)
 {
     if (conversions != null && conversions.Count > 0) {
         for (int i = 0; i < conversions.Count; i++) {
             if (conversions[i].ReturnType == to.type) {
                 try {
                     result = conversions[i].Invoke(null, new object[] { value });
                     return true;
                 } catch (TargetInvocationException tie) {
                     throw ExceptionConverter.UpdateForRethrow(tie);
                 }
             }
         }
     }
     result = null;
     return false;
 }
Exemplo n.º 7
0
 private string GetName(Type type)
 {
     if (type == typeof(object)) { return "object"; } //???
     PythonTypeAttribute attr = (PythonTypeAttribute)
         Attribute.GetCustomAttribute(type, typeof(PythonTypeAttribute));
     if (attr != null) {
         if (attr.impersonateType != null) {
             this.effectivePythonType = (ReflectedType)Ops.GetDynamicTypeFromType(attr.impersonateType);
         }
         return attr.name;
     } else {
         string name;
         NameConverter.TryGetName(this, type, out name);
         return name;
     }
 }
Exemplo n.º 8
0
        internal static PythonModule MakePythonModule(SystemState state, string name, ReflectedType type)
        {
            type.Initialize();
            FieldIdDict dict = new FieldIdDict();

            foreach (string attrName in type.GetAttrNames(DefaultContext.Default)) {

                dict[SymbolTable.StringToId(attrName)] = type.GetAttr(DefaultContext.Default, null, SymbolTable.StringToId(attrName));
            }
            PythonModule ret = new PythonModule(name, dict, state);
            state.modules[name] = ret;
            return ret;
        }
Exemplo n.º 9
0
        public static ReflectedType MakeDynamicType(ReflectedType type)
        {
            if (BoolType == null) {
                OpsReflectedType ret = new OpsReflectedType("bool", typeof(bool), typeof(BoolOps), null, new CallTarget1(FastNew));
                if (Interlocked.CompareExchange<ReflectedType>(ref BoolType, ret, null) == null)
                    return ret;
            }

            return BoolType;
        }
Exemplo n.º 10
0
        public static NameType TryGetName(ReflectedType outerType, Type t, out string name)
        {
            name = t.Name;

            int backtickIndex;
            if ((backtickIndex = name.IndexOf(ReflectionUtil.GenericArityDelimiter)) != -1) {
                name = name.Substring(0, backtickIndex);
                if (!t.ContainsGenericParameters) {
                    Type[] typeOf = t.GetGenericArguments();
                    StringBuilder sb = new StringBuilder(name);
                    sb.Append('[');
                    bool first = true;
                    foreach (Type tof in typeOf) {
                        if (first) first = false; else sb.Append(", ");
                        sb.Append(Ops.GetDynamicTypeFromType(tof).Name);
                    }
                    sb.Append(']');
                    name = sb.ToString();
                }
            }

            string namePrefix = "";
            if ((t.IsNested && !t.IsNestedPublic) || (t.IsNestedAssembly && !t.IsNestedFamORAssem)) {
                if (!Options.PrivateBinding) {
                    return NameType.None;
                } else {
                    namePrefix = "_" + Ops.GetDynamicTypeFromType(t.DeclaringType).Name + "__";
                }
            }

            NameType res = NameType.Type;
            if (outerType.IsPythonType) {
                object[] attribute = t.GetCustomAttributes(typeof(PythonTypeAttribute), false);

                if (attribute.Length > 0) {
                    PythonTypeAttribute attr = attribute[0] as PythonTypeAttribute;
                    if (attr.name != null && attr.name.Length > 0) {
                        res = NameType.PythonType;
                        name = attr.name;
                    }
                }
            } else {
                res = NameType.PythonType;
            }

            name = namePrefix + name;
            return res;
        }
Exemplo n.º 11
0
        public static NameType TryGetName(ReflectedType dt, PropertyInfo pi, MethodInfo prop, out string name)
        {
            Debug.Assert(dt.IsSubclassOf(DynamicType.GetDeclaringType(pi)));

            name = pi.Name;
            NameType res = dt.IsClsType ? NameType.PythonProperty : NameType.Property;

            return GetNameFromMethod(dt, prop, res, ref name);
        }
Exemplo n.º 12
0
        private static NameType GetNameFromMethod(ReflectedType dt, MethodInfo mi, NameType res, ref string name)
        {
            string namePrefix = null;

            if (mi.IsPrivate || (mi.IsAssembly && !mi.IsFamilyOrAssembly)) {
                // allow explicitly implemented interface
                if (!(mi.IsPrivate && mi.IsFinal && mi.IsHideBySig && mi.IsVirtual)) {
                    if (!Options.PrivateBinding) {
                        return NameType.None;
                    } else {
                        // mangle protectes to private
                        namePrefix = "_" + dt.Name + "__";
                    }
                } else {
                    // explicitly implemented interface

                    // drop the namespace, leave the interface name, and replace
                    // the dot with an underscore.  Eg System.IConvertible.ToBoolean
                    // becomes IConvertible_ToBoolean
                    int lastDot = name.LastIndexOf(Type.Delimiter);
                    if (lastDot != -1) {
                        name = name.Substring(lastDot + 1);
                    }
                }
            }

            object[] attribute = mi.GetCustomAttributes(typeof(PythonNameAttribute), false);

            if (namePrefix != null) name = namePrefix + name;
            if (attribute.Length > 0) {
                PythonNameAttribute attr = attribute[0] as PythonNameAttribute;
                if (attr.name != null && attr.name.Length > 0) {
                    if (attr is PythonClassMethodAttribute) res |= NameType.ClassMember;

                    res |= NameType.Python;
                    name = attr.name;
                }
            }

            return res;
        }