/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_CORE_DLR
using System.Linq.Expressions;
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Scripting;
using Microsoft.Scripting.Ast;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using IronPython.Runtime.Binding;
namespace IronPython.Runtime {
///
/// Python module. Stores classes, functions, and data. Usually a module
/// is created by importing a file or package from disk. But a module can also
/// be directly created by calling the module type and providing a name or
/// optionally a documentation string.
///
[PythonType("module"), DebuggerTypeProxy(typeof(PythonModule.DebugProxy)), DebuggerDisplay("module: {GetName()}")]
public class PythonModule : IDynamicMetaObjectProvider, IPythonMembersList {
private readonly PythonDictionary _dict;
private Scope _scope;
public PythonModule() {
_dict = new PythonDictionary();
if (GetType() != typeof(PythonModule) && this is IPythonObject) {
// we should share the user dict w/ our dict.
((IPythonObject)this).ReplaceDict(_dict);
}
}
///
/// Creates a new module backed by a Scope. Used for creating modules for foreign Scope's.
///
internal PythonModule(PythonContext context, Scope scope) {
_dict = new PythonDictionary(new ScopeDictionaryStorage(context, scope));
_scope = scope;
}
///
/// Creates a new PythonModule with the specified dictionary.
///
/// Used for creating modules for builtin modules which don't have any code associated with them.
///
internal PythonModule(PythonDictionary dict) {
_dict = dict;
}
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;
}
[StaticExtensionMethod]
public static PythonModule/*!*/ __new__(CodeContext/*!*/ context, PythonType/*!*/ cls, [ParamDictionary]IDictionary