forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.cs
More file actions
101 lines (84 loc) · 3.55 KB
/
Console.cs
File metadata and controls
101 lines (84 loc) · 3.55 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
/* ****************************************************************************
*
* 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
* ironpy@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.
*
*
* ***************************************************************************/
using System;
using System.Diagnostics;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
using Microsoft.Scripting.Hosting.Shell;
using IronPython.Hosting;
using IronPython.Runtime;
internal sealed class PythonConsoleHost : ConsoleHost {
protected override Type Provider {
get { return typeof(PythonContext); }
}
protected override CommandLine/*!*/ CreateCommandLine() {
return new PythonCommandLine();
}
protected override OptionsParser/*!*/ CreateOptionsParser() {
return new PythonOptionsParser();
}
protected override ScriptRuntimeSetup CreateRuntimeSetup() {
ScriptRuntimeSetup srs = ScriptRuntimeSetup.ReadConfiguration();
foreach (var langSetup in srs.LanguageSetups) {
if (langSetup.FileExtensions.Contains(".py")) {
langSetup.Options["SearchPaths"] = new string[0];
}
}
return srs;
}
protected override IConsole CreateConsole(ScriptEngine engine, CommandLine commandLine, ConsoleOptions options) {
PythonConsoleOptions pyoptions = (PythonConsoleOptions)options;
return pyoptions.BasicConsole ? new BasicConsole(options.ColorfulConsole) : new SuperConsole(commandLine, options.ColorfulConsole);
}
protected override void ParseHostOptions(string/*!*/[]/*!*/ args) {
// Python doesn't want any of the DLR base options.
foreach (string s in args) {
Options.IgnoredArgs.Add(s);
}
}
protected override void ExecuteInternal() {
var pc = HostingHelpers.GetLanguageContext(Engine) as PythonContext;
pc.SetModuleState(typeof(ScriptEngine), Engine);
base.ExecuteInternal();
}
#if DEBUG
private static string[] MaybeAttachDebugger(string[] args) {
int attachDebugger = Array.IndexOf(args, "-X:Attach");
if (attachDebugger != -1) {
// Remove -X:Attach from the arg list, since after this point it's no use
string[] newArgs = new string[args.Length - 1];
Array.Copy(args, newArgs, attachDebugger);
Array.Copy(args, attachDebugger + 1, newArgs, attachDebugger, newArgs.Length - attachDebugger);
args = newArgs;
// Launch a debugger. This seems to be more reliable than
// Debugger.Break().
if (Debugger.IsAttached == false) Debugger.Launch();
}
return args;
}
#endif
[STAThread]
public static int Main(string[] args) {
// Work around issue w/ pydoc - piping to more doesn't work so
// instead indicate that we're a dumb terminal
if (Environment.GetEnvironmentVariable("TERM") == null) {
Environment.SetEnvironmentVariable("TERM", "dumb");
}
#if DEBUG
args = MaybeAttachDebugger(args);
#endif
return new PythonConsoleHost().Run(args);
}
}