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+
57using System ;
68using System . Diagnostics ;
79
1113using IronPython . Compiler ;
1214
1315namespace 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
0 commit comments