本文整理汇总了C#中IronPython.Runtime.PythonModule类的典型用法代码示例。如果您正苦于以下问题:C# PythonModule类的具体用法?C# PythonModule怎么用?C# PythonModule使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PythonModule类属于IronPython.Runtime命名空间,在下文中一共展示了PythonModule类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NameEnvironment
public NameEnvironment(PythonModule globals, object locals)
{
this.globals = globals;
if (locals == null) locals = globals.__dict__;
this.locals = locals;
this.builtin = TypeCache.Builtin;
}
示例2: PyModule_New
PyModule_New(string name)
{
PythonModule module = new PythonModule();
module.Get__dict__()["__name__"] = name;
module.Get__dict__()["__doc__"] = "";
return this.Store(module);
}
示例3: Py_InitModule4
Py_InitModule4(string name, IntPtr methodsPtr, string doc, IntPtr selfPtr, int apiver)
{
name = this.FixImportName(name);
PythonDictionary methodTable = new PythonDictionary();
PythonModule module = new PythonModule();
this.AddModule(name, module);
this.CreateModulesContaining(name);
PythonDictionary __dict__ = module.Get__dict__();
__dict__["__doc__"] = doc;
__dict__["__name__"] = name;
string __file__ = this.importFiles.Peek();
__dict__["__file__"] = __file__;
List __path__ = new List();
if (__file__ != null)
{
__path__.append(Path.GetDirectoryName(__file__));
}
__dict__["__path__"] = __path__;
__dict__["_dispatcher"] = new Dispatcher(this, methodTable, selfPtr);
StringBuilder moduleCode = new StringBuilder();
moduleCode.Append(CodeSnippets.USEFUL_IMPORTS);
CallableBuilder.GenerateFunctions(moduleCode, methodsPtr, methodTable);
this.ExecInModule(moduleCode.ToString(), module);
return this.Store(module);
}
示例4: ImportBuiltin
internal static object ImportBuiltin(PythonModule mod, string name)
{
mod.SystemState.TopPackage.Initialize(mod.SystemState);
if (name.Equals("sys")) return mod.SystemState;
if (name.Equals("clr")) {
((ICallerContext)mod).ContextFlags |= CallerContextAttributes.ShowCls;
return ((ICallerContext)mod).SystemState.ClrModule;
}
Type ty;
if (mod.SystemState.TopPackage.Builtins.TryGetValue(name, out ty)) {
// run the type's .cctor before doing any custom reflection on the type.
// This allows modules to lazily initialize DynamicType's to custom values
// rather than having them get populated w/ the ReflectedType. W/o this the
// cctor runs after we've done a bunch of reflection over the type that doesn't
// force the cctor to run.
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(ty.TypeHandle);
if (typeof(CompiledModule).IsAssignableFrom(ty)) {
return InitializeModule(name, CompiledModule.Load(name, ty, mod.SystemState));
} else {
return MakePythonModule(mod.SystemState, name, (ReflectedType)Ops.GetDynamicTypeFromType(ty));
}
}
return null;
}
示例5: ModuleScope
internal ModuleScope(PythonModule mod, IAttributesDictionary globals, object locals)
{
__module__ = mod;
f_globals = globals;
f_locals = locals;
__builtin__ = TypeCache.Builtin;
}
示例6: CreateModule
public ScriptScope/*!*/ CreateModule(string name, string filename, string docString) {
var module = new PythonModule();
_context.PublishModule(name, module);
module.__init__(name, docString);
module.__dict__["__file__"] = filename;
return HostingHelpers.CreateScriptScope(_engine, module.Scope);
}
示例7: ModuleContext
/// <summary>
/// Creates a new ModuleContext for the specified module.
/// </summary>
public ModuleContext(PythonModule/*!*/ module, PythonContext/*!*/ creatingContext) {
ContractUtils.RequiresNotNull(module, "module");
ContractUtils.RequiresNotNull(creatingContext, "creatingContext");
_globals = module.__dict__;
_pyContext = creatingContext;
_globalContext = new CodeContext(_globals, this);
_module = module;
}
示例8: BuiltinModule
public BuiltinModule(PythonModule module, ProjectState projectState, bool showClr)
: base(new LazyDotNetDict(new object[] { module }, projectState, showClr))
{
object name;
if (!module.Get__dict__().TryGetValue("__name__", out name) || !(name is string)) {
_name = String.Empty;
} else {
_name = name as string;
}
}
示例9: EngineModule
internal EngineModule(string moduleName, IDictionary<string, object> globalsDict, SystemState systemState)
{
Debug.Assert(moduleName != null);
globals = globalsDict;
if (globals is IAttributesDictionary)
globalsAdapter = globals as IAttributesDictionary;
else
globalsAdapter = new StringDictionaryAdapterDict(globalsDict);
PythonModule pythonModule = new PythonModule(moduleName, globalsAdapter, systemState);
defaultModuleScope = new ModuleScope(pythonModule);
}
示例10: __new__
public static PythonModule/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, params object[]/*!*/ args\u00F8) {
PythonModule res;
if (cls == TypeCache.Module) {
res = new PythonModule();
} else if (cls.IsSubclassOf(TypeCache.Module)) {
res = (PythonModule)cls.CreateInstance(context);
} else {
throw PythonOps.TypeError("{0} is not a subtype of module", cls.Name);
}
return res;
}
示例11: TryGetNameAndPath
/// <summary>
/// Interrogates the importing module for __name__ and __path__, which determine
/// whether the imported module (whose name is 'name') is being imported as nested
/// module (__path__ is present) or as sibling.
///
/// For sibling import, the full name of the imported module is parent.sibling
/// For nested import, the full name of the imported module is parent.module.nested
/// where parent.module is the mod.__name__
/// </summary>
/// <param name="context"></param>
/// <param name="globals">the globals dictionary</param>
/// <param name="name">Name of the module to be imported</param>
/// <param name="full">Output - full name of the module being imported</param>
/// <param name="path">Path to use to search for "full"</param>
/// <param name="level">the import level for relaive imports</param>
/// <param name="parentMod">the parent module</param>
/// <param name="package">the global __package__ value</param>
/// <returns></returns>
private static bool TryGetNameAndPath(CodeContext/*!*/ context, object globals, string name, int level, string package, out string full, out List path, out PythonModule parentMod) {
Debug.Assert(level != 0); // shouldn't be here for absolute imports
// Unless we can find enough information to perform relative import,
// we are going to import the module whose name we got
full = name;
path = null;
parentMod = null;
// We need to get __name__ to find the name of the imported module.
// If absent, fall back to absolute import
object attribute;
PythonDictionary pyGlobals = globals as PythonDictionary;
if (pyGlobals == null || !pyGlobals._storage.TryGetName(out attribute)) {
return false;
}
// And the __name__ needs to be string
string modName = attribute as string;
if (modName == null) {
return false;
}
string pn;
if (package == null) {
// If the module has __path__ (and __path__ is list), nested module is being imported
// otherwise, importing sibling to the importing module
if (pyGlobals._storage.TryGetPath(out attribute) && (path = attribute as List) != null) {
// found __path__, importing nested module. The actual name of the nested module
// is the name of the mod plus the name of the imported module
if (level == -1) {
// absolute import of some module
full = modName + "." + name;
object parentModule;
if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(modName, out parentModule)) {
parentMod = parentModule as PythonModule;
}
} else if (String.IsNullOrEmpty(name)) {
// relative import of ancestor
full = (StringOps.rsplit(modName, ".", level - 1)[0] as string);
} else {
// relative import of some ancestors child
string parentName = (StringOps.rsplit(modName, ".", level - 1)[0] as string);
full = parentName + "." + name;
object parentModule;
if (PythonContext.GetContext(context).SystemStateModules.TryGetValue(parentName, out parentModule)) {
parentMod = parentModule as PythonModule;
}
}
return true;
}
// importing sibling. The name of the imported module replaces
// the last element in the importing module name
string[] names = modName.Split('.');
if (names.Length == 1) {
// name doesn't include dot, only absolute import possible
if (level > 0) {
throw PythonOps.ValueError("Attempted relative import in non-package");
}
return false;
}
pn = GetParentPackageName(level, names);
} else {
// __package__ doesn't include module name, so level is - 1.
pn = GetParentPackageName(level - 1, package.Split('.'));
}
path = GetParentPathAndModule(context, pn, out parentMod);
if (path != null) {
if (String.IsNullOrEmpty(name)) {
full = pn;
} else {
full = pn + "." + name;
}
return true;
}
if (level > 0) {
//.........这里部分代码省略.........
示例12: ImportModule
/// <summary>
/// Called by the __builtin__.__import__ functions (general importing) and ScriptEngine (for site.py)
///
/// level indiciates whether to perform absolute or relative imports.
/// -1 indicates both should be performed
/// 0 indicates only absolute imports should be performed
/// Positive numbers indicate the # of parent directories to search relative to the calling module
/// </summary>
public static object ImportModule(CodeContext/*!*/ context, object globals, string/*!*/ modName, bool bottom, int level) {
if (modName.IndexOf(Path.DirectorySeparatorChar) != -1) {
throw PythonOps.ImportError("Import by filename is not supported.", modName);
}
string package = null;
object attribute;
PythonDictionary pyGlobals = globals as PythonDictionary;
if (pyGlobals != null) {
if (pyGlobals._storage.TryGetPackage(out attribute)) {
package = attribute as string;
if (package == null && attribute != null) {
throw PythonOps.ValueError("__package__ set to non-string");
}
} else {
package = null;
if (level > 0) {
// explicit relative import, calculate and store __package__
object pathAttr, nameAttr;
if (pyGlobals._storage.TryGetName(out nameAttr) && nameAttr is string) {
if (pyGlobals._storage.TryGetPath(out pathAttr)) {
pyGlobals["__package__"] = nameAttr;
} else {
pyGlobals["__package__"] = ((string)nameAttr).rpartition(".")[0];
}
}
}
}
}
object newmod = null;
string[] parts = modName.Split('.');
string finalName = null;
if (level != 0) {
// try a relative import
// if importing a.b.c, import "a" first and then import b.c from a
string name; // name of the module we are to import in relation to the current module
PythonModule parentModule;
List path; // path to search
if (TryGetNameAndPath(context, globals, parts[0], level, package, out name, out path, out parentModule)) {
finalName = name;
// import relative
if (!TryGetExistingOrMetaPathModule(context, name, path, out newmod)) {
newmod = ImportFromPath(context, parts[0], name, path);
if (newmod != null && parentModule != null) {
parentModule.__dict__[modName] = newmod;
}
} else if (parts.Length == 1) {
// if we imported before having the assembly
// loaded and then loaded the assembly we want
// to make the assembly available now.
if (newmod is NamespaceTracker) {
context.ShowCls = true;
}
}
}
}
if (level <= 0) {
// try an absolute import
if (newmod == null) {
object parentPkg;
if (!String.IsNullOrEmpty(package) && !PythonContext.GetContext(context).SystemStateModules.TryGetValue(package, out parentPkg)) {
PythonModule warnModule = new PythonModule();
warnModule.__dict__["__file__"] = package;
warnModule.__dict__["__name__"] = package;
ModuleContext modContext = new ModuleContext(warnModule.__dict__, context.LanguageContext);
PythonOps.Warn(
modContext.GlobalContext,
PythonExceptions.RuntimeWarning,
"Parent module '{0}' not found while handling absolute import",
package);
}
newmod = ImportTopAbsolute(context, parts[0]);
finalName = parts[0];
if (newmod == null) {
return null;
}
}
}
// now import the a.b.c etc. a needs to be included here
// because the process of importing could have modified
// sys.modules.
object next = newmod;
string curName = null;
for (int i = 0; i < parts.Length; i++) {
curName = i == 0 ? finalName : curName + "." + parts[i];
//.........这里部分代码省略.........
示例13: ReloadBuiltinModule
private static void ReloadBuiltinModule(CodeContext/*!*/ context, PythonModule/*!*/ module) {
Assert.NotNull(module);
Debug.Assert(module.GetName() is string, "Module is reloadable only if its name is a non-null string");
Type type;
string name = (string)module.GetName();
PythonContext pc = PythonContext.GetContext(context);
if (!pc.Builtins.TryGetValue(name, out type)) {
throw new NotImplementedException();
}
// should be a built-in module which we can reload.
Debug.Assert(module.Scope.Dict is PythonDictionary);
Debug.Assert(((PythonDictionary)module.Scope.Dict)._storage is ModuleDictionaryStorage);
((ModuleDictionaryStorage)((PythonDictionary)module.Scope.Dict)._storage).Reload();
}
示例14: DebugProxy
public DebugProxy(PythonModule module) {
_module = module;
}
示例15: Import
/// <summary>
/// Gateway into importing ... called from Ops. Performs the initial import of
/// a module and returns the module.
/// </summary>
internal static object Import(PythonModule mod, string fullName, List from)
{
object importFunction = FindImportFunction(mod);
return Ops.CallWithContext(mod, importFunction, fullName, null, null, from);
}