forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonService.cs
More file actions
149 lines (129 loc) · 5.28 KB
/
PythonService.cs
File metadata and controls
149 lines (129 loc) · 5.28 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
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_REMOTING
using System.Runtime.Remoting;
#else
using MarshalByRefObject = System.Object;
#endif
using System;
using System.Collections.Generic;
using System.Threading;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
using Microsoft.Scripting.Utils;
namespace IronPython.Hosting {
/// <summary>
/// Helper class for implementing the Python class.
///
/// This is exposed as a service through PythonEngine and the helper class
/// uses this service to get the correct remoting semantics.
/// </summary>
public sealed class PythonService : MarshalByRefObject {
private readonly ScriptEngine/*!*/ _engine;
private readonly PythonContext/*!*/ _context;
private ScriptScope _sys, _builtins, _clr;
public PythonService(PythonContext/*!*/ context, ScriptEngine/*!*/ engine) {
Assert.NotNull(context, engine);
_context = context;
_engine = engine;
}
public ScriptScope/*!*/ GetSystemState() {
if (_sys == null) {
Interlocked.CompareExchange(
ref _sys,
HostingHelpers.CreateScriptScope(_engine, _context.SystemState.Scope),
null
);
}
return _sys;
}
public ScriptScope/*!*/ GetBuiltins() {
if (_builtins == null) {
Interlocked.CompareExchange(
ref _builtins,
HostingHelpers.CreateScriptScope(_engine, _context.BuiltinModuleInstance.Scope),
null
);
}
return _builtins;
}
public ScriptScope/*!*/ GetClr() {
if (_clr == null) {
Interlocked.CompareExchange(
ref _clr,
HostingHelpers.CreateScriptScope(_engine, _context.ClrModule.Scope),
null
);
}
return _clr;
}
public ScriptScope/*!*/ CreateModule(string name, string filename, string docString) {
var module = new PythonModule();
_context.PublishModule(name, module);
module.__init__(name, docString);
module.__dict__["__file__"] = filename;
return HostingHelpers.CreateScriptScope(_engine, module.Scope);
}
public ScriptScope/*!*/ ImportModule(ScriptEngine/*!*/ engine, string/*!*/ name) {
PythonModule module = Importer.ImportModule(_context.SharedClsContext, _context.SharedClsContext.GlobalDict, name, false, -1) as PythonModule;
if (module != null) {
return HostingHelpers.CreateScriptScope(engine, module.Scope);
}
throw PythonOps.ImportError("no module named {0}", name);
}
public string[] GetModuleFilenames() {
List<string> res = new List<string>();
PythonDictionary dict = (object)_engine.GetSysModule().GetVariable("modules") as PythonDictionary;
if (dict != null) {
foreach (var kvp in dict) {
string key = kvp.Key as string;
PythonModule module = kvp.Value as PythonModule;
if (key != null && module != null) {
var modDict = module.Get__dict__();
object file;
if (modDict.TryGetValue("__file__", out file) && file != null) {
res.Add(key);
}
}
}
}
return res.ToArray();
}
public void DispatchCommand(Action command) {
_context.DispatchCommand(command);
}
#if FEATURE_REMOTING
public ObjectHandle GetSetCommandDispatcher(ObjectHandle dispatcher) {
var res = _context.GetSetCommandDispatcher((Action<Action>)dispatcher.Unwrap());
if (res != null) {
return new ObjectHandle(res);
}
return null;
}
/// <summary>
/// Returns an ObjectHandle to a delegate of type Action[Action] which calls the current
/// command dispatcher.
/// </summary>
public ObjectHandle GetLocalCommandDispatcher() {
return new ObjectHandle((Action<Action>)(action => _context.DispatchCommand(action)));
}
public override object InitializeLifetimeService() {
return _engine.InitializeLifetimeService();
}
#endif
}
}