Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions Src/IronPython.Modules/IterTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
using System.Numerics;
using System.Runtime.InteropServices;

using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

using IronPython.Runtime;
using IronPython.Runtime.Binding;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;

using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

[assembly: PythonModule("itertools", typeof(IronPython.Modules.PythonIterTools))]
namespace IronPython.Modules {
public static class PythonIterTools {
Expand Down Expand Up @@ -233,7 +233,7 @@ private static void EnsureIterator(CodeContext/*!*/ context, object iter) {
if (iter == null ||
!PythonOps.HasAttr(context, iter, "__iter__") &&
!PythonOps.HasAttr(context, iter, "__getitem__")) {
throw PythonOps.TypeError("'{0}' object is not iterable", PythonOps.GetPythonTypeName(iter));
throw PythonOps.TypeError("'{0}' object is not iterable", PythonOps.GetPythonTypeName(iter));
}
}

Expand Down Expand Up @@ -276,13 +276,13 @@ public count(BigInteger start) {
InnerEnumerator = BigIntYielder(this, start, 1);
}

public count(int start=0, int step=1) {
public count(int start = 0, int step = 1) {
_curInt = start;
_step = step;
InnerEnumerator = IntYielder(this, start, step);
}

public count([DefaultParameterValue(0)]int start, BigInteger step) {
public count([DefaultParameterValue(0)] int start, BigInteger step) {
_curInt = start;
_step = step;
InnerEnumerator = IntYielder(this, start, step);
Expand All @@ -300,7 +300,7 @@ public count(BigInteger start, BigInteger step) {
InnerEnumerator = BigIntYielder(this, start, step);
}

public count(CodeContext/*!*/ context, [DefaultParameterValue(0)]object start, [DefaultParameterValue(1)]object step) {
public count(CodeContext/*!*/ context, [DefaultParameterValue(0)] object start, [DefaultParameterValue(1)] object step) {
EnsureNumeric(context, start);
EnsureNumeric(context, step);
_cur = start;
Expand All @@ -315,7 +315,7 @@ private static void EnsureNumeric(CodeContext/*!*/ context, object num) {
if (num == null ||
!PythonOps.HasAttr(context, num, "__int__") &&
!PythonOps.HasAttr(context, num, "__float__")) {
throw PythonOps.TypeError("a number is required");
throw PythonOps.TypeError("a number is required");
}
}

Expand Down Expand Up @@ -548,9 +548,9 @@ private IEnumerator<object> Yielder(IEnumerator iter) {
private IEnumerator<object> Grouper(IEnumerator iter, object curKey) {
while (PythonContext.Equal(GetKey(iter.Current), curKey)) {
yield return iter.Current;
if (!MoveNextHelper(iter)) {
_fFinished = true;
yield break;
if (!MoveNextHelper(iter)) {
_fFinished = true;
yield break;
}
}
}
Expand Down Expand Up @@ -675,7 +675,7 @@ public zip_longest(params object[] iterables) {
}
}

public zip_longest([ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
public zip_longest([ParamDictionary] IDictionary<object, object> paramDict, params object[] iterables) {
object fill;

if (paramDict.TryGetValue("fillvalue", out fill)) {
Expand Down Expand Up @@ -760,11 +760,14 @@ private static Exception UnexpectedKeywordArgument(IDictionary<object, object> p

[PythonType]
public class product : IterBase {
private PythonTuple[] tuples;

public product(CodeContext context, params object[] iterables) {
InnerEnumerator = Yielder(ArrayUtils.ConvertAll(iterables, x => new PythonList(context, PythonOps.GetEnumerator(x))));
tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(PythonOps.GetEnumerator(x)));
InnerEnumerator = Yielder(tuples);
}

public product(CodeContext context, [ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
public product(CodeContext context, [ParamDictionary] IDictionary<object, object> paramDict, params object[] iterables) {
object repeat;
int iRepeat = 1;
if (paramDict.TryGetValue("repeat", out repeat)) {
Expand All @@ -782,29 +785,28 @@ public product(CodeContext context, [ParamDictionary]IDictionary<object, object>
throw UnexpectedKeywordArgument(paramDict);
}

PythonList[] finalIterables = new PythonList[iterables.Length * iRepeat];
tuples = new PythonTuple[iterables.Length * iRepeat];
for (int i = 0; i < iRepeat; i++) {
for (int j = 0; j < iterables.Length; j++) {
finalIterables[i * iterables.Length + j] = new PythonList(context, iterables[j]);
tuples[i * iterables.Length + j] = new PythonTuple(iterables[j]);
}
}
InnerEnumerator = Yielder(finalIterables);
InnerEnumerator = Yielder(tuples);
}

public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
PythonTuple.MakeTuple(tuples), // arguments
null // TODO: state
);
}

public void __setstate__(object state) {
// TODO
}

private IEnumerator<object> Yielder(PythonList[] iterables) {
private IEnumerator<object> Yielder(PythonTuple[] iterables) {
if (iterables.Length > 0) {
IEnumerator[] enums = new IEnumerator[iterables.Length];
enums[0] = iterables[0].GetEnumerator();
Expand Down Expand Up @@ -833,6 +835,7 @@ private IEnumerator<object> Yielder(PythonList[] iterables) {
} else {
yield return PythonTuple.EMPTY;
}
tuples = new PythonTuple[1] { PythonTuple.EMPTY };
}
}

Expand Down Expand Up @@ -987,7 +990,7 @@ public permutations(CodeContext context, object iterable) {

public permutations(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);

InnerEnumerator = Yielder(GetR(r, _data));
}

Expand Down Expand Up @@ -1078,7 +1081,7 @@ public repeat(object @object) {
public repeat(object @object, int times) {
_obj = @object;
InnerEnumerator = Yielder();
_remaining = times;
_remaining = times < 0 ? 0 : times;
}

private IEnumerator<object> Yielder() {
Expand All @@ -1094,10 +1097,9 @@ public int __length_hint__() {
}

public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple() // arguments
_fInfinite ? PythonTuple.MakeTuple(_obj) : PythonTuple.MakeTuple(_obj, _remaining) // arguments
);
}

Expand Down Expand Up @@ -1150,9 +1152,9 @@ IEnumerator IEnumerable.GetEnumerator() {

#endregion
}

[PythonType]
public class starmap : IterBase {
public class starmap : IterBase {
public starmap(CodeContext context, object function, object iterable) {
InnerEnumerator = Yielder(context, function, PythonOps.GetEnumerator(iterable));
}
Expand Down Expand Up @@ -1211,7 +1213,7 @@ public void __setstate__(object state) {

private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
while (MoveNextHelper(iter)) {
if(!Converter.ConvertToBoolean(
if (!Converter.ConvertToBoolean(
_context.LanguageContext.CallSplat(predicate, iter.Current)
)) {
break;
Expand All @@ -1222,8 +1224,8 @@ private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
}
}

[PythonHidden]
public class TeeIterator : IEnumerator, IWeakReferenceable {
[PythonType("_tee")]
public sealed class TeeIterator : IEnumerator, IWeakReferenceable {
internal IEnumerator _iter;
internal PythonList _data;
private int _curIndex = -1;
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython.Modules/_datetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ internal override int CompareTo(date other) {
if (_tz != null) {
sb.AppendFormat(", tzinfo={0}", PythonOps.Repr(context, _tz));
}
sb.AppendFormat(")");
sb.Append(')');
return sb.ToString();
}
#endregion
Expand Down Expand Up @@ -1395,7 +1395,7 @@ private int CompareTo(time other) {
sb.AppendFormat(", tzinfo={0}", ltzname.ToLowerInvariant());
}

sb.AppendFormat(")");
sb.Append(')');

return sb.ToString();
}
Expand Down
47 changes: 23 additions & 24 deletions Src/IronPython/Runtime/Binding/PythonBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
// 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.

using System.Linq.Expressions;
using System.Numerics;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Threading;

using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;

using Microsoft.Scripting;
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Actions.Calls;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;

namespace IronPython.Runtime.Binding {
using Ast = Expression;
using AstUtils = Microsoft.Scripting.Ast.Utils;
Expand All @@ -43,7 +42,7 @@ public PythonBinder(PythonContext/*!*/ pythonContext, CodeContext context) {
ContractUtils.RequiresNotNull(pythonContext, nameof(pythonContext));

_dlrExtensionTypes = MakeExtensionTypes();
_context = pythonContext;
_context = pythonContext;
if (context != null) {
context.LanguageContext.DomainManager.AssemblyLoaded += new EventHandler<AssemblyLoadedEventArgs>(DomainManager_AssemblyLoaded);

Expand Down Expand Up @@ -82,7 +81,7 @@ public PythonBinder(PythonBinder binder) {
}
return expr;
}

Type visType = Context.Binder.PrivateBinding ? toType : CompilerHelpers.GetVisibleType(toType);

if (exprType == typeof(PythonType) && visType == typeof(Type)) {
Expand Down Expand Up @@ -185,17 +184,17 @@ public override ErrorInfo MakeSetValueTypeFieldError(FieldTracker field, Dynamic
Expression.Assign(
Expression.Field(
AstUtils.Convert(
instance.Expression,
instance.Expression,
field.DeclaringType
),
),
field.Field
),
ConvertExpression(
value.Expression,
field.FieldType,
ConversionResultKind.ExplicitCast,
value.Expression,
field.FieldType,
ConversionResultKind.ExplicitCast,
new PythonOverloadResolverFactory(
this,
this,
Expression.Constant(_context.SharedContext)
)
)
Expand Down Expand Up @@ -237,7 +236,7 @@ public override ErrorInfo MakeConversionError(Type toType, Expression value) {
return MakeMissingMemberError(accessingType, instance, info.Name);
}

public override ErrorInfo/*!*/ MakeStaticPropertyInstanceAccessError(PropertyTracker/*!*/ tracker, bool isAssignment, IList<DynamicMetaObject>/*!*/ parameters) {
public override ErrorInfo/*!*/ MakeStaticPropertyInstanceAccessError(PropertyTracker/*!*/ tracker, bool isAssignment, IList<DynamicMetaObject>/*!*/ parameters) {
ContractUtils.RequiresNotNull(tracker, nameof(tracker));
ContractUtils.RequiresNotNull(parameters, nameof(parameters));

Expand Down Expand Up @@ -281,7 +280,7 @@ public override string GetTypeName(Type t) {
}


public override ErrorInfo/*!*/ MakeEventValidation(MemberGroup/*!*/ members, DynamicMetaObject eventObject, DynamicMetaObject/*!*/ value, OverloadResolverFactory/*!*/ factory) {
public override ErrorInfo/*!*/ MakeEventValidation(MemberGroup/*!*/ members, DynamicMetaObject eventObject, DynamicMetaObject/*!*/ value, OverloadResolverFactory/*!*/ factory) {
EventTracker ev = (EventTracker)members[0];

return ErrorInfo.FromValueNoError(
Expand Down Expand Up @@ -405,7 +404,7 @@ public override IList<Type> GetExtensionTypes(Type t) {

return list;
}

private void AddExtensionTypes(Type t, List<Type> list) {
ExtensionTypeInfo extType;
if (_sysTypes.TryGetValue(t, out extType)) {
Expand Down Expand Up @@ -461,16 +460,16 @@ private static DynamicMetaObject ReturnMemberTracker(Type type, MemberTracker me
case TrackerTypes.Bound:
return new DynamicMetaObject(ReturnBoundTracker((BoundMemberTracker)memberTracker, privateBinding), BindingRestrictions.Empty);
case TrackerTypes.Property:
return new DynamicMetaObject(ReturnPropertyTracker((PropertyTracker)memberTracker, privateBinding), BindingRestrictions.Empty);;
return new DynamicMetaObject(ReturnPropertyTracker((PropertyTracker)memberTracker, privateBinding), BindingRestrictions.Empty); ;
case TrackerTypes.Event:
return new DynamicMetaObject(Ast.Call(
typeof(PythonOps).GetMethod(nameof(PythonOps.MakeBoundEvent)),
AstUtils.Constant(PythonTypeOps.GetReflectedEvent((EventTracker)memberTracker)),
AstUtils.Constant(null),
AstUtils.Constant(type)
), BindingRestrictions.Empty);;
), BindingRestrictions.Empty);
case TrackerTypes.Field:
return new DynamicMetaObject(ReturnFieldTracker((FieldTracker)memberTracker), BindingRestrictions.Empty);;
return new DynamicMetaObject(ReturnFieldTracker((FieldTracker)memberTracker), BindingRestrictions.Empty); ;
case TrackerTypes.MethodGroup:
return new DynamicMetaObject(ReturnMethodGroup((MethodGroup)memberTracker), BindingRestrictions.Empty); ;
case TrackerTypes.Constructor:
Expand Down Expand Up @@ -589,7 +588,7 @@ public void LookupMembers(CodeContext/*!*/ context, PythonType/*!*/ type, Python

if (!members.ContainsKey(rm.Name)) {
members[rm.Name] = new KeyValuePair<PythonTypeSlot, MemberGroup>(
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
rm.Member
);
}
Expand Down Expand Up @@ -625,7 +624,7 @@ public void ResolveMemberNames(CodeContext/*!*/ context, PythonType/*!*/ type, P

if (!members.ContainsKey(rm.Name)) {
members[rm.Name] = new KeyValuePair<PythonTypeSlot, MemberGroup>(
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
rm.Member
);
}
Expand Down Expand Up @@ -1084,7 +1083,7 @@ private void EnsureInfo() {
private class SlotCacheInfo {
public SlotCacheInfo() {
Members = new Dictionary<string/*!*/, KeyValuePair<PythonTypeSlot, MemberGroup/*!*/>>(StringComparer.Ordinal);
}
}

public bool TryGetSlot(string/*!*/ name, out PythonTypeSlot slot) {
Debug.Assert(name != null);
Expand Down
Loading
Loading