forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatexit.cs
More file actions
148 lines (121 loc) · 5.35 KB
/
atexit.cs
File metadata and controls
148 lines (121 loc) · 5.35 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
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Scripting;
using IronPython.Runtime;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Exceptions;
[assembly: PythonModule("atexit", typeof(IronPython.Modules.PythonAtExit))]
namespace IronPython.Modules {
public static class PythonAtExit {
public const string __doc__ = @"allow programmer to define multiple exit functions to be executed upon normal program termination.
Two public functions, register and unregister, are defined.
";
private static readonly object _registry_key = new object();
private class FunctionDescriptor {
public FunctionDescriptor(object func, IDictionary<object, object> kwargs, object[] args) {
Func = func;
KeywordArgs = kwargs;
Args = args;
}
public object Func {
get;
}
public IDictionary<object, object> KeywordArgs {
get;
}
public object[] Args {
get;
}
}
[SpecialName]
public static void PerformModuleReload(PythonContext context, PythonDictionary dict) {
if (!context.HasModuleState(_registry_key))
context.SetModuleState(_registry_key, new List<FunctionDescriptor>());
context.SetSystemStateValue("exitfunc", (Action<CodeContext>)_run_exitfuncs);
}
[Documentation(@"register(func, *args, **kwargs) -> func
Register a function to be executed upon normal program termination\n\
func - function to be called at exit
args - optional arguments to pass to func
kwargs - optional keyword arguments to pass to func
func is returned to facilitate usage as a decorator.")]
public static object register(CodeContext context, object func, [ParamDictionary]IDictionary<object, object> kwargs, params object[] args) {
if (!PythonOps.IsCallable(context, func)) {
throw PythonOps.TypeError("the first argument must be callable");
}
var functions = context.LanguageContext.GetModuleState(_registry_key) as List<FunctionDescriptor>;
if (functions != null) {
lock (functions) {
functions.Add(new FunctionDescriptor(func, kwargs, args));
}
}
return func;
}
[Documentation(@"unregister(func) -> None
Unregister an exit function which was previously registered using
atexit.register
func - function to be unregistered")]
public static void unregister(CodeContext context, object func) {
var functions = context.LanguageContext.GetModuleState(_registry_key) as List<FunctionDescriptor>;
if (functions != null) {
lock (functions) {
functions.RemoveAll(x => PythonOps.Compare(context, x.Func, func) == 0);
}
}
}
[Documentation(@"_clear() -> None
Clear the list of previously registered exit functions.")]
public static void _clear(CodeContext context) {
var functions = context.LanguageContext.GetModuleState(_registry_key) as List<FunctionDescriptor>;
if (functions != null) {
lock (functions) {
functions.Clear();
}
}
}
[Documentation(@"_run_exitfuncs() -> None
Run all registered exit functions.")]
public static void _run_exitfuncs(CodeContext context) {
var pc = context.LanguageContext;
var functions = pc.GetModuleState(_registry_key) as List<FunctionDescriptor>;
if (functions != null) {
Exception lastException = null;
lock (functions) {
for (int i = functions.Count - 1; i >= 0; i--) {
var func = functions[i];
try {
if (func.KeywordArgs.Count > 0) {
pc.CallWithKeywordsAndContext(context, func.Func, func.Args, func.KeywordArgs);
} else {
pc.CallWithContext(context, func.Func, func.Args);
}
} catch (SystemExitException ex) {
lastException = ex;
} catch (Exception ex) {
lastException = ex;
PythonOps.PrintWithDest(context, pc.SystemStandardError, "Error in atexit._run_exitfuncs:\n");
PythonOps.PrintException(context, ex);
}
}
}
if (lastException != null) {
throw lastException;
}
}
}
[Documentation(@"_ncallbacks() -> int
Return the number of registered exit functions.")]
public static int _ncallbacks(CodeContext context) {
int result = 0;
var functions = context.LanguageContext.GetModuleState(_registry_key) as List<FunctionDescriptor>;
if (functions != null) {
lock (functions) {
result = functions.Count;
}
}
return result;
}
}
}