forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiledLoader.cs
More file actions
49 lines (41 loc) · 1.84 KB
/
Copy pathCompiledLoader.cs
File metadata and controls
49 lines (41 loc) · 1.84 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
// 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.
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using IronPython.Compiler;
using Microsoft.Scripting;
namespace IronPython.Runtime {
public class CompiledLoader {
private readonly Dictionary<string, OnDiskScriptCode> _codes = new Dictionary<string, OnDiskScriptCode>();
internal void AddScriptCode(ScriptCode code) {
if (code is OnDiskScriptCode onDiskCode) {
if (onDiskCode.ModuleName == "__main__") {
_codes["__main__"] = onDiskCode;
} else {
string name = code.SourceUnit.Path ?? throw new InvalidOperationException("OnDiskScriptCode must have a path");
name = name.Replace(Path.DirectorySeparatorChar, '.');
if (name.EndsWith("__init__.py", StringComparison.Ordinal)) {
name = name.Substring(0, name.Length - ".__init__.py".Length);
}
_codes[name] = onDiskCode;
}
}
}
public ModuleLoader? find_module(CodeContext/*!*/ context, string fullname, PythonList? path = null) {
if (_codes.TryGetValue(fullname, out OnDiskScriptCode? sc)) {
int sep = fullname.LastIndexOf('.');
string name = fullname;
string? parentName = null;
if (sep != -1) {
parentName = fullname.Substring(0, sep);
name = fullname.Substring(sep + 1);
}
return new ModuleLoader(sc, parentName, name);
}
return null;
}
}
}