Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit33b107c

Browse files
committed
Move clr module to separate file
1 parent3c4dd16 commit33b107c

File tree

2 files changed

+198
-191
lines changed

2 files changed

+198
-191
lines changed

‎src/runtime/Types/ClrModule.cs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
usingSystem;
2+
usingSystem.Linq;
3+
usingSystem.IO;
4+
usingSystem.Reflection;
5+
6+
namespacePython.Runtime
7+
{
8+
/// <summary>
9+
/// The CLR module is the root handler used by the magic import hook
10+
/// to import assemblies. It has a fixed module name "clr" and doesn't
11+
/// provide a namespace.
12+
/// </summary>
13+
[Serializable]
14+
internalclassCLRModule:ModuleObject
15+
{
16+
protectedstaticboolinteractive_preload=true;
17+
internalstaticboolpreload;
18+
// XXX Test performance of new features //
19+
internalstaticbool_SuppressDocs=false;
20+
internalstaticbool_SuppressOverloads=false;
21+
22+
staticCLRModule()
23+
{
24+
Reset();
25+
}
26+
27+
privateCLRModule():base("clr")
28+
{
29+
_namespace=string.Empty;
30+
}
31+
32+
internalstaticNewReferenceCreate(outCLRModulemodule)
33+
{
34+
module=newCLRModule();
35+
returnmodule.Alloc();
36+
}
37+
38+
publicstaticvoidReset()
39+
{
40+
interactive_preload=true;
41+
preload=false;
42+
43+
// XXX Test performance of new features //
44+
_SuppressDocs=false;
45+
_SuppressOverloads=false;
46+
}
47+
48+
/// <summary>
49+
/// The initializing of the preload hook has to happen as late as
50+
/// possible since sys.ps1 is created after the CLR module is
51+
/// created.
52+
/// </summary>
53+
internalvoidInitializePreload()
54+
{
55+
if(interactive_preload)
56+
{
57+
interactive_preload=false;
58+
if(!Runtime.PySys_GetObject("ps1").IsNull)
59+
{
60+
preload=true;
61+
}
62+
else
63+
{
64+
Exceptions.Clear();
65+
preload=false;
66+
}
67+
}
68+
}
69+
70+
[ModuleFunction]
71+
publicstaticboolgetPreload()
72+
{
73+
returnpreload;
74+
}
75+
76+
[ModuleFunction]
77+
publicstaticvoidsetPreload(boolpreloadFlag)
78+
{
79+
preload=preloadFlag;
80+
}
81+
82+
//[ModuleProperty]
83+
publicstaticboolSuppressDocs
84+
{
85+
get{return_SuppressDocs;}
86+
set{_SuppressDocs=value;}
87+
}
88+
89+
//[ModuleProperty]
90+
publicstaticboolSuppressOverloads
91+
{
92+
get{return_SuppressOverloads;}
93+
set{_SuppressOverloads=value;}
94+
}
95+
96+
[ModuleFunction]
97+
[ForbidPythonThreads]
98+
publicstaticAssemblyAddReference(stringname)
99+
{
100+
AssemblyManager.UpdatePath();
101+
varorigNs=AssemblyManager.GetNamespaces();
102+
Assembly?assembly=AssemblyManager.FindLoadedAssembly(name);
103+
if(assembly==null)
104+
{
105+
assembly=AssemblyManager.LoadAssemblyPath(name);
106+
}
107+
if(assembly==null&&AssemblyManager.TryParseAssemblyName(name)is{}parsedName)
108+
{
109+
assembly=AssemblyManager.LoadAssembly(parsedName);
110+
}
111+
if(assembly==null)
112+
{
113+
assembly=AssemblyManager.LoadAssemblyFullPath(name);
114+
}
115+
if(assembly==null)
116+
{
117+
thrownewFileNotFoundException($"Unable to find assembly '{name}'.");
118+
}
119+
// Classes that are not in a namespace needs an extra nudge to be found.
120+
ImportHook.UpdateCLRModuleDict();
121+
122+
// A bit heavyhanded, but we can't use the AssemblyManager's AssemblyLoadHandler
123+
// method because it may be called from other threads, leading to deadlocks
124+
// if it is called while Python code is executing.
125+
varcurrNs=AssemblyManager.GetNamespaces().Except(origNs);
126+
foreach(varnsincurrNs)
127+
{
128+
ImportHook.AddNamespaceWithGIL(ns);
129+
}
130+
returnassembly;
131+
}
132+
133+
/// <summary>
134+
/// Get a Type instance for a class object.
135+
/// clr.GetClrType(IComparable) gives you the Type for IComparable,
136+
/// that you can e.g. perform reflection on. Similar to typeof(IComparable) in C#
137+
/// or clr.GetClrType(IComparable) in IronPython.
138+
///
139+
/// </summary>
140+
/// <param name="type"></param>
141+
/// <returns>The Type object</returns>
142+
143+
[ModuleFunction]
144+
publicstaticTypeGetClrType(Typetype)
145+
{
146+
returntype;
147+
}
148+
149+
[ModuleFunction]
150+
[ForbidPythonThreads]
151+
publicstaticstringFindAssembly(stringname)
152+
{
153+
AssemblyManager.UpdatePath();
154+
returnAssemblyManager.FindAssembly(name);
155+
}
156+
157+
[ModuleFunction]
158+
publicstaticstring[]ListAssemblies(boolverbose)
159+
{
160+
AssemblyName[]assnames=AssemblyManager.ListAssemblies();
161+
varnames=newstring[assnames.Length];
162+
for(vari=0;i<assnames.Length;i++)
163+
{
164+
if(verbose)
165+
{
166+
names[i]=assnames[i].FullName;
167+
}
168+
else
169+
{
170+
names[i]=assnames[i].Name;
171+
}
172+
}
173+
returnnames;
174+
}
175+
176+
/// <summary>
177+
/// Note: This should *not* be called directly.
178+
/// The function that get/import a CLR assembly as a python module.
179+
/// This function should only be called by the import machinery as seen
180+
/// in importhook.cs
181+
/// </summary>
182+
/// <param name="spec">A ModuleSpec Python object</param>
183+
/// <returns>A new reference to the imported module, as a PyObject.</returns>
184+
[ModuleFunction]
185+
[ForbidPythonThreads]
186+
publicstaticPyObject_load_clr_module(PyObjectspec)
187+
{
188+
usingvarmodname=spec.GetAttr("name");
189+
stringname=modname.As<string?>()??thrownewArgumentException("name must not be None");
190+
varmod=ImportHook.Import(name);
191+
returnmod;
192+
}
193+
194+
[ModuleFunction]
195+
[ForbidPythonThreads]
196+
publicstaticint_add_pending_namespaces()=>ImportHook.AddPendingNamespaces();
197+
}
198+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp