This repository was archived by the owner on Apr 24, 2024. It is now read-only.
forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython.cs
More file actions
368 lines (306 loc) · 15.4 KB
/
Python.cs
File metadata and controls
368 lines (306 loc) · 15.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// 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.Collections.Generic;
using Microsoft.Scripting.Utils;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
using IronPython.Modules;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
namespace IronPython.Hosting {
/// <summary>
/// Provides helpers for interacting with IronPython.
/// </summary>
public static class Python {
#region Public APIs
/// <summary>
/// Creates a new ScriptRuntime with the IronPython scipting engine pre-configured.
/// </summary>
/// <returns></returns>
public static ScriptRuntime/*!*/ CreateRuntime() {
return new ScriptRuntime(CreateRuntimeSetup(null));
}
/// <summary>
/// Creates a new ScriptRuntime with the IronPython scipting engine pre-configured and
/// additional options.
/// </summary>
public static ScriptRuntime/*!*/ CreateRuntime(IDictionary<string, object> options) {
return new ScriptRuntime(CreateRuntimeSetup(options));
}
#if FEATURE_REMOTING
/// <summary>
/// Creates a new ScriptRuntime with the IronPython scripting engine pre-configured
/// in the specified AppDomain. The remote ScriptRuntime may be manipulated from
/// the local domain but all code will run in the remote domain.
/// </summary>
public static ScriptRuntime/*!*/ CreateRuntime(AppDomain/*!*/ domain) {
ContractUtils.RequiresNotNull(domain, nameof(domain));
return ScriptRuntime.CreateRemote(domain, CreateRuntimeSetup(null));
}
/// <summary>
/// Creates a new ScriptRuntime with the IronPython scripting engine pre-configured
/// in the specified AppDomain with additional options. The remote ScriptRuntime may
/// be manipulated from the local domain but all code will run in the remote domain.
/// </summary>
public static ScriptRuntime/*!*/ CreateRuntime(AppDomain/*!*/ domain, IDictionary<string, object> options) {
ContractUtils.RequiresNotNull(domain, nameof(domain));
return ScriptRuntime.CreateRemote(domain, CreateRuntimeSetup(options));
}
#endif
/// <summary>
/// Creates a new ScriptRuntime and returns the ScriptEngine for IronPython. If
/// the ScriptRuntime is required it can be acquired from the Runtime property
/// on the engine.
/// </summary>
public static ScriptEngine/*!*/ CreateEngine() {
return GetEngine(CreateRuntime());
}
/// <summary>
/// Creates a new ScriptRuntime with the specified options and returns the
/// ScriptEngine for IronPython. If the ScriptRuntime is required it can be
/// acquired from the Runtime property on the engine.
/// </summary>
public static ScriptEngine/*!*/ CreateEngine(IDictionary<string, object> options) {
return GetEngine(CreateRuntime(options));
}
#if FEATURE_REMOTING
/// <summary>
/// Creates a new ScriptRuntime and returns the ScriptEngine for IronPython. If
/// the ScriptRuntime is required it can be acquired from the Runtime property
/// on the engine.
///
/// The remote ScriptRuntime may be manipulated from the local domain but
/// all code will run in the remote domain.
/// </summary>
public static ScriptEngine/*!*/ CreateEngine(AppDomain/*!*/ domain) {
return GetEngine(CreateRuntime(domain));
}
/// <summary>
/// Creates a new ScriptRuntime with the specified options and returns the
/// ScriptEngine for IronPython. If the ScriptRuntime is required it can be
/// acquired from the Runtime property on the engine.
///
/// The remote ScriptRuntime may be manipulated from the local domain but
/// all code will run in the remote domain.
/// </summary>
public static ScriptEngine/*!*/ CreateEngine(AppDomain/*!*/ domain, IDictionary<string, object> options) {
return GetEngine(CreateRuntime(domain, options));
}
#endif
/// <summary>
/// Given a ScriptRuntime gets the ScriptEngine for IronPython.
/// </summary>
public static ScriptEngine/*!*/ GetEngine(ScriptRuntime/*!*/ runtime) {
return runtime.GetEngineByTypeName(typeof(PythonContext).AssemblyQualifiedName);
}
/// <summary>
/// Gets a ScriptScope which is the Python sys module for the provided ScriptRuntime.
/// </summary>
public static ScriptScope/*!*/ GetSysModule(this ScriptRuntime/*!*/ runtime) {
ContractUtils.RequiresNotNull(runtime, nameof(runtime));
return GetSysModule(GetEngine(runtime));
}
/// <summary>
/// Gets a ScriptScope which is the Python sys module for the provided ScriptEngine.
/// </summary>
public static ScriptScope/*!*/ GetSysModule(this ScriptEngine/*!*/ engine) {
ContractUtils.RequiresNotNull(engine, nameof(engine));
return GetPythonService(engine).GetSystemState();
}
/// <summary>
/// Gets a ScriptScope which is the Python __builtin__ module for the provided ScriptRuntime.
/// </summary>
public static ScriptScope/*!*/ GetBuiltinModule(this ScriptRuntime/*!*/ runtime) {
ContractUtils.RequiresNotNull(runtime, nameof(runtime));
return GetBuiltinModule(GetEngine(runtime));
}
/// <summary>
/// Gets a ScriptScope which is the Python __builtin__ module for the provided ScriptEngine.
/// </summary>
public static ScriptScope/*!*/ GetBuiltinModule(this ScriptEngine/*!*/ engine) {
ContractUtils.RequiresNotNull(engine, nameof(engine));
return GetPythonService(engine).GetBuiltins();
}
/// <summary>
/// Gets a ScriptScope which is the Python clr module for the provided ScriptRuntime.
/// </summary>
public static ScriptScope/*!*/ GetClrModule(this ScriptRuntime/*!*/ runtime) {
ContractUtils.RequiresNotNull(runtime, nameof(runtime));
return GetClrModule(GetEngine(runtime));
}
/// <summary>
/// Gets a ScriptScope which is the Python clr module for the provided ScriptEngine.
/// </summary>
public static ScriptScope/*!*/ GetClrModule(this ScriptEngine/*!*/ engine) {
ContractUtils.RequiresNotNull(engine, nameof(engine));
return GetPythonService(engine).GetClr();
}
/// <summary>
/// Imports the Python module by the given name and returns its ScriptSCope. If the
/// module does not exist an exception is raised.
/// </summary>
public static ScriptScope/*!*/ ImportModule(this ScriptRuntime/*!*/ runtime, string/*!*/ moduleName) {
ContractUtils.RequiresNotNull(runtime, nameof(runtime));
ContractUtils.RequiresNotNull(moduleName, nameof(moduleName));
return ImportModule(GetEngine(runtime), moduleName);
}
/// <summary>
/// Imports the Python module by the given name and returns its ScriptSCope. If the
/// module does not exist an exception is raised.
/// </summary>
public static ScriptScope/*!*/ ImportModule(this ScriptEngine/*!*/ engine, string/*!*/ moduleName) {
ContractUtils.RequiresNotNull(engine, nameof(engine));
ContractUtils.RequiresNotNull(moduleName, nameof(moduleName));
return GetPythonService(engine).ImportModule(engine, moduleName);
}
/// <summary>
/// Imports the Python module by the given name and inserts it into the ScriptScope as that name. If the
/// module does not exist an exception is raised.
/// </summary>
/// <param name="scope"></param>
/// <param name="moduleName"></param>
public static void ImportModule (this ScriptScope/*!*/ scope, string/*!*/ moduleName) {
ContractUtils.RequiresNotNull (scope, nameof(scope));
ContractUtils.RequiresNotNull (moduleName, nameof(moduleName));
scope.SetVariable (moduleName, scope.Engine.ImportModule (moduleName));
}
/// <summary>
/// Sets sys.exec_prefix, sys.executable and sys.version and adds the prefix to sys.path
/// </summary>
public static void SetHostVariables(this ScriptRuntime/*!*/ runtime, string/*!*/ prefix, string/*!*/ executable, string/*!*/ version) {
ContractUtils.RequiresNotNull(runtime, nameof(runtime));
ContractUtils.RequiresNotNull(prefix, nameof(prefix));
ContractUtils.RequiresNotNull(executable, nameof(executable));
ContractUtils.RequiresNotNull(version, nameof(version));
GetPythonContext(GetEngine(runtime)).SetHostVariables(prefix, executable, version);
}
/// <summary>
/// Sets sys.exec_prefix, sys.executable and sys.version and adds the prefix to sys.path
/// </summary>
public static void SetHostVariables(this ScriptEngine/*!*/ engine, string/*!*/ prefix, string/*!*/ executable, string/*!*/ version) {
ContractUtils.RequiresNotNull(engine, nameof(engine));
ContractUtils.RequiresNotNull(prefix, nameof(prefix));
ContractUtils.RequiresNotNull(executable, nameof(executable));
ContractUtils.RequiresNotNull(version, nameof(version));
GetPythonContext(engine).SetHostVariables(prefix, executable, version);
}
/// <summary>
/// Enables call tracing for the current thread in this ScriptEngine.
///
/// TracebackDelegate will be called back for each function entry, exit, exception, and line change.
/// </summary>
public static void SetTrace(this ScriptEngine/*!*/ engine, TracebackDelegate traceFunc) {
SysModule.settrace(GetPythonContext(engine).SharedContext, traceFunc);
}
/// <summary>
/// Enables call tracing for the current thread for the Python engine in this ScriptRuntime.
///
/// TracebackDelegate will be called back for each function entry, exit, exception, and line change.
/// </summary>
public static void SetTrace(this ScriptRuntime/*!*/ runtime, TracebackDelegate traceFunc) {
SetTrace(GetEngine(runtime), traceFunc);
}
/// <summary>
/// Provides nested level debugging support when SetTrace or SetProfile are used.
///
/// This saves the current tracing information and then calls the provided object.
/// </summary>
public static void CallTracing(this ScriptRuntime/*!*/ runtime, object traceFunc, params object[] args) {
CallTracing(GetEngine(runtime), traceFunc, args);
}
/// <summary>
/// Provides nested level debugging support when SetTrace or SetProfile are used.
///
/// This saves the current tracing information and then calls the provided object.
/// </summary>
public static void CallTracing(this ScriptEngine/*!*/ engine, object traceFunc, params object[] args) {
SysModule.call_tracing(GetPythonContext(engine).SharedContext, traceFunc, PythonTuple.MakeTuple(args));
}
/// <summary>
/// Creates a ScriptRuntimeSetup object which includes the Python script engine with the specified options.
///
/// The ScriptRuntimeSetup object can then be additional configured and used to create a ScriptRuntime.
/// </summary>
public static ScriptRuntimeSetup/*!*/ CreateRuntimeSetup(IDictionary<string, object> options) {
ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
setup.LanguageSetups.Add(CreateLanguageSetup(options));
if (options != null) {
object value;
if (options.TryGetValue("Debug", out value) &&
value is bool &&
(bool)value) {
setup.DebugMode = true;
}
if (options.TryGetValue("PrivateBinding", out value) &&
value is bool &&
(bool)value) {
setup.PrivateBinding = true;
}
}
return setup;
}
/// <summary>
/// Creates a LanguageSetup object which includes the Python script engine with the specified options.
///
/// The LanguageSetup object can be used with other LanguageSetup objects from other languages to
/// configure a ScriptRuntimeSetup object.
/// </summary>
public static LanguageSetup/*!*/ CreateLanguageSetup(IDictionary<string, object> options) {
var setup = new LanguageSetup(
typeof(PythonContext).AssemblyQualifiedName,
PythonContext.IronPythonDisplayName,
PythonContext.IronPythonNames.Split(';'),
PythonContext.IronPythonFileExtensions.Split(';')
);
if (options != null) {
foreach (var entry in options) {
setup.Options.Add(entry.Key, entry.Value);
}
}
return setup;
}
/// <summary>
/// Creates a new PythonModule with the specified name and published it in sys.modules.
///
/// Returns the ScriptScope associated with the module.
/// </summary>
public static ScriptScope CreateModule(this ScriptEngine engine, string name) {
return GetPythonService(engine).CreateModule(name, String.Empty, String.Empty);
}
/// <summary>
/// Creates a new PythonModule with the specified name and filename published it
/// in sys.modules.
///
/// Returns the ScriptScope associated with the module.
/// </summary>
public static ScriptScope CreateModule(this ScriptEngine engine, string name, string filename) {
return GetPythonService(engine).CreateModule(name, filename, String.Empty);
}
/// <summary>
/// Creates a new PythonModule with the specified name, filename, and doc string and
/// published it in sys.modules.
///
/// Returns the ScriptScope associated with the module.
/// </summary>
public static ScriptScope CreateModule(this ScriptEngine engine, string name, string filename, string docString) {
return GetPythonService(engine).CreateModule(name, filename, docString);
}
/// <summary>
/// Gets the list of loaded Python module files names which are available in the provided ScriptEngine.
/// </summary>
public static string[] GetModuleFilenames(this ScriptEngine engine) {
return GetPythonService(engine).GetModuleFilenames();
}
#endregion
#region Private helpers
private static PythonService/*!*/ GetPythonService(ScriptEngine/*!*/ engine) {
return engine.GetService<PythonService>(engine);
}
private static PythonContext/*!*/ GetPythonContext(ScriptEngine/*!*/ engine) {
return HostingHelpers.GetLanguageContext(engine) as PythonContext;
}
#endregion
}
}