22// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33// See the LICENSE file in the project root for more information.
44
5+ #nullable enable
6+
7+ #if NETCOREAPP3_1 // IDictionary<object?, object?> is incorrectly annotated with TKey : notnull
8+ #pragma warning disable CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
9+ #endif
10+
511using System ;
612using System . Collections ;
713using System . Collections . Generic ;
1319
1420namespace IronPython . Runtime . Types {
1521 [ PythonType ( "mappingproxy" ) ]
16- public sealed class MappingProxy : IDictionary < object , object > , IDictionary {
17- internal PythonDictionary GetDictionary ( CodeContext context ) => dictionary ?? type . GetMemberDictionary ( context , false ) ;
22+ public sealed class MappingProxy : IDictionary < object ? , object ? > , IDictionary {
23+ internal PythonDictionary GetDictionary ( CodeContext context ) => dictionary ?? type ! . GetMemberDictionary ( context , false ) ;
1824
19- private readonly PythonDictionary dictionary ;
20- private readonly PythonType type ;
25+ private readonly PythonDictionary ? dictionary ;
26+ private readonly PythonType ? type ;
2127
2228 internal MappingProxy ( CodeContext context , PythonType /*!*/ dt ) {
2329 Debug . Assert ( dt != null ) ;
2430 type = dt ;
2531 }
2632
27- public MappingProxy ( [ NotNull ] PythonDictionary dict ) {
33+ public MappingProxy ( [ NotNull ] PythonDictionary dict ) {
2834 dictionary = dict ;
2935 }
3036
3137 #region Python Public API Surface
3238
3339 public int __len__ ( CodeContext context ) => GetDictionary ( context ) . Count ;
3440
35- public bool __contains__ ( CodeContext /*!*/ context , object value ) => GetDictionary ( context ) . TryGetValue ( value , out _ ) ;
41+ public bool __contains__ ( CodeContext /*!*/ context , object ? value ) => GetDictionary ( context ) . TryGetValue ( value , out _ ) ;
3642
3743 public string /*!*/ __str__ ( CodeContext /*!*/ context ) => DictionaryOps . __repr__ ( context , this ) ;
3844
39- public object get ( CodeContext /*!*/ context , [ NotNull ] object k , object d = null ) {
40- object res ;
41- if ( ! GetDictionary ( context ) . TryGetValue ( k , out res ) ) {
42- res = d ;
45+ public string __repr__ ( CodeContext /*!*/ context ) {
46+ var dict = GetDictionary ( context ) ;
47+ List < object > ? infinite = PythonOps . GetAndCheckInfinite ( this ) ;
48+ if ( infinite == null ) {
49+ return "mappingproxy({...})" ;
4350 }
4451
45- return res ;
52+ int infiniteIndex = infinite . Count ;
53+ infinite . Add ( this ) ;
54+ try {
55+ return $ "mappingproxy({ PythonOps . Repr ( context , dict ) } )";
56+ } finally {
57+ System . Diagnostics . Debug . Assert ( infiniteIndex == infinite . Count - 1 ) ;
58+ infinite . RemoveAt ( infiniteIndex ) ;
59+ }
60+ }
61+
62+ public object ? get ( CodeContext /*!*/ context , object ? k , object ? d = null ) {
63+ if ( GetDictionary ( context ) . TryGetValue ( k , out object ? res ) ) {
64+ return res ;
65+ }
66+
67+ return d ;
4668 }
4769
4870 public object keys ( CodeContext context ) {
@@ -66,11 +88,11 @@ public object items(CodeContext context) {
6688 return items ;
6789 }
6890
69- public PythonDictionary copy ( CodeContext /*!*/ context ) => new PythonDictionary ( context , this ) ;
91+ public PythonDictionary copy ( CodeContext /*!*/ context ) => GetDictionary ( context ) . copy ( context ) ;
7092
7193 public const object __hash__ = null ;
7294
73- public object __eq__ ( CodeContext /*!*/ context , object other ) {
95+ public object __eq__ ( CodeContext /*!*/ context , object ? other ) {
7496 if ( other is MappingProxy proxy ) {
7597 if ( type == null ) {
7698 return __eq__ ( context , proxy . GetDictionary ( context ) ) ;
@@ -90,7 +112,7 @@ public object __eq__(CodeContext/*!*/ context, object other) {
90112
91113 #region IDictionary Members
92114
93- public object this [ object key ] {
115+ public object ? this [ object ? key ] {
94116 get => GetDictionary ( DefaultContext . Default ) [ key ] ;
95117 [ PythonHidden ]
96118 set => throw PythonOps . TypeError ( "'mappingproxy' object does not support item assignment" ) ;
@@ -109,7 +131,7 @@ public object this[object key] {
109131 #region IDictionary Members
110132
111133 [ PythonHidden ]
112- public void Add ( object key , object value ) {
134+ public void Add ( object ? key , object ? value ) {
113135 this [ key ] = value ;
114136 }
115137
@@ -149,11 +171,7 @@ ICollection IDictionary.Values {
149171
150172 #region ICollection Members
151173
152- void ICollection . CopyTo ( Array array , int index ) {
153- foreach ( DictionaryEntry de in ( IDictionary ) this ) {
154- array . SetValue ( de , index ++ ) ;
155- }
156- }
174+ void ICollection . CopyTo ( Array array , int index ) => throw new NotImplementedException ( "The method or operation is not implemented." ) ;
157175
158176 int ICollection . Count => __len__ ( DefaultContext . Default ) ;
159177
@@ -165,43 +183,43 @@ void ICollection.CopyTo(Array array, int index) {
165183
166184 #region IDictionary<object,object> Members
167185
168- bool IDictionary < object , object > . ContainsKey ( object key ) => __contains__ ( DefaultContext . Default , key ) ;
186+ bool IDictionary < object ? , object ? > . ContainsKey ( object ? key ) => __contains__ ( DefaultContext . Default , key ) ;
169187
170- ICollection < object > IDictionary < object , object > . Keys => GetDictionary ( DefaultContext . Default ) . Keys ;
188+ ICollection < object ? > IDictionary < object ? , object ? > . Keys => GetDictionary ( DefaultContext . Default ) . Keys ;
171189
172- bool IDictionary < object , object > . Remove ( object key ) => throw new InvalidOperationException ( "mappingproxy is read-only" ) ;
190+ bool IDictionary < object ? , object ? > . Remove ( object ? key ) => throw new InvalidOperationException ( "mappingproxy is read-only" ) ;
173191
174- bool IDictionary < object , object > . TryGetValue ( object key , out object value ) => GetDictionary ( DefaultContext . Default ) . TryGetValue ( key , out value ) ;
192+ bool IDictionary < object ? , object ? > . TryGetValue ( object ? key , out object ? value ) => GetDictionary ( DefaultContext . Default ) . TryGetValue ( key , out value ) ;
175193
176- ICollection < object > IDictionary < object , object > . Values => GetDictionary ( DefaultContext . Default ) . Values ;
194+ ICollection < object ? > IDictionary < object ? , object ? > . Values => GetDictionary ( DefaultContext . Default ) . Values ;
177195
178196 #endregion
179197
180198 #region ICollection<KeyValuePair<object,object>> Members
181199
182- void ICollection < KeyValuePair < object , object > > . Add ( KeyValuePair < object , object > item ) {
200+ void ICollection < KeyValuePair < object ? , object ? > > . Add ( KeyValuePair < object ? , object ? > item ) {
183201 this [ item . Key ] = item . Value ;
184202 }
185203
186- bool ICollection < KeyValuePair < object , object > > . Contains ( KeyValuePair < object , object > item ) => __contains__ ( DefaultContext . Default , item . Key ) ;
204+ bool ICollection < KeyValuePair < object ? , object ? > > . Contains ( KeyValuePair < object ? , object ? > item ) => __contains__ ( DefaultContext . Default , item . Key ) ;
187205
188- void ICollection < KeyValuePair < object , object > > . CopyTo ( KeyValuePair < object , object > [ ] array , int arrayIndex ) {
189- foreach ( KeyValuePair < object , object > de in ( IEnumerable < KeyValuePair < object , object > > ) this ) {
206+ void ICollection < KeyValuePair < object ? , object ? > > . CopyTo ( KeyValuePair < object ? , object ? > [ ] array , int arrayIndex ) {
207+ foreach ( KeyValuePair < object ? , object ? > de in ( IEnumerable < KeyValuePair < object ? , object ? > > ) this ) {
190208 array . SetValue ( de , arrayIndex ++ ) ;
191209 }
192210 }
193211
194- int ICollection < KeyValuePair < object , object > > . Count => __len__ ( DefaultContext . Default ) ;
212+ int ICollection < KeyValuePair < object ? , object ? > > . Count => __len__ ( DefaultContext . Default ) ;
195213
196- bool ICollection < KeyValuePair < object , object > > . IsReadOnly => true ;
214+ bool ICollection < KeyValuePair < object ? , object ? > > . IsReadOnly => true ;
197215
198- bool ICollection < KeyValuePair < object , object > > . Remove ( KeyValuePair < object , object > item ) => ( ( IDictionary < object , object > ) this ) . Remove ( item . Key ) ;
216+ bool ICollection < KeyValuePair < object ? , object ? > > . Remove ( KeyValuePair < object ? , object ? > item ) => ( ( IDictionary < object ? , object ? > ) this ) . Remove ( item . Key ) ;
199217
200218 #endregion
201219
202220 #region IEnumerable<KeyValuePair<object,object>> Members
203221
204- IEnumerator < KeyValuePair < object , object > > IEnumerable < KeyValuePair < object , object > > . GetEnumerator ( ) => GetDictionary ( DefaultContext . Default ) . GetEnumerator ( ) ;
222+ IEnumerator < KeyValuePair < object ? , object ? > > IEnumerable < KeyValuePair < object ? , object ? > > . GetEnumerator ( ) => GetDictionary ( DefaultContext . Default ) . GetEnumerator ( ) ;
205223
206224 #endregion
207225 }
0 commit comments