Skip to content

Commit d42aa93

Browse files
authored
Misc changes (#1861)
* Misc changes * Misc bytes change * Only create OSError subclass when two args are used * Enable some test_itertools_stdlib tests
1 parent 97fc78a commit d42aa93

File tree

16 files changed

+158
-161
lines changed

16 files changed

+158
-161
lines changed

Src/IronPython.Modules/IterTools.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
using System.Numerics;
99
using System.Runtime.InteropServices;
1010

11-
using Microsoft.Scripting;
12-
using Microsoft.Scripting.Runtime;
13-
using Microsoft.Scripting.Utils;
14-
1511
using IronPython.Runtime;
1612
using IronPython.Runtime.Binding;
1713
using IronPython.Runtime.Exceptions;
1814
using IronPython.Runtime.Operations;
1915
using IronPython.Runtime.Types;
2016

17+
using Microsoft.Scripting;
18+
using Microsoft.Scripting.Runtime;
19+
using Microsoft.Scripting.Utils;
20+
2121
[assembly: PythonModule("itertools", typeof(IronPython.Modules.PythonIterTools))]
2222
namespace IronPython.Modules {
2323
public static class PythonIterTools {
@@ -233,7 +233,7 @@ private static void EnsureIterator(CodeContext/*!*/ context, object iter) {
233233
if (iter == null ||
234234
!PythonOps.HasAttr(context, iter, "__iter__") &&
235235
!PythonOps.HasAttr(context, iter, "__getitem__")) {
236-
throw PythonOps.TypeError("'{0}' object is not iterable", PythonOps.GetPythonTypeName(iter));
236+
throw PythonOps.TypeError("'{0}' object is not iterable", PythonOps.GetPythonTypeName(iter));
237237
}
238238
}
239239

@@ -276,13 +276,13 @@ public count(BigInteger start) {
276276
InnerEnumerator = BigIntYielder(this, start, 1);
277277
}
278278

279-
public count(int start=0, int step=1) {
279+
public count(int start = 0, int step = 1) {
280280
_curInt = start;
281281
_step = step;
282282
InnerEnumerator = IntYielder(this, start, step);
283283
}
284284

285-
public count([DefaultParameterValue(0)]int start, BigInteger step) {
285+
public count([DefaultParameterValue(0)] int start, BigInteger step) {
286286
_curInt = start;
287287
_step = step;
288288
InnerEnumerator = IntYielder(this, start, step);
@@ -300,7 +300,7 @@ public count(BigInteger start, BigInteger step) {
300300
InnerEnumerator = BigIntYielder(this, start, step);
301301
}
302302

303-
public count(CodeContext/*!*/ context, [DefaultParameterValue(0)]object start, [DefaultParameterValue(1)]object step) {
303+
public count(CodeContext/*!*/ context, [DefaultParameterValue(0)] object start, [DefaultParameterValue(1)] object step) {
304304
EnsureNumeric(context, start);
305305
EnsureNumeric(context, step);
306306
_cur = start;
@@ -315,7 +315,7 @@ private static void EnsureNumeric(CodeContext/*!*/ context, object num) {
315315
if (num == null ||
316316
!PythonOps.HasAttr(context, num, "__int__") &&
317317
!PythonOps.HasAttr(context, num, "__float__")) {
318-
throw PythonOps.TypeError("a number is required");
318+
throw PythonOps.TypeError("a number is required");
319319
}
320320
}
321321

@@ -548,9 +548,9 @@ private IEnumerator<object> Yielder(IEnumerator iter) {
548548
private IEnumerator<object> Grouper(IEnumerator iter, object curKey) {
549549
while (PythonContext.Equal(GetKey(iter.Current), curKey)) {
550550
yield return iter.Current;
551-
if (!MoveNextHelper(iter)) {
552-
_fFinished = true;
553-
yield break;
551+
if (!MoveNextHelper(iter)) {
552+
_fFinished = true;
553+
yield break;
554554
}
555555
}
556556
}
@@ -675,7 +675,7 @@ public zip_longest(params object[] iterables) {
675675
}
676676
}
677677

678-
public zip_longest([ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
678+
public zip_longest([ParamDictionary] IDictionary<object, object> paramDict, params object[] iterables) {
679679
object fill;
680680

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

761761
[PythonType]
762762
public class product : IterBase {
763+
private PythonTuple[] tuples;
764+
763765
public product(CodeContext context, params object[] iterables) {
764-
InnerEnumerator = Yielder(ArrayUtils.ConvertAll(iterables, x => new PythonList(context, PythonOps.GetEnumerator(x))));
766+
tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(PythonOps.GetEnumerator(x)));
767+
InnerEnumerator = Yielder(tuples);
765768
}
766769

767-
public product(CodeContext context, [ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
770+
public product(CodeContext context, [ParamDictionary] IDictionary<object, object> paramDict, params object[] iterables) {
768771
object repeat;
769772
int iRepeat = 1;
770773
if (paramDict.TryGetValue("repeat", out repeat)) {
@@ -782,29 +785,28 @@ public product(CodeContext context, [ParamDictionary]IDictionary<object, object>
782785
throw UnexpectedKeywordArgument(paramDict);
783786
}
784787

785-
PythonList[] finalIterables = new PythonList[iterables.Length * iRepeat];
788+
tuples = new PythonTuple[iterables.Length * iRepeat];
786789
for (int i = 0; i < iRepeat; i++) {
787790
for (int j = 0; j < iterables.Length; j++) {
788-
finalIterables[i * iterables.Length + j] = new PythonList(context, iterables[j]);
791+
tuples[i * iterables.Length + j] = new PythonTuple(iterables[j]);
789792
}
790793
}
791-
InnerEnumerator = Yielder(finalIterables);
794+
InnerEnumerator = Yielder(tuples);
792795
}
793796

794797
public PythonTuple __reduce__() {
795-
// TODO
796798
return PythonTuple.MakeTuple(
797799
DynamicHelpers.GetPythonType(this),
798-
PythonTuple.MakeTuple(), // arguments
799-
null // state
800+
PythonTuple.MakeTuple(tuples), // arguments
801+
null // TODO: state
800802
);
801803
}
802804

803805
public void __setstate__(object state) {
804806
// TODO
805807
}
806808

807-
private IEnumerator<object> Yielder(PythonList[] iterables) {
809+
private IEnumerator<object> Yielder(PythonTuple[] iterables) {
808810
if (iterables.Length > 0) {
809811
IEnumerator[] enums = new IEnumerator[iterables.Length];
810812
enums[0] = iterables[0].GetEnumerator();
@@ -833,6 +835,7 @@ private IEnumerator<object> Yielder(PythonList[] iterables) {
833835
} else {
834836
yield return PythonTuple.EMPTY;
835837
}
838+
tuples = new PythonTuple[1] { PythonTuple.EMPTY };
836839
}
837840
}
838841

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

988991
public permutations(CodeContext context, object iterable, object r) {
989992
_data = new PythonList(context, iterable);
990-
993+
991994
InnerEnumerator = Yielder(GetR(r, _data));
992995
}
993996

@@ -1078,7 +1081,7 @@ public repeat(object @object) {
10781081
public repeat(object @object, int times) {
10791082
_obj = @object;
10801083
InnerEnumerator = Yielder();
1081-
_remaining = times;
1084+
_remaining = times < 0 ? 0 : times;
10821085
}
10831086

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

10961099
public PythonTuple __reduce__() {
1097-
// TODO
10981100
return PythonTuple.MakeTuple(
10991101
DynamicHelpers.GetPythonType(this),
1100-
PythonTuple.MakeTuple() // arguments
1102+
_fInfinite ? PythonTuple.MakeTuple(_obj) : PythonTuple.MakeTuple(_obj, _remaining) // arguments
11011103
);
11021104
}
11031105

@@ -1150,9 +1152,9 @@ IEnumerator IEnumerable.GetEnumerator() {
11501152

11511153
#endregion
11521154
}
1153-
1155+
11541156
[PythonType]
1155-
public class starmap : IterBase {
1157+
public class starmap : IterBase {
11561158
public starmap(CodeContext context, object function, object iterable) {
11571159
InnerEnumerator = Yielder(context, function, PythonOps.GetEnumerator(iterable));
11581160
}
@@ -1211,7 +1213,7 @@ public void __setstate__(object state) {
12111213

12121214
private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
12131215
while (MoveNextHelper(iter)) {
1214-
if(!Converter.ConvertToBoolean(
1216+
if (!Converter.ConvertToBoolean(
12151217
_context.LanguageContext.CallSplat(predicate, iter.Current)
12161218
)) {
12171219
break;
@@ -1222,8 +1224,8 @@ private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
12221224
}
12231225
}
12241226

1225-
[PythonHidden]
1226-
public class TeeIterator : IEnumerator, IWeakReferenceable {
1227+
[PythonType("_tee")]
1228+
public sealed class TeeIterator : IEnumerator, IWeakReferenceable {
12271229
internal IEnumerator _iter;
12281230
internal PythonList _data;
12291231
private int _curIndex = -1;

Src/IronPython.Modules/_datetime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ internal override int CompareTo(date other) {
10991099
if (_tz != null) {
11001100
sb.AppendFormat(", tzinfo={0}", PythonOps.Repr(context, _tz));
11011101
}
1102-
sb.AppendFormat(")");
1102+
sb.Append(')');
11031103
return sb.ToString();
11041104
}
11051105
#endregion
@@ -1395,7 +1395,7 @@ private int CompareTo(time other) {
13951395
sb.AppendFormat(", tzinfo={0}", ltzname.ToLowerInvariant());
13961396
}
13971397

1398-
sb.AppendFormat(")");
1398+
sb.Append(')');
13991399

14001400
return sb.ToString();
14011401
}

Src/IronPython/Runtime/Binding/PythonBinder.cs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Linq.Expressions;
6-
using System.Numerics;
7-
85
using System;
96
using System.Collections;
107
using System.Collections.Generic;
118
using System.Diagnostics;
129
using System.Dynamic;
1310
using System.Linq;
11+
using System.Linq.Expressions;
12+
using System.Numerics;
1413
using System.Reflection;
1514
using System.Threading;
1615

16+
using IronPython.Runtime.Exceptions;
17+
using IronPython.Runtime.Operations;
18+
using IronPython.Runtime.Types;
19+
1720
using Microsoft.Scripting;
1821
using Microsoft.Scripting.Actions;
1922
using Microsoft.Scripting.Actions.Calls;
2023
using Microsoft.Scripting.Generation;
2124
using Microsoft.Scripting.Runtime;
2225
using Microsoft.Scripting.Utils;
2326

24-
using IronPython.Runtime.Exceptions;
25-
using IronPython.Runtime.Operations;
26-
using IronPython.Runtime.Types;
27-
2827
namespace IronPython.Runtime.Binding {
2928
using Ast = Expression;
3029
using AstUtils = Microsoft.Scripting.Ast.Utils;
@@ -43,7 +42,7 @@ public PythonBinder(PythonContext/*!*/ pythonContext, CodeContext context) {
4342
ContractUtils.RequiresNotNull(pythonContext, nameof(pythonContext));
4443

4544
_dlrExtensionTypes = MakeExtensionTypes();
46-
_context = pythonContext;
45+
_context = pythonContext;
4746
if (context != null) {
4847
context.LanguageContext.DomainManager.AssemblyLoaded += new EventHandler<AssemblyLoadedEventArgs>(DomainManager_AssemblyLoaded);
4948

@@ -82,7 +81,7 @@ public PythonBinder(PythonBinder binder) {
8281
}
8382
return expr;
8483
}
85-
84+
8685
Type visType = Context.Binder.PrivateBinding ? toType : CompilerHelpers.GetVisibleType(toType);
8786

8887
if (exprType == typeof(PythonType) && visType == typeof(Type)) {
@@ -185,17 +184,17 @@ public override ErrorInfo MakeSetValueTypeFieldError(FieldTracker field, Dynamic
185184
Expression.Assign(
186185
Expression.Field(
187186
AstUtils.Convert(
188-
instance.Expression,
187+
instance.Expression,
189188
field.DeclaringType
190-
),
189+
),
191190
field.Field
192191
),
193192
ConvertExpression(
194-
value.Expression,
195-
field.FieldType,
196-
ConversionResultKind.ExplicitCast,
193+
value.Expression,
194+
field.FieldType,
195+
ConversionResultKind.ExplicitCast,
197196
new PythonOverloadResolverFactory(
198-
this,
197+
this,
199198
Expression.Constant(_context.SharedContext)
200199
)
201200
)
@@ -237,7 +236,7 @@ public override ErrorInfo MakeConversionError(Type toType, Expression value) {
237236
return MakeMissingMemberError(accessingType, instance, info.Name);
238237
}
239238

240-
public override ErrorInfo/*!*/ MakeStaticPropertyInstanceAccessError(PropertyTracker/*!*/ tracker, bool isAssignment, IList<DynamicMetaObject>/*!*/ parameters) {
239+
public override ErrorInfo/*!*/ MakeStaticPropertyInstanceAccessError(PropertyTracker/*!*/ tracker, bool isAssignment, IList<DynamicMetaObject>/*!*/ parameters) {
241240
ContractUtils.RequiresNotNull(tracker, nameof(tracker));
242241
ContractUtils.RequiresNotNull(parameters, nameof(parameters));
243242

@@ -281,7 +280,7 @@ public override string GetTypeName(Type t) {
281280
}
282281

283282

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

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

406405
return list;
407406
}
408-
407+
409408
private void AddExtensionTypes(Type t, List<Type> list) {
410409
ExtensionTypeInfo extType;
411410
if (_sysTypes.TryGetValue(t, out extType)) {
@@ -461,16 +460,16 @@ private static DynamicMetaObject ReturnMemberTracker(Type type, MemberTracker me
461460
case TrackerTypes.Bound:
462461
return new DynamicMetaObject(ReturnBoundTracker((BoundMemberTracker)memberTracker, privateBinding), BindingRestrictions.Empty);
463462
case TrackerTypes.Property:
464-
return new DynamicMetaObject(ReturnPropertyTracker((PropertyTracker)memberTracker, privateBinding), BindingRestrictions.Empty);;
463+
return new DynamicMetaObject(ReturnPropertyTracker((PropertyTracker)memberTracker, privateBinding), BindingRestrictions.Empty); ;
465464
case TrackerTypes.Event:
466465
return new DynamicMetaObject(Ast.Call(
467466
typeof(PythonOps).GetMethod(nameof(PythonOps.MakeBoundEvent)),
468467
AstUtils.Constant(PythonTypeOps.GetReflectedEvent((EventTracker)memberTracker)),
469468
AstUtils.Constant(null),
470469
AstUtils.Constant(type)
471-
), BindingRestrictions.Empty);;
470+
), BindingRestrictions.Empty);
472471
case TrackerTypes.Field:
473-
return new DynamicMetaObject(ReturnFieldTracker((FieldTracker)memberTracker), BindingRestrictions.Empty);;
472+
return new DynamicMetaObject(ReturnFieldTracker((FieldTracker)memberTracker), BindingRestrictions.Empty); ;
474473
case TrackerTypes.MethodGroup:
475474
return new DynamicMetaObject(ReturnMethodGroup((MethodGroup)memberTracker), BindingRestrictions.Empty); ;
476475
case TrackerTypes.Constructor:
@@ -589,7 +588,7 @@ public void LookupMembers(CodeContext/*!*/ context, PythonType/*!*/ type, Python
589588

590589
if (!members.ContainsKey(rm.Name)) {
591590
members[rm.Name] = new KeyValuePair<PythonTypeSlot, MemberGroup>(
592-
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
591+
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
593592
rm.Member
594593
);
595594
}
@@ -625,7 +624,7 @@ public void ResolveMemberNames(CodeContext/*!*/ context, PythonType/*!*/ type, P
625624

626625
if (!members.ContainsKey(rm.Name)) {
627626
members[rm.Name] = new KeyValuePair<PythonTypeSlot, MemberGroup>(
628-
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
627+
PythonTypeOps.GetSlot(rm.Member, rm.Name, PrivateBinding),
629628
rm.Member
630629
);
631630
}
@@ -1084,7 +1083,7 @@ private void EnsureInfo() {
10841083
private class SlotCacheInfo {
10851084
public SlotCacheInfo() {
10861085
Members = new Dictionary<string/*!*/, KeyValuePair<PythonTypeSlot, MemberGroup/*!*/>>(StringComparer.Ordinal);
1087-
}
1086+
}
10881087

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

0 commit comments

Comments
 (0)