PageRenderTime 71ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/Types/PythonType.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 3232 lines | 2364 code | 557 blank | 311 comment | 676 complexity | 752f5f948f96afb4e8139d82ff0a3283 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !CLR2
  16. using System.Linq.Expressions;
  17. using System.Numerics;
  18. #else
  19. using Microsoft.Scripting.Ast;
  20. using Microsoft.Scripting.Math;
  21. using Complex = Microsoft.Scripting.Math.Complex64;
  22. #endif
  23. using System;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using System.Diagnostics;
  27. using System.Globalization;
  28. using System.Reflection;
  29. using System.Runtime.CompilerServices;
  30. using System.Dynamic;
  31. using System.Threading;
  32. using Microsoft.Scripting;
  33. using Microsoft.Scripting.Actions;
  34. using Microsoft.Scripting.Generation;
  35. using Microsoft.Scripting.Runtime;
  36. using Microsoft.Scripting.Utils;
  37. using IronPython.Runtime.Binding;
  38. using IronPython.Runtime.Operations;
  39. namespace IronPython.Runtime.Types {
  40. /// <summary>
  41. /// Represents a PythonType. Instances of PythonType are created via PythonTypeBuilder.
  42. /// </summary>
  43. [DebuggerDisplay("PythonType: {Name}"), DebuggerTypeProxy(typeof(PythonType.DebugProxy))]
  44. [PythonType("type")]
  45. [Documentation(@"type(object) -> gets the type of the object
  46. type(name, bases, dict) -> creates a new type instance with the given name, base classes, and members from the dictionary")]
  47. public partial class PythonType : IPythonMembersList, IDynamicMetaObjectProvider, IWeakReferenceable, ICodeFormattable, IFastGettable, IFastSettable, IFastInvokable {
  48. private Type/*!*/ _underlyingSystemType; // the underlying CLI system type for this type
  49. private string _name; // the name of the type
  50. private Dictionary<string, PythonTypeSlot> _dict; // type-level slots & attributes
  51. private PythonTypeAttributes _attrs; // attributes of the type
  52. private int _version = GetNextVersion(); // version of the type
  53. private List<WeakReference> _subtypes; // all of the subtypes of the PythonType
  54. private PythonContext _pythonContext; // the context the type was created from, or null for system types.
  55. private bool? _objectNew, _objectInit; // true if the type doesn't override __new__ / __init__ from object.
  56. internal Dictionary<string, FastGetBase> _cachedGets; // cached gets on user defined type instances
  57. internal Dictionary<string, FastGetBase> _cachedTryGets; // cached try gets on used defined type instances
  58. internal Dictionary<SetMemberKey, FastSetBase> _cachedSets; // cached sets on user defined instances
  59. internal Dictionary<string, TypeGetBase> _cachedTypeGets; // cached gets on types (system and user types)
  60. internal Dictionary<string, TypeGetBase> _cachedTypeTryGets; // cached gets on types (system and user types)
  61. // commonly calculatable
  62. private List<PythonType> _resolutionOrder; // the search order for methods in the type
  63. private PythonType/*!*/[]/*!*/ _bases; // the base classes of the type
  64. private BuiltinFunction _ctor; // the built-in function which allocates an instance - a .NET ctor
  65. // fields that frequently remain null
  66. private WeakRefTracker _weakrefTracker; // storage for Python style weak references
  67. private WeakReference _weakRef; // single weak ref instance used for all user PythonTypes.
  68. private string[] _slots; // the slots when the class was created
  69. private OldClass _oldClass; // the associated OldClass or null for new-style types
  70. private int _originalSlotCount; // the number of slots when the type was created
  71. private InstanceCreator _instanceCtor; // creates instances
  72. private CallSite<Func<CallSite, object, int>> _hashSite;
  73. private CallSite<Func<CallSite, object, object, bool>> _eqSite;
  74. private CallSite<Func<CallSite, object, object, int>> _compareSite;
  75. private Dictionary<CallSignature, LateBoundInitBinder> _lateBoundInitBinders;
  76. private string[] _optimizedInstanceNames; // optimized names stored in a custom dictionary
  77. private int _optimizedInstanceVersion;
  78. private PythonSiteCache _siteCache = new PythonSiteCache();
  79. private PythonTypeSlot _lenSlot; // cached length slot, cleared when the type is mutated
  80. [MultiRuntimeAware]
  81. private static int MasterVersion = 1;
  82. private static readonly CommonDictionaryStorage _pythonTypes = new CommonDictionaryStorage();
  83. internal static PythonType _pythonTypeType = DynamicHelpers.GetPythonTypeFromType(typeof(PythonType));
  84. private static readonly WeakReference[] _emptyWeakRef = new WeakReference[0];
  85. private static object _subtypesLock = new object();
  86. /// <summary>
  87. /// Provides delegates that will invoke a parameterless type ctor. The first key provides
  88. /// the dictionary for a specific type, the 2nd key provides the delegate for a specific
  89. /// call site type used in conjunction w/ our IFastInvokable implementation.
  90. /// </summary>
  91. private static Dictionary<Type, Dictionary<Type, Delegate>> _fastBindCtors = new Dictionary<Type, Dictionary<Type, Delegate>>();
  92. /// <summary>
  93. /// Shared built-in functions for creating instances of user defined types. Because all
  94. /// types w/ the same UnderlyingSystemType share the same constructors these can be
  95. /// shared across multiple types.
  96. /// </summary>
  97. private static Dictionary<Type, BuiltinFunction> _userTypeCtors = new Dictionary<Type, BuiltinFunction>();
  98. /// <summary>
  99. /// Creates a new type for a user defined type. The name, base classes (a tuple of type
  100. /// objects), and a dictionary of members is provided.
  101. /// </summary>
  102. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
  103. public PythonType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary dict)
  104. : this(context, name, bases, dict, String.Empty) {
  105. }
  106. /// <summary>
  107. /// Creates a new type for a user defined type. The name, base classes (a tuple of type
  108. /// objects), and a dictionary of members is provided.
  109. /// </summary>
  110. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
  111. internal PythonType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary dict, string selfNames) {
  112. InitializeUserType(context, name, bases, dict, selfNames);
  113. }
  114. internal PythonType() {
  115. }
  116. /// <summary>
  117. /// Creates a new PythonType object which is backed by the specified .NET type for
  118. /// storage. The type is considered a system type which can not be modified
  119. /// by the user.
  120. /// </summary>
  121. /// <param name="underlyingSystemType"></param>
  122. internal PythonType(Type underlyingSystemType) {
  123. _underlyingSystemType = underlyingSystemType;
  124. InitializeSystemType();
  125. }
  126. /// <summary>
  127. /// Creates a new PythonType which is a subclass of the specified PythonType.
  128. ///
  129. /// Used for runtime defined new-style classes which require multiple inheritance. The
  130. /// primary example of this is the exception system.
  131. /// </summary>
  132. internal PythonType(PythonType baseType, string name) {
  133. _underlyingSystemType = baseType.UnderlyingSystemType;
  134. IsSystemType = baseType.IsSystemType;
  135. IsPythonType = baseType.IsPythonType;
  136. Name = name;
  137. _bases = new PythonType[] { baseType };
  138. ResolutionOrder = Mro.Calculate(this, _bases);
  139. _attrs |= PythonTypeAttributes.HasDictionary;
  140. }
  141. /// <summary>
  142. /// Creates a new PythonType which is a subclass of the specified PythonType.
  143. ///
  144. /// Used for runtime defined new-style classes which require multiple inheritance. The
  145. /// primary example of this is the exception system.
  146. /// </summary>
  147. internal PythonType(PythonContext context, PythonType baseType, string name, string module, string doc)
  148. : this(baseType, name) {
  149. EnsureDict();
  150. _dict["__doc__"] = new PythonTypeUserDescriptorSlot(doc, true);
  151. _dict["__module__"] = new PythonTypeUserDescriptorSlot(module, true);
  152. IsSystemType = false;
  153. IsPythonType = false;
  154. _pythonContext = context;
  155. _attrs |= PythonTypeAttributes.HasDictionary;
  156. }
  157. /// <summary>
  158. /// Creates a new PythonType object which represents an Old-style class.
  159. /// </summary>
  160. internal PythonType(OldClass oc) {
  161. EnsureDict();
  162. _underlyingSystemType = typeof(OldInstance);
  163. Name = oc.Name;
  164. OldClass = oc;
  165. List<PythonType> ocs = new List<PythonType>(oc.BaseClasses.Count);
  166. foreach (OldClass klass in oc.BaseClasses) {
  167. ocs.Add(klass.TypeObject);
  168. }
  169. List<PythonType> mro = new List<PythonType>();
  170. mro.Add(this);
  171. _bases = ocs.ToArray();
  172. _resolutionOrder = mro;
  173. AddSlot("__class__", new PythonTypeUserDescriptorSlot(this, true));
  174. }
  175. internal BuiltinFunction Ctor {
  176. get {
  177. EnsureConstructor();
  178. return _ctor;
  179. }
  180. }
  181. #region Public API
  182. public static object __new__(CodeContext/*!*/ context, PythonType cls, string name, PythonTuple bases, PythonDictionary dict) {
  183. return __new__(context, cls, name, bases, dict, String.Empty);
  184. }
  185. internal static object __new__(CodeContext/*!*/ context, PythonType cls, string name, PythonTuple bases, PythonDictionary dict, string selfNames) {
  186. if (name == null) {
  187. throw PythonOps.TypeError("type() argument 1 must be string, not None");
  188. }
  189. if (bases == null) {
  190. throw PythonOps.TypeError("type() argument 2 must be tuple, not None");
  191. }
  192. if (dict == null) {
  193. throw PythonOps.TypeError("TypeError: type() argument 3 must be dict, not None");
  194. }
  195. EnsureModule(context, dict);
  196. PythonType meta = FindMetaClass(cls, bases);
  197. if (meta != TypeCache.OldInstance && meta != TypeCache.PythonType) {
  198. if (meta != cls) {
  199. // the user has a custom __new__ which picked the wrong meta class, call the correct metaclass
  200. return PythonCalls.Call(context, meta, name, bases, dict);
  201. }
  202. // we have the right user __new__, call our ctor method which will do the actual
  203. // creation.
  204. return meta.CreateInstance(context, name, bases, dict);
  205. }
  206. // no custom user type for __new__
  207. return new PythonType(context, name, bases, dict, selfNames);
  208. }
  209. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
  210. public void __init__(string name, PythonTuple bases, PythonDictionary dict) {
  211. }
  212. internal static PythonType FindMetaClass(PythonType cls, PythonTuple bases) {
  213. PythonType meta = cls;
  214. foreach (object dt in bases) {
  215. PythonType metaCls = DynamicHelpers.GetPythonType(dt);
  216. if (metaCls == TypeCache.OldClass) continue;
  217. if (meta.IsSubclassOf(metaCls)) continue;
  218. if (metaCls.IsSubclassOf(meta)) {
  219. meta = metaCls;
  220. continue;
  221. }
  222. throw PythonOps.TypeError("metaclass conflict {0} and {1}", metaCls.Name, meta.Name);
  223. }
  224. return meta;
  225. }
  226. public static object __new__(CodeContext/*!*/ context, object cls, object o) {
  227. return DynamicHelpers.GetPythonType(o);
  228. }
  229. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
  230. public void __init__(object o) {
  231. }
  232. [SpecialName, PropertyMethod, WrapperDescriptor]
  233. public static PythonTuple Get__bases__(CodeContext/*!*/ context, PythonType/*!*/ type) {
  234. return type.GetBasesTuple();
  235. }
  236. private PythonTuple GetBasesTuple() {
  237. object[] res = new object[BaseTypes.Count];
  238. IList<PythonType> bases = BaseTypes;
  239. for (int i = 0; i < bases.Count; i++) {
  240. PythonType baseType = bases[i];
  241. if (baseType.IsOldClass) {
  242. res[i] = baseType.OldClass;
  243. } else {
  244. res[i] = baseType;
  245. }
  246. }
  247. return PythonTuple.MakeTuple(res);
  248. }
  249. [SpecialName, PropertyMethod, WrapperDescriptor]
  250. public static PythonType Get__base__(CodeContext/*!*/ context, PythonType/*!*/ type) {
  251. foreach (object typeObj in Get__bases__(context, type)) {
  252. PythonType pt = typeObj as PythonType;
  253. if (pt != null) {
  254. return pt;
  255. }
  256. }
  257. return null;
  258. }
  259. /// <summary>
  260. /// Used in copy_reg which is the only consumer of __flags__ in the standard library.
  261. ///
  262. /// Set if the type is user defined
  263. /// </summary>
  264. private const int TypeFlagHeapType = 1 << 9;
  265. [SpecialName, PropertyMethod, WrapperDescriptor]
  266. public static int Get__flags__(CodeContext/*!*/ context, PythonType/*!*/ type) {
  267. if (type.IsSystemType) {
  268. return 0;
  269. }
  270. return TypeFlagHeapType;
  271. }
  272. [SpecialName, PropertyMethod, WrapperDescriptor]
  273. public static void Set__bases__(CodeContext/*!*/ context, PythonType/*!*/ type, object value) {
  274. // validate we got a tuple...
  275. PythonTuple t = value as PythonTuple;
  276. if (t == null) throw PythonOps.TypeError("expected tuple of types or old-classes, got '{0}'", PythonTypeOps.GetName(value));
  277. List<PythonType> ldt = new List<PythonType>();
  278. foreach (object o in t) {
  279. // gather all the type objects...
  280. PythonType adt = o as PythonType;
  281. if (adt == null) {
  282. OldClass oc = o as OldClass;
  283. if (oc == null) {
  284. throw PythonOps.TypeError("expected tuple of types, got '{0}'", PythonTypeOps.GetName(o));
  285. }
  286. adt = oc.TypeObject;
  287. }
  288. ldt.Add(adt);
  289. }
  290. // Ensure that we are not switching the CLI type
  291. Type newType = NewTypeMaker.GetNewType(type.Name, t);
  292. if (type.UnderlyingSystemType != newType)
  293. throw PythonOps.TypeErrorForIncompatibleObjectLayout("__bases__ assignment", type, newType);
  294. // set bases & the new resolution order
  295. List<PythonType> mro = CalculateMro(type, ldt);
  296. type.BaseTypes = ldt;
  297. type._resolutionOrder = mro;
  298. }
  299. private static List<PythonType> CalculateMro(PythonType type, IList<PythonType> ldt) {
  300. return Mro.Calculate(type, ldt);
  301. }
  302. private static bool TryReplaceExtensibleWithBase(Type curType, out Type newType) {
  303. if (curType.IsGenericType &&
  304. curType.GetGenericTypeDefinition() == typeof(Extensible<>)) {
  305. newType = curType.GetGenericArguments()[0];
  306. return true;
  307. }
  308. newType = null;
  309. return false;
  310. }
  311. public object __call__(CodeContext context, params object[] args) {
  312. return PythonTypeOps.CallParams(context, this, args);
  313. }
  314. public object __call__(CodeContext context, [ParamDictionary]IDictionary<string, object> kwArgs, params object[] args) {
  315. return PythonTypeOps.CallWorker(context, this, kwArgs, args);
  316. }
  317. public int __cmp__([NotNull]PythonType other) {
  318. if (other != this) {
  319. int res = Name.CompareTo(other.Name);
  320. if (res == 0) {
  321. long thisId = IdDispenser.GetId(this);
  322. long otherId = IdDispenser.GetId(other);
  323. if (thisId > otherId) {
  324. return 1;
  325. } else {
  326. return -1;
  327. }
  328. }
  329. return res;
  330. }
  331. return 0;
  332. }
  333. [Python3Warning("type inequality comparisons not supported in 3.x")]
  334. public static bool operator >(PythonType self, PythonType other) {
  335. return self.__cmp__(other) > 0;
  336. }
  337. [Python3Warning("type inequality comparisons not supported in 3.x")]
  338. public static bool operator <(PythonType self, PythonType other) {
  339. return self.__cmp__(other) < 0;
  340. }
  341. [Python3Warning("type inequality comparisons not supported in 3.x")]
  342. public static bool operator >=(PythonType self, PythonType other) {
  343. return self.__cmp__(other) >= 0;
  344. }
  345. [Python3Warning("type inequality comparisons not supported in 3.x")]
  346. public static bool operator <=(PythonType self, PythonType other) {
  347. return self.__cmp__(other) <= 0;
  348. }
  349. public void __delattr__(CodeContext/*!*/ context, string name) {
  350. DeleteCustomMember(context, name);
  351. }
  352. [SlotField]
  353. public static PythonTypeSlot __dict__ = new PythonTypeDictSlot(_pythonTypeType);
  354. [SpecialName, PropertyMethod, WrapperDescriptor]
  355. public static object Get__doc__(CodeContext/*!*/ context, PythonType self) {
  356. PythonTypeSlot pts;
  357. object res;
  358. if (self.TryLookupSlot(context, "__doc__", out pts) &&
  359. pts.TryGetValue(context, null, self, out res)) {
  360. return res;
  361. } else if (self.IsSystemType) {
  362. return PythonTypeOps.GetDocumentation(self.UnderlyingSystemType);
  363. }
  364. return null;
  365. }
  366. public object __getattribute__(CodeContext/*!*/ context, string name) {
  367. object value;
  368. if (TryGetBoundCustomMember(context, name, out value)) {
  369. return value;
  370. }
  371. throw PythonOps.AttributeError("type object '{0}' has no attribute '{1}'", Name, name);
  372. }
  373. public PythonType this[params Type[] args] {
  374. get {
  375. if (UnderlyingSystemType == typeof(Array)) {
  376. if (args.Length == 1) {
  377. return DynamicHelpers.GetPythonTypeFromType(args[0].MakeArrayType());
  378. }
  379. throw PythonOps.TypeError("expected one argument to make array type, got {0}", args.Length);
  380. }
  381. if (!UnderlyingSystemType.IsGenericTypeDefinition) {
  382. throw new InvalidOperationException("MakeGenericType on non-generic type");
  383. }
  384. return DynamicHelpers.GetPythonTypeFromType(UnderlyingSystemType.MakeGenericType(args));
  385. }
  386. }
  387. [SpecialName, PropertyMethod, WrapperDescriptor]
  388. public static object Get__module__(CodeContext/*!*/ context, PythonType self) {
  389. PythonTypeSlot pts;
  390. object res;
  391. if (self._dict != null &&
  392. self._dict.TryGetValue("__module__", out pts) &&
  393. pts.TryGetValue(context, self, DynamicHelpers.GetPythonType(self), out res)) {
  394. return res;
  395. }
  396. return PythonTypeOps.GetModuleName(context, self.UnderlyingSystemType);
  397. }
  398. [SpecialName, PropertyMethod, WrapperDescriptor]
  399. public static void Set__module__(CodeContext/*!*/ context, PythonType self, object value) {
  400. if (self.IsSystemType) {
  401. throw PythonOps.TypeError("can't set {0}.__module__", self.Name);
  402. }
  403. Debug.Assert(self._dict != null);
  404. self._dict["__module__"] = new PythonTypeUserDescriptorSlot(value);
  405. self.UpdateVersion();
  406. }
  407. [SpecialName, PropertyMethod, WrapperDescriptor]
  408. public static void Delete__module__(CodeContext/*!*/ context, PythonType self) {
  409. throw PythonOps.TypeError("can't delete {0}.__module__", self.Name);
  410. }
  411. [SpecialName, PropertyMethod, WrapperDescriptor]
  412. public static PythonTuple Get__mro__(PythonType type) {
  413. return PythonTypeOps.MroToPython(type.ResolutionOrder);
  414. }
  415. [SpecialName, PropertyMethod, WrapperDescriptor]
  416. public static string Get__name__(PythonType type) {
  417. return type.Name;
  418. }
  419. [SpecialName, PropertyMethod, WrapperDescriptor]
  420. public static void Set__name__(PythonType type, string name) {
  421. if (type.IsSystemType) {
  422. throw PythonOps.TypeError("can't set attributes of built-in/extension type '{0}'", type.Name);
  423. }
  424. type.Name = name;
  425. }
  426. public string/*!*/ __repr__(CodeContext/*!*/ context) {
  427. string name = Name;
  428. if (IsSystemType) {
  429. if (PythonTypeOps.IsRuntimeAssembly(UnderlyingSystemType.Assembly) || IsPythonType) {
  430. object module = Get__module__(context, this);
  431. if (!module.Equals("__builtin__")) {
  432. return string.Format("<type '{0}.{1}'>", module, Name);
  433. }
  434. }
  435. return string.Format("<type '{0}'>", Name);
  436. } else {
  437. PythonTypeSlot dts;
  438. string module = "unknown";
  439. object modObj;
  440. if (TryLookupSlot(context, "__module__", out dts) &&
  441. dts.TryGetValue(context, this, this, out modObj)) {
  442. module = modObj as string;
  443. }
  444. return string.Format("<class '{0}.{1}'>", module, name);
  445. }
  446. }
  447. internal string/*!*/ GetTypeDebuggerDisplay() {
  448. PythonTypeSlot dts;
  449. string module = "unknown";
  450. object modObj;
  451. if (TryLookupSlot(Context.SharedContext, "__module__", out dts) &&
  452. dts.TryGetValue(Context.SharedContext, this, this, out modObj)) {
  453. module = modObj as string;
  454. }
  455. return string.Format("{0}.{1} instance", module, Name);
  456. }
  457. public void __setattr__(CodeContext/*!*/ context, string name, object value) {
  458. SetCustomMember(context, name, value);
  459. }
  460. public List __subclasses__(CodeContext/*!*/ context) {
  461. List ret = new List();
  462. IList<WeakReference> subtypes = SubTypes;
  463. if (subtypes != null) {
  464. PythonContext pc = PythonContext.GetContext(context);
  465. foreach (WeakReference wr in subtypes) {
  466. if (wr.IsAlive) {
  467. PythonType pt = (PythonType)wr.Target;
  468. if (pt.PythonContext == null || pt.PythonContext == pc) {
  469. ret.AddNoLock(wr.Target);
  470. }
  471. }
  472. }
  473. }
  474. return ret;
  475. }
  476. public virtual List mro() {
  477. return new List(Get__mro__(this));
  478. }
  479. /// <summary>
  480. /// Returns true if the specified object is an instance of this type.
  481. /// </summary>
  482. public virtual bool __instancecheck__(object instance) {
  483. return SubclassImpl(DynamicHelpers.GetPythonType(instance));
  484. }
  485. public virtual bool __subclasscheck__(PythonType sub) {
  486. return SubclassImpl(sub);
  487. }
  488. private bool SubclassImpl(PythonType sub) {
  489. if (UnderlyingSystemType.IsInterface) {
  490. // interfaces aren't in bases, and therefore IsSubclassOf doesn't do this check.
  491. if (UnderlyingSystemType.IsAssignableFrom(sub.UnderlyingSystemType)) {
  492. return true;
  493. }
  494. }
  495. return sub.IsSubclassOf(this);
  496. }
  497. public virtual bool __subclasscheck__(OldClass sub) {
  498. return IsSubclassOf(sub.TypeObject);
  499. }
  500. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2225:OperatorOverloadsHaveNamedAlternates")]
  501. public static implicit operator Type(PythonType self) {
  502. return self.UnderlyingSystemType;
  503. }
  504. public static implicit operator TypeTracker(PythonType self) {
  505. return ReflectionCache.GetTypeTracker(self.UnderlyingSystemType);
  506. }
  507. #endregion
  508. #region Internal API
  509. internal bool IsMixedNewStyleOldStyle() {
  510. if (!IsOldClass) {
  511. foreach (PythonType baseType in ResolutionOrder) {
  512. if (baseType.IsOldClass) {
  513. // mixed new-style/old-style class, we can't handle
  514. // __init__ in an old-style class yet (it doesn't show
  515. // up in a slot).
  516. return true;
  517. }
  518. }
  519. }
  520. return false;
  521. }
  522. internal int SlotCount {
  523. get {
  524. return _originalSlotCount;
  525. }
  526. }
  527. /// <summary>
  528. /// Gets the name of the dynamic type
  529. /// </summary>
  530. internal string Name {
  531. get {
  532. return _name;
  533. }
  534. set {
  535. _name = value;
  536. }
  537. }
  538. internal int Version {
  539. get {
  540. return _version;
  541. }
  542. }
  543. internal bool IsNull {
  544. get {
  545. return UnderlyingSystemType == typeof(DynamicNull);
  546. }
  547. }
  548. /// <summary>
  549. /// Gets the resolution order used for attribute lookup
  550. /// </summary>
  551. internal IList<PythonType> ResolutionOrder {
  552. get {
  553. return _resolutionOrder;
  554. }
  555. set {
  556. lock (SyncRoot) {
  557. _resolutionOrder = new List<PythonType>(value);
  558. }
  559. }
  560. }
  561. /// <summary>
  562. /// Gets the dynamic type that corresponds with the provided static type.
  563. ///
  564. /// Returns null if no type is available. TODO: In the future this will
  565. /// always return a PythonType created by the DLR.
  566. /// </summary>
  567. /// <param name="type"></param>
  568. /// <returns></returns>
  569. internal static PythonType/*!*/ GetPythonType(Type type) {
  570. object res;
  571. if (!_pythonTypes.TryGetValue(type, out res)) {
  572. lock (_pythonTypes) {
  573. if (!_pythonTypes.TryGetValue(type, out res)) {
  574. res = new PythonType(type);
  575. _pythonTypes.Add(type, res);
  576. }
  577. }
  578. }
  579. return (PythonType)res;
  580. }
  581. /// <summary>
  582. /// Sets the python type that corresponds with the provided static type.
  583. ///
  584. /// This is used for built-in types which have a metaclass. Currently
  585. /// only used by ctypes.
  586. /// </summary>
  587. internal static PythonType SetPythonType(Type type, PythonType pyType) {
  588. lock (_pythonTypes) {
  589. Debug.Assert(!_pythonTypes.Contains(type));
  590. Debug.Assert(pyType.GetType() != typeof(PythonType));
  591. _pythonTypes.Add(type, pyType);
  592. }
  593. return pyType;
  594. }
  595. /// <summary>
  596. /// Allocates the storage for the instance running the .NET constructor. This provides
  597. /// the creation functionality for __new__ implementations.
  598. /// </summary>
  599. internal object CreateInstance(CodeContext/*!*/ context) {
  600. EnsureInstanceCtor();
  601. return _instanceCtor.CreateInstance(context);
  602. }
  603. /// <summary>
  604. /// Allocates the storage for the instance running the .NET constructor. This provides
  605. /// the creation functionality for __new__ implementations.
  606. /// </summary>
  607. internal object CreateInstance(CodeContext/*!*/ context, object arg0) {
  608. EnsureInstanceCtor();
  609. return _instanceCtor.CreateInstance(context, arg0);
  610. }
  611. /// <summary>
  612. /// Allocates the storage for the instance running the .NET constructor. This provides
  613. /// the creation functionality for __new__ implementations.
  614. /// </summary>
  615. internal object CreateInstance(CodeContext/*!*/ context, object arg0, object arg1) {
  616. EnsureInstanceCtor();
  617. return _instanceCtor.CreateInstance(context, arg0, arg1);
  618. }
  619. /// <summary>
  620. /// Allocates the storage for the instance running the .NET constructor. This provides
  621. /// the creation functionality for __new__ implementations.
  622. /// </summary>
  623. internal object CreateInstance(CodeContext/*!*/ context, object arg0, object arg1, object arg2) {
  624. EnsureInstanceCtor();
  625. return _instanceCtor.CreateInstance(context, arg0, arg1, arg2);
  626. }
  627. /// <summary>
  628. /// Allocates the storage for the instance running the .NET constructor. This provides
  629. /// the creation functionality for __new__ implementations.
  630. /// </summary>
  631. internal object CreateInstance(CodeContext context, params object[] args) {
  632. Assert.NotNull(args);
  633. EnsureInstanceCtor();
  634. // unpack args for common cases so we don't generate code to do it...
  635. switch (args.Length) {
  636. case 0: return _instanceCtor.CreateInstance(context);
  637. case 1: return _instanceCtor.CreateInstance(context, args[0]);
  638. case 2: return _instanceCtor.CreateInstance(context, args[0], args[1]);
  639. case 3: return _instanceCtor.CreateInstance(context, args[0], args[1], args[2]);
  640. default:
  641. return _instanceCtor.CreateInstance(context, args);
  642. }
  643. }
  644. /// <summary>
  645. /// Allocates the storage for the instance running the .NET constructor. This provides
  646. /// the creation functionality for __new__ implementations.
  647. /// </summary>
  648. internal object CreateInstance(CodeContext context, object[] args, string[] names) {
  649. Assert.NotNull(args, "args");
  650. Assert.NotNull(names, "names");
  651. EnsureInstanceCtor();
  652. return _instanceCtor.CreateInstance(context, args, names);
  653. }
  654. internal int Hash(object o) {
  655. EnsureHashSite();
  656. return _hashSite.Target(_hashSite, o);
  657. }
  658. internal bool TryGetLength(CodeContext context, object o, out int length) {
  659. CallSite<Func<CallSite, CodeContext, object, object>> lenSite;
  660. if (IsSystemType) {
  661. lenSite = PythonContext.GetContext(context).GetSiteCacheForSystemType(UnderlyingSystemType).GetLenSite(context);
  662. } else {
  663. lenSite = _siteCache.GetLenSite(context);
  664. }
  665. PythonTypeSlot lenSlot = _lenSlot;
  666. if (lenSlot == null && !PythonOps.TryResolveTypeSlot(context, this, "__len__", out lenSlot)) {
  667. length = 0;
  668. return false;
  669. }
  670. object func;
  671. if (!lenSlot.TryGetValue(context, o, this, out func)) {
  672. length = 0;
  673. return false;
  674. }
  675. object res = lenSite.Target(lenSite, context, func);
  676. if (!(res is int)) {
  677. throw PythonOps.ValueError("__len__ must return int");
  678. }
  679. length = (int)res;
  680. return true;
  681. }
  682. internal bool EqualRetBool(object self, object other) {
  683. if (_eqSite == null) {
  684. Interlocked.CompareExchange(
  685. ref _eqSite,
  686. Context.CreateComparisonSite(PythonOperationKind.Equal),
  687. null
  688. );
  689. }
  690. return _eqSite.Target(_eqSite, self, other);
  691. }
  692. internal int Compare(object self, object other) {
  693. if (_compareSite == null) {
  694. Interlocked.CompareExchange(
  695. ref _compareSite,
  696. Context.MakeSortCompareSite(),
  697. null
  698. );
  699. }
  700. return _compareSite.Target(_compareSite, self, other);
  701. }
  702. internal bool TryGetBoundAttr(CodeContext context, object o, string name, out object ret) {
  703. CallSite<Func<CallSite, object, CodeContext, object>> site;
  704. if (IsSystemType) {
  705. site = PythonContext.GetContext(context).GetSiteCacheForSystemType(UnderlyingSystemType).GetTryGetMemberSite(context, name);
  706. } else {
  707. site = _siteCache.GetTryGetMemberSite(context, name);
  708. }
  709. try {
  710. ret = site.Target(site, o, context);
  711. return ret != OperationFailed.Value;
  712. } catch (MissingMemberException) {
  713. ExceptionHelpers.DynamicStackFrames = null;
  714. ret = null;
  715. return false;
  716. }
  717. }
  718. internal CallSite<Func<CallSite, object, int>> HashSite {
  719. get {
  720. EnsureHashSite();
  721. return _hashSite;
  722. }
  723. }
  724. private void EnsureHashSite() {
  725. if(_hashSite == null) {
  726. Interlocked.CompareExchange(
  727. ref _hashSite,
  728. CallSite<Func<CallSite, object, int>>.Create(
  729. Context.Operation(
  730. PythonOperationKind.Hash
  731. )
  732. ),
  733. null
  734. );
  735. }
  736. }
  737. /// <summary>
  738. /// Gets the underlying system type that is backing this type. All instances of this
  739. /// type are an instance of the underlying system type.
  740. /// </summary>
  741. internal Type/*!*/ UnderlyingSystemType {
  742. get {
  743. return _underlyingSystemType;
  744. }
  745. }
  746. /// <summary>
  747. /// Gets the extension type for this type. The extension type provides
  748. /// a .NET type which can be inherited from to extend sealed classes
  749. /// or value types which Python allows inheritance from.
  750. /// </summary>
  751. internal Type/*!*/ ExtensionType {
  752. get {
  753. if (!_underlyingSystemType.IsEnum) {
  754. switch (Type.GetTypeCode(_underlyingSystemType)) {
  755. case TypeCode.String: return typeof(ExtensibleString);
  756. case TypeCode.Int32: return typeof(Extensible<int>);
  757. case TypeCode.Double: return typeof(Extensible<double>);
  758. case TypeCode.Object:
  759. if (_underlyingSystemType == typeof(BigInteger)) {
  760. return typeof(Extensible<BigInteger>);
  761. } else if (_underlyingSystemType == typeof(Complex)) {
  762. return typeof(ExtensibleComplex);
  763. }
  764. break;
  765. }
  766. }
  767. return _underlyingSystemType;
  768. }
  769. }
  770. /// <summary>
  771. /// Gets the base types from which this type inherits.
  772. /// </summary>
  773. internal IList<PythonType>/*!*/ BaseTypes {
  774. get {
  775. return _bases;
  776. }
  777. set {
  778. // validate input...
  779. foreach (PythonType pt in value) {
  780. if (pt == null) throw new ArgumentNullException("value", "a PythonType was null while assigning base classes");
  781. }
  782. // first update our sub-type list
  783. lock (_bases) {
  784. foreach (PythonType dt in _bases) {
  785. dt.RemoveSubType(this);
  786. }
  787. // set the new bases
  788. List<PythonType> newBases = new List<PythonType>(value);
  789. // add us as subtypes of our new bases
  790. foreach (PythonType dt in newBases) {
  791. dt.AddSubType(this);
  792. }
  793. UpdateVersion();
  794. _bases = newBases.ToArray();
  795. }
  796. }
  797. }
  798. /// <summary>
  799. /// Returns true if this type is a subclass of other
  800. /// </summary>
  801. internal bool IsSubclassOf(PythonType other) {
  802. // check for a type match
  803. if (other == this) {
  804. return true;
  805. }
  806. //Python doesn't have value types inheriting from ValueType, but we fake this for interop
  807. if (other.UnderlyingSystemType == typeof(ValueType) && UnderlyingSystemType.IsValueType) {
  808. return true;
  809. }
  810. return IsSubclassWorker(other);
  811. }
  812. private bool IsSubclassWorker(PythonType other) {
  813. for (int i = 0; i < _bases.Length; i++) {
  814. PythonType baseClass = _bases[i];
  815. if (baseClass == other || baseClass.IsSubclassWorker(other)) {
  816. return true;
  817. }
  818. }
  819. return false;
  820. }
  821. /// <summary>
  822. /// True if the type is a system type. A system type is a type which represents an
  823. /// underlying .NET type and not a subtype of one of these types.
  824. /// </summary>
  825. internal bool IsSystemType {
  826. get {
  827. return (_attrs & PythonTypeAttributes.SystemType) != 0;
  828. }
  829. set {
  830. if (value) _attrs |= PythonTypeAttributes.SystemType;
  831. else _attrs &= (~PythonTypeAttributes.SystemType);
  832. }
  833. }
  834. internal bool IsWeakReferencable {
  835. get {
  836. return (_attrs & PythonTypeAttributes.WeakReferencable) != 0;
  837. }
  838. set {
  839. if (value) _attrs |= PythonTypeAttributes.WeakReferencable;
  840. else _attrs &= (~PythonTypeAttributes.WeakReferencable);
  841. }
  842. }
  843. internal bool HasDictionary {
  844. get {
  845. return (_attrs & PythonTypeAttributes.HasDictionary) != 0;
  846. }
  847. set {
  848. if (value) _attrs |= PythonTypeAttributes.HasDictionary;
  849. else _attrs &= (~PythonTypeAttributes.HasDictionary);
  850. }
  851. }
  852. internal bool HasSystemCtor {
  853. get {
  854. return (_attrs & PythonTypeAttributes.SystemCtor) != 0;
  855. }
  856. }
  857. internal void SetConstructor(BuiltinFunction ctor) {
  858. _ctor = ctor;
  859. }
  860. internal bool IsPythonType {
  861. get {
  862. return (_attrs & PythonTypeAttributes.IsPythonType) != 0;
  863. }
  864. set {
  865. if (value) {
  866. _attrs |= PythonTypeAttributes.IsPythonType;
  867. } else {
  868. _attrs &= ~PythonTypeAttributes.IsPythonType;
  869. }
  870. }
  871. }
  872. internal OldClass OldClass {
  873. get {
  874. return _oldClass;
  875. }
  876. set {
  877. _oldClass = value;
  878. }
  879. }
  880. internal bool IsOldClass {
  881. get {
  882. return _oldClass != null;
  883. }
  884. }
  885. internal PythonContext PythonContext {
  886. get {
  887. return _pythonContext;
  888. }
  889. }
  890. internal PythonContext/*!*/ Context {
  891. get {
  892. return _pythonContext ?? DefaultContext.DefaultPythonContext;
  893. }
  894. }
  895. internal object SyncRoot {
  896. get {
  897. // TODO: This is un-ideal, we should lock on something private.
  898. return this;
  899. }
  900. }
  901. internal bool IsHiddenMember(string name) {
  902. PythonTypeSlot dummySlot;
  903. return !TryResolveSlot(DefaultContext.Default, name, out dummySlot) &&
  904. TryResolveSlot(DefaultContext.DefaultCLS, name, out dummySlot);
  905. }
  906. internal LateBoundInitBinder GetLateBoundInitBinder(CallSignature signature) {
  907. Debug.Assert(!IsSystemType); // going to hold onto a PythonContext, shouldn't ever be a system type
  908. Debug.Assert(_pythonContext != null);
  909. if (_lateBoundInitBinders == null) {
  910. Interlocked.CompareExchange(ref _lateBoundInitBinders, new Dictionary<CallSignature, LateBoundInitBinder>(), null);
  911. }
  912. lock(_lateBoundInitBinders) {
  913. LateBoundInitBinder res;
  914. if (!_lateBoundInitBinders.TryGetValue(signature, out res)) {
  915. _lateBoundInitBinders[signature] = res = new LateBoundInitBinder(this, signature);
  916. }
  917. return res;
  918. }
  919. }
  920. #endregion
  921. #region Type member access
  922. /// <summary>
  923. /// Looks up a slot on the dynamic type
  924. /// </summary>
  925. internal bool TryLookupSlot(CodeContext context, string name, out PythonTypeSlot slot) {
  926. if (IsSystemType) {
  927. return PythonBinder.GetBinder(context).TryLookupSlot(context, this, name, out slot);
  928. }
  929. return _dict.TryGetValue(name, out slot);
  930. }
  931. /// <summary>
  932. /// Searches the resolution order for a slot matching by name
  933. /// </summary>
  934. internal bool TryResolveSlot(CodeContext context, string name, out PythonTypeSlot slot) {
  935. for (int i = 0; i < _resolutionOrder.Count; i++) {
  936. PythonType dt = _resolutionOrder[i];
  937. // don't look at interfaces - users can inherit from them, but we resolve members
  938. // via methods implemented on types and defined by Python.
  939. if (dt.IsSystemType && !dt.UnderlyingSystemType.IsInterface) {
  940. return PythonBinder.GetBinder(context).TryResolveSlot(context, dt, this, name, out slot);
  941. }
  942. if (dt.TryLookupSlot(context, name, out slot)) {
  943. return true;
  944. }
  945. }
  946. if (UnderlyingSystemType.IsInterface) {
  947. return TypeCache.Object.TryResolveSlot(context, name, out slot);
  948. }
  949. slot = null;
  950. return false;
  951. }
  952. /// <summary>
  953. /// Searches the resolution order for a slot matching by name.
  954. ///
  955. /// Includes searching for methods in old-style classes
  956. /// </summary>
  957. internal bool TryResolveMixedSlot(CodeContext context, string name, out PythonTypeSlot slot) {
  958. for (int i = 0; i < _resolutionOrder.Count; i++) {
  959. PythonType dt = _resolutionOrder[i];
  960. if (dt.TryLookupSlot(context, name, out slot)) {
  961. return true;
  962. }
  963. if (dt.OldClass != null) {
  964. object ret;
  965. if (dt.OldClass.TryLookupSlot(name, out ret)) {
  966. slot = ToTypeSlot(ret);
  967. return true;
  968. }
  969. }
  970. }
  971. slot = null;
  972. return false;
  973. }
  974. /// <summary>
  975. /// Internal helper to add a new slot to the type
  976. /// </summary>
  977. /// <param name="name"></param>
  978. /// <param name="slot"></param>
  979. internal void AddSlot(string name, PythonTypeSlot slot) {
  980. Debug.Assert(!IsSystemType);
  981. _dict[name] = slot;
  982. if (name == "__new__") {
  983. _objectNew = null;
  984. ClearObjectNewInSubclasses(this);
  985. } else if (name == "__init__") {
  986. _objectInit = null;
  987. ClearObjectInitInSubclasses(this);
  988. }
  989. }
  990. private void ClearObjectNewInSubclasses(PythonType pt) {
  991. lock (_subtypesLock) {
  992. if (pt._subtypes != null) {
  993. foreach (WeakReference wr in pt._subtypes) {
  994. PythonType type = wr.Target as PythonType;
  995. if (type != null) {
  996. type._objectNew = null;
  997. ClearObjectNewInSubclasses(type);
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. private void ClearObjectInitInSubclasses(PythonType pt) {
  1004. lock (_subtypesLock) {
  1005. if (pt._subtypes != null) {
  1006. foreach (WeakReference wr in pt._subtypes) {
  1007. PythonType type = wr.Target as PythonType;
  1008. if (type != null) {
  1009. type._objectInit = null;
  1010. ClearObjectInitInSubclasses(type);
  1011. }
  1012. }
  1013. }
  1014. }
  1015. }
  1016. internal bool TryGetCustomSetAttr(CodeContext context, out PythonTypeSlot pts) {
  1017. PythonContext pc = PythonContext.GetContext(context);
  1018. return pc.Binder.TryResolveSlot(
  1019. context,
  1020. DynamicHelpers.GetPythonType(this),
  1021. this,
  1022. "__setattr__",
  1023. out pts) &&
  1024. pts is BuiltinMethodDescriptor &&
  1025. ((BuiltinMethodDescriptor)pts).DeclaringType != typeof(PythonType);
  1026. }
  1027. internal void SetCustomMember(CodeContext/*!*/ context, string name, object value) {
  1028. Debug.Assert(context != null);
  1029. PythonTypeSlot dts;
  1030. if (TryResolveSlot(context, name, out dts)) {
  1031. if (dts.TrySetValue(context, null, this, value))
  1032. return;
  1033. }
  1034. if (PythonType._pythonTypeType.TryResolveSlot(context, name, out dts)) {
  1035. if (dts.TrySetValue(context, this, PythonType._pythonTypeType, value))
  1036. return;
  1037. }
  1038. if (IsSystemType) {
  1039. throw new MissingMemberException(String.Format("'{0}' object has no attribute '{1}'", Name, name));
  1040. }
  1041. PythonTypeSlot curSlot;
  1042. if (!(value is PythonTypeSlot) && _dict.TryGetValue(name, out curSlot) && curSlot is PythonTypeUserDescriptorSlot) {
  1043. ((PythonTypeUserDescriptorSlot)curSlot).Value = value;
  1044. } else {
  1045. AddSlot(name, ToTypeSlot(value));
  1046. UpdateVersion();
  1047. }
  1048. }
  1049. internal static PythonTypeSlot ToTypeSlot(object value) {
  1050. PythonTypeSlot pts = value as PythonTypeSlot;
  1051. if (pts != null) {
  1052. return pts;
  1053. }
  1054. // We could do more checks for things which aren't descriptors
  1055. if (value != null) {
  1056. return new PythonTypeUserDescriptorSlot(value);
  1057. }
  1058. return new PythonTypeUserDescriptorSlot(value, true);
  1059. }
  1060. internal bool DeleteCustomMember(CodeContext/*!*/ context, string name) {
  1061. Debug.Assert(context != null);
  1062. PythonTypeSlot dts;
  1063. if (TryResolveSlot(context, name, out dts)) {
  1064. if (dts.TryDeleteValue(context, null, this))
  1065. return true;
  1066. }
  1067. if (IsSystemType) {
  1068. throw new MissingMemberException(String.Format("can't delete attributes of built-in/extension type '{0}'", Name, name));
  1069. }
  1070. if (!_dict.Remove(name)) {
  1071. throw new MissingMemberException(String.Format(CultureInfo.CurrentCulture,
  1072. IronPython.Resources.MemberDoesNotExist,
  1073. name.ToString()));
  1074. }
  1075. // match CPython's buggy behavior, there's a test in test_class for this.
  1076. /*
  1077. if (name == "__new__") {
  1078. _objectNew = null;
  1079. ClearObjectNewInSubclasses(this);
  1080. } else if (name == "__init__") {
  1081. _objectInit = null;
  1082. ClearObjectInitInSubclasses(this);
  1083. }*/
  1084. UpdateVersion();
  1085. return true;
  1086. }
  1087. internal bool TryGetBoundCustomMember(CodeContext context, string name, out object value) {
  1088. PythonTypeSlot dts;
  1089. if (TryResolveSlot(context, name, out dts)) {
  1090. if (dts.TryGetValue(context, null, this, out value)) {
  1091. return true;
  1092. }
  1093. }
  1094. // search the type
  1095. PythonType myType = DynamicHelpers.GetPythonType(this);
  1096. if (myType.TryResolveSlot(context, name, out dts)) {
  1097. if (dts.TryGetValue(context, this, myType, out value)) {
  1098. return true;
  1099. }
  1100. }
  1101. value = null;
  1102. return false;
  1103. }
  1104. #region IFastGettable Members
  1105. T IFastGettable.MakeGetBinding<T>(CallSite<T> site, PythonGetMemberBinder/*!*/ binder, CodeContext context, string name) {
  1106. return (T)(object)new MetaPythonType.FastGetBinderHelper(this, context, binder).GetBinding();
  1107. }
  1108. #endregion
  1109. #endregion
  1110. #region Instance member access
  1111. internal object GetMember(CodeContext context, object instance, string name) {
  1112. object res;
  1113. if (TryGetMember(context, instance, name, out res)) {
  1114. return res;
  1115. }
  1116. throw new MissingMemberException(String.Format(CultureInfo.CurrentCulture,
  1117. IronPython.Resources.CantFindMember,
  1118. name));
  1119. }
  1120. internal void SetMember(CodeContext context, object instance, string name, object value) {
  1121. if (TrySetMember(context, instance, name, value)) {
  1122. return;
  1123. }
  1124. throw new MissingMemberException(
  1125. String.Format(CultureInfo.CurrentCulture,
  1126. IronPython.Resources.Slot_CantSet,
  1127. name));
  1128. }
  1129. internal void DeleteMember(CodeContext context, object instance, string name) {
  1130. if (TryDeleteMember(context, instance, name)) {
  1131. return;
  1132. }
  1133. throw new MissingMemberException(String.Format(CultureInfo.CurrentCulture, "couldn't delete member {0}", name));
  1134. }
  1135. /// <summary>
  1136. /// Gets a value from a dynamic type and any sub-types. Values are stored in slots (which serve as a level of
  1137. /// indirection). This searches the types resolution order and returns the first slot that
  1138. /// contains the value.
  1139. /// </summary>
  1140. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
  1141. internal bool TryGetMember(CodeContext context, object instance, string name, out object value) {
  1142. if (TryGetNonCustomMember(context, instance, name, out value)) {
  1143. return true;
  1144. }
  1145. try {
  1146. if (PythonTypeOps.TryInvokeBinaryOperator(context, instance, name, "__getattr__", out value)) {
  1147. return true;
  1148. }
  1149. } catch (MissingMemberException) {
  1150. //!!! when do we let the user see this exception?
  1151. ExceptionHelpers.DynamicStackFrames = null;
  1152. }
  1153. return false;
  1154. }
  1155. /// <summary>
  1156. /// Attempts to lookup a member w/o using the customizer. Equivelent to object.__getattribute__
  1157. /// but it doens't throw an exception.
  1158. /// </summary>
  1159. /// <returns></returns>
  1160. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
  1161. internal bool TryGetNonCustomMember(CodeContext context, object instance, string name, out object value) {
  1162. PythonType pt;
  1163. IPythonObject sdo;
  1164. bool hasValue = false;
  1165. value = null;
  1166. // first see if we have the value in the instance dictionary...
  1167. // TODO: Instance checks should also work on functions,
  1168. if ((pt = instance as PythonType) != null) {
  1169. PythonTypeSlot pts;
  1170. if (pt.TryLookupSlot(context, name, out pts)) {
  1171. hasValue = pts.TryGetValue(context, null, this, out value);
  1172. }
  1173. } else if ((sdo = instance as IPythonObject) != null) {
  1174. PythonDictionary dict = sdo.Dict;
  1175. hasValue = dict != null && dict.TryGetValue(name, out value);
  1176. }
  1177. // then check through all the descriptors. If we have a data
  1178. // descriptor it takes priority over the value we found in the
  1179. // dictionary. Otherwise only run a get descriptor if we don't
  1180. // already have a value.
  1181. for (int i = 0; i < _resolutionOrder.Count; i++) {
  1182. PythonType dt = _resolutionOrder[i];
  1183. PythonTypeSlot slot;
  1184. object newValue;
  1185. if (dt.TryLookupSlot(context, name, out slot)) {
  1186. if (!hasValue || slot.IsSetDescriptor(context, this)) {
  1187. if (slot.TryGetValue(context, instance, this, out newValue))
  1188. value = newValue;
  1189. return true;
  1190. }
  1191. }
  1192. }
  1193. return hasValue;
  1194. }
  1195. /// <summary>
  1196. /// Gets a value from a dynamic type and any sub-types. Values are stored in slots (which serve as a level of
  1197. /// indirection). This searches the types resolution order and returns the first slot that
  1198. /// contains the value.
  1199. /// </summary>
  1200. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
  1201. internal bool TryGetBoundMember(CodeContext context, object instance, string name, out object value) {
  1202. object getattr;
  1203. if (TryResolveNonObjectSlot(context, instance, "__getattribute__", out getattr)) {
  1204. value = InvokeGetAttributeMethod(context, name, getattr);
  1205. return true;
  1206. }
  1207. return TryGetNonCustomBoundMember(context, instance, name, out value);
  1208. }
  1209. private object InvokeGetAttributeMethod(CodeContext context, string name, object getattr) {
  1210. CallSite<Func<CallSite, CodeContext, object, string, object>> getAttributeSite;
  1211. if (IsSystemType) {
  1212. getAttributeSite = PythonContext.GetContext(context).GetSiteCacheForSystemType(UnderlyingSystemType).GetGetAttributeSite(context);
  1213. } else {
  1214. getAttributeSite = _siteCache.GetGetAttributeSite(context);
  1215. }
  1216. return getAttributeSite.Target(getAttributeSite, context, getattr, name);
  1217. }
  1218. /// <summary>
  1219. /// Attempts to lookup a member w/o using the customizer.
  1220. /// </summary>
  1221. /// <returns></returns>
  1222. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
  1223. internal bool TryGetNonCustomBoundMember(CodeContext context, object instance, string name, out object value) {
  1224. IPythonObject sdo = instance as IPythonObject;
  1225. if (sdo != null) {
  1226. PythonDictionary iac = sdo.Dict;
  1227. if (iac != null && iac.TryGetValue(name, out value)) {
  1228. return true;
  1229. }
  1230. }
  1231. if (TryResolveSlot(context, instance, name, out value)) {
  1232. return true;
  1233. }
  1234. try {
  1235. object getattr;
  1236. if (TryResolveNonObjectSlot(context, instance, "__getattr__", out getattr)) {
  1237. value = InvokeGetAttributeMethod(context, name, getattr);
  1238. return true;
  1239. }
  1240. } catch (MissingMemberException) {
  1241. //!!! when do we let the user see this exception?
  1242. ExceptionHelpers.DynamicStackFrames = null;
  1243. }
  1244. value = null;
  1245. return false;
  1246. }
  1247. private bool TryResolveSlot(CodeContext context, object instance, string name, out object value) {
  1248. for (int i = 0; i < _resolutionOrder.Count; i++) {
  1249. PythonType dt = _resolutionOrder[i];
  1250. PythonTypeSlot slot;
  1251. if (dt.TryLookupSlot(context, name, out slot)) {
  1252. if (slot.TryGetValue(context, instance, this, out value))
  1253. return true;
  1254. }
  1255. }
  1256. value = null;
  1257. return false;
  1258. }
  1259. private bool TryResolveNonObjectSlot(CodeContext context, object instance, string name, out object value) {
  1260. for (int i = 0; i < _resolutionOrder.Count; i++) {
  1261. PythonType dt = _resolutionOrder[i];
  1262. if (dt == TypeCache.Object) break;
  1263. PythonTypeSlot slot;
  1264. if (dt.TryLookupSlot(context, name, out slot)) {
  1265. if (slot.TryGetValue(context, instance, this, out value))
  1266. return true;
  1267. }
  1268. }
  1269. value = null;
  1270. return false;
  1271. }
  1272. /// <summary>
  1273. /// Sets a value on an instance. If a slot is available in the most derived type the slot
  1274. /// is set there, otherwise the value is stored directly in the instance.
  1275. /// </summary>
  1276. internal bool TrySetMember(CodeContext context, object instance, string name, object value) {
  1277. object setattr;
  1278. if (TryResolveNonObjectSlot(context, instance, "__setattr__", out setattr)) {
  1279. CallSite<Func<CallSite, CodeContext, object, object, string, object, object>> setAttrSite;
  1280. if (IsSystemType) {
  1281. setAttrSite = PythonContext.GetContext(context).GetSiteCacheForSystemType(UnderlyingSystemType).GetSetAttrSite(context);
  1282. } else {
  1283. setAttrSite = _siteCache.GetSetAttrSite(context);
  1284. }
  1285. setAttrSite.Target(setAttrSite, context, setattr, instance, name, value);
  1286. return true;
  1287. }
  1288. return TrySetNonCustomMember(context, instance, name, value);
  1289. }
  1290. /// <summary>
  1291. /// Attempst to set a value w/o going through the customizer.
  1292. ///
  1293. /// This enables languages to provide the "base" implementation for setting attributes
  1294. /// so that the customizer can call back here.
  1295. /// </summary>
  1296. internal bool TrySetNonCustomMember(CodeContext context, object instance, string name, object value) {
  1297. PythonTypeSlot slot;
  1298. if (TryResolveSlot(context, name, out slot)) {
  1299. if (slot.TrySetValue(context, instance, this, value)) {
  1300. return true;
  1301. }
  1302. }
  1303. // set the attribute on the instance
  1304. IPythonObject sdo = instance as IPythonObject;
  1305. if (sdo != null) {
  1306. PythonDictionary iac = sdo.Dict;
  1307. if (iac == null && sdo.PythonType.HasDictionary) {
  1308. iac = MakeDictionary();
  1309. if ((iac = sdo.SetDict(iac)) == null) {
  1310. return false;
  1311. }
  1312. }
  1313. iac[name] = value;
  1314. return true;
  1315. }
  1316. return false;
  1317. }
  1318. internal bool TryDeleteMember(CodeContext context, object instance, string name) {
  1319. try {
  1320. object delattr;
  1321. if (TryResolveNonObjectSlot(context, instance, "__delattr__", out delattr)) {
  1322. InvokeGetAttributeMethod(context, name, delattr);
  1323. return true;
  1324. }
  1325. } catch (MissingMemberException) {
  1326. //!!! when do we let the user see this exception?
  1327. ExceptionHelpers.DynamicStackFrames = null;
  1328. }
  1329. return TryDeleteNonCustomMember(context, instance, name);
  1330. }
  1331. internal bool TryDeleteNonCustomMember(CodeContext context, object instance, string name) {
  1332. PythonTypeSlot slot;
  1333. if (TryResolveSlot(context, name, out slot)) {
  1334. if (slot.TryDeleteValue(context, instance, this)) {
  1335. return true;
  1336. }
  1337. }
  1338. // set the attribute on the instance
  1339. IPythonObject sdo = instance as IPythonObject;
  1340. if (sdo != null) {
  1341. PythonDictionary dict = sdo.Dict;
  1342. if (dict == null && sdo.PythonType.HasDictionary) {
  1343. dict = MakeDictionary();
  1344. if ((dict = sdo.SetDict(dict)) == null) {
  1345. return false;
  1346. }
  1347. }
  1348. return dict.Remove(name);
  1349. }
  1350. return false;
  1351. }
  1352. #endregion
  1353. #region Member lists
  1354. /// <summary>
  1355. /// Returns a list of all slot names for the type and any subtypes.
  1356. /// </summary>
  1357. /// <param name="context">The context that is doing the inquiry of InvariantContext.Instance.</param>
  1358. internal List GetMemberNames(CodeContext context) {
  1359. return GetMemberNames(context, null);
  1360. }
  1361. /// <summary>
  1362. /// Returns a list of all slot names for the type, any subtypes, and the instance.
  1363. /// </summary>
  1364. /// <param name="context">The context that is doing the inquiry of InvariantContext.Instance.</param>
  1365. /// <param name="self">the instance to get instance members from, or null.</param>
  1366. internal List GetMemberNames(CodeContext context, object self) {
  1367. List res = TryGetCustomDir(context, self);
  1368. if (res != null) {
  1369. return res;
  1370. }
  1371. Dictionary<string, string> keys = new Dictionary<string, string>();
  1372. res = new List();
  1373. for (int i = 0; i < _resolutionOrder.Count; i++) {
  1374. PythonType dt = _resolutionOrder[i];
  1375. if (dt.IsSystemType) {
  1376. PythonBinder.GetBinder(context).ResolveMemberNames(context, dt, this, keys);
  1377. } else {
  1378. AddUserTypeMembers(context, keys, dt, res);
  1379. }
  1380. }
  1381. return AddInstanceMembers(self, keys, res);
  1382. }
  1383. private List TryGetCustomDir(CodeContext context, object self) {
  1384. if (self != null) {
  1385. object dir;
  1386. if (TryResolveNonObjectSlot(context, self, "__dir__", out dir)) {
  1387. CallSite<Func<CallSite, CodeContext, object, object>> dirSite;
  1388. if (IsSystemType) {
  1389. dirSite = PythonContext.GetContext(context).GetSiteCacheForSystemType(UnderlyingSystemType).GetDirSite(context);
  1390. } else {
  1391. dirSite = _siteCache.GetDirSite(context);
  1392. }
  1393. return new List(dirSite.Target(dirSite, context, dir));
  1394. }
  1395. }
  1396. return null;
  1397. }
  1398. /// <summary>
  1399. /// Adds members from a user defined type.
  1400. /// </summary>
  1401. private static void AddUserTypeMembers(CodeContext context, Dictionary<string, string> keys, PythonType dt, List res) {
  1402. if (dt.OldClass != null) {
  1403. foreach (KeyValuePair<object, object> kvp in dt.OldClass._dict) {
  1404. AddOneMember(keys, res, kvp.Key);
  1405. }
  1406. } else {
  1407. foreach (KeyValuePair<string, PythonTypeSlot> kvp in dt._dict) {
  1408. if (keys.ContainsKey(kvp.Key)) continue;
  1409. keys[kvp.Key] = kvp.Key;
  1410. }
  1411. }
  1412. }
  1413. private static void AddOneMember(Dictionary<string, string> keys, List res, object name) {
  1414. string strKey = name as string;
  1415. if (strKey != null) {
  1416. keys[strKey] = strKey;
  1417. } else {
  1418. res.Add(name);
  1419. }
  1420. }
  1421. /// <summary>
  1422. /// Adds members from a user defined type instance
  1423. /// </summary>
  1424. private static List AddInstanceMembers(object self, Dictionary<string, string> keys, List res) {
  1425. IPythonObject dyno = self as IPythonObject;
  1426. if (dyno != null) {
  1427. PythonDictionary dict = dyno.Dict;
  1428. if (dict != null) {
  1429. lock (dict) {
  1430. foreach (object name in dict.Keys) {
  1431. AddOneMember(keys, res, name);
  1432. }
  1433. }
  1434. }
  1435. }
  1436. List<string> strKeys = new List<string>(keys.Keys);
  1437. strKeys.Sort();
  1438. res.extend(strKeys);
  1439. return res;
  1440. }
  1441. internal PythonDictionary GetMemberDictionary(CodeContext context) {
  1442. return GetMemberDictionary(context, true);
  1443. }
  1444. internal PythonDictionary GetMemberDictionary(CodeContext context, bool excludeDict) {
  1445. PythonDictionary dict = PythonDictionary.MakeSymbolDictionary();
  1446. if (IsSystemType) {
  1447. PythonBinder.GetBinder(context).LookupMembers(context, this, dict);
  1448. } else {
  1449. foreach (string x in _dict.Keys) {
  1450. if (excludeDict && x.ToString() == "__dict__") {
  1451. continue;
  1452. }
  1453. PythonTypeSlot dts;
  1454. if (TryLookupSlot(context, x, out dts)) {
  1455. //??? why check for DTVS?
  1456. object val;
  1457. if (dts.TryGetValue(context, null, this, out val)) {
  1458. if (dts is PythonTypeUserDescriptorSlot) {
  1459. dict[x] = val;
  1460. } else {
  1461. dict[x] = dts;
  1462. }
  1463. }
  1464. }
  1465. }
  1466. }
  1467. return dict;
  1468. }
  1469. #endregion
  1470. #region User type initialization
  1471. private void InitializeUserType(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary vars, string selfNames) {
  1472. // we don't support overriding __mro__
  1473. if (vars.ContainsKey("__mro__"))
  1474. throw new NotImplementedException("Overriding __mro__ of built-in types is not implemented");
  1475. // cannot override mro when inheriting from type
  1476. if (vars.ContainsKey("mro")) {
  1477. foreach (object o in bases) {
  1478. PythonType dt = o as PythonType;
  1479. if (dt != null && dt.IsSubclassOf(TypeCache.PythonType)) {
  1480. throw new NotImplementedException("Overriding type.mro is not implemented");
  1481. }
  1482. }
  1483. }
  1484. bases = ValidateBases(bases);
  1485. _name = name;
  1486. _bases = GetBasesAsList(bases).ToArray();
  1487. _pythonContext = PythonContext.GetContext(context);
  1488. _resolutionOrder = CalculateMro(this, _bases);
  1489. bool hasSlots = false;
  1490. foreach (PythonType pt in _bases) {
  1491. // if we directly inherit from 2 types with slots then the indexes would
  1492. // conflict so inheritance isn't allowed.
  1493. int slotCount = pt.GetUsedSlotCount();
  1494. if (slotCount != 0) {
  1495. if (hasSlots) {
  1496. throw PythonOps.TypeError("multiple bases have instance lay-out conflict");
  1497. }
  1498. hasSlots = true;
  1499. }
  1500. pt.AddSubType(this);
  1501. }
  1502. HashSet<string> optimizedInstanceNames = null;
  1503. foreach (PythonType pt in _resolutionOrder) {
  1504. // we need to calculate the number of slots from resolution
  1505. // order to deal with multiple bases having __slots__ that
  1506. // directly inherit from each other.
  1507. _originalSlotCount += pt.GetUsedSlotCount();
  1508. if (pt._optimizedInstanceNames != null) {
  1509. if (optimizedInstanceNames == null) {
  1510. optimizedInstanceNames = new HashSet<string>();
  1511. }
  1512. optimizedInstanceNames.UnionWith(pt._optimizedInstanceNames);
  1513. }
  1514. }
  1515. if (!String.IsNullOrEmpty(selfNames)) {
  1516. if (optimizedInstanceNames == null) {
  1517. optimizedInstanceNames = new HashSet<string>();
  1518. }
  1519. optimizedInstanceNames.UnionWith(selfNames.Split(','));
  1520. }
  1521. if (optimizedInstanceNames != null) {
  1522. _optimizedInstanceVersion = CustomInstanceDictionaryStorage.AllocateVersion();
  1523. _optimizedInstanceNames = new List<string>(optimizedInstanceNames).ToArray();
  1524. }
  1525. EnsureDict();
  1526. PopulateDictionary(context, name, bases, vars);
  1527. // calculate the .NET type once so it can be used for things like super calls
  1528. _underlyingSystemType = NewTypeMaker.GetNewType(name, bases);
  1529. // then let the user intercept and rewrite the type - the user can't create
  1530. // instances of this type yet.
  1531. _underlyingSystemType = __clrtype__();
  1532. if (_underlyingSystemType == null) {
  1533. throw PythonOps.ValueError("__clrtype__ must return a type, not None");
  1534. }
  1535. // finally assign the ctors from the real type the user provided
  1536. lock (_userTypeCtors) {
  1537. if (!_userTypeCtors.TryGetValue(_underlyingSystemType, out _ctor)) {
  1538. ConstructorInfo[] ctors = _underlyingSystemType.GetConstructors();
  1539. bool isPythonType = false;
  1540. foreach (ConstructorInfo ci in ctors) {
  1541. ParameterInfo[] pis = ci.GetParameters();
  1542. if((pis.Length > 1 && pis[0].ParameterType == typeof(CodeContext) && pis[1].ParameterType == typeof(PythonType)) ||
  1543. (pis.Length > 0 && pis[0].ParameterType == typeof(PythonType))) {
  1544. isPythonType = true;
  1545. break;
  1546. }
  1547. }
  1548. _ctor = BuiltinFunction.MakeFunction(Name, ctors, _underlyingSystemType);
  1549. if (isPythonType) {
  1550. _userTypeCtors[_underlyingSystemType] = _ctor;
  1551. } else {
  1552. // __clrtype__ returned a type w/o any PythonType parameters, force this to
  1553. // be created like a normal .NET type. Presumably the user is planning on storing
  1554. // the Python type in a static field or something and passing the Type object to
  1555. // some .NET API which wants to Activator.CreateInstance on it w/o providing a
  1556. // PythonType object.
  1557. _instanceCtor = new SystemInstanceCreator(this);
  1558. _attrs |= PythonTypeAttributes.SystemCtor;
  1559. }
  1560. }
  1561. }
  1562. UpdateObjectNewAndInit(context);
  1563. }
  1564. internal PythonDictionary MakeDictionary() {
  1565. if (_optimizedInstanceNames != null) {
  1566. return new PythonDictionary(new CustomInstanceDictionaryStorage(_optimizedInstanceNames, _optimizedInstanceVersion));
  1567. }
  1568. return PythonDictionary.MakeSymbolDictionary();
  1569. }
  1570. internal IList<string> GetOptimizedInstanceNames() {
  1571. return _optimizedInstanceNames;
  1572. }
  1573. internal int GetOptimizedInstanceVersion() {
  1574. return _optimizedInstanceVersion;
  1575. }
  1576. internal IList<string> GetTypeSlots() {
  1577. PythonTypeSlot pts;
  1578. if(_dict != null && _dict.TryGetValue("__slots__", out pts) && pts is PythonTypeUserDescriptorSlot) {
  1579. return SlotsToList(((PythonTypeUserDescriptorSlot)pts).Value);
  1580. }
  1581. return ArrayUtils.EmptyStrings;
  1582. }
  1583. internal static List<string> GetSlots(PythonDictionary dict) {
  1584. List<string> res = null;
  1585. object slots;
  1586. if (dict != null && dict.TryGetValue("__slots__", out slots)) {
  1587. res = SlotsToList(slots);
  1588. }
  1589. return res;
  1590. }
  1591. internal static List<string> SlotsToList(object slots) {
  1592. List<string> res = new List<string>();
  1593. IList<object> seq = slots as IList<object>;
  1594. if (seq != null) {
  1595. res = new List<string>(seq.Count);
  1596. for (int i = 0; i < seq.Count; i++) {
  1597. res.Add(GetSlotName(seq[i]));
  1598. }
  1599. res.Sort();
  1600. } else {
  1601. res = new List<string>(1);
  1602. res.Add(GetSlotName(slots));
  1603. }
  1604. return res;
  1605. }
  1606. internal bool HasObjectNew(CodeContext context) {
  1607. if (!_objectNew.HasValue) {
  1608. UpdateObjectNewAndInit(context);
  1609. }
  1610. Debug.Assert(_objectNew.HasValue);
  1611. return _objectNew.Value;
  1612. }
  1613. internal bool HasObjectInit(CodeContext context) {
  1614. if (!_objectInit.HasValue) {
  1615. UpdateObjectNewAndInit(context);
  1616. }
  1617. Debug.Assert(_objectInit.HasValue);
  1618. return _objectInit.Value;
  1619. }
  1620. private void UpdateObjectNewAndInit(CodeContext context) {
  1621. PythonTypeSlot slot;
  1622. object funcObj;
  1623. foreach (PythonType pt in _bases) {
  1624. if (pt == TypeCache.Object) {
  1625. continue;
  1626. }
  1627. if (pt._objectNew == null || pt._objectInit == null) {
  1628. pt.UpdateObjectNewAndInit(context);
  1629. }
  1630. Debug.Assert(pt._objectInit != null && pt._objectNew != null);
  1631. if (!pt._objectNew.Value) {
  1632. _objectNew = false;
  1633. }
  1634. if (!pt._objectInit.Value) {
  1635. _objectInit = false;
  1636. }
  1637. }
  1638. if (_objectInit == null) {
  1639. _objectInit = TryResolveSlot(context, "__init__", out slot) && slot.TryGetValue(context, null, this, out funcObj) && funcObj == InstanceOps.Init;
  1640. }
  1641. if (_objectNew == null) {
  1642. _objectNew = TryResolveSlot(context, "__new__", out slot) && slot.TryGetValue(context, null, this, out funcObj) && funcObj == InstanceOps.New;
  1643. }
  1644. }
  1645. private static string GetSlotName(object o) {
  1646. string value;
  1647. if (!Converter.TryConvertToString(o, out value) || String.IsNullOrEmpty(value))
  1648. throw PythonOps.TypeError("slots must be one string or a list of strings");
  1649. for (int i = 0; i < value.Length; i++) {
  1650. if ((value[i] >= 'a' && value[i] <= 'z') ||
  1651. (value[i] >= 'A' && value[i] <= 'Z') ||
  1652. (i != 0 && value[i] >= '0' && value[i] <= '9') ||
  1653. value[i] == '_') {
  1654. continue;
  1655. }
  1656. throw PythonOps.TypeError("__slots__ must be valid identifiers");
  1657. }
  1658. return value;
  1659. }
  1660. private int GetUsedSlotCount() {
  1661. int slotCount = 0;
  1662. if (_slots != null) {
  1663. slotCount = _slots.Length;
  1664. if (Array.IndexOf(_slots, "__weakref__") != -1) {
  1665. slotCount--;
  1666. }
  1667. if (Array.IndexOf(_slots, "__dict__") != -1) {
  1668. slotCount--;
  1669. }
  1670. }
  1671. return slotCount;
  1672. }
  1673. private void PopulateDictionary(CodeContext/*!*/ context, string name, PythonTuple bases, PythonDictionary vars) {
  1674. PopulateSlot("__doc__", null);
  1675. List<string> slots = GetSlots(vars);
  1676. if (slots != null) {
  1677. _slots = slots.ToArray();
  1678. int index = _originalSlotCount;
  1679. string typeName = IronPython.Compiler.Parser.GetPrivatePrefix(name);
  1680. for (int i = 0; i < slots.Count; i++) {
  1681. string slotName = slots[i];
  1682. if (slotName.StartsWith("__") && !slotName.EndsWith("__")) {
  1683. slotName = "_" + typeName + slotName;
  1684. }
  1685. AddSlot(slotName, new ReflectedSlotProperty(slotName, name, i + index));
  1686. }
  1687. _originalSlotCount += slots.Count;
  1688. }
  1689. // check the slots to see if we're weak refable
  1690. if (CheckForSlotWithDefault(context, bases, slots, "__weakref__")) {
  1691. _attrs |= PythonTypeAttributes.WeakReferencable;
  1692. AddSlot("__weakref__", new PythonTypeWeakRefSlot(this));
  1693. }
  1694. if (CheckForSlotWithDefault(context, bases, slots, "__dict__")) {
  1695. _attrs |= PythonTypeAttributes.HasDictionary;
  1696. PythonTypeSlot pts;
  1697. bool inheritsDict = false;
  1698. for(int i = 1; i<_resolutionOrder.Count; i++) {
  1699. PythonType pt = _resolutionOrder[i];
  1700. if (pt.TryResolveSlot(context, "__dict__", out pts)) {
  1701. inheritsDict = true;
  1702. break;
  1703. }
  1704. }
  1705. if (!inheritsDict) {
  1706. AddSlot("__dict__", new PythonTypeDictSlot(this));
  1707. }
  1708. }
  1709. object modName;
  1710. if (context.TryGetVariable("__name__", out modName)) {
  1711. PopulateSlot("__module__", modName);
  1712. }
  1713. foreach (var kvp in vars) {
  1714. if (kvp.Key is string) {
  1715. PopulateSlot((string)kvp.Key, kvp.Value);
  1716. }
  1717. }
  1718. PythonTypeSlot val;
  1719. if (_dict.TryGetValue("__new__", out val) && val is PythonFunction) {
  1720. AddSlot("__new__", new staticmethod(val));
  1721. }
  1722. }
  1723. private static bool CheckForSlotWithDefault(CodeContext context, PythonTuple bases, List<string> slots, string name) {
  1724. bool hasSlot = true;
  1725. if (slots != null && !slots.Contains(name)) {
  1726. hasSlot = false;
  1727. foreach (object pt in bases) {
  1728. PythonType dt = pt as PythonType;
  1729. PythonTypeSlot dummy;
  1730. if (dt != null && dt.TryLookupSlot(context, name, out dummy)) {
  1731. hasSlot = true;
  1732. }
  1733. }
  1734. } else if (slots != null) {
  1735. // check and see if we have 2
  1736. if(bases.Count > 0) {
  1737. PythonType dt = bases[0] as PythonType;
  1738. PythonTypeSlot dummy;
  1739. if (dt != null && dt.TryLookupSlot(context, name, out dummy)) {
  1740. throw PythonOps.TypeError(name + " slot disallowed: we already got one");
  1741. }
  1742. }
  1743. }
  1744. return hasSlot;
  1745. }
  1746. /// <summary>
  1747. /// Gets the .NET type which is used for instances of the Python type.
  1748. ///
  1749. /// When overridden by a metaclass enables a customization of the .NET type which
  1750. /// is used for instances of the Python type. Meta-classes can construct custom
  1751. /// types at runtime which include new .NET methods, fields, custom attributes or
  1752. /// other features to better interoperate with .NET.
  1753. /// </summary>
  1754. [PythonHidden]
  1755. public virtual Type __clrtype__() {
  1756. return _underlyingSystemType;
  1757. }
  1758. private void PopulateSlot(string key, object value) {
  1759. AddSlot(key, ToTypeSlot(value));
  1760. }
  1761. private static List<PythonType> GetBasesAsList(PythonTuple bases) {
  1762. List<PythonType> newbs = new List<PythonType>();
  1763. foreach (object typeObj in bases) {
  1764. PythonType dt = typeObj as PythonType;
  1765. if (dt == null) {
  1766. dt = ((OldClass)typeObj).TypeObject;
  1767. }
  1768. newbs.Add(dt);
  1769. }
  1770. return newbs;
  1771. }
  1772. private static PythonTuple ValidateBases(PythonTuple bases) {
  1773. PythonTuple newBases = PythonTypeOps.EnsureBaseType(bases);
  1774. for (int i = 0; i < newBases.__len__(); i++) {
  1775. for (int j = 0; j < newBases.__len__(); j++) {
  1776. if (i != j && newBases[i] == newBases[j]) {
  1777. OldClass oc = newBases[i] as OldClass;
  1778. if (oc != null) {
  1779. throw PythonOps.TypeError("duplicate base class {0}", oc.Name);
  1780. } else {
  1781. throw PythonOps.TypeError("duplicate base class {0}", ((PythonType)newBases[i]).Name);
  1782. }
  1783. }
  1784. }
  1785. }
  1786. return newBases;
  1787. }
  1788. private static void EnsureModule(CodeContext context, PythonDictionary dict) {
  1789. if (!dict.ContainsKey("__module__")) {
  1790. object modName;
  1791. if (context.TryGetVariable("__name__", out modName)) {
  1792. dict["__module__"] = modName;
  1793. }
  1794. }
  1795. }
  1796. #endregion
  1797. #region System type initialization
  1798. /// <summary>
  1799. /// Initializes a PythonType that represents a standard .NET type. The same .NET type
  1800. /// can be shared with the Python type system. For example object, string, int,
  1801. /// etc... are all the same types.
  1802. /// </summary>
  1803. private void InitializeSystemType() {
  1804. IsSystemType = true;
  1805. IsPythonType = PythonBinder.IsPythonType(_underlyingSystemType);
  1806. _name = NameConverter.GetTypeName(_underlyingSystemType);
  1807. AddSystemBases();
  1808. }
  1809. private void AddSystemBases() {
  1810. List<PythonType> mro = new List<PythonType>();
  1811. mro.Add(this);
  1812. if (_underlyingSystemType.BaseType != null) {
  1813. Type baseType;
  1814. if (_underlyingSystemType == typeof(bool)) {
  1815. // bool inherits from int in python
  1816. baseType = typeof(int);
  1817. } else if (_underlyingSystemType.BaseType == typeof(ValueType)) {
  1818. // hide ValueType, it doesn't exist in Python
  1819. baseType = typeof(object);
  1820. } else {
  1821. baseType = _underlyingSystemType.BaseType;
  1822. }
  1823. _bases = new PythonType[] { GetPythonType(baseType) };
  1824. Type curType = baseType;
  1825. while (curType != null) {
  1826. Type newType;
  1827. if (TryReplaceExtensibleWithBase(curType, out newType)) {
  1828. mro.Add(DynamicHelpers.GetPythonTypeFromType(newType));
  1829. } else {
  1830. mro.Add(DynamicHelpers.GetPythonTypeFromType(curType));
  1831. }
  1832. curType = curType.BaseType;
  1833. }
  1834. if (!IsPythonType) {
  1835. AddSystemInterfaces(mro);
  1836. }
  1837. } else if (_underlyingSystemType.IsInterface) {
  1838. // add interfaces to MRO & create bases list
  1839. Type[] interfaces = _underlyingSystemType.GetInterfaces();
  1840. PythonType[] bases = new PythonType[interfaces.Length];
  1841. for (int i = 0; i < interfaces.Length; i++) {
  1842. Type iface = interfaces[i];
  1843. PythonType it = DynamicHelpers.GetPythonTypeFromType(iface);
  1844. mro.Add(it);
  1845. bases[i] = it;
  1846. }
  1847. _bases = bases;
  1848. } else {
  1849. _bases = new PythonType[0];
  1850. }
  1851. _resolutionOrder = mro;
  1852. }
  1853. private void AddSystemInterfaces(List<PythonType> mro) {
  1854. if (_underlyingSystemType.IsArray) {
  1855. // include the standard array interfaces in the array MRO. We pick the
  1856. // non-strongly typed versions which are also in Array.__mro__
  1857. mro.Add(DynamicHelpers.GetPythonTypeFromType(typeof(IList)));
  1858. mro.Add(DynamicHelpers.GetPythonTypeFromType(typeof(ICollection)));
  1859. mro.Add(DynamicHelpers.GetPythonTypeFromType(typeof(IEnumerable)));
  1860. return;
  1861. }
  1862. Type[] interfaces = _underlyingSystemType.GetInterfaces();
  1863. Dictionary<string, Type> methodMap = new Dictionary<string, Type>();
  1864. bool hasExplicitIface = false;
  1865. List<Type> nonCollidingInterfaces = new List<Type>(interfaces);
  1866. foreach (Type iface in interfaces) {
  1867. InterfaceMapping mapping = _underlyingSystemType.GetInterfaceMap(iface);
  1868. // grab all the interface methods which would hide other members
  1869. for (int i = 0; i < mapping.TargetMethods.Length; i++) {
  1870. MethodInfo target = mapping.TargetMethods[i];
  1871. if (target == null) {
  1872. continue;
  1873. }
  1874. if (!target.IsPrivate) {
  1875. methodMap[target.Name] = null;
  1876. } else {
  1877. hasExplicitIface = true;
  1878. }
  1879. }
  1880. if (hasExplicitIface) {
  1881. for (int i = 0; i < mapping.TargetMethods.Length; i++) {
  1882. MethodInfo target = mapping.TargetMethods[i];
  1883. MethodInfo iTarget = mapping.InterfaceMethods[i];
  1884. // any methods which aren't explicit are picked up at the appropriate
  1885. // time earlier in the MRO so they can be ignored
  1886. if (target != null && target.IsPrivate) {
  1887. hasExplicitIface = true;
  1888. Type existing;
  1889. if (methodMap.TryGetValue(iTarget.Name, out existing)) {
  1890. if (existing != null) {
  1891. // collision, multiple interfaces implement the same name, and
  1892. // we're not hidden by another method. remove both interfaces,
  1893. // but leave so future interfaces get removed
  1894. nonCollidingInterfaces.Remove(iface);
  1895. nonCollidingInterfaces.Remove(methodMap[iTarget.Name]);
  1896. break;
  1897. }
  1898. } else {
  1899. // no collisions so far...
  1900. methodMap[iTarget.Name] = iface;
  1901. }
  1902. }
  1903. }
  1904. }
  1905. }
  1906. if (hasExplicitIface) {
  1907. // add any non-colliding interfaces into the MRO
  1908. foreach (Type t in nonCollidingInterfaces) {
  1909. Debug.Assert(t.IsInterface);
  1910. mro.Add(DynamicHelpers.GetPythonTypeFromType(t));
  1911. }
  1912. }
  1913. }
  1914. /// <summary>
  1915. /// Creates a __new__ method for the type. If the type defines interesting constructors
  1916. /// then the __new__ method will call that. Otherwise if it has only a single argless
  1917. /// </summary>
  1918. private void AddSystemConstructors() {
  1919. if (typeof(Delegate).IsAssignableFrom(_underlyingSystemType)) {
  1920. SetConstructor(
  1921. BuiltinFunction.MakeFunction(
  1922. _underlyingSystemType.Name,
  1923. new[] { typeof(DelegateOps).GetMethod("__new__") },
  1924. _underlyingSystemType
  1925. )
  1926. );
  1927. } else if (!_underlyingSystemType.IsAbstract) {
  1928. BuiltinFunction reflectedCtors = GetConstructors();
  1929. if (reflectedCtors == null) {
  1930. return; // no ctors, no __new__
  1931. }
  1932. SetConstructor(reflectedCtors);
  1933. }
  1934. }
  1935. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
  1936. private BuiltinFunction GetConstructors() {
  1937. Type type = _underlyingSystemType;
  1938. string name = Name;
  1939. return PythonTypeOps.GetConstructorFunction(type, name);
  1940. }
  1941. private void EnsureConstructor() {
  1942. if (_ctor == null) {
  1943. AddSystemConstructors();
  1944. if (_ctor == null) {
  1945. throw PythonOps.TypeError(_underlyingSystemType.FullName + " does not define any public constructors.");
  1946. }
  1947. }
  1948. }
  1949. private void EnsureInstanceCtor() {
  1950. if (_instanceCtor == null) {
  1951. _instanceCtor = InstanceCreator.Make(this);
  1952. }
  1953. }
  1954. #endregion
  1955. #region Private implementation details
  1956. private void UpdateVersion() {
  1957. foreach (WeakReference wr in SubTypes) {
  1958. if (wr.IsAlive) {
  1959. ((PythonType)wr.Target).UpdateVersion();
  1960. }
  1961. }
  1962. _lenSlot = null;
  1963. _version = GetNextVersion();
  1964. }
  1965. /// <summary>
  1966. /// This will return a unique integer for every version of every type in the system.
  1967. /// This means that DynamicSite code can generate a check to see if it has the correct
  1968. /// PythonType and version with a single integer compare.
  1969. ///
  1970. /// TODO - This method and related code should fail gracefully on overflow.
  1971. /// </summary>
  1972. private static int GetNextVersion() {
  1973. if (MasterVersion < 0) {
  1974. throw new InvalidOperationException(IronPython.Resources.TooManyVersions);
  1975. }
  1976. return Interlocked.Increment(ref MasterVersion);
  1977. }
  1978. private void EnsureDict() {
  1979. if (_dict == null) {
  1980. Interlocked.CompareExchange<Dictionary<string, PythonTypeSlot>>(
  1981. ref _dict,
  1982. new Dictionary<string, PythonTypeSlot>(StringComparer.Ordinal),
  1983. null);
  1984. }
  1985. }
  1986. /// <summary>
  1987. /// Internal helper function to add a subtype
  1988. /// </summary>
  1989. private void AddSubType(PythonType subtype) {
  1990. if (_subtypes == null) {
  1991. Interlocked.CompareExchange<List<WeakReference>>(ref _subtypes, new List<WeakReference>(), null);
  1992. }
  1993. lock (_subtypesLock) {
  1994. _subtypes.Add(new WeakReference(subtype));
  1995. }
  1996. }
  1997. private void RemoveSubType(PythonType subtype) {
  1998. int i = 0;
  1999. if (_subtypes != null) {
  2000. lock (_subtypesLock) {
  2001. while (i < _subtypes.Count) {
  2002. if (!_subtypes[i].IsAlive || _subtypes[i].Target == subtype) {
  2003. _subtypes.RemoveAt(i);
  2004. continue;
  2005. }
  2006. i++;
  2007. }
  2008. }
  2009. }
  2010. }
  2011. /// <summary>
  2012. /// Gets a list of weak references to all the subtypes of this class. May return null
  2013. /// if there are no subtypes of the class.
  2014. /// </summary>
  2015. private IList<WeakReference> SubTypes {
  2016. get {
  2017. if (_subtypes == null) return _emptyWeakRef;
  2018. lock (_subtypesLock) {
  2019. return _subtypes.ToArray();
  2020. }
  2021. }
  2022. }
  2023. [Flags]
  2024. private enum PythonTypeAttributes {
  2025. None = 0x00,
  2026. Immutable = 0x01,
  2027. SystemType = 0x02,
  2028. IsPythonType = 0x04,
  2029. WeakReferencable = 0x08,
  2030. HasDictionary = 0x10,
  2031. /// <summary>
  2032. /// The type has a ctor which does not accept PythonTypes. This is used
  2033. /// for user defined types which implement __clrtype__
  2034. /// </summary>
  2035. SystemCtor = 0x20
  2036. }
  2037. #endregion
  2038. #region IMembersList Members
  2039. IList<string> IMembersList.GetMemberNames() {
  2040. return PythonOps.GetStringMemberList(this);
  2041. }
  2042. IList<object> IPythonMembersList.GetMemberNames(CodeContext/*!*/ context) {
  2043. IList<object> res = GetMemberNames(context);
  2044. object[] arr = new object[res.Count];
  2045. res.CopyTo(arr, 0);
  2046. Array.Sort(arr);
  2047. return arr;
  2048. }
  2049. #endregion
  2050. #region IWeakReferenceable Members
  2051. WeakRefTracker IWeakReferenceable.GetWeakRef() {
  2052. return _weakrefTracker;
  2053. }
  2054. bool IWeakReferenceable.SetWeakRef(WeakRefTracker value) {
  2055. return Interlocked.CompareExchange<WeakRefTracker>(ref _weakrefTracker, value, null) == null;
  2056. }
  2057. void IWeakReferenceable.SetFinalizer(WeakRefTracker value) {
  2058. _weakrefTracker = value;
  2059. }
  2060. #endregion
  2061. #region IDynamicMetaObjectProvider Members
  2062. [PythonHidden]
  2063. public DynamicMetaObject/*!*/ GetMetaObject(Expression/*!*/ parameter) {
  2064. return new Binding.MetaPythonType(parameter, BindingRestrictions.Empty, this);
  2065. }
  2066. #endregion
  2067. /// <summary>
  2068. /// Returns a CLR WeakReference object to this PythonType that can be shared
  2069. /// between anyone who needs a weak reference to the type.
  2070. /// </summary>
  2071. internal WeakReference/*!*/ GetSharedWeakReference() {
  2072. if (_weakRef == null) {
  2073. _weakRef = new WeakReference(this);
  2074. }
  2075. return _weakRef;
  2076. }
  2077. #region IFastSettable Members
  2078. T IFastSettable.MakeSetBinding<T>(CallSite<T> site, PythonSetMemberBinder binder) {
  2079. PythonTypeSlot pts;
  2080. // if our meta class has a custom __setattr__ then we don't handle it in the
  2081. // fast path. Usually this is handled by a user defined type (UserTypeOps) IDynamicMetaObjectProvider
  2082. // class. But w/ _ctypes support we can have a built-in meta class which doesn't
  2083. // get this treatment.
  2084. if (!IsSystemType && !TryGetCustomSetAttr(Context.SharedContext, out pts)) {
  2085. CodeContext context = PythonContext.GetPythonContext(binder).SharedContext;
  2086. string name = binder.Name;
  2087. // optimized versions for possible literals that can show up in code.
  2088. Type setType = typeof(T);
  2089. if (setType == typeof(Func<CallSite, object, object, object>)) {
  2090. return (T)(object)MakeFastSet<object>(context, name);
  2091. } else if (setType == typeof(Func<CallSite, object, string, object>)) {
  2092. return (T)(object)MakeFastSet<string>(context, name);
  2093. } else if (setType == typeof(Func<CallSite, object, int, object>)) {
  2094. return (T)(object)MakeFastSet<int>(context, name);
  2095. } else if (setType == typeof(Func<CallSite, object, double, object>)) {
  2096. return (T)(object)MakeFastSet<double>(context, name);
  2097. } else if (setType == typeof(Func<CallSite, object, List, object>)) {
  2098. return (T)(object)MakeFastSet<List>(context, name);
  2099. } else if (setType == typeof(Func<CallSite, object, PythonTuple, object>)) {
  2100. return (T)(object)MakeFastSet<PythonTuple>(context, name);
  2101. } else if (setType == typeof(Func<CallSite, object, PythonDictionary, object>)) {
  2102. return (T)(object)MakeFastSet<PythonDictionary>(context, name);
  2103. }
  2104. }
  2105. return null;
  2106. }
  2107. private static Func<CallSite, object, T, object> MakeFastSet<T>(CodeContext/*!*/ context, string name) {
  2108. return new Setter<T>(context, name).Target;
  2109. }
  2110. class Setter<T> : FastSetBase<T> {
  2111. private readonly CodeContext/*!*/ _context;
  2112. private readonly string _name;
  2113. public Setter(CodeContext/*!*/ context, string name)
  2114. : base(-1) {
  2115. _context = context;
  2116. _name = name;
  2117. }
  2118. public object Target(CallSite site, object self, T value) {
  2119. PythonType type = self as PythonType;
  2120. if (type != null && !type.IsSystemType) {
  2121. type.SetCustomMember(_context, _name, value);
  2122. return value;
  2123. }
  2124. return Update(site, self, value);
  2125. }
  2126. }
  2127. #endregion
  2128. internal class DebugProxy {
  2129. private readonly PythonType _type;
  2130. public DebugProxy(PythonType type) {
  2131. _type = type;
  2132. }
  2133. public PythonType[] __bases__ {
  2134. get {
  2135. return ArrayUtils.ToArray(_type.BaseTypes);
  2136. }
  2137. }
  2138. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  2139. public List<ObjectDebugView> Members {
  2140. get {
  2141. var res = new List<ObjectDebugView>();
  2142. if (_type._dict != null) {
  2143. foreach (var v in _type._dict) {
  2144. if (v.Value is PythonTypeUserDescriptorSlot) {
  2145. res.Add(new ObjectDebugView(v.Key, ((PythonTypeUserDescriptorSlot)v.Value).Value));
  2146. } else {
  2147. res.Add(new ObjectDebugView(v.Key, v.Value));
  2148. }
  2149. }
  2150. }
  2151. return res;
  2152. }
  2153. }
  2154. }
  2155. }
  2156. enum OptimizedGetKind {
  2157. None,
  2158. SlotDict,
  2159. SlotOnly,
  2160. PropertySlot,
  2161. UserSlotDict,
  2162. UserSlotOnly,
  2163. }
  2164. class UserGetBase : FastGetBase {
  2165. internal readonly int _version;
  2166. public UserGetBase(PythonGetMemberBinder binder, int version) {
  2167. _version = version;
  2168. }
  2169. public override bool IsValid(PythonType type) {
  2170. return _version == type.Version;
  2171. }
  2172. }
  2173. class ChainedUserGet : UserGetBase {
  2174. public ChainedUserGet(PythonGetMemberBinder binder, int version, Func<CallSite, object, CodeContext, object> func)
  2175. : base(binder, version) {
  2176. _func = func;
  2177. }
  2178. internal override bool ShouldCache {
  2179. get {
  2180. return false;
  2181. }
  2182. }
  2183. }
  2184. class GetAttributeDelegates : UserGetBase {
  2185. private readonly string _name;
  2186. private readonly PythonTypeSlot _getAttributeSlot;
  2187. private readonly PythonTypeSlot _getAttrSlot;
  2188. private readonly SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, string, object>>>/*!*/ _storage;
  2189. private readonly bool _isNoThrow;
  2190. public GetAttributeDelegates(PythonGetMemberBinder/*!*/ binder, string/*!*/ name, int version, PythonTypeSlot/*!*/ getAttributeSlot, PythonTypeSlot/*!*/ getAttrSlot)
  2191. : base(binder, version) {
  2192. Assert.NotNull(binder, getAttributeSlot);
  2193. _storage = new SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object, string, object>>>();
  2194. _getAttributeSlot = getAttributeSlot;
  2195. _getAttrSlot = getAttrSlot;
  2196. _name = name;
  2197. _func = GetAttribute;
  2198. _isNoThrow = binder.IsNoThrow;
  2199. }
  2200. public object GetAttribute(CallSite site, object self, CodeContext context) {
  2201. IPythonObject ipo = self as IPythonObject;
  2202. if (ipo != null && ipo.PythonType.Version == _version) {
  2203. if (_isNoThrow) {
  2204. return UserTypeOps.GetAttributeNoThrow(context, self, _name, _getAttributeSlot, _getAttrSlot, _storage);
  2205. }
  2206. return UserTypeOps.GetAttribute(context, self, _name, _getAttributeSlot, _getAttrSlot, _storage);
  2207. }
  2208. return Update(site, self, context);
  2209. }
  2210. }
  2211. class GetMemberDelegates : UserGetBase {
  2212. private readonly string _name;
  2213. private readonly bool _isNoThrow;
  2214. private readonly PythonTypeSlot _slot, _getattrSlot;
  2215. private readonly SlotGetValue _slotFunc;
  2216. private readonly Func<CallSite, object, CodeContext, object> _fallback;
  2217. private readonly int _dictVersion, _dictIndex;
  2218. public GetMemberDelegates(OptimizedGetKind getKind, PythonType type, PythonGetMemberBinder binder, string name, int version, PythonTypeSlot slot, PythonTypeSlot getattrSlot, SlotGetValue slotFunc, Func<CallSite, object, CodeContext, object> fallback)
  2219. : base(binder, version) {
  2220. _slot = slot;
  2221. _name = name;
  2222. _getattrSlot = getattrSlot;
  2223. _slotFunc = slotFunc;
  2224. _fallback = fallback;
  2225. _isNoThrow = binder.IsNoThrow;
  2226. var optNames = type.GetOptimizedInstanceNames();
  2227. switch (getKind) {
  2228. case OptimizedGetKind.SlotDict:
  2229. if (optNames != null) {
  2230. _dictIndex = optNames.IndexOf(name);
  2231. }
  2232. if (optNames != null && _dictIndex != -1) {
  2233. _func = SlotDictOptimized;
  2234. _dictVersion = type.GetOptimizedInstanceVersion();
  2235. } else {
  2236. _func = SlotDict;
  2237. }
  2238. break;
  2239. case OptimizedGetKind.SlotOnly: _func = SlotOnly; break;
  2240. case OptimizedGetKind.PropertySlot: _func = UserSlot; break;
  2241. case OptimizedGetKind.UserSlotDict:
  2242. if (optNames != null) {
  2243. _dictIndex = optNames.IndexOf(name);
  2244. }
  2245. if (optNames != null && _dictIndex != -1) {
  2246. _dictVersion = type.GetOptimizedInstanceVersion();
  2247. if (_getattrSlot != null) {
  2248. _func = UserSlotDictGetAttrOptimized;
  2249. } else {
  2250. _func = UserSlotDictOptimized;
  2251. }
  2252. } else if (_getattrSlot != null) {
  2253. _func = UserSlotDictGetAttr;
  2254. } else {
  2255. _func = UserSlotDict;
  2256. }
  2257. break;
  2258. case OptimizedGetKind.UserSlotOnly:
  2259. if (_getattrSlot != null) {
  2260. _func = UserSlotOnlyGetAttr;
  2261. } else {
  2262. _func = UserSlotOnly;
  2263. }
  2264. break;
  2265. default: throw new InvalidOperationException();
  2266. }
  2267. }
  2268. public object SlotDict(CallSite site, object self, CodeContext context) {
  2269. IPythonObject ipo = self as IPythonObject;
  2270. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2271. _hitCount++;
  2272. object res;
  2273. if (ipo.Dict != null && ipo.Dict.TryGetValue(_name, out res)) {
  2274. return res;
  2275. }
  2276. if (_slot != null && _slot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2277. return res;
  2278. }
  2279. if (_getattrSlot != null && _getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2280. return GetAttr(context, res);
  2281. }
  2282. return TypeError(site, ipo, context);
  2283. }
  2284. return Update(site, self, context);
  2285. }
  2286. public object SlotDictOptimized(CallSite site, object self, CodeContext context) {
  2287. IPythonObject ipo = self as IPythonObject;
  2288. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2289. _hitCount++;
  2290. object res;
  2291. PythonDictionary dict = ipo.Dict;
  2292. if (UserTypeOps.TryGetDictionaryValue(dict, _name, _dictVersion, _dictIndex, out res)) {
  2293. return res;
  2294. }
  2295. if (_slot != null && _slot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2296. return res;
  2297. }
  2298. if (_getattrSlot != null && _getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2299. return GetAttr(context, res);
  2300. }
  2301. return TypeError(site, ipo, context);
  2302. }
  2303. return Update(site, self, context);
  2304. }
  2305. public object SlotOnly(CallSite site, object self, CodeContext context) {
  2306. IPythonObject ipo = self as IPythonObject;
  2307. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2308. _hitCount++;
  2309. object res;
  2310. if (_slot != null && _slot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2311. return res;
  2312. }
  2313. if (_getattrSlot != null && _getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2314. return GetAttr(context, res);
  2315. }
  2316. return TypeError(site, ipo, context);
  2317. }
  2318. return Update(site, self, context);
  2319. }
  2320. public object UserSlotDict(CallSite site, object self, CodeContext context) {
  2321. IPythonObject ipo = self as IPythonObject;
  2322. if (ipo != null && ipo.PythonType.Version == _version) {
  2323. object res;
  2324. if (ipo.Dict != null && ipo.Dict.TryGetValue(_name, out res)) {
  2325. return res;
  2326. }
  2327. return ((PythonTypeUserDescriptorSlot)_slot).GetValue(context, self, ipo.PythonType);
  2328. }
  2329. return Update(site, self, context);
  2330. }
  2331. public object UserSlotDictOptimized(CallSite site, object self, CodeContext context) {
  2332. IPythonObject ipo = self as IPythonObject;
  2333. if (ipo != null && ipo.PythonType.Version == _version) {
  2334. object res;
  2335. PythonDictionary dict = ipo.Dict;
  2336. if (UserTypeOps.TryGetDictionaryValue(dict, _name, _dictVersion, _dictIndex, out res)) {
  2337. return res;
  2338. }
  2339. return ((PythonTypeUserDescriptorSlot)_slot).GetValue(context, self, ipo.PythonType);
  2340. }
  2341. return Update(site, self, context);
  2342. }
  2343. public object UserSlotOnly(CallSite site, object self, CodeContext context) {
  2344. IPythonObject ipo = self as IPythonObject;
  2345. if (ipo != null && ipo.PythonType.Version == _version) {
  2346. return ((PythonTypeUserDescriptorSlot)_slot).GetValue(context, self, ipo.PythonType);
  2347. }
  2348. return Update(site, self, context);
  2349. }
  2350. public object UserSlotDictGetAttr(CallSite site, object self, CodeContext context) {
  2351. IPythonObject ipo = self as IPythonObject;
  2352. if (ipo != null && ipo.PythonType.Version == _version) {
  2353. object res;
  2354. if (ipo.Dict != null && ipo.Dict.TryGetValue(_name, out res)) {
  2355. return res;
  2356. }
  2357. try {
  2358. return ((PythonTypeUserDescriptorSlot)_slot).GetValue(context, self, ipo.PythonType);
  2359. } catch (MissingMemberException) {
  2360. ExceptionHelpers.DynamicStackFrames = null;
  2361. }
  2362. if (_getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2363. return GetAttr(context, res);
  2364. }
  2365. return TypeError(site, ipo, context);
  2366. }
  2367. return Update(site, self, context);
  2368. }
  2369. public object UserSlotDictGetAttrOptimized(CallSite site, object self, CodeContext context) {
  2370. IPythonObject ipo = self as IPythonObject;
  2371. if (ipo != null && ipo.PythonType.Version == _version) {
  2372. object res;
  2373. PythonDictionary dict = ipo.Dict;
  2374. if (UserTypeOps.TryGetDictionaryValue(dict, _name, _dictVersion, _dictIndex, out res)) {
  2375. return res;
  2376. }
  2377. try {
  2378. return ((PythonTypeUserDescriptorSlot)_slot).GetValue(context, self, ipo.PythonType);
  2379. } catch (MissingMemberException) {
  2380. ExceptionHelpers.DynamicStackFrames = null;
  2381. }
  2382. if (_getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2383. return GetAttr(context, res);
  2384. }
  2385. return TypeError(site, ipo, context);
  2386. }
  2387. return Update(site, self, context);
  2388. }
  2389. public object UserSlotOnlyGetAttr(CallSite site, object self, CodeContext context) {
  2390. IPythonObject ipo = self as IPythonObject;
  2391. if (ipo != null && ipo.PythonType.Version == _version) {
  2392. try {
  2393. return ((PythonTypeUserDescriptorSlot)_slot).GetValue(context, self, ipo.PythonType);
  2394. } catch (MissingMemberException) {
  2395. ExceptionHelpers.DynamicStackFrames = null;
  2396. }
  2397. object res;
  2398. if (_getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2399. return GetAttr(context, res);
  2400. }
  2401. return TypeError(site, ipo, context);
  2402. }
  2403. return Update(site, self, context);
  2404. }
  2405. public object UserSlot(CallSite site, object self, CodeContext context) {
  2406. IPythonObject ipo = self as IPythonObject;
  2407. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2408. object res = _slotFunc(self);
  2409. if (res != Uninitialized.Instance) {
  2410. return res;
  2411. }
  2412. if (_getattrSlot != null && _getattrSlot.TryGetValue(context, self, ipo.PythonType, out res)) {
  2413. return GetAttr(context, res);
  2414. }
  2415. return TypeError(site, ipo, context);
  2416. }
  2417. return Update(site, self, context);
  2418. }
  2419. private object GetAttr(CodeContext context, object res) {
  2420. if (_isNoThrow) {
  2421. try {
  2422. return PythonContext.GetContext(context).Call(context, res, _name);
  2423. } catch (MissingMemberException) {
  2424. ExceptionHelpers.DynamicStackFrames = null;
  2425. return OperationFailed.Value;
  2426. }
  2427. } else {
  2428. return PythonContext.GetContext(context).Call(context, res, _name);
  2429. }
  2430. }
  2431. private object TypeError(CallSite site, IPythonObject ipo, CodeContext context) {
  2432. return _fallback(site, ipo, context);
  2433. }
  2434. }
  2435. enum OptimizedSetKind {
  2436. None,
  2437. SetAttr,
  2438. UserSlot,
  2439. SetDict,
  2440. Error
  2441. }
  2442. class SetMemberDelegates<TValue> : FastSetBase<TValue> {
  2443. private readonly string _name;
  2444. private readonly PythonTypeSlot _slot;
  2445. private readonly SlotSetValue _slotFunc;
  2446. private readonly CodeContext _context;
  2447. private readonly int _index, _keysVersion;
  2448. public SetMemberDelegates(CodeContext context, PythonType type, OptimizedSetKind kind, string name, int version, PythonTypeSlot slot, SlotSetValue slotFunc)
  2449. : base(version) {
  2450. _slot = slot;
  2451. _name = name;
  2452. _slotFunc = slotFunc;
  2453. _context = context;
  2454. switch (kind) {
  2455. case OptimizedSetKind.SetAttr: _func = new Func<CallSite, object, TValue, object>(SetAttr); break;
  2456. case OptimizedSetKind.UserSlot: _func = new Func<CallSite, object, TValue, object>(UserSlot); break;
  2457. case OptimizedSetKind.SetDict:
  2458. var optNames = type.GetOptimizedInstanceNames();
  2459. int index;
  2460. if (optNames != null && (index = optNames.IndexOf(name)) != -1) {
  2461. _index = index;
  2462. _keysVersion = type.GetOptimizedInstanceVersion();
  2463. _func = new Func<CallSite, object, TValue, object>(SetDictOptimized);
  2464. } else {
  2465. _func = new Func<CallSite, object, TValue, object>(SetDict);
  2466. }
  2467. break;
  2468. case OptimizedSetKind.Error: _func = new Func<CallSite, object, TValue, object>(Error); break;
  2469. }
  2470. }
  2471. public object SetAttr(CallSite site, object self, TValue value) {
  2472. IPythonObject ipo = self as IPythonObject;
  2473. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2474. _hitCount++;
  2475. object res;
  2476. if (_slot.TryGetValue(_context, self, ipo.PythonType, out res)) {
  2477. return PythonOps.CallWithContext(_context, res, _name, value);
  2478. }
  2479. return TypeError(ipo);
  2480. }
  2481. return Update(site, self, value);
  2482. }
  2483. public object SetDictOptimized(CallSite site, object self, TValue value) {
  2484. IPythonObject ipo = self as IPythonObject;
  2485. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2486. _hitCount++;
  2487. return UserTypeOps.SetDictionaryValueOptimized(ipo, _name, value, _keysVersion, _index);
  2488. }
  2489. return Update(site, self, value);
  2490. }
  2491. public object SetDict(CallSite site, object self, TValue value) {
  2492. IPythonObject ipo = self as IPythonObject;
  2493. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2494. _hitCount++;
  2495. UserTypeOps.SetDictionaryValue(ipo, _name, value);
  2496. return null;
  2497. }
  2498. return Update(site, self, value);
  2499. }
  2500. public object Error(CallSite site, object self, TValue value) {
  2501. IPythonObject ipo = self as IPythonObject;
  2502. if (ipo != null && ipo.PythonType.Version == _version) {
  2503. return TypeError(ipo);
  2504. }
  2505. return Update(site, self, value);
  2506. }
  2507. public object UserSlot(CallSite site, object self, TValue value) {
  2508. IPythonObject ipo = self as IPythonObject;
  2509. if (ipo != null && ipo.PythonType.Version == _version && ShouldUseNonOptimizedSite) {
  2510. _hitCount++;
  2511. _slotFunc(self, value);
  2512. return null;
  2513. }
  2514. return Update(site, self, value);
  2515. }
  2516. private object TypeError(IPythonObject ipo) {
  2517. throw PythonOps.AttributeErrorForMissingAttribute(ipo.PythonType.Name, _name);
  2518. }
  2519. }
  2520. class SetMemberKey : IEquatable<SetMemberKey> {
  2521. public readonly Type Type;
  2522. public readonly string Name;
  2523. public SetMemberKey(Type type, string name) {
  2524. Type = type;
  2525. Name = name;
  2526. }
  2527. #region IEquatable<SetMemberKey> Members
  2528. public bool Equals(SetMemberKey other) {
  2529. return Type == other.Type && Name == other.Name;
  2530. }
  2531. #endregion
  2532. public override bool Equals(object obj) {
  2533. SetMemberKey other = obj as SetMemberKey;
  2534. if (other == null) {
  2535. return false;
  2536. }
  2537. return Equals(other);
  2538. }
  2539. public override int GetHashCode() {
  2540. return Type.GetHashCode() ^ Name.GetHashCode();
  2541. }
  2542. }
  2543. abstract class TypeGetBase : FastGetBase {
  2544. private readonly FastGetDelegate[] _delegates;
  2545. public TypeGetBase(PythonGetMemberBinder binder, FastGetDelegate[] delegates) {
  2546. _delegates = delegates;
  2547. }
  2548. protected object RunDelegates(object self, CodeContext context) {
  2549. _hitCount++;
  2550. for (int i = 0; i < _delegates.Length; i++) {
  2551. object res;
  2552. if (_delegates[i](context, self, out res)) {
  2553. return res;
  2554. }
  2555. }
  2556. // last delegate should always throw or succeed, this should be unreachable
  2557. throw new InvalidOperationException();
  2558. }
  2559. protected object RunDelegatesNoOptimize(object self, CodeContext context) {
  2560. for (int i = 0; i < _delegates.Length; i++) {
  2561. object res;
  2562. if (_delegates[i](context, self, out res)) {
  2563. return res;
  2564. }
  2565. }
  2566. // last delegate should always throw or succeed, this should be unreachable
  2567. throw new InvalidOperationException();
  2568. }
  2569. }
  2570. delegate bool FastGetDelegate(CodeContext context, object self, out object result);
  2571. class TypeGet : TypeGetBase {
  2572. private int _version;
  2573. public TypeGet(PythonGetMemberBinder binder, FastGetDelegate[] delegates, int version, bool isMeta, bool canOptimize)
  2574. : base(binder, delegates) {
  2575. _version = version;
  2576. if (canOptimize) {
  2577. if (isMeta) {
  2578. _func = MetaOnlyTargetOptimizing;
  2579. } else {
  2580. _func = TargetOptimizing;
  2581. }
  2582. } else {
  2583. if (isMeta) {
  2584. _func = MetaOnlyTarget;
  2585. } else {
  2586. _func = Target;
  2587. }
  2588. }
  2589. }
  2590. public object Target(CallSite site, object self, CodeContext context) {
  2591. PythonType pt = self as PythonType;
  2592. if (pt != null && pt.Version == _version) {
  2593. return RunDelegatesNoOptimize(self, context);
  2594. }
  2595. return Update(site, self, context);
  2596. }
  2597. public object MetaOnlyTarget(CallSite site, object self, CodeContext context) {
  2598. PythonType pt = self as PythonType;
  2599. if (pt != null && PythonOps.CheckTypeVersion(pt, _version)) {
  2600. return RunDelegatesNoOptimize(self, context);
  2601. }
  2602. return Update(site, self, context);
  2603. }
  2604. public object TargetOptimizing(CallSite site, object self, CodeContext context) {
  2605. PythonType pt = self as PythonType;
  2606. if (pt != null && pt.Version == _version && ShouldUseNonOptimizedSite) {
  2607. return RunDelegates(self, context);
  2608. }
  2609. return Update(site, self, context);
  2610. }
  2611. public object MetaOnlyTargetOptimizing(CallSite site, object self, CodeContext context) {
  2612. PythonType pt = self as PythonType;
  2613. if (pt != null && PythonOps.CheckTypeVersion(pt, _version) && ShouldUseNonOptimizedSite) {
  2614. return RunDelegates(self, context);
  2615. }
  2616. return Update(site, self, context);
  2617. }
  2618. public override bool IsValid(PythonType type) {
  2619. if (_func == MetaOnlyTarget || _func == MetaOnlyTargetOptimizing) {
  2620. return PythonOps.CheckTypeVersion(type, _version);
  2621. }
  2622. return type.Version == _version;
  2623. }
  2624. }
  2625. class SystemTypeGet : TypeGetBase {
  2626. private readonly PythonType _self;
  2627. public SystemTypeGet(PythonGetMemberBinder binder, FastGetDelegate[] delegates, PythonType type, bool isMeta, bool optimizing)
  2628. : base(binder, delegates) {
  2629. _self = type;
  2630. if (optimizing) {
  2631. if (isMeta) {
  2632. _func = MetaOnlyTargetOptimizing;
  2633. } else {
  2634. _func = TargetOptimizing;
  2635. }
  2636. } else {
  2637. if (isMeta) {
  2638. _func = MetaOnlyTarget;
  2639. } else {
  2640. _func = Target;
  2641. }
  2642. }
  2643. }
  2644. public object Target(CallSite site, object self, CodeContext context) {
  2645. if (self == _self) {
  2646. return RunDelegatesNoOptimize(self, context);
  2647. }
  2648. return Update(site, self, context);
  2649. }
  2650. public object MetaOnlyTarget(CallSite site, object self, CodeContext context) {
  2651. if (self is PythonType) {
  2652. return RunDelegatesNoOptimize(self, context);
  2653. }
  2654. return Update(site, self, context);
  2655. }
  2656. public object TargetOptimizing(CallSite site, object self, CodeContext context) {
  2657. if (self == _self && ShouldUseNonOptimizedSite) {
  2658. return RunDelegates(self, context);
  2659. }
  2660. return Update(site, self, context);
  2661. }
  2662. public object MetaOnlyTargetOptimizing(CallSite site, object self, CodeContext context) {
  2663. if (self is PythonType && ShouldUseNonOptimizedSite) {
  2664. return RunDelegates(self, context);
  2665. }
  2666. return Update(site, self, context);
  2667. }
  2668. public override bool IsValid(PythonType type) {
  2669. if (_func == MetaOnlyTarget || _func == MetaOnlyTargetOptimizing) {
  2670. return true;
  2671. }
  2672. return type == _self;
  2673. }
  2674. }
  2675. }