internal static PythonList /*!*/ SplitInternal(IList <byte> /*!*/ bytes, byte[] seps, int maxsplit, Func <List <byte> /*!*/, object> /*!*/ ctor) { Debug.Assert(ctor != null); if (bytes.Count == 0) { return(SplitEmptyString(seps != null, ctor)); } else { List <byte>[] r = null; // If the optional second argument sep is absent or None, the words are separated // by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed); r = bytes.Split(seps, (maxsplit < 0) ? Int32.MaxValue : maxsplit + 1, GetStringSplitOptions(seps)); PythonList ret = PythonOps.MakeEmptyList(r.Length); foreach (List <byte> s in r) { ret.AddNoLock(ctor(s)); } return(ret); } }
internal static PythonList /*!*/ Split(this IList <byte> /*!*/ bytes, IList <byte> sep, int maxsplit, Func <List <byte> /*!*/, object> /*!*/ ctor) { Debug.Assert(ctor != null); if (sep == null) { if (maxsplit == 0) { // Corner case for CPython compatibility PythonList result = PythonOps.MakeEmptyList(1); result.AddNoLock(ctor(bytes.LeftStrip() ?? bytes as List <byte> ?? new List <byte>(bytes))); return(result); } return(SplitInternal(bytes, (byte[])null, maxsplit, ctor)); } if (sep.Count == 0) { throw PythonOps.ValueError("empty separator"); } else if (sep.Count == 1) { return(SplitInternal(bytes, new byte[] { sep[0] }, maxsplit, ctor)); } else { return(SplitInternal(bytes, sep, maxsplit, ctor)); } }
public static void PerformModuleReload(PythonContext /*!*/ context, PythonDictionary /*!*/ dict) { PythonList defaultFilters = new PythonList(); defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.DeprecationWarning, null, 0)); defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.PendingDeprecationWarning, null, 0)); defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.ImportWarning, null, 0)); defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.BytesWarning, null, 0)); context.GetOrCreateModuleState(_keyFields, () => { dict.Add(_keyDefaultAction, "default"); dict.Add(_keyOnceRegistry, new PythonDictionary()); dict.Add(_keyFilters, defaultFilters); return(dict); }); }
public static void heappush(CodeContext /*!*/ context, PythonList list, object item) { lock (list) { list.AddNoLock(item); SiftUp(context, list, list._size - 1); } }
internal static PythonList /*!*/ RightSplit(this IList <byte> /*!*/ bytes, IList <byte> /*!*/ sep, int maxsplit, Func <IList <byte> /*!*/, IList <byte> > /*!*/ ctor) { // rsplit works like split but needs to split from the right; // reverse the original string (and the sep), split, reverse // the split list and finally reverse each element of the list IList <byte> reversed = bytes.ReverseBytes(); sep = sep?.ReverseBytes(); PythonList temp = null, ret = null; temp = ctor(reversed).Split(sep, maxsplit, x => ctor(x)); temp.reverse(); int resultlen = temp.__len__(); if (resultlen != 0) { ret = new PythonList(resultlen); foreach (IList <byte> s in temp) { ret.AddNoLock(ctor(s.ReverseBytes())); } } else { ret = temp; } return(ret); }
public object this[Slice index] { get { if (index.stop == null) { throw PythonOps.ValueError("slice stop is required"); } int start = index.start != null ? (int)index.start : 0; int stop = index.stop != null ? (int)index.stop : 0; int step = index.step != null ? (int)index.step : 1; if (step < 0 && index.start == null) { throw PythonOps.ValueError("slice start is required for step < 0"); } if (start < 0) { start = 0; } INativeType type = ((PointerType)NativeType)._type; SimpleType elemType = type as SimpleType; if ((stop < start && step > 0) || (start < stop && step < 0)) { if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) { return(String.Empty); } return(new PythonList()); } MemoryHolder address = _memHolder.ReadMemoryHolder(0); if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) { int elmSize = ((INativeType)elemType).Size; StringBuilder res = new StringBuilder(); for (int i = start; stop > start ? i <stop : i> stop; i += step) { res.Append( elemType.ReadChar(address, checked (i * elmSize)) ); } return(res.ToString()); } else { PythonList res = new PythonList((stop - start) / step); for (int i = start; stop > start ? i <stop : i> stop; i += step) { res.AddNoLock( type.GetValue(address, this, checked (type.Size * i), false) ); } return(res); } } }
internal static PythonList SplitLines(this IList <byte> bytes, bool keepends, Func <List <byte>, IList <byte> > ctor) { PythonList ret = new PythonList(); int i, linestart; for (i = 0, linestart = 0; i < bytes.Count; i++) { if (bytes[i] == '\n' || bytes[i] == '\r') { // special case of "\r\n" as end of line marker if (i < bytes.Count - 1 && bytes[i] == '\r' && bytes[i + 1] == '\n') { if (keepends) { ret.AddNoLock(ctor(bytes.Substring(linestart, i - linestart + 2))); } else { ret.AddNoLock(ctor(bytes.Substring(linestart, i - linestart))); } linestart = i + 2; i++; } else //'\r', '\n', or unicode new line as end of line marker { if (keepends) { ret.AddNoLock(ctor(bytes.Substring(linestart, i - linestart + 1))); } else { ret.AddNoLock(ctor(bytes.Substring(linestart, i - linestart))); } linestart = i + 1; } } } // the last line needs to be accounted for if it is not empty if (i - linestart != 0) { ret.AddNoLock(ctor(bytes.Substring(linestart, i - linestart))); } return(ret); }
private static PythonList ToList(IDictionary <object, object> self) { PythonList ret = new PythonList(self.Count); foreach (KeyValuePair <object, object> kv in self) { ret.AddNoLock(PythonTuple.MakeTuple(kv.Key, kv.Value)); } return(ret); }
static PythonList SplitEmptyString(bool separators, Func <List <byte>, object> ctor) { PythonList ret = new PythonList(1); if (separators) { ret.AddNoLock(ctor(new List <byte>(0))); } return(ret); }
private static PythonList /*!*/ SplitEmptyString(bool separators, Func <List <byte> /*!*/, object> /*!*/ ctor) { Debug.Assert(ctor != null); PythonList ret = PythonOps.MakeEmptyList(1); if (separators) { ret.AddNoLock(ctor(new List <byte>(0))); } return(ret); }
private static void OtherSliceAssign(SliceAssign assign, int start, int stop, int step, object?value) { // get enumerable data into a list, and then // do the slice. IEnumerator enumerator = PythonOps.GetEnumerator(value); PythonList sliceData = new PythonList(); while (enumerator.MoveNext()) { sliceData.AddNoLock(enumerator.Current); } DoSliceAssign(assign, start, stop, step, sliceData); }
public static PythonList GetMemberNames(CodeContext /*!*/ context, Assembly self) { Debug.Assert(self != null); PythonList ret = DynamicHelpers.GetPythonTypeFromType(self.GetType()).GetMemberNames(context); foreach (object o in GetReflectedAssembly(context, self).Keys) { if (o is string) { ret.AddNoLock((string)o); } } return(ret); }
internal static PythonList GetWeakRefs(PythonContext context, object o) { PythonList l = new PythonList(); IWeakReferenceable iwr; if (context.TryConvertToWeakReferenceable(o, out iwr)) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { for (int i = 0; i < wrt.HandlerCount; i++) { l.AddNoLock(wrt.GetWeakRef(i)); } } } return(l); }
public static PythonList /*!*/ Get__all__ <T>(CodeContext /*!*/ context) { Debug.Assert(typeof(T).IsSealed && typeof(T).IsAbstract, "__all__ should only be produced for static members"); PythonType pt = DynamicHelpers.GetPythonTypeFromType(typeof(T)); PythonList names = new PythonList(); foreach (string name in pt.GetMemberNames(context)) { object res; if (IsStaticTypeMemberInAll(context, pt, name, out res)) { names.AddNoLock(name); } } return(names); }
internal static PythonList SplitInternal(this IList <byte> bytes, IList <byte>?separator, int maxsplit, Func <List <byte>, IList <byte> > ctor) { if (bytes.Count == 0) { return(SplitEmptyString(separator != null, ctor)); } // If the optional second argument sep is absent or None, the words are separated // by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed); List <byte>[] r = bytes.Split(separator, (maxsplit <0 || maxsplit> bytes.Count) ? bytes.Count + 1 : maxsplit + 1, GetStringSplitOptions(separator)); PythonList ret = new PythonList(r.Length); foreach (List <byte> s in r) { ret.AddNoLock(ctor(s)); } return(ret);
internal static PythonList /*!*/ SplitInternal(this IList <byte> /*!*/ bytes, IList <byte> /*!*/ separator, int maxsplit, Func <List <byte> /*!*/, object> /*!*/ ctor) { Debug.Assert(ctor != null); if (bytes.Count == 0) { return(SplitEmptyString(separator != null, ctor)); } List <byte>[] r = bytes.Split(separator, (maxsplit < 0) ? Int32.MaxValue : maxsplit + 1, GetStringSplitOptions(separator)); PythonList ret = PythonOps.MakeEmptyList(r.Length); foreach (List <byte> s in r) { ret.AddNoLock(ctor(s)); } return(ret); }
private IEnumerator <object> Yielder(IEnumerator iter) { PythonList result = new PythonList(); while (MoveNextHelper(iter)) { result.AddNoLock(iter.Current); yield return(iter.Current); } if (result.__len__() != 0) { for (; ;) { for (int i = 0; i < result.__len__(); i++) { yield return(result[i]); } } } }
public override PythonList readlines(object?hint = null) { _checkClosed(); int size = GetInt(hint, -1); PythonList lines = new PythonList(); for (var line = readline(-1); line.Length > 0; line = readline(-1)) { lines.AddNoLock(line); if (size > 0) { size -= line.Length; if (size <= 0) { break; } } } return(lines); }
private static PythonList GroupsToList(int[] groups) { // .NET: values from 0-9, if the last digit is zero, remaining digits // go ungrouped, otherwise they're grouped based upon the last value. // locale: ends in CHAR_MAX if no further grouping is performed, ends in // zero if the last group size is repeatedly used. PythonList res = new PythonList(groups); if (groups.Length > 0 && groups[groups.Length - 1] == 0) { // replace zero w/ CHAR_MAX, no further grouping is performed res[res.__len__() - 1] = CHAR_MAX; } else { // append 0 to indicate we should repeatedly use the last one res.AddNoLock(0); } return(res); }
internal static PythonList Split(this IList <byte> bytes, IList <byte>?sep, int maxsplit, Func <List <byte>, IList <byte> > ctor) { if (sep == null) { if (maxsplit == 0) { // Corner case for CPython compatibility PythonList result = new PythonList(1); result.AddNoLock(ctor(bytes.LeftStrip() ?? bytes as List <byte> ?? new List <byte>(bytes))); return(result); } return(SplitInternal(bytes, null, maxsplit, ctor)); } if (sep.Count == 0) { throw PythonOps.ValueError("empty separator"); } else { return(SplitInternal(bytes, sep, maxsplit, ctor)); } }
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); }
public object this[Slice index] { get { if (index.stop == null) { throw PythonOps.ValueError("slice stop is required"); } int start = index.start != null ? (int)index.start : 0; int stop = index.stop != null ? (int)index.stop : 0; int step = index.step != null ? (int)index.step : 1; if (step < 0 && index.start == null) { throw PythonOps.ValueError("slice start is required for step < 0"); } if (start < 0) { start = 0; } INativeType type = ((PointerType)NativeType)._type; SimpleType elemType = type as SimpleType; if ((stop < start && step > 0) || (start < stop && step < 0)) { if (elemType != null) { if (elemType._type == SimpleTypeKind.Char) { return(Bytes.Empty); } if (elemType._type == SimpleTypeKind.WChar) { return(string.Empty); } } return(new PythonList()); } MemoryHolder address = MemHolder.ReadMemoryHolder(0); if (elemType != null) { if (elemType._type == SimpleTypeKind.Char) { Debug.Assert(((INativeType)elemType).Size == 1); var sb = new MemoryStream(); for (int i = start; stop > start ? i <stop : i> stop; i += step) { sb.WriteByte(address.ReadByte(i)); } return(Bytes.Make(sb.ToArray())); } if (elemType._type == SimpleTypeKind.WChar) { int elmSize = ((INativeType)elemType).Size; var sb = new StringBuilder(); for (int i = start; stop > start ? i <stop : i> stop; i += step) { sb.Append((char)address.ReadInt16(checked (i * elmSize))); } return(sb.ToString()); } } PythonList res = new PythonList((stop - start) / step); for (int i = start; stop > start ? i <stop : i> stop; i += step) { res.AddNoLock( type.GetValue(address, this, checked (type.Size * i), false) ); } return(res); } }