Skip to content

Commit 25a94c3

Browse files
authored
Use MutableTuple.GetAccessPath with size (#931)
* Use MutableTuple.GetAccessPath with size * Add test
1 parent d9fe0be commit 25a94c3

4 files changed

Lines changed: 28 additions & 7 deletions

File tree

Src/IronPython/Compiler/GeneratorRewriter.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal sealed class GeneratorRewriter : DynamicExpressionVisitor {
3131
private readonly Expression _body;
3232
private readonly string _name;
3333
private readonly StrongBox<Type> _tupleType = new StrongBox<Type>(null);
34+
private readonly StrongBox<int> _tupleSize = new StrongBox<int>();
3435
private readonly StrongBox<ParameterExpression> _tupleExpr = new StrongBox<ParameterExpression>(null);
3536

3637
// The one return label, or more than one if we're in a finally
@@ -101,6 +102,7 @@ internal Expression Reduce(bool shouldInterpret, bool emitDebugSymbols, int comp
101102

102103
Expression newTuple = MutableTuple.Create(tupleExprs);
103104
Type tupleType = _tupleType.Value = newTuple.Type;
105+
_tupleSize.Value = tupleExprs.Length;
104106
ParameterExpression tupleExpr = _tupleExpr.Value = Expression.Parameter(tupleType, "tuple");
105107
ParameterExpression tupleArg = Expression.Parameter(typeof(MutableTuple), "tupleArg");
106108
_temps.Add(_gotoRouter);
@@ -152,7 +154,7 @@ internal Expression Reduce(bool shouldInterpret, bool emitDebugSymbols, int comp
152154
)
153155
),
154156
new DelayedTupleAssign(
155-
new DelayedTupleExpression(liftedGen.Index, new StrongBox<ParameterExpression>(tupleTmp), _tupleType, typeof(PythonGenerator)),
157+
new DelayedTupleExpression(liftedGen.Index, new StrongBox<ParameterExpression>(tupleTmp), _tupleType, _tupleSize, typeof(PythonGenerator)),
156158
ret
157159
),
158160
ret
@@ -653,7 +655,7 @@ protected override Expression VisitBlock(BlockExpression node) {
653655
private DelayedTupleExpression LiftVariable(ParameterExpression param) {
654656
DelayedTupleExpression res;
655657
if (!_vars.TryGetValue(param, out res)) {
656-
_vars[param] = res = new DelayedTupleExpression(_vars.Count, _tupleExpr, _tupleType, param.Type);
658+
_vars[param] = res = new DelayedTupleExpression(_vars.Count, _tupleExpr, _tupleType, _tupleSize, param.Type);
657659
_orderedVars.Add(new KeyValuePair<ParameterExpression, DelayedTupleExpression>(param, res));
658660
}
659661

@@ -981,19 +983,21 @@ internal YieldMarker(int state) {
981983
internal sealed class DelayedTupleExpression : Expression {
982984
public readonly int Index;
983985
private readonly StrongBox<Type> _tupleType;
986+
private readonly StrongBox<int> _tupleSize;
984987
private readonly StrongBox<ParameterExpression> _tupleExpr;
985988
private readonly Type _type;
986989

987-
public DelayedTupleExpression(int index, StrongBox<ParameterExpression> tupleExpr, StrongBox<Type> tupleType, Type type) {
990+
public DelayedTupleExpression(int index, StrongBox<ParameterExpression> tupleExpr, StrongBox<Type> tupleType, StrongBox<int> tupleSize, Type type) {
988991
Index = index;
989992
_tupleType = tupleType;
993+
_tupleSize = tupleSize;
990994
_tupleExpr = tupleExpr;
991995
_type = type;
992996
}
993997

994998
public override Expression Reduce() {
995999
Expression res = _tupleExpr.Value;
996-
foreach (PropertyInfo pi in MutableTuple.GetAccessPath(_tupleType.Value, Index)) {
1000+
foreach (PropertyInfo pi in MutableTuple.GetAccessPath(_tupleType.Value, _tupleSize.Value, Index)) {
9971001
res = Expression.Property(res, pi);
9981002
}
9991003
return res;

Src/IronPythonTest/IronPythonTest.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="NUnitLite" Version="3.12.0" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
19-
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
19+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

Tests/test_json.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the .NET Foundation under one or more agreements.
2+
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
# See the LICENSE file in the project root for more information.
4+
5+
import json
6+
import unittest
7+
8+
from iptest import run_test
9+
10+
class JsonTest(unittest.TestCase):
11+
12+
def test_ipy3_gh926(self):
13+
"""https://github.com/IronLanguages/ironpython3/issues/926"""
14+
15+
self.assertEqual(json.dumps([]), "[]")
16+
17+
run_test(__name__)

0 commit comments

Comments
 (0)