public Interactive(PythonList body) : this() { _body = body; }
public With(expr context_expr, [Optional]expr optional_vars, PythonList body, [Optional]int? lineno, [Optional]int? col_offset) : this() { _context_expr = context_expr; _optional_vars = optional_vars; _body = body; _lineno = lineno; _col_offset = col_offset; }
private static PythonTuple FindModuleBuiltinOrPath(CodeContext /*!*/ context, string name, PythonList path) { if (name.Equals("sys")) { return(BuiltinModuleTuple(name)); } if (name.Equals("clr")) { context.ShowCls = true; return(BuiltinModuleTuple(name)); } Type ty; if (context.LanguageContext.BuiltinModules.TryGetValue(name, out ty)) { return(BuiltinModuleTuple(name)); } return(FindModulePath(context, name, path)); }
public TryExcept(PythonList body, PythonList handlers, [Optional]PythonList orelse, [Optional]int? lineno, [Optional]int? col_offset ) : this() { _body = body; _handlers = handlers; if (null == orelse) _orelse = new PythonList(); else _orelse = orelse; _lineno = lineno; _col_offset = col_offset; }
public Tuple(PythonList elts, expr_context ctx, [Optional]int? lineno, [Optional]int? col_offset) : this() { _elts = elts; _ctx = ctx; _lineno = lineno; _col_offset = col_offset; }
public Set(PythonList elts, [Optional]int? lineno, [Optional]int? col_offset) : this() { _elts = elts; _lineno = lineno; _col_offset = col_offset; }
public Assign(PythonList targets, expr value, [Optional]int? lineno, [Optional]int? col_offset) : this() { _targets = targets; _value = value; _lineno = lineno; _col_offset = col_offset; }
private bool ListContains(CallSite site, object other, PythonList value) { return(value.ContainsWorker(other)); }
/// <summary> /// Process a sequence of objects that are compatible with ObjectToSocket(). Return two /// things as out params: an in-order List of sockets that correspond to the original /// objects in the passed-in sequence, and a mapping of these socket objects to their /// original objects. /// /// The socketToOriginal mapping is generated because the CPython select module supports /// passing to select either file descriptor numbers or an object with a fileno() method. /// We try to be faithful to what was originally requested when we return. /// </summary> private static void ProcessSocketSequence(CodeContext context, object sequence, out PythonList socketList, out Dictionary <Socket, object> socketToOriginal) { socketToOriginal = new Dictionary <Socket, object>(); socketList = new PythonList(); IEnumerator cursor = PythonOps.GetEnumerator(sequence); while (cursor.MoveNext()) { object original = cursor.Current; Socket socket = ObjectToSocket(context, original); socketList.append(socket); socketToOriginal[socket] = original; } }
private static Assembly LoadAssembly(string name, string hintPath = null) { string assemblyPath; // If assembly was already loaded, just return it Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == name); if (assembly != null) { return(assembly); } // If the path is rooted, just load the file if (Path.IsPathRooted(name)) { assembly = CheckAndLoad(name); if (assembly != null) { return(assembly); } } // If a hint path is given, try to find the assembly if (hintPath != null) { assemblyPath = Path.Combine(hintPath, name); if (!assemblyPath.ToLower().EndsWith(".dll")) { assemblyPath += ".dll"; } if (File.Exists(assemblyPath)) { return(CheckAndLoad(assemblyPath)); } } // If the name is not a file, query the GAC bool hasExtension = name.ToLower().EndsWith(".dll"); assemblyPath = name; if (!hasExtension) { assemblyPath += ".dll"; } assemblyPath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), assemblyPath); if (File.Exists(assemblyPath)) { return(CheckAndLoad(assemblyPath)); } // Then check in python path PythonList pythonPath = (PythonList)PySys_GetObject("path"); for (int i = 0; i < pythonPath.Size; i++) { string fullPath = (pythonPath[i] as PythonString).ToString(); if (!Path.IsPathRooted(fullPath)) { fullPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), fullPath); } string filePath = Path.Combine(fullPath, name); if (File.Exists(filePath)) { return(CheckAndLoad(filePath)); } if (!hasExtension) { filePath = Path.Combine(fullPath, name + ".dll"); if (File.Exists(filePath)) { return(CheckAndLoad(filePath)); } } } return(null); }
private KeyValuePair <IEnumerator, IDisposable> GetListEnumerator(CallSite site, PythonList value) { return(new KeyValuePair <IEnumerator, IDisposable>(new ListIterator(value), null)); }
public CharRNNModel(CharRNNModelParameters parameters, bool training = true) { this.parameters = parameters ?? throw new ArgumentNullException(nameof(parameters)); if (!training) { this.parameters.BatchSize = 1; this.parameters.SeqLength = 1; } if (!ModelTypeToCellFunction.TryGetValue(parameters.ModelType, out this.cellFactory)) { throw new NotSupportedException(parameters.ModelType.ToString()); } for (int i = 0; i < parameters.LayerCount; i++) { RNNCell cell = this.cellFactory(parameters.RNNSize); if (training && (parameters.KeepOutputProbability < 1 || parameters.KeepInputProbability < 1)) { cell = new DropoutWrapper(cell, input_keep_prob: parameters.KeepInputProbability, output_keep_prob: parameters.KeepOutputProbability); } this.cells.Add(cell); } this.rnn = new MultiRNNCell(this.cells, state_is_tuple: true); this.inputData = tf.placeholder(tf.int32, new TensorShape(parameters.BatchSize, parameters.SeqLength)); this.targets = tf.placeholder(tf.int32, new TensorShape(parameters.BatchSize, parameters.SeqLength)); this.initialState = this.rnn.zero_state(parameters.BatchSize, tf.float32); Variable softmax_W = null, softmax_b = null; new variable_scope("rnnlm").UseSelf(_ => { softmax_W = tf.get_variable("softmax_w", new TensorShape(parameters.RNNSize, parameters.VocabularySize)); softmax_b = tf.get_variable("softmax_b", new TensorShape(parameters.VocabularySize)); }); Variable embedding = tf.get_variable("embedding", new TensorShape(parameters.VocabularySize, parameters.RNNSize)); Tensor input = tf.nn.embedding_lookup(embedding, this.inputData); // dropout beta testing: double check which one should affect next line if (training && parameters.KeepOutputProbability < 1) { input = tf.nn.dropout(input, parameters.KeepOutputProbability); } PythonList <Tensor> inputs = tf.split(input, parameters.SeqLength, axis: 1); inputs = inputs.Select(i => (Tensor)tf.squeeze(i, axis: 1)).ToPythonList(); dynamic Loop(dynamic prev, dynamic _) { prev = tf.matmul(prev, softmax_W) + softmax_b; var prevSymbol = tf.stop_gradient(tf.argmax(prev, 1)); return(tf.nn.embedding_lookup(embedding, prevSymbol)); } var decoder = tensorflow.contrib.legacy_seq2seq.legacy_seq2seq.rnn_decoder(inputs, initialState.Items().Cast <object>(), this.rnn, loop_function: training ? null : PythonFunctionContainer.Of(new Func <dynamic, dynamic, dynamic>(Loop)), scope: "rnnlm"); var outputs = decoder.Item1; var lastState = (seq2seqState)decoder.Item2; dynamic contatenatedOutputs = tf.concat(outputs, 1); var output = tensorflow.tf.reshape(contatenatedOutputs, new[] { -1, parameters.RNNSize }); this.logits = tf.matmul(output, softmax_W) + softmax_b; this.probs = tf.nn.softmax(new[] { this.logits }); this.loss = tensorflow.contrib.legacy_seq2seq.legacy_seq2seq.sequence_loss_by_example( new[] { this.logits }, new[] { tf.reshape(targets, new[] { -1 }) }, new[] { tf.ones(new[] { parameters.BatchSize *parameters.SeqLength }) }); Tensor cost = null; new name_scope("cost").UseSelf(_ => { cost = tf.reduce_sum(this.loss) / parameters.BatchSize / parameters.SeqLength; }); this.cost = cost; this.finalState = lastState; this.learningRate = new Variable(0.0, trainable: false); var tvars = tf.trainable_variables(); IEnumerable <object> grads = tf.clip_by_global_norm(tf.gradients(this.cost, tvars), parameters.GradientClip).Item1; AdamOptimizer optimizer = null; new name_scope("optimizer").UseSelf(_ => optimizer = new AdamOptimizer(this.learningRate)); this.trainOp = optimizer.apply_gradients(grads.Zip(tvars, (grad, @var) => (dynamic)(grad, @var))); tf.summary.histogram("logits", new[] { this.logits }); tf.summary.histogram("loss", new[] { this.loss }); tf.summary.histogram("train_loss", new[] { this.cost }); }
public static PythonTuple find_module(CodeContext /*!*/ context, string /*!*/ name, PythonList path) { if (name == null) { throw PythonOps.TypeError("find_module() argument 1 must be string, not None"); } if (path == null) { return(FindBuiltinOrSysPath(context, name)); } else { return(FindModulePath(context, name, path)); } }
public static PythonList get_suffixes() { return(PythonList.FromArrayNoCopy(PythonOps.MakeTuple(".py", "U", PythonSource))); }
public Module(PythonList body) : this() { _body = body; }
public static void warn(CodeContext context, object message, PythonType category = null, int stacklevel = 1) { PythonContext pContext = context.LanguageContext; PythonList argv = pContext.GetSystemStateValue("argv") as PythonList; if (PythonOps.IsInstance(message, PythonExceptions.Warning)) { category = DynamicHelpers.GetPythonType(message); } if (category == null) { category = PythonExceptions.UserWarning; } if (!category.IsSubclassOf(PythonExceptions.Warning)) { throw PythonOps.ValueError("category is not a subclass of Warning"); } TraceBackFrame caller = null; PythonDictionary globals; int lineno; if (context.LanguageContext.PythonOptions.Frames) { try { caller = SysModule._getframeImpl(context, stacklevel - 1); } catch (ValueErrorException) { } } if (caller == null) { globals = Builtin.globals(context) as PythonDictionary; lineno = 1; } else { globals = caller.f_globals; lineno = (int)caller.f_lineno; } string module; string filename; if (globals != null && globals.ContainsKey("__name__")) { module = (string)globals.get("__name__"); } else { module = "<string>"; } filename = globals.get("__file__") as string; if (filename == null || filename == "") { if (module == "__main__") { if (argv != null && argv.Count > 0) { filename = argv[0] as string; } else { // interpreter lacks sys.argv filename = "__main__"; } } if (filename == null || filename == "") { filename = module; } } PythonDictionary registry = (PythonDictionary)globals.setdefault("__warningregistry__", new PythonDictionary()); warn_explicit(context, message, category, filename, lineno, module, registry, globals); }
public Print([Optional]expr dest, PythonList values, bool nl, [Optional]int? lineno, [Optional]int? col_offset) : this() { _dest = dest; _values = values; _nl = nl; _lineno = lineno; _col_offset = col_offset; }
internal static int CompareToWorker(CodeContext /*!*/ context, IDictionary <object, object> left, PythonList ritems) { PythonList litems = ToList(left); litems.sort(context); ritems.sort(context); return(litems.CompareToWorker(ritems)); }
public SetComp(expr elt, PythonList generators, [Optional]int? lineno, [Optional]int? col_offset) : this() { _elt = elt; _generators = generators; _lineno = lineno; _col_offset = col_offset; }
public Bytes /*!*/ center(int width, PythonList fillchar) { throw PythonOps.TypeError("center() argument 2 must be byte, not list"); }
internal Assign(AssignmentStatement stmt) : this() { _targets = PythonOps.MakeEmptyList(stmt.Left.Count); foreach (AstExpression expr in stmt.Left) _targets.Add(Convert(expr, Store.Instance)); _value = Convert(stmt.Right); }
public int count(PythonList /*!*/ ssub, int start, int end) { throw PythonOps.TypeError("expected bytes or bytearray, got list"); }
public TryFinally(PythonList body, PythonList finalBody, [Optional]int? lineno, [Optional]int? col_offset) : this() { _body = body; _finalbody = finalbody; _lineno = lineno; _col_offset = col_offset; }
public bool endswith(PythonList /*!*/ suffix, int start, int end) { throw PythonOps.TypeError("expected bytes or bytearray, got list"); }
public While(expr test, PythonList body, [Optional]PythonList orelse, [Optional]int? lineno, [Optional]int? col_offset) : this() { _test = test; _body = body; if (null == orelse) _orelse = new PythonList(); else _orelse = orelse; _lineno = lineno; _col_offset = col_offset; }
public static int Where(this PythonList list, PythonFunction function) { return(42); }
internal static PythonList Convert(IList<ComprehensionIterator> iterators) { ComprehensionIterator[] iters = new ComprehensionIterator[iterators.Count]; iterators.CopyTo(iters, 0); PythonList comps = new PythonList(); int start = 1; for (int i = 0; i < iters.Length; i++) { if (i == 0 || iters[i] is ComprehensionIf) if (i == iters.Length - 1) i++; else continue; ComprehensionIf[] ifs = new ComprehensionIf[i - start]; Array.Copy(iters, start, ifs, 0, ifs.Length); comps.Add(new comprehension((ComprehensionFor)iters[start - 1], ifs)); start = i + 1; } return comps; }
internal static object ReloadModule(CodeContext /*!*/ context, PythonModule /*!*/ module, PythonFile file) { PythonContext pc = context.LanguageContext; // We created the module and it only contains Python code. If the user changes // __file__ we'll reload from that file. string fileName = module.GetFile() as string; // built-in module: if (fileName == null) { ReloadBuiltinModule(context, module); return(module); } string name = module.GetName(); if (name != null) { PythonList path = null; // find the parent module and get it's __path__ property int dotIndex = name.LastIndexOf('.'); if (dotIndex != -1) { PythonModule parentModule; path = GetParentPathAndModule(context, name.Substring(0, dotIndex), out parentModule); } object reloaded; if (TryLoadMetaPathModule(context, module.GetName() as string, path, out reloaded) && reloaded != null) { return(module); } PythonList sysPath; if (context.LanguageContext.TryGetSystemPath(out sysPath)) { object ret = ImportFromPathHook(context, name, name, sysPath, null); if (ret != null) { return(ret); } } } SourceUnit sourceUnit; if (file != null) { sourceUnit = pc.CreateSourceUnit(new PythonFileStreamContentProvider(file), fileName, file.Encoding, SourceCodeKind.File); } else { if (!pc.DomainManager.Platform.FileExists(fileName)) { throw PythonOps.SystemError("module source file not found"); } sourceUnit = pc.CreateFileUnit(fileName, pc.DefaultEncoding, SourceCodeKind.File); } pc.GetScriptCode(sourceUnit, name, ModuleOptions.None, Compiler.CompilationMode.Lookup).Run(module.Scope); return(module); }
public ImportFrom(FromImportStatement stmt) : this() { _module = stmt.Root.MakeString(); _module = string.IsNullOrEmpty(_module) ? null : _module; _names = ConvertAliases(stmt.Names, stmt.AsNames); if (stmt.Root is RelativeModuleName) _level = ((RelativeModuleName)stmt.Root).DotCount; }
public Bytes([NotNull] PythonList bytes) { _bytes = ByteOps.GetBytes(bytes).ToArray(); }
internal Interactive(SuiteStatement suite) : this() { _body = ConvertStatements(suite); }
private object UpdateStack(object res) { ProcStack curStack = _stack.Peek(); switch (curStack.StackType) { case StackType.Dict: PythonDictionary od = curStack.StackObj as PythonDictionary; if (curStack.HaveKey) { od[curStack.Key] = res; curStack.HaveKey = false; } else { curStack.HaveKey = true; curStack.Key = res; } break; case StackType.Tuple: List <object> objs = curStack.StackObj as List <object>; objs.Add(res); curStack.StackCount--; if (curStack.StackCount == 0) { _stack.Pop(); object tuple = PythonTuple.Make(objs); if (_stack.Count == 0) { _result = tuple; } return(tuple); } break; case StackType.List: PythonList ol = curStack.StackObj as PythonList; ol.AddNoLock(res); curStack.StackCount--; if (curStack.StackCount == 0) { _stack.Pop(); if (_stack.Count == 0) { _result = ol; } return(ol); } break; case StackType.Set: SetCollection os = curStack.StackObj as SetCollection; os.add(res); curStack.StackCount--; if (curStack.StackCount == 0) { _stack.Pop(); if (_stack.Count == 0) { _result = os; } return(os); } break; case StackType.FrozenSet: List <object> ofs = curStack.StackObj as List <object>; ofs.Add(res); curStack.StackCount--; if (curStack.StackCount == 0) { _stack.Pop(); object frozenSet = FrozenSetCollection.Make(TypeCache.FrozenSet, ofs); if (_stack.Count == 0) { _result = frozenSet; } return(frozenSet); } break; } return(null); }
internal Module(SuiteStatement suite) : this() { _body = ConvertStatements(suite); }
private static void QueryValueExImpl(SafeRegistryHandle handle, string valueName, out int valueKind, out object value) { valueName = valueName ?? ""; // it looks like RegQueryValueEx can fail with null, use empty string instead valueKind = 0; int dwRet; byte[] data = new byte[128]; uint length = (uint)data.Length; // query the size first, reading the data as we query... dwRet = RegQueryValueEx(handle, valueName, IntPtr.Zero, out valueKind, data, ref length); while (dwRet == ERROR_MORE_DATA) { data = new byte[data.Length * 2]; length = (uint)data.Length; dwRet = RegQueryValueEx(handle, valueName, IntPtr.Zero, out valueKind, data, ref length); } if (dwRet == PythonExceptions._OSError.ERROR_FILE_NOT_FOUND) { throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_FILE_NOT_FOUND, "The system cannot find the file specified", null, PythonExceptions._OSError.ERROR_FILE_NOT_FOUND); } if (dwRet != ERROR_SUCCESS) { throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, dwRet); } // convert the result into a Python object switch (valueKind) { case REG_MULTI_SZ: PythonList list = new PythonList(); int curIndex = 0; while (curIndex < length) { for (int dataIndex = curIndex; dataIndex < length; dataIndex += 2) { if (data[dataIndex] == 0 && data[dataIndex + 1] == 0) { // got a full string list.Add(ExtractString(data, curIndex, dataIndex)); curIndex = dataIndex + 2; if (curIndex + 2 <= length && data[curIndex] == 0 && data[curIndex + 1] == 0) { // double null terminated curIndex = data.Length; break; } } } if (curIndex != data.Length) { // not null terminated list.Add(ExtractString(data, curIndex, data.Length)); } } value = list; break; case REG_BINARY: var tight_fit_data = new byte[length]; for (int i = 0; i < length; i++) { tight_fit_data[i] = data[i]; } value = length == 0 ? null : Bytes.Make(tight_fit_data); break; case REG_EXPAND_SZ: case REG_SZ: if (length >= 2 && data[length - 1] == 0 && data[length - 2] == 0) { value = ExtractString(data, 0, (int)length - 2); } else { value = ExtractString(data, 0, (int)length); } break; case REG_DWORD: if (BitConverter.IsLittleEndian) { value = (uint)((data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]); } else { value = (uint)((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]); } break; default: value = null; break; } }
internal Print(PrintStatement stmt) : this() { if (stmt.Destination != null) _dest = Convert(stmt.Destination); _values = PythonOps.MakeEmptyList(stmt.Expressions.Count); foreach (AstExpression expr in stmt.Expressions) _values.Add(Convert(expr)); _nl = !stmt.TrailingComma; }
/// <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 PythonList 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; if (!(globals is PythonDictionary pyGlobals) || !pyGlobals._storage.TryGetName(out attribute)) { return(false); } // And the __name__ needs to be string if (!(attribute is string modName)) { 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 PythonList) != 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 (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 (context.LanguageContext.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 int lastDot = modName.LastIndexOf('.'); if (lastDot == -1) { // name doesn't include dot, only absolute import possible if (level > 0) { throw PythonOps.SystemError("Parent module '{0}' not loaded, cannot perform relative import", string.Empty); } return(false); } // need to remove more than one name int tmpLevel = level; while (tmpLevel > 1 && lastDot != -1) { lastDot = modName.LastIndexOf('.', lastDot - 1); tmpLevel--; } if (lastDot == -1) { pn = modName; } else { pn = modName.Substring(0, lastDot); } } 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) { throw PythonOps.SystemError("Parent module '{0}' not loaded, cannot perform relative import", pn); } // not enough information - absolute import return(false); }
internal Set(SetExpression setExpression) : this() { _elts = new PythonList(setExpression.Items.Count); foreach (AstExpression item in setExpression.Items) { _elts.Add(Convert(item)); } }
/// <summary> /// Trys to get an existing module and if that fails fall backs to searching /// </summary> private static bool TryGetExistingOrMetaPathModule(CodeContext /*!*/ context, string fullName, PythonList path, out object ret) { if (TryGetExistingModule(context, fullName, out ret)) { return(true); } return(TryLoadMetaPathModule(context, fullName, path, out ret)); }
internal SetComp(SetComprehension comp) : this() { _elt = Convert(comp.Item); _generators = Convert(comp.Iterators); }
/// <summary> /// Attempts to load a module from sys.meta_path as defined in PEP 302. /// /// The meta_path provides a list of importer objects which can be used to load modules before /// searching sys.path but after searching built-in modules. /// </summary> private static bool TryLoadMetaPathModule(CodeContext /*!*/ context, string fullName, PythonList path, out object ret) { if (context.LanguageContext.GetSystemStateValue("meta_path") is PythonList metaPath) { foreach (object importer in (IEnumerable)metaPath) { if (FindAndLoadModuleFromImporter(context, importer, fullName, path, out ret)) { return(true); } } } ret = null; return(false); }
internal static Statement RevertStmts(PythonList stmts) { if (stmts.Count == 1) return ((stmt)stmts[0]).Revert(); Statement[] statements = new Statement[stmts.Count]; for (int i = 0; i < stmts.Count; i++) statements[i] = ((stmt)stmts[i]).Revert(); return new SuiteStatement(statements); }
/// <summary> /// Given a user defined importer object as defined in PEP 302 tries to load a module. /// /// First the find_module(fullName, path) is invoked to get a loader, then load_module(fullName) is invoked /// </summary> private static bool FindAndLoadModuleFromImporter(CodeContext /*!*/ context, object importer, string fullName, PythonList path, out object ret) { object find_module = PythonOps.GetBoundAttr(context, importer, "find_module"); PythonContext pycontext = context.LanguageContext; object loader = path == null?pycontext.Call(context, find_module, fullName) : pycontext.Call(context, find_module, fullName, path); if (loader != null) { object findMod = PythonOps.GetBoundAttr(context, loader, "load_module"); ret = pycontext.Call(context, findMod, fullName); return(ret != null); } ret = null; return(false); }
public Suite(PythonList body) : this() { _body = body; }
private static object ImportFromPath(CodeContext /*!*/ context, string /*!*/ name, string /*!*/ fullName, PythonList /*!*/ path) { return(ImportFromPathHook(context, name, fullName, path, LoadFromDisk)); }
internal TryExcept(TryStatement stmt) : this() { _body = ConvertStatements(stmt.Body); _handlers = PythonOps.MakeEmptyList(stmt.Handlers.Count); foreach (TryStatementHandler tryStmt in stmt.Handlers) _handlers.Add(Convert(tryStmt)); _orelse = ConvertStatements(stmt.Else, true); }
private static object ImportFromPathHook(CodeContext /*!*/ context, string /*!*/ name, string /*!*/ fullName, PythonList /*!*/ path, Func <CodeContext, string, string, string, object> defaultLoader) { Assert.NotNull(context, name, fullName, path); if (!(context.LanguageContext.GetSystemStateValue("path_importer_cache") is IDictionary <object, object> importCache)) { return(null); } foreach (object dirname in path) { string str = dirname as string; if (str != null || (Converter.TryConvertToString(dirname, out str) && str != null)) // ignore non-string { object importer; if (!importCache.TryGetValue(str, out importer)) { importCache[str] = importer = FindImporterForPath(context, str); } if (importer != null) { // user defined importer object, get the loader and use it. object ret; if (FindAndLoadModuleFromImporter(context, importer, fullName, null, out ret)) { return(ret); } } else if (defaultLoader != null) { object res = defaultLoader(context, name, fullName, str); if (res != null) { return(res); } } } } return(null); }
internal TryFinally(PythonList body, PythonList finalbody) : this() { _body = body; _finalbody = finalbody; }
public static void warn_explicit(CodeContext context, object message, PythonType category, string filename, int lineno, string module = null, PythonDictionary registry = null, object module_globals = null) { PythonContext pContext = context.LanguageContext; PythonDictionary fields = (PythonDictionary)pContext.GetModuleState(_keyFields); object warnings = pContext.GetWarningsModule(); PythonExceptions.BaseException msg; string text; // message text if (string.IsNullOrEmpty(module)) { module = (filename == null || filename == "") ? "<unknown>" : filename; if (module.EndsWith(".py")) { module = module.Substring(0, module.Length - 3); } } if (registry == null) { registry = new PythonDictionary(); } if (PythonOps.IsInstance(message, PythonExceptions.Warning)) { msg = (PythonExceptions.BaseException)message; text = msg.ToString(); category = DynamicHelpers.GetPythonType(msg); } else { text = message.ToString(); msg = PythonExceptions.CreatePythonThrowable(category, message.ToString()); } PythonTuple key = PythonTuple.MakeTuple(text, category, lineno); if (registry.ContainsKey(key)) { return; } string action = Converter.ConvertToString(fields[_keyDefaultAction]); PythonTuple last_filter = null; bool loop_break = false; PythonList filters = (PythonList)fields[_keyFilters]; if (warnings != null) { filters = PythonOps.GetBoundAttr(context, warnings, "filters") as PythonList; if (filters == null) { throw PythonOps.ValueError("_warnings.filters must be a list"); } } foreach (PythonTuple filter in filters) { last_filter = filter; action = (string)filter._data[0]; PythonRegex.Pattern fMsg = (PythonRegex.Pattern)filter._data[1]; PythonType fCat = (PythonType)filter._data[2]; PythonRegex.Pattern fMod = (PythonRegex.Pattern)filter._data[3]; int fLno; if (filter._data[4] is int) { fLno = (int)filter._data[4]; } else { fLno = (Extensible <int>)filter._data[4]; } if ((fMsg == null || fMsg.match(text) != null) && category.IsSubclassOf(fCat) && (fMod == null || fMod.match(module) != null) && (fLno == 0 || fLno == lineno)) { loop_break = true; break; } } if (!loop_break) { action = Converter.ConvertToString(fields[_keyDefaultAction]); } switch (action) { case "ignore": registry.Add(key, 1); return; case "error": throw msg.GetClrException(); case "once": registry.Add(key, 1); PythonTuple onceKey = PythonTuple.MakeTuple(text, category); PythonDictionary once_reg = (PythonDictionary)fields[_keyOnceRegistry]; if (once_reg.ContainsKey(onceKey)) { return; } once_reg.Add(key, 1); break; case "always": break; case "module": registry.Add(key, 1); PythonTuple altKey = PythonTuple.MakeTuple(text, category, 0); if (registry.ContainsKey(altKey)) { return; } registry.Add(altKey, 1); break; case "default": registry.Add(key, 1); break; default: throw PythonOps.RuntimeError("Unrecognized action ({0}) in warnings.filters:\n {1}", action, last_filter); } if (warnings != null) { object show_fxn = PythonOps.GetBoundAttr(context, warnings, "showwarning"); if (show_fxn != null) { PythonCalls.Call( context, show_fxn, msg, category, filename, lineno, null, null); } else { showwarning(context, msg, category, filename, lineno, null, null); } } else { showwarning(context, msg, category, filename, lineno, null, null); } }
internal Tuple(TupleExpression list, expr_context ctx) : this() { _elts = PythonOps.MakeEmptyList(list.Items.Count); foreach (AstExpression expr in list.Items) _elts.Add(Convert(expr, ctx)); _ctx = ctx; }
public Import(PythonList names, [Optional]int? lineno, [Optional]int? col_offset) : this() { _names = names; _lineno = lineno; _col_offset = col_offset; }
internal While(WhileStatement stmt) : this() { _test = Convert(stmt.Test); _body = ConvertStatements(stmt.Body); _orelse = ConvertStatements(stmt.ElseStatement, true); }
internal Import(ImportStatement stmt) : this() { _names = ConvertAliases(stmt.Names, stmt.AsNames); }
internal With(WithStatement with) : this() { _context_expr = Convert(with.ContextManager); if (with.Variable != null) _optional_vars = Convert(with.Variable); _body = ConvertStatements(with.Body); }
public ImportFrom([Optional]string module, PythonList names, [Optional]int level, [Optional]int? lineno, [Optional]int? col_offset) : this() { _module = module; _names = names; _level = level; _lineno = lineno; _col_offset = col_offset; }
internal static PythonList Convert(ComprehensionIterator[] iters) { Generic.List<ComprehensionFor> cfCollector = new Generic.List<ComprehensionFor>(); Generic.List<Generic.List<ComprehensionIf>> cifCollector = new Generic.List<Generic.List<ComprehensionIf>>(); Generic.List<ComprehensionIf> cif = null; for (int i = 0; i < iters.Length; i++) { if (iters[i] is ComprehensionFor) { ComprehensionFor cf = (ComprehensionFor)iters[i]; cfCollector.Add(cf); cif = new Generic.List<ComprehensionIf>(); cifCollector.Add(cif); } else { ComprehensionIf ci = (ComprehensionIf)iters[i]; cif.Add(ci); } } PythonList comps = new PythonList(); for (int i = 0; i < cfCollector.Count; i++) comps.Add(new comprehension(cfCollector[i], cifCollector[i].ToArray())); return comps; }
private static PythonTuple FindModulePath(CodeContext /*!*/ context, string name, PythonList path) { Debug.Assert(path != null); if (name == null) { throw PythonOps.TypeError("find_module() argument 1 must be string, not None"); } PlatformAdaptationLayer pal = context.LanguageContext.DomainManager.Platform; foreach (object d in path) { string dir = d as string; if (dir == null) { continue; // skip invalid entries } string pathName = Path.Combine(dir, name); if (pal.DirectoryExists(pathName)) { if (pal.FileExists(Path.Combine(pathName, "__init__.py"))) { return(PythonTuple.MakeTuple(null, pathName, PythonTuple.MakeTuple("", "", PackageDirectory))); } } string fileName = pathName + ".py"; if (pal.FileExists(fileName)) { Stream fs = pal.OpenInputFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); PythonFile pf = PythonFile.Create(context, fs, fileName, "U"); return(PythonTuple.MakeTuple(pf, fileName, PythonTuple.MakeTuple(".py", "U", PythonSource))); } } throw PythonOps.ImportError("No module named {0}", name); }