forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgc.cs
More file actions
118 lines (90 loc) · 4.22 KB
/
gc.cs
File metadata and controls
118 lines (90 loc) · 4.22 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
// 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.Linq;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute;
[assembly: PythonModule("gc", typeof(IronPython.Modules.PythonGC))]
namespace IronPython.Modules {
public static class PythonGC {
public const string __doc__ = "Provides functions for inspecting, configuring, and forcing garbage collection.";
public const int DEBUG_STATS = 1;
public const int DEBUG_COLLECTABLE = 2;
public const int DEBUG_UNCOLLECTABLE = 4;
public const int DEBUG_SAVEALL = 32;
public const int DEBUG_LEAK = (DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE | DEBUG_SAVEALL);
private static readonly object _threadholdKey = new object();
[SpecialName]
public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
context.SetModuleState(_threadholdKey, PythonTuple.MakeTuple(64 * 1024, 256 * 1024, 1024 * 1024));
}
public static void enable() {
}
public static void disable(CodeContext/*!*/ context) {
PythonOps.Warn(context, PythonExceptions.RuntimeWarning, "IronPython has no support for disabling the GC");
}
public static object isenabled() {
return ScriptingRuntimeHelpers.True;
}
public static int collect(CodeContext context, int generation) {
return context.LanguageContext.Collect(generation);
}
public static int collect(CodeContext context) {
return collect(context, GC.MaxGeneration);
}
public static void set_debug(object o) {
throw PythonOps.NotImplementedError("gc.set_debug isn't implemented");
}
public static object get_debug() {
return 0;
}
public static object[] get_objects() {
throw PythonOps.NotImplementedError("gc.get_objects isn't implemented");
}
public static void set_threshold(CodeContext/*!*/ context, params object[] args) {
if(args.Length == 0) {
throw PythonOps.TypeError("set_threshold() takes at least 1 argument (0 given)");
}
if(args.Length > 3) {
throw PythonOps.TypeError("set_threshold() takes at most 3 arguments ({0} given)", args.Length);
}
if(args.Any(x => x is double)) {
throw PythonOps.TypeError("integer argument expected, got float");
}
if(!args.All(x => x is int)) {
throw PythonOps.TypeError("an integer is required");
}
PythonTuple current = get_threshold(context);
object[] threshold = args.Take(args.Length)
.Concat(current.ToArray().Skip(args.Length))
.ToArray();
SetThresholds(context, PythonTuple.MakeTuple(threshold));
}
public static PythonTuple get_threshold(CodeContext/*!*/ context) {
return GetThresholds(context);
}
public static object[] get_referrers(params object[] objs) {
throw PythonOps.NotImplementedError("gc.get_referrers isn't implemented");
}
public static object[] get_referents(params object[] objs) {
throw PythonOps.NotImplementedError("gc.get_referents isn't implemented");
}
public static PythonList garbage {
get {
return new PythonList();
}
}
private static PythonTuple GetThresholds(CodeContext/*!*/ context) {
return (PythonTuple)context.LanguageContext.GetModuleState(_threadholdKey);
}
private static void SetThresholds(CodeContext/*!*/ context, PythonTuple thresholds) {
context.LanguageContext.SetModuleState(_threadholdKey, thresholds);
}
}
}