forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleContext.cs
More file actions
154 lines (139 loc) · 5.41 KB
/
ModuleContext.cs
File metadata and controls
154 lines (139 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
namespace IronPython.Runtime {
/// <summary>
/// Captures the globals and other state of module code.
/// </summary>
public sealed class ModuleContext {
private readonly PythonContext/*!*/ _pyContext;
private readonly PythonDictionary/*!*/ _globals;
private readonly CodeContext/*!*/ _globalContext;
private readonly PythonModule _module;
private ExtensionMethodSet _extensionMethods = ExtensionMethodSet.Empty;
private ModuleOptions _features;
/// <summary>
/// Creates a new ModuleContext which is backed by the specified dictionary.
/// </summary>
public ModuleContext(PythonDictionary/*!*/ globals, PythonContext/*!*/ creatingContext) {
ContractUtils.RequiresNotNull(globals, nameof(globals));
ContractUtils.RequiresNotNull(creatingContext, nameof(creatingContext));
_globals = globals;
_pyContext = creatingContext;
_globalContext = new CodeContext(globals, this);
_module = new PythonModule(globals);
_module.Scope.SetExtension(_pyContext.ContextId, new PythonScopeExtension(_pyContext, _module, this));
}
/// <summary>
/// Creates a new ModuleContext for the specified module.
/// </summary>
public ModuleContext(PythonModule/*!*/ module, PythonContext/*!*/ creatingContext) {
ContractUtils.RequiresNotNull(module, nameof(module));
ContractUtils.RequiresNotNull(creatingContext, nameof(creatingContext));
_globals = module.__dict__;
_pyContext = creatingContext;
_globalContext = new CodeContext(_globals, this);
_module = module;
}
/// <summary>
/// Gets the dictionary used for the global variables in the module
/// </summary>
public PythonDictionary/*!*/ Globals {
get {
return _globals;
}
}
/// <summary>
/// Gets the language context which created this module.
/// </summary>
public PythonContext/*!*/ Context {
get {
return _pyContext;
}
}
/// <summary>
/// Gets the DLR Scope object which is associated with the modules dictionary.
/// </summary>
public Scope/*!*/ GlobalScope {
get {
return _module.Scope;
}
}
/// <summary>
/// Gets the global CodeContext object which is used for execution of top-level code.
/// </summary>
public CodeContext/*!*/ GlobalContext {
get {
return _globalContext;
}
}
/// <summary>
/// Gets the module object which this code is executing in.
///
/// This module may or may not be published in sys.modules. For user defined
/// code typically the module gets published at the start of execution. But if
/// this ModuleContext is attached to a Scope, or if we've just created a new
/// module context for executing code it will not be in sys.modules.
/// </summary>
public PythonModule Module {
get {
return _module;
}
}
/// <summary>
/// Gets the features that code has been compiled with in the module.
/// </summary>
public ModuleOptions Features {
get {
return _features;
}
set {
_features = value;
}
}
/// <summary>
/// Gets or sets whether code running in this context should display
/// CLR members (for example .ToString on objects).
/// </summary>
public bool ShowCls {
get {
return (_features & ModuleOptions.ShowClsMethods) != 0;
}
set {
Debug.Assert(this != _pyContext.SharedContext.ModuleContext || !value);
if (value) {
_features |= ModuleOptions.ShowClsMethods;
} else {
_features &= ~ModuleOptions.ShowClsMethods;
}
}
}
internal ExtensionMethodSet ExtensionMethods {
get {
return _extensionMethods;
}
set {
_extensionMethods = value;
}
}
/// <summary>
/// Initializes __builtins__ for the module scope.
/// </summary>
internal void InitializeBuiltins(bool moduleBuiltins) {
// adds __builtin__ variable if necessary. Python adds the module directly to
// __main__ and __builtin__'s dictionary for all other modules. Our callers
// pass the appropriate flags to control this behavior.
if (!Globals.ContainsKey("__builtins__")) {
if (moduleBuiltins) {
Globals["__builtins__"] = Context.BuiltinModuleInstance;
} else {
Globals["__builtins__"] = Context.BuiltinModuleDict;
}
}
}
}
}