Skip to content

Commit e970303

Browse files
authored
Get rid of ArrayUtils.EmptyObjects (#1820)
* Get rid of ArrayUtils.EmptyObjects * Remove more ArrayUtils.EmptyObjects
1 parent aefaecb commit e970303

22 files changed

+204
-223
lines changed

Src/IronPython.Modules/_thread.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ public void Start() {
228228
try {
229229
#pragma warning disable 618 // TODO: obsolete
230230
if (_kwargs != null) {
231-
PythonOps.CallWithArgsTupleAndKeywordDictAndContext(_context, _func, ArrayUtils.EmptyObjects, ArrayUtils.EmptyStrings, _args, _kwargs);
231+
PythonOps.CallWithArgsTupleAndKeywordDictAndContext(_context, _func, [], [], _args, _kwargs);
232232
} else {
233-
PythonOps.CallWithArgsTuple(_func, ArrayUtils.EmptyObjects, _args);
233+
PythonOps.CallWithArgsTuple(_func, [], _args);
234234
}
235235
#pragma warning restore 618
236236
} catch (SystemExitException) {

Src/IronPython/Compiler/Ast/ClassDefinition.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using IronPython.Runtime;
1616

1717
using Microsoft.Scripting;
18-
using Microsoft.Scripting.Actions;
1918
using Microsoft.Scripting.Utils;
2019

2120
using AstUtils = Microsoft.Scripting.Ast.Utils;
@@ -403,7 +402,7 @@ public static string[] FindNames(FunctionDefinition function) {
403402

404403
if (parameters.Count == 0) {
405404
// no point analyzing function with no parameters
406-
return ArrayUtils.EmptyStrings;
405+
return [];
407406
}
408407

409408
var finder = new SelfNameFinder(function, parameters[0]);

Src/IronPython/Compiler/Ast/FunctionDefinition.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Runtime.CompilerServices;
89
using System.Text;
910
using System.Threading;
10-
using System.Runtime.CompilerServices;
11-
12-
using Microsoft.Scripting;
13-
using Microsoft.Scripting.Interpreter;
14-
using Microsoft.Scripting.Utils;
1511

1612
using IronPython.Runtime;
1713
using IronPython.Runtime.Operations;
1814

19-
using MSAst = System.Linq.Expressions;
15+
using Microsoft.Scripting;
16+
using Microsoft.Scripting.Interpreter;
17+
using Microsoft.Scripting.Utils;
2018

21-
using LightLambdaExpression = Microsoft.Scripting.Ast.LightLambdaExpression;
2219
using AstUtils = Microsoft.Scripting.Ast.Utils;
20+
using LightLambdaExpression = Microsoft.Scripting.Ast.LightLambdaExpression;
21+
using MSAst = System.Linq.Expressions;
2322

2423
namespace IronPython.Compiler.Ast {
2524
using Ast = MSAst.Expression;
@@ -85,8 +84,7 @@ internal override MSAst.Expression LocalContext {
8584
internal override int ArgCount {
8685
get {
8786
int argCount = 0;
88-
for (argCount = 0; argCount < _parameters.Length; argCount++)
89-
{
87+
for (argCount = 0; argCount < _parameters.Length; argCount++) {
9088
Parameter p = _parameters[argCount];
9189
if (p.IsDictionary || p.IsList || p.IsKeywordOnly) break;
9290
}
@@ -256,7 +254,7 @@ internal override void Bind(PythonNameBinder binder) {
256254
NeedsLocalsDictionary = true;
257255
}
258256
}
259-
257+
260258
internal override void FinishBind(PythonNameBinder binder) {
261259
foreach (var param in _parameters) {
262260
_variableMapping[param.PythonVariable] = param.FinishBind(forceClosureCell: ExposesLocalVariable(param.PythonVariable));
@@ -286,7 +284,7 @@ public override MSAst.Expression Reduce() {
286284
new SourceSpan(GlobalParent.IndexToLocation(StartIndex), GlobalParent.IndexToLocation(HeaderIndex))
287285
);
288286
}
289-
287+
290288
/// <summary>
291289
/// Returns an expression which creates the function object.
292290
/// </summary>
@@ -542,7 +540,7 @@ public override int Run(InterpretedFrame frame) {
542540
defaults[i] = frame.Pop();
543541
}
544542
} else {
545-
defaults = ArrayUtils.EmptyObjects;
543+
defaults = [];
546544
}
547545

548546
object modName;
@@ -779,7 +777,7 @@ internal override IList<string> GetVarNames() {
779777

780778
return res;
781779
}
782-
780+
783781
private void InitializeParameters(List<MSAst.Expression> init, bool needsWrapperMethod, MSAst.Expression[] parameters) {
784782
for (int i = 0; i < _parameters.Length; i++) {
785783
Parameter p = _parameters[i];
@@ -829,10 +827,10 @@ private static Type GetDelegateType(Parameter[] parameters, bool wrapper, out De
829827

830828
internal override void RewriteBody(MSAst.ExpressionVisitor visitor) {
831829
_dlrBody = null; // clear the cached body if we've been reduced
832-
830+
833831
MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
834832
FuncCodeExpr = funcCode;
835-
833+
836834
Body = new RewrittenBodyStatement(Body, visitor.Visit(Body));
837835
}
838836

Src/IronPython/Compiler/Ast/ScopeStatement.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public abstract class ScopeStatement : Statement {
9292
///
9393
/// It is used to ensure that the first argument is accessible through a closure cell.
9494
/// </summary>
95-
internal bool ContainsSuperCall{ get; set; }
95+
internal bool ContainsSuperCall { get; set; }
9696

9797
public virtual string Name => "<unknown scope>";
9898

@@ -116,7 +116,7 @@ public abstract class ScopeStatement : Statement {
116116
internal bool NeedsLocalContext
117117
=> NeedsLocalsDictionary || ContainsNestedFreeVariables || ContainsSuperCall;
118118

119-
internal virtual string[] ParameterNames => ArrayUtils.EmptyStrings;
119+
internal virtual string[] ParameterNames => [];
120120

121121
internal virtual int ArgCount => 0;
122122

@@ -439,7 +439,7 @@ private PythonVariable CreateNonlocalVariable(string name) {
439439
}
440440

441441
internal void EnsureNonlocalVariable(string name, NonlocalStatement node) {
442-
_nonlocalVars ??= new();
442+
_nonlocalVars ??= new();
443443
if (!_nonlocalVars.ContainsKey(name)) {
444444
CreateNonlocalVariable(name);
445445
_nonlocalVars[name] = node;
@@ -611,7 +611,7 @@ internal MSAst.Expression? FuncCodeExpr {
611611

612612
internal MSAst.MethodCallExpression CreateLocalContext(MSAst.Expression parentContext, bool newNamespace = true) {
613613
var closureVariables = _closureVariables ?? Array.Empty<ClosureInfo>();
614-
614+
615615
int numFreeVars = FreeVariables?.Count ?? 0;
616616
int firstArgIdx = -1;
617617
if ((NeedsLocalsDictionary || ContainsSuperCall) && ArgCount > 0) {

Src/IronPython/Compiler/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ private ModuleName ParseRelativeModuleName() {
804804
dotCount++;
805805
}
806806

807-
string[] names = ArrayUtils.EmptyStrings;
807+
string[] names = [];
808808
if (PeekToken() is NameToken) {
809809
names = ReadNames();
810810
}

Src/IronPython/Runtime/Binding/WarningInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
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;
56
using System.Dynamic;
67
using System.Linq.Expressions;
78

89
using IronPython.Runtime.Operations;
910
using IronPython.Runtime.Types;
10-
using Microsoft.Scripting.Utils;
1111

1212
using AstUtils = Microsoft.Scripting.Ast.Utils;
1313

@@ -29,7 +29,7 @@ public WarningInfo(PythonType/*!*/ type, string/*!*/ message, Expression conditi
2929
codeContext,
3030
AstUtils.Constant(_type),
3131
AstUtils.Constant(_message),
32-
AstUtils.Constant(ArrayUtils.EmptyObjects)
32+
AstUtils.Constant(Array.Empty<object>())
3333
);
3434

3535
if (_condition != null) {

Src/IronPython/Runtime/BuiltinPythonModule.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
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;
65
using System.Collections.Generic;
6+
77
using IronPython.Compiler;
8+
89
using Microsoft.Scripting.Utils;
910

1011
namespace IronPython.Runtime {
@@ -50,7 +51,7 @@ protected internal virtual void Initialize(CodeContext/*!*/ codeContext, Diction
5051
/// direct access to global members.
5152
/// </summary>
5253
protected internal virtual IEnumerable<string/*!*/>/*!*/ GetGlobalVariableNames() {
53-
return ArrayUtils.EmptyStrings;
54+
return [];
5455
}
5556

5657
/// <summary>

Src/IronPython/Runtime/Converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ public static bool CanConvertFrom(Type fromType, Type toType, NarrowingLevel all
532532
private static TypeConverter GetTypeConverter(TypeConverterAttribute tca) {
533533
try {
534534
ConstructorInfo ci = Type.GetType(tca.ConverterTypeName).GetConstructor(ReflectionUtils.EmptyTypes);
535-
if (ci != null) return ci.Invoke(ArrayUtils.EmptyObjects) as TypeConverter;
535+
if (ci != null) return ci.Invoke([]) as TypeConverter;
536536
} catch (TargetInvocationException) {
537537
}
538538
return null;

Src/IronPython/Runtime/Exceptions/PythonExceptions.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
using System.Collections.Generic;
77
using System.ComponentModel;
88
using System.Diagnostics;
9-
using System.Dynamic;
109
using System.IO;
11-
using System.Linq.Expressions;
1210
using System.Reflection;
13-
using System.Runtime.CompilerServices;
1411
using System.Runtime.InteropServices;
1512
using System.Text;
1613
using System.Threading;
@@ -122,7 +119,7 @@ public override string ToString() {
122119
}
123120

124121
public partial class _OSError {
125-
public static new object __new__(PythonType cls, [ParamDictionary]IDictionary<string, object> kwArgs, params object[] args) {
122+
public static new object __new__(PythonType cls, [ParamDictionary] IDictionary<string, object> kwArgs, params object[] args) {
126123
if (cls == OSError && args.Length >= 1 && args[0] is int errno) {
127124
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
128125
if (args.Length >= 4 && args[3] is int winerror) {
@@ -223,8 +220,7 @@ private static Error ErrnoToErrorEnum(int errno) {
223220
}
224221

225222
private static PythonType ErrnoToPythonType(Error errno) {
226-
var res = errno switch
227-
{
223+
var res = errno switch {
228224
Error.EPERM => PermissionError,
229225
Error.ENOENT => FileNotFoundError,
230226
Error.ESRCH => ProcessLookupError,
@@ -239,8 +235,7 @@ private static PythonType ErrnoToPythonType(Error errno) {
239235
_ => null
240236
};
241237
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
242-
res ??= errno switch
243-
{
238+
res ??= errno switch {
244239
// Windows or remapped OSX
245240
Error.WSAEWOULDBLOCK => BlockingIOError,
246241
Error.WSAEINPROGRESS => BlockingIOError,
@@ -253,8 +248,7 @@ private static PythonType ErrnoToPythonType(Error errno) {
253248
_ => null
254249
};
255250
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
256-
res ??= errno switch
257-
{
251+
res ??= errno switch {
258252
// Linux
259253
Error.ECONNABORTED => ConnectionAbortedError,
260254
Error.ECONNRESET => ConnectionResetError,
@@ -529,7 +523,7 @@ internal static int WinErrorToErrno(int winerror) {
529523
}
530524

531525
public partial class _ImportError {
532-
public void __init__([ParamDictionary]IDictionary<string, object> kwargs, params object[] args) {
526+
public void __init__([ParamDictionary] IDictionary<string, object> kwargs, params object[] args) {
533527
base.__init__(args);
534528

535529
foreach (var pair in kwargs) {
@@ -702,7 +696,7 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte
702696
if (PythonOps.IsInstance(value, type)) {
703697
pyEx = value;
704698
} else if (value is PythonTuple) {
705-
pyEx = PythonOps.CallWithArgsTuple(type, ArrayUtils.EmptyObjects, value);
699+
pyEx = PythonOps.CallWithArgsTuple(type, [], value);
706700
} else if (value != null) {
707701
pyEx = PythonCalls.Call(context, type, value);
708702
} else {

Src/IronPython/Runtime/FunctionCode.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
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 Microsoft.Scripting.Ast;
7-
85
using System;
96
using System.Collections.Generic;
107
using System.Diagnostics;
8+
using System.Linq.Expressions;
119
using System.Reflection;
1210
using System.Runtime.CompilerServices;
1311
using System.Threading;
1412

13+
using IronPython.Compiler;
14+
using IronPython.Runtime.Operations;
15+
using IronPython.Runtime.Types;
16+
1517
using Microsoft.Scripting;
18+
using Microsoft.Scripting.Ast;
1619
using Microsoft.Scripting.Debugging.CompilerServices;
1720
using Microsoft.Scripting.Generation;
1821
using Microsoft.Scripting.Interpreter;
1922
using Microsoft.Scripting.Runtime;
2023
using Microsoft.Scripting.Utils;
2124

22-
using IronPython.Compiler;
23-
using IronPython.Runtime.Operations;
24-
using IronPython.Runtime.Types;
25-
2625
namespace IronPython.Runtime {
2726
/// <summary>
2827
/// Represents a piece of code. This can reference either a CompiledCode
@@ -601,7 +600,7 @@ internal object Call(CodeContext/*!*/ context) {
601600
return optimizedModuleCode(this);
602601
}
603602

604-
var func = new PythonFunction(context, this, null, ArrayUtils.EmptyObjects, null, null, new MutableTuple<object>());
603+
var func = new PythonFunction(context, this, null, [], null, null, new MutableTuple<object>());
605604
CallSite<Func<CallSite, CodeContext, PythonFunction, object>> site = context.LanguageContext.FunctionCallSite;
606605
return site.Target(site, context, func);
607606
}
@@ -772,7 +771,7 @@ private LambdaExpression GetGeneratorOrNormalLambdaTracing(PythonContext context
772771
);
773772
}
774773

775-
774+
776775
/// <summary>
777776
/// Gets the correct final LambdaExpression for this piece of code.
778777
///
@@ -784,8 +783,8 @@ private LightLambdaExpression GetGeneratorOrNormalLambda() {
784783
finalCode = Code;
785784
} else {
786785
finalCode = Code.ToGenerator(
787-
_lambda.ShouldInterpret,
788-
_lambda.EmitDebugSymbols,
786+
_lambda.ShouldInterpret,
787+
_lambda.EmitDebugSymbols,
789788
_lambda.GlobalParent.PyContext.Options.CompilationThreshold
790789
);
791790
}
@@ -838,7 +837,7 @@ private Delegate CompileLambda(LambdaExpression code, EventHandler<LightLambdaCo
838837

839838
internal Delegate AddRecursionCheck(PythonContext context, Delegate finalTarget) {
840839
if (context.RecursionLimit != Int32.MaxValue) {
841-
if (finalTarget is Func<CodeContext, CodeContext> ||
840+
if (finalTarget is Func<CodeContext, CodeContext> ||
842841
finalTarget is Func<FunctionCode, object> ||
843842
finalTarget is LookupCompilationDelegate) {
844843
// no recursion enforcement on classes or modules
@@ -1185,7 +1184,6 @@ public Dictionary<int, bool> LoopOrFinallyIds {
11851184
return _loopIds;
11861185
}
11871186
}
1188-
11891187
}
11901188
}
11911189
}

0 commit comments

Comments
 (0)