本文整理汇总了C#中IronPython.Runtime.Types.PythonTypeSlot类的典型用法代码示例。如果您正苦于以下问题:C# PythonTypeSlot类的具体用法?C# PythonTypeSlot怎么用?C# PythonTypeSlot使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PythonTypeSlot类属于IronPython.Runtime.Types命名空间,在下文中一共展示了PythonTypeSlot类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeTryGetTypeMember
internal static MethodCallExpression MakeTryGetTypeMember(PythonContext/*!*/ PythonContext, PythonTypeSlot dts, ParameterExpression tmp, Expression instance, Expression pythonType) {
return Ast.Call(
PythonTypeInfo._PythonOps.SlotTryGetBoundValue,
AstUtils.Constant(PythonContext.SharedContext),
AstUtils.Convert(Utils.WeakConstant(dts), typeof(PythonTypeSlot)),
AstUtils.Convert(instance, typeof(object)),
AstUtils.Convert(
pythonType,
typeof(PythonType)
),
tmp
);
}
示例2: SlotInitAdapter
public SlotInitAdapter(PythonTypeSlot/*!*/ slot, ArgumentValues/*!*/ ai, PythonContext/*!*/ state, Expression/*!*/ codeContext)
: base(ai, state, codeContext) {
_slot = slot;
}
示例3: MakeOneTarget
private static bool MakeOneTarget(PythonContext/*!*/ state, SlotOrFunction/*!*/ target, PythonTypeSlot slotTarget, ConditionalBuilder/*!*/ bodyBuilder, bool reverse, DynamicMetaObject/*!*/[]/*!*/ types) {
if (target == SlotOrFunction.Empty && slotTarget == null) return true;
if (slotTarget != null) {
MakeSlotCall(state, types, bodyBuilder, slotTarget, reverse);
return true;
} else if (target.MaybeNotImplemented) {
Debug.Assert(target.ReturnType == typeof(object));
ParameterExpression tmp = Ast.Variable(typeof(object), "slot");
bodyBuilder.AddVariable(tmp);
bodyBuilder.AddCondition(
Ast.NotEqual(
Ast.Assign(
tmp,
target.Target.Expression
),
Ast.Property(null, typeof(PythonOps).GetProperty("NotImplemented"))
),
tmp
);
return true;
} else {
bodyBuilder.FinishCondition(target.Target.Expression, typeof(object));
return false;
}
}
示例4: GetOperatorMethods
private static void GetOperatorMethods(DynamicMetaObject/*!*/[]/*!*/ types, PythonOperationKind oper, PythonContext state, out SlotOrFunction fbinder, out SlotOrFunction rbinder, out PythonTypeSlot fSlot, out PythonTypeSlot rSlot) {
oper = NormalizeOperator(oper);
oper &= ~PythonOperationKind.InPlace;
string op, rop;
if (!IsReverseOperator(oper)) {
op = Symbols.OperatorToSymbol(oper);
rop = Symbols.OperatorToReversedSymbol(oper);
} else {
// coming back after coercion, just try reverse operator.
rop = Symbols.OperatorToSymbol(oper);
op = Symbols.OperatorToReversedSymbol(oper);
}
fSlot = null;
rSlot = null;
PythonType fParent, rParent;
if (oper == PythonOperationKind.Multiply &&
IsSequence(types[0]) &&
!PythonOps.IsNonExtensibleNumericType(types[1].GetLimitType())) {
// class M:
// def __rmul__(self, other):
// print "CALLED"
// return 1
//
// print [1,2] * M()
//
// in CPython this results in a successful call to __rmul__ on the type ignoring the forward
// multiplication. But calling the __mul__ method directly does NOT return NotImplemented like
// one might expect. Therefore we explicitly convert the MetaObject argument into an Index
// for binding purposes. That allows this to work at multiplication time but not with
// a direct call to __mul__.
DynamicMetaObject[] newTypes = new DynamicMetaObject[2];
newTypes[0] = types[0];
newTypes[1] = new DynamicMetaObject(
Ast.New(
typeof(Index).GetConstructor(new Type[] { typeof(object) }),
AstUtils.Convert(types[1].Expression, typeof(object))
),
BindingRestrictions.Empty
);
types = newTypes;
}
if (!SlotOrFunction.TryGetBinder(state, types, op, null, out fbinder, out fParent)) {
foreach (PythonType pt in MetaPythonObject.GetPythonType(types[0]).ResolutionOrder) {
if (pt.TryLookupSlot(state.SharedContext, op, out fSlot)) {
fParent = pt;
break;
}
}
}
if (!SlotOrFunction.TryGetBinder(state, types, null, rop, out rbinder, out rParent)) {
foreach (PythonType pt in MetaPythonObject.GetPythonType(types[1]).ResolutionOrder) {
if (pt.TryLookupSlot(state.SharedContext, rop, out rSlot)) {
rParent = pt;
break;
}
}
}
if (fParent != null && (rbinder.Success || rSlot != null) && rParent != fParent && rParent.IsSubclassOf(fParent)) {
// Python says if x + subx and subx defines __r*__ we should call r*.
fbinder = SlotOrFunction.Empty;
fSlot = null;
}
if (!fbinder.Success && !rbinder.Success && fSlot == null && rSlot == null) {
if (op == "__truediv__" || op == "__rtruediv__") {
// true div on a type which doesn't support it, go ahead and try normal divide
PythonOperationKind newOp = op == "__truediv__" ? PythonOperationKind.Divide : PythonOperationKind.ReverseDivide;
GetOperatorMethods(types, newOp, state, out fbinder, out rbinder, out fSlot, out rSlot);
}
}
}
示例5: MakeCallable
/// <summary>
/// Creates a new CallableObject. If BuiltinFunction is available we'll create a BuiltinCallable otherwise
/// we create a SlotCallable.
/// </summary>
public static Callable MakeCallable(PythonContext/*!*/ binder, PythonIndexType op, BuiltinFunction itemFunc, PythonTypeSlot itemSlot) {
if (itemFunc != null) {
// we'll call a builtin function to produce the rule
return new BuiltinCallable(binder, op, itemFunc);
} else if (itemSlot != null) {
// we'll call a PythonTypeSlot to produce the rule
return new SlotCallable(binder, op, itemSlot);
}
return null;
}
示例6: TryGetSlot
public bool TryGetSlot(string/*!*/ name, out PythonTypeSlot slot) {
Debug.Assert(name != null);
KeyValuePair<PythonTypeSlot, MemberGroup> kvp;
if (Members.TryGetValue(name, out kvp)) {
slot = kvp.Key;
return true;
}
slot = null;
return false;
}
示例7: TryGetCachedSlot
/// <summary>
/// Looks up a cached type slot for the specified member and type. This may return true and return a null slot - that indicates
/// that a cached result for a member which doesn't exist has been stored. Otherwise it returns true if a slot is found or
/// false if it is not.
/// </summary>
public bool TryGetCachedSlot(Type/*!*/ type, string/*!*/ name, out PythonTypeSlot slot) {
Debug.Assert(type != null); Debug.Assert(name != null);
if (_cachedInfos != null) {
lock (_cachedInfos) {
SlotCacheInfo slots;
if (_cachedInfos.TryGetValue(type, out slots) &&
(slots.TryGetSlot(name, out slot) || slots.ResolvedAll)) {
return true;
}
}
}
slot = null;
return false;
}
示例8: TryResolveSlot
/// <summary>
/// Performs .NET member resolution. This looks the type and any base types
/// for members. It also searches for extension members in the type and any base types.
/// </summary>
public bool TryResolveSlot(CodeContext/*!*/ context, PythonType/*!*/ type, PythonType/*!*/ owner, SymbolId name, out PythonTypeSlot slot) {
Debug.Assert(type.IsSystemType);
string strName = SymbolTable.IdToString(name);
Type curType = type.UnderlyingSystemType;
if (!_resolvedMembers.TryGetCachedSlot(curType, strName, out slot)) {
MemberGroup mg = TypeInfo.GetMemberAll(
this,
OldGetMemberAction.Make(this, strName),
curType,
strName);
slot = PythonTypeOps.GetSlot(mg, SymbolTable.IdToString(name), PrivateBinding);
_resolvedMembers.CacheSlot(curType, strName, slot, mg);
}
if (slot != null && (slot.IsAlwaysVisible || PythonOps.IsClsVisible(context))) {
return true;
}
slot = null;
return false;
}
示例9: MakeSlotAccess
private static void MakeSlotAccess(GetBindingInfo/*!*/ info, PythonTypeSlot dts) {
ReflectedSlotProperty rsp = dts as ReflectedSlotProperty;
if (rsp != null) {
// we need to fall back to __getattr__ if the value is not defined, so call it and check the result.
info.Body.AddCondition(
Ast.NotEqual(
Ast.Assign(
info.Result,
Ast.ArrayAccess(
Ast.Call(
Ast.Convert(info.Self, typeof(IObjectWithSlots)),
typeof(IObjectWithSlots).GetMethod("GetSlots")
),
Ast.Constant(rsp.Index)
)
),
Ast.Field(null, typeof(Uninitialized).GetField("Instance"))
),
info.Result
);
return;
}
PythonTypeUserDescriptorSlot slot = dts as PythonTypeUserDescriptorSlot;
if (slot != null && !(slot.Value is PythonTypeSlot)) {
PythonType slottype = DynamicHelpers.GetPythonType(slot.Value);
if (slottype.IsSystemType) {
// this is a user slot that's known not to be a descriptor
// so we can just burn the value in. For it to change the
// slot will need to be replaced reving the type version.
info.Body.FinishCondition(
AstUtils.Convert(AstUtils.WeakConstant(slot.Value), typeof(object))
);
return;
}
}
// users can subclass PythonProperty so check the type explicitly
// and only in-line the ones we fully understand.
if (dts.GetType() == typeof(PythonProperty)) {
// properties are mutable so we generate code to get the value rather
// than burning it into the rule.
Expression getter = Ast.Property(
Ast.Convert(AstUtils.WeakConstant(dts), typeof(PythonProperty)),
"fget"
);
ParameterExpression tmpGetter = Ast.Variable(typeof(object), "tmpGet");
info.Body.AddVariable(tmpGetter);
info.Body.FinishCondition(
Ast.Block(
Ast.Assign(tmpGetter, getter),
Ast.Condition(
Ast.NotEqual(
tmpGetter,
Ast.Constant(null)
),
Ast.Dynamic(
new PythonInvokeBinder(
BinderState.GetBinderState(info.Action),
new CallSignature(1)
),
typeof(object),
Ast.Constant(BinderState.GetBinderState(info.Action).Context),
tmpGetter,
info.Self
),
Ast.Throw(Ast.Call(typeof(PythonOps).GetMethod("UnreadableProperty")), typeof(object))
)
)
);
return;
}
Expression tryGet = Ast.Call(
TypeInfo._PythonOps.SlotTryGetBoundValue,
Ast.Constant(BinderState.GetBinderState(info.Action).Context),
Ast.Convert(AstUtils.WeakConstant(dts), typeof(PythonTypeSlot)),
AstUtils.Convert(info.Self, typeof(object)),
Ast.Property(
Ast.Convert(
info.Self,
typeof(IPythonObject)),
TypeInfo._IPythonObject.PythonType
),
info.Result
);
if (dts.GetAlwaysSucceeds) {
info.Body.FinishCondition(
Ast.Block(tryGet, info.Result)
);
} else {
info.Body.AddCondition(
tryGet,
info.Result
);
}
}
示例10: TryGetGetAttribute
/// <summary>
/// Checks to see if this type has __getattribute__ that overrides all other attribute lookup.
///
/// This is more complex then it needs to be. The problem is that when we have a
/// mixed new-style/old-style class we have a weird __getattribute__ defined. When
/// we always dispatch through rules instead of PythonTypes it should be easy to remove
/// this.
/// </summary>
private static bool TryGetGetAttribute(CodeContext/*!*/ context, PythonType/*!*/ type, out PythonTypeSlot dts) {
if (type.TryResolveSlot(context, Symbols.GetAttribute, out dts)) {
BuiltinMethodDescriptor bmd = dts as BuiltinMethodDescriptor;
if (bmd == null || bmd.DeclaringType != typeof(object) ||
bmd.Template.Targets.Count != 1 ||
bmd.Template.Targets[0].DeclaringType != typeof(ObjectOps) ||
bmd.Template.Targets[0].Name != "__getattribute__") {
return dts != null;
}
}
return false;
}
示例11: MakeGetItemIterable
private static DynamicMetaObject MakeGetItemIterable(DynamicMetaObject metaUserObject, BinderState state, PythonTypeSlot pts, string method) {
ParameterExpression tmp = Ast.Parameter(typeof(object), "getitemVal");
return new DynamicMetaObject(
Expression.Block(
new[] { tmp },
Expression.Call(
typeof(PythonOps).GetMethod(method),
Ast.Block(
MetaPythonObject.MakeTryGetTypeMember(
state,
pts,
tmp,
metaUserObject.Expression,
Ast.Call(
typeof(DynamicHelpers).GetMethod("GetPythonType"),
AstUtils.Convert(
metaUserObject.Expression,
typeof(object)
)
)
),
tmp
),
AstUtils.Constant(
CallSite<Func<CallSite, CodeContext, object, int, object>>.Create(
new PythonInvokeBinder(state, new CallSignature(1))
)
)
)
),
metaUserObject.Restrictions
);
}
示例12: CustomAttributeTracker
public CustomAttributeTracker(Type/*!*/ declaringType, string/*!*/ name, PythonTypeSlot/*!*/ slot) {
Debug.Assert(slot != null);
Debug.Assert(declaringType != null);
Debug.Assert(name != null);
_declType = declaringType;
_name = name;
_slot = slot;
}
示例13: GetAttributeNoThrow
public static object GetAttributeNoThrow(CodeContext/*!*/ context, object self, string name, PythonTypeSlot getAttributeSlot, PythonTypeSlot getAttrSlot, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, string, object>>>/*!*/ callSite) {
object value;
if (callSite.Data == null) {
callSite.Data = MakeGetAttrSite(context);
}
try {
if (getAttributeSlot.TryGetBoundValue(context, self, ((IPythonObject)self).PythonType, out value)) {
return callSite.Data.Target(callSite.Data, context, value, name);
}
} catch (MissingMemberException) {
try {
if (getAttrSlot != null && getAttrSlot.TryGetBoundValue(context, self, ((IPythonObject)self).PythonType, out value)) {
return callSite.Data.Target(callSite.Data, context, value, name);
}
return OperationFailed.Value;
} catch (MissingMemberException) {
return OperationFailed.Value;
}
}
try {
if (getAttrSlot != null && getAttrSlot.TryGetBoundValue(context, self, ((IPythonObject)self).PythonType, out value)) {
return callSite.Data.Target(callSite.Data, context, value, name);
}
} catch (MissingMemberException) {
}
return OperationFailed.Value;
}
示例14: TryLookupSlot
/// <summary>
/// Performs .NET member resolution. This looks within the given type and also
/// includes any extension members. Base classes and their extension members are
/// not searched.
/// </summary>
public bool TryLookupSlot(CodeContext/*!*/ context, PythonType/*!*/ type, SymbolId name, out PythonTypeSlot slot) {
Debug.Assert(type.IsSystemType);
return TryLookupProtectedSlot(context, type, name, out slot);
}
示例15: MakeGetAttributeRule
/// <summary>
/// Makes a rule which calls a user-defined __getattribute__ function and falls back to __getattr__ if that
/// raises an AttributeError.
///
/// slot is the __getattribute__ method to be called.
/// </summary>
private DynamicMetaObject/*!*/ MakeGetAttributeRule(GetBindingInfo/*!*/ info, IPythonObject/*!*/ obj, PythonTypeSlot/*!*/ slot, Expression codeContext) {
// if the type implements IDynamicObject and we picked up it's __getattribute__ then we want to just
// dispatch to the base meta object (or to the default binder). an example of this is:
//
// class mc(type):
// def __getattr__(self, name):
// return 42
//
// class nc_ga(object):
// __metaclass__ = mc
//
// a = nc_ga.x # here we want to dispatch to the type's rule, not call __getattribute__ directly.
CodeContext context = BinderState.GetBinderState(info.Action).Context;
Type finalType = PythonTypeOps.GetFinalSystemType(obj.PythonType.UnderlyingSystemType);
if (typeof(IDynamicObject).IsAssignableFrom(finalType)) {
PythonTypeSlot baseSlot;
if (TryGetGetAttribute(context, DynamicHelpers.GetPythonTypeFromType(finalType), out baseSlot) && baseSlot == slot) {
return Fallback(info.Action, codeContext);
}
}
// otherwise generate code into a helper function. This will do the slot lookup and exception
// handling for both __getattribute__ as well as __getattr__ if it exists.
PythonTypeSlot getattr;
obj.PythonType.TryResolveSlot(context, Symbols.GetBoundAttr, out getattr);
DynamicMetaObject self = Restrict(Value.GetType());
string methodName = BindingHelpers.IsNoThrow(info.Action) ? "GetAttributeNoThrow" : "GetAttribute";
return BindingHelpers.AddDynamicTestAndDefer(
info.Action,
new DynamicMetaObject(
Ast.Call(
typeof(UserTypeOps).GetMethod(methodName),
Ast.Constant(BinderState.GetBinderState(info.Action).Context),
info.Args[0].Expression,
Ast.Constant(GetGetMemberName(info.Action)),
Ast.Constant(slot, typeof(PythonTypeSlot)),
Ast.Constant(getattr, typeof(PythonTypeSlot)),
Ast.Constant(new SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, string, object>>>())
),
self.Restrictions
),
info.Args,
info.Validation
);
}