Skip to content

Commit 6aec8cf

Browse files
authored
Add nullable annotations to CodeContext (#972)
1 parent 8b6e34a commit 6aec8cf

File tree

7 files changed

+53
-102
lines changed

7 files changed

+53
-102
lines changed

Src/IronPython.Modules/array.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ internal arrayiterator(array a) {
11131113
public object __iter__() => this;
11141114

11151115
public object __reduce__(CodeContext context) {
1116-
object iter;
1116+
object? iter;
11171117
context.TryLookupBuiltin("iter", out iter);
11181118
if (_iterating) {
11191119
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(_array), _index + 1);

Src/IronPython/Runtime/CodeContext.cs

Lines changed: 36 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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+
57
using System;
68
using System.Diagnostics;
79

@@ -11,54 +13,39 @@
1113
using IronPython.Compiler;
1214

1315
namespace IronPython.Runtime {
14-
16+
1517
/// <summary>
1618
/// Captures and flows the state of executing code from the generated
1719
/// Python code into the IronPython runtime.
1820
/// </summary>
1921
[DebuggerTypeProxy(typeof(DebugProxy)), DebuggerDisplay("ModuleName = {ModuleName}")]
2022
public sealed class CodeContext {
21-
private readonly ModuleContext/*!*/ _modContext;
22-
private readonly PythonDictionary/*!*/ _dict;
23-
2423
/// <summary>
2524
/// Creates a new CodeContext which is backed by the specified Python dictionary.
2625
/// </summary>
2726
public CodeContext(PythonDictionary/*!*/ dict, ModuleContext/*!*/ moduleContext) {
2827
ContractUtils.RequiresNotNull(dict, nameof(dict));
2928
ContractUtils.RequiresNotNull(moduleContext, nameof(moduleContext));
30-
_dict = dict;
31-
_modContext = moduleContext;
29+
Dict = dict;
30+
ModuleContext = moduleContext;
3231
}
3332

3433
#region Public APIs
3534

3635
/// <summary>
3736
/// Gets the module state for top-level code.
3837
/// </summary>
39-
public ModuleContext ModuleContext {
40-
get {
41-
return _modContext;
42-
}
43-
}
38+
public ModuleContext ModuleContext { get; }
4439

4540
/// <summary>
4641
/// Gets the DLR scope object that corresponds to the global variables of this context.
4742
/// </summary>
48-
public Scope GlobalScope {
49-
get {
50-
return _modContext.GlobalScope;
51-
}
52-
}
53-
43+
public Scope GlobalScope => ModuleContext.GlobalScope;
44+
5445
/// <summary>
5546
/// Gets the PythonContext which created the CodeContext.
5647
/// </summary>
57-
public PythonContext LanguageContext {
58-
get {
59-
return _modContext.Context;
60-
}
61-
}
48+
public PythonContext LanguageContext => ModuleContext.Context;
6249

6350
#endregion
6451

@@ -67,12 +54,8 @@ public PythonContext LanguageContext {
6754
/// <summary>
6855
/// Gets the dictionary for the global variables from the ModuleContext.
6956
/// </summary>
70-
internal PythonDictionary GlobalDict {
71-
get {
72-
return _modContext.Globals;
73-
}
74-
}
75-
57+
internal PythonDictionary GlobalDict => ModuleContext.Globals;
58+
7659
/// <summary>
7760
/// True if this global context should display CLR members on shared types (for example .ToString on int/bool/etc...)
7861
///
@@ -86,24 +69,18 @@ internal bool ShowCls {
8669
ModuleContext.ShowCls = value;
8770
}
8871
}
89-
72+
9073
/// <summary>
9174
/// Attempts to lookup the provided name in this scope or any outer scope.
9275
/// </summary>
93-
internal bool TryLookupName(string name, out object value) {
94-
string strName = name;
95-
if (_dict.TryGetValue(strName, out value)) {
96-
return true;
97-
}
98-
99-
return _modContext.Globals.TryGetValue(strName, out value);
100-
}
76+
internal bool TryLookupName(string name, out object? value)
77+
=> Dict.TryGetValue(name, out value) || ModuleContext.Globals.TryGetValue(name, out value);
10178

10279
/// <summary>
10380
/// Looks up a global variable. If the variable is not defined in the
10481
/// global scope then built-ins is consulted.
10582
/// </summary>
106-
internal bool TryLookupBuiltin(string name, out object value) {
83+
internal bool TryLookupBuiltin(string name, out object? value) {
10784
object builtins;
10885
if (!GlobalDict.TryGetValue("__builtins__", out builtins)) {
10986
value = null;
@@ -125,71 +102,57 @@ internal bool TryLookupBuiltin(string name, out object value) {
125102
/// <summary>
126103
/// Gets the dictionary used for storage of local variables.
127104
/// </summary>
128-
internal PythonDictionary Dict {
129-
get {
130-
return _dict;
131-
}
132-
}
105+
internal PythonDictionary Dict { get; }
133106

134107
/// <summary>
135108
/// Attempts to lookup the variable in the local scope.
136109
/// </summary>
137-
internal bool TryGetVariable(string name, out object value) {
138-
return Dict.TryGetValue(name, out value);
139-
}
110+
internal bool TryGetVariable(string name, out object? value)
111+
=> Dict.TryGetValue(name, out value);
140112

141113
/// <summary>
142114
/// Removes a variable from the local scope.
143115
/// </summary>
144-
internal bool TryRemoveVariable(string name) {
145-
return Dict.Remove(name);
146-
}
116+
internal bool TryRemoveVariable(string name)
117+
=> Dict.Remove(name);
147118

148119
/// <summary>
149120
/// Sets a variable in the local scope.
150121
/// </summary>
151-
internal void SetVariable(string name, object value) {
152-
Dict.Add(name, value);
153-
}
122+
internal void SetVariable(string name, object? value)
123+
=> Dict.Add(name, value);
154124

155125
/// <summary>
156126
/// Gets a variable from the global scope.
157127
/// </summary>
158-
internal bool TryGetGlobalVariable(string name, out object res) {
159-
return GlobalDict.TryGetValue(name, out res);
160-
}
128+
internal bool TryGetGlobalVariable(string name, out object? res)
129+
=> GlobalDict.TryGetValue(name, out res);
161130

162131

163132
/// <summary>
164133
/// Sets a variable in the global scope.
165134
/// </summary>
166-
internal void SetGlobalVariable(string name, object value) {
167-
GlobalDict.Add(name, value);
168-
}
135+
internal void SetGlobalVariable(string name, object? value)
136+
=> GlobalDict.Add(name, value);
169137

170138
/// <summary>
171139
/// Removes a variable from the global scope.
172140
/// </summary>
173-
internal bool TryRemoveGlobalVariable(string name) {
174-
return GlobalDict.Remove(name);
175-
}
141+
internal bool TryRemoveGlobalVariable(string name)
142+
=> GlobalDict.Remove(name);
176143

177-
internal PythonGlobal/*!*/[] GetGlobalArray() {
178-
return ((GlobalDictionaryStorage)_dict._storage).Data;
179-
}
144+
internal PythonGlobal/*!*/[] GetGlobalArray()
145+
=> ((GlobalDictionaryStorage)Dict._storage).Data;
180146

181-
internal bool IsTopLevel {
182-
get {
183-
return Dict != ModuleContext.Globals;
184-
}
185-
}
147+
internal bool IsTopLevel
148+
=> Dict != ModuleContext.Globals;
186149

187150
/// <summary>
188151
/// Returns the dictionary associated with __builtins__ if one is
189152
/// set or null if it's not available. If __builtins__ is a module
190153
/// the module's dictionary is returned.
191154
/// </summary>
192-
internal PythonDictionary GetBuiltinsDict() {
155+
internal PythonDictionary? GetBuiltinsDict() {
193156
object builtins;
194157
if (GlobalDict._storage.TryGetBuiltins(out builtins)) {
195158
if (builtins is PythonModule builtinsScope) {
@@ -202,17 +165,9 @@ internal PythonDictionary GetBuiltinsDict() {
202165
return null;
203166
}
204167

205-
internal PythonModule Module {
206-
get {
207-
return _modContext.Module;
208-
}
209-
}
168+
internal PythonModule Module => ModuleContext.Module;
210169

211-
internal string ModuleName {
212-
get {
213-
return Module.GetName();
214-
}
215-
}
170+
internal string ModuleName => Module.GetName();
216171

217172
internal class DebugProxy {
218173
private readonly CodeContext _context;
@@ -222,11 +177,7 @@ public DebugProxy(CodeContext context) {
222177
}
223178

224179
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
225-
public PythonModule Members {
226-
get {
227-
return _context.Module;
228-
}
229-
}
180+
public PythonModule Members => _context.Module;
230181
}
231182

232183
#endregion

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,8 +3365,8 @@ public static void RemoveName(CodeContext/*!*/ context, string name) {
33653365
/// <summary>
33663366
/// Called from generated code, helper to do name lookup
33673367
/// </summary>
3368-
public static object LookupName(CodeContext/*!*/ context, string name) {
3369-
object value;
3368+
public static object? LookupName(CodeContext/*!*/ context, string name) {
3369+
object? value;
33703370
if (context.TryLookupName(name, out value)) {
33713371
return value;
33723372
} else if (context.TryLookupBuiltin(name, out value)) {
@@ -3439,16 +3439,16 @@ public static object SetName(CodeContext/*!*/ context, string name, object value
34393439
return generator.Context;
34403440
}
34413441

3442-
public static object GetGlobal(CodeContext/*!*/ context, string name) {
3442+
public static object? GetGlobal(CodeContext/*!*/ context, string name) {
34433443
return GetVariable(context, name, true, false);
34443444
}
34453445

3446-
public static object GetLocal(CodeContext/*!*/ context, string name) {
3446+
public static object? GetLocal(CodeContext/*!*/ context, string name) {
34473447
return GetVariable(context, name, false, false);
34483448
}
34493449

3450-
internal static object GetVariable(CodeContext/*!*/ context, string name, bool isGlobal, bool lightThrow) {
3451-
object res;
3450+
internal static object? GetVariable(CodeContext/*!*/ context, string name, bool isGlobal, bool lightThrow) {
3451+
object? res;
34523452
if (isGlobal) {
34533453
if (context.TryGetGlobalVariable(name, out res)) {
34543454
return res;
@@ -3459,7 +3459,7 @@ internal static object GetVariable(CodeContext/*!*/ context, string name, bool i
34593459
}
34603460
}
34613461

3462-
PythonDictionary builtins = context.GetBuiltinsDict();
3462+
var builtins = context.GetBuiltinsDict();
34633463
if (builtins != null && builtins.TryGetValue(name, out res)) {
34643464
return res;
34653465
}
@@ -3471,16 +3471,16 @@ internal static object GetVariable(CodeContext/*!*/ context, string name, bool i
34713471
throw ex;
34723472
}
34733473

3474-
public static object RawGetGlobal(CodeContext/*!*/ context, string name) {
3475-
if (context.TryGetGlobalVariable(name, out object res)) {
3474+
public static object? RawGetGlobal(CodeContext/*!*/ context, string name) {
3475+
if (context.TryGetGlobalVariable(name, out object? res)) {
34763476
return res;
34773477
}
34783478

34793479
return Uninitialized.Instance;
34803480
}
34813481

3482-
public static object RawGetLocal(CodeContext/*!*/ context, string name) {
3483-
if (context.TryLookupName(name, out object res)) {
3482+
public static object? RawGetLocal(CodeContext/*!*/ context, string name) {
3483+
if (context.TryLookupName(name, out object? res)) {
34843484
return res;
34853485
}
34863486

Src/IronPython/Runtime/PythonList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ internal PythonListIterator(PythonList l) {
13741374
#region Pickling
13751375

13761376
public object __reduce__(CodeContext context) {
1377-
object iter;
1377+
object? iter;
13781378
context.TryLookupBuiltin("iter", out iter);
13791379
if (_iterating) {
13801380
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(_list), _index + 1);
@@ -1452,7 +1452,7 @@ public object __reduce__(CodeContext context) {
14521452
if (_iterating) {
14531453
return PythonTuple.MakeTuple(Modules.Builtin.reversed, PythonTuple.MakeTuple(_list), _list._size - _index - 1);
14541454
}
1455-
object iter;
1455+
object? iter;
14561456
context.TryLookupBuiltin("iter", out iter);
14571457
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(new PythonList()));
14581458
}

Src/IronPython/Runtime/PythonStrIterator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal PythonStrIterator(string s) {
2929
}
3030

3131
public PythonTuple __reduce__(CodeContext context) {
32-
object iter;
32+
object? iter;
3333
context.TryLookupBuiltin("iter", out iter);
3434
return PythonTuple.MakeTuple(
3535
iter,

Src/IronPython/Runtime/PythonTuple.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ public IEnumerator GetEnumerator() {
671671
#endregion
672672

673673
public PythonTuple __reduce__(CodeContext/*!*/ context) {
674-
object iter;
674+
object? iter;
675675
context.TryLookupBuiltin("iter", out iter);
676676
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(_tuple), _curIndex + 1);
677677
}

Src/IronPython/Runtime/Set.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ public IEnumerator GetEnumerator() {
14741474
#endregion
14751475

14761476
public PythonTuple __reduce__(CodeContext/*!*/ context) {
1477-
object iter;
1477+
object? iter;
14781478
context.TryLookupBuiltin("iter", out iter);
14791479
if (_cnt < 0)
14801480
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(new PythonList()));

0 commit comments

Comments
 (0)