Skip to content

Commit

Permalink
Get rid of ArrayUtils.EmptyObjects (#1820)
Browse files Browse the repository at this point in the history
* Get rid of ArrayUtils.EmptyObjects

* Remove more ArrayUtils.EmptyObjects
  • Loading branch information
slozier authored Nov 29, 2024
1 parent aefaecb commit e970303
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 223 deletions.
4 changes: 2 additions & 2 deletions Src/IronPython.Modules/_thread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ public void Start() {
try {
#pragma warning disable 618 // TODO: obsolete
if (_kwargs != null) {
PythonOps.CallWithArgsTupleAndKeywordDictAndContext(_context, _func, ArrayUtils.EmptyObjects, ArrayUtils.EmptyStrings, _args, _kwargs);
PythonOps.CallWithArgsTupleAndKeywordDictAndContext(_context, _func, [], [], _args, _kwargs);
} else {
PythonOps.CallWithArgsTuple(_func, ArrayUtils.EmptyObjects, _args);
PythonOps.CallWithArgsTuple(_func, [], _args);
}
#pragma warning restore 618
} catch (SystemExitException) {
Expand Down
3 changes: 1 addition & 2 deletions Src/IronPython/Compiler/Ast/ClassDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using IronPython.Runtime;

using Microsoft.Scripting;
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Utils;

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

if (parameters.Count == 0) {
// no point analyzing function with no parameters
return ArrayUtils.EmptyStrings;
return [];
}

var finder = new SelfNameFinder(function, parameters[0]);
Expand Down
28 changes: 13 additions & 15 deletions Src/IronPython/Compiler/Ast/FunctionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Runtime.CompilerServices;

using Microsoft.Scripting;
using Microsoft.Scripting.Interpreter;
using Microsoft.Scripting.Utils;

using IronPython.Runtime;
using IronPython.Runtime.Operations;

using MSAst = System.Linq.Expressions;
using Microsoft.Scripting;
using Microsoft.Scripting.Interpreter;
using Microsoft.Scripting.Utils;

using LightLambdaExpression = Microsoft.Scripting.Ast.LightLambdaExpression;
using AstUtils = Microsoft.Scripting.Ast.Utils;
using LightLambdaExpression = Microsoft.Scripting.Ast.LightLambdaExpression;
using MSAst = System.Linq.Expressions;

namespace IronPython.Compiler.Ast {
using Ast = MSAst.Expression;
Expand Down Expand Up @@ -85,8 +84,7 @@ internal override MSAst.Expression LocalContext {
internal override int ArgCount {
get {
int argCount = 0;
for (argCount = 0; argCount < _parameters.Length; argCount++)
{
for (argCount = 0; argCount < _parameters.Length; argCount++) {
Parameter p = _parameters[argCount];
if (p.IsDictionary || p.IsList || p.IsKeywordOnly) break;
}
Expand Down Expand Up @@ -256,7 +254,7 @@ internal override void Bind(PythonNameBinder binder) {
NeedsLocalsDictionary = true;
}
}

internal override void FinishBind(PythonNameBinder binder) {
foreach (var param in _parameters) {
_variableMapping[param.PythonVariable] = param.FinishBind(forceClosureCell: ExposesLocalVariable(param.PythonVariable));
Expand Down Expand Up @@ -286,7 +284,7 @@ public override MSAst.Expression Reduce() {
new SourceSpan(GlobalParent.IndexToLocation(StartIndex), GlobalParent.IndexToLocation(HeaderIndex))
);
}

/// <summary>
/// Returns an expression which creates the function object.
/// </summary>
Expand Down Expand Up @@ -542,7 +540,7 @@ public override int Run(InterpretedFrame frame) {
defaults[i] = frame.Pop();
}
} else {
defaults = ArrayUtils.EmptyObjects;
defaults = [];
}

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

return res;
}

private void InitializeParameters(List<MSAst.Expression> init, bool needsWrapperMethod, MSAst.Expression[] parameters) {
for (int i = 0; i < _parameters.Length; i++) {
Parameter p = _parameters[i];
Expand Down Expand Up @@ -829,10 +827,10 @@ private static Type GetDelegateType(Parameter[] parameters, bool wrapper, out De

internal override void RewriteBody(MSAst.ExpressionVisitor visitor) {
_dlrBody = null; // clear the cached body if we've been reduced

MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
FuncCodeExpr = funcCode;

Body = new RewrittenBodyStatement(Body, visitor.Visit(Body));
}

Expand Down
8 changes: 4 additions & 4 deletions Src/IronPython/Compiler/Ast/ScopeStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public abstract class ScopeStatement : Statement {
///
/// It is used to ensure that the first argument is accessible through a closure cell.
/// </summary>
internal bool ContainsSuperCall{ get; set; }
internal bool ContainsSuperCall { get; set; }

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

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

internal virtual string[] ParameterNames => ArrayUtils.EmptyStrings;
internal virtual string[] ParameterNames => [];

internal virtual int ArgCount => 0;

Expand Down Expand Up @@ -439,7 +439,7 @@ private PythonVariable CreateNonlocalVariable(string name) {
}

internal void EnsureNonlocalVariable(string name, NonlocalStatement node) {
_nonlocalVars ??= new();
_nonlocalVars ??= new();
if (!_nonlocalVars.ContainsKey(name)) {
CreateNonlocalVariable(name);
_nonlocalVars[name] = node;
Expand Down Expand Up @@ -611,7 +611,7 @@ internal MSAst.Expression? FuncCodeExpr {

internal MSAst.MethodCallExpression CreateLocalContext(MSAst.Expression parentContext, bool newNamespace = true) {
var closureVariables = _closureVariables ?? Array.Empty<ClosureInfo>();

int numFreeVars = FreeVariables?.Count ?? 0;
int firstArgIdx = -1;
if ((NeedsLocalsDictionary || ContainsSuperCall) && ArgCount > 0) {
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Compiler/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ private ModuleName ParseRelativeModuleName() {
dotCount++;
}

string[] names = ArrayUtils.EmptyStrings;
string[] names = [];
if (PeekToken() is NameToken) {
names = ReadNames();
}
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Runtime/Binding/WarningInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// 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;
using System.Dynamic;
using System.Linq.Expressions;

using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Microsoft.Scripting.Utils;

using AstUtils = Microsoft.Scripting.Ast.Utils;

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

if (_condition != null) {
Expand Down
5 changes: 3 additions & 2 deletions Src/IronPython/Runtime/BuiltinPythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// 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;
using System.Collections.Generic;

using IronPython.Compiler;

using Microsoft.Scripting.Utils;

namespace IronPython.Runtime {
Expand Down Expand Up @@ -50,7 +51,7 @@ protected internal virtual void Initialize(CodeContext/*!*/ codeContext, Diction
/// direct access to global members.
/// </summary>
protected internal virtual IEnumerable<string/*!*/>/*!*/ GetGlobalVariableNames() {
return ArrayUtils.EmptyStrings;
return [];
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public static bool CanConvertFrom(Type fromType, Type toType, NarrowingLevel all
private static TypeConverter GetTypeConverter(TypeConverterAttribute tca) {
try {
ConstructorInfo ci = Type.GetType(tca.ConverterTypeName).GetConstructor(ReflectionUtils.EmptyTypes);
if (ci != null) return ci.Invoke(ArrayUtils.EmptyObjects) as TypeConverter;
if (ci != null) return ci.Invoke([]) as TypeConverter;
} catch (TargetInvocationException) {
}
return null;
Expand Down
18 changes: 6 additions & 12 deletions Src/IronPython/Runtime/Exceptions/PythonExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -122,7 +119,7 @@ public override string ToString() {
}

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

private static PythonType ErrnoToPythonType(Error errno) {
var res = errno switch
{
var res = errno switch {
Error.EPERM => PermissionError,
Error.ENOENT => FileNotFoundError,
Error.ESRCH => ProcessLookupError,
Expand All @@ -239,8 +235,7 @@ private static PythonType ErrnoToPythonType(Error errno) {
_ => null
};
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
res ??= errno switch
{
res ??= errno switch {
// Windows or remapped OSX
Error.WSAEWOULDBLOCK => BlockingIOError,
Error.WSAEINPROGRESS => BlockingIOError,
Expand All @@ -253,8 +248,7 @@ private static PythonType ErrnoToPythonType(Error errno) {
_ => null
};
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
res ??= errno switch
{
res ??= errno switch {
// Linux
Error.ECONNABORTED => ConnectionAbortedError,
Error.ECONNRESET => ConnectionResetError,
Expand Down Expand Up @@ -529,7 +523,7 @@ internal static int WinErrorToErrno(int winerror) {
}

public partial class _ImportError {
public void __init__([ParamDictionary]IDictionary<string, object> kwargs, params object[] args) {
public void __init__([ParamDictionary] IDictionary<string, object> kwargs, params object[] args) {
base.__init__(args);

foreach (var pair in kwargs) {
Expand Down Expand Up @@ -702,7 +696,7 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte
if (PythonOps.IsInstance(value, type)) {
pyEx = value;
} else if (value is PythonTuple) {
pyEx = PythonOps.CallWithArgsTuple(type, ArrayUtils.EmptyObjects, value);
pyEx = PythonOps.CallWithArgsTuple(type, [], value);
} else if (value != null) {
pyEx = PythonCalls.Call(context, type, value);
} else {
Expand Down
24 changes: 11 additions & 13 deletions Src/IronPython/Runtime/FunctionCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
// 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 Microsoft.Scripting.Ast;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

using IronPython.Compiler;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;

using Microsoft.Scripting;
using Microsoft.Scripting.Ast;
using Microsoft.Scripting.Debugging.CompilerServices;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Interpreter;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

using IronPython.Compiler;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;

namespace IronPython.Runtime {
/// <summary>
/// Represents a piece of code. This can reference either a CompiledCode
Expand Down Expand Up @@ -601,7 +600,7 @@ internal object Call(CodeContext/*!*/ context) {
return optimizedModuleCode(this);
}

var func = new PythonFunction(context, this, null, ArrayUtils.EmptyObjects, null, null, new MutableTuple<object>());
var func = new PythonFunction(context, this, null, [], null, null, new MutableTuple<object>());
CallSite<Func<CallSite, CodeContext, PythonFunction, object>> site = context.LanguageContext.FunctionCallSite;
return site.Target(site, context, func);
}
Expand Down Expand Up @@ -772,7 +771,7 @@ private LambdaExpression GetGeneratorOrNormalLambdaTracing(PythonContext context
);
}


/// <summary>
/// Gets the correct final LambdaExpression for this piece of code.
///
Expand All @@ -784,8 +783,8 @@ private LightLambdaExpression GetGeneratorOrNormalLambda() {
finalCode = Code;
} else {
finalCode = Code.ToGenerator(
_lambda.ShouldInterpret,
_lambda.EmitDebugSymbols,
_lambda.ShouldInterpret,
_lambda.EmitDebugSymbols,
_lambda.GlobalParent.PyContext.Options.CompilationThreshold
);
}
Expand Down Expand Up @@ -838,7 +837,7 @@ private Delegate CompileLambda(LambdaExpression code, EventHandler<LightLambdaCo

internal Delegate AddRecursionCheck(PythonContext context, Delegate finalTarget) {
if (context.RecursionLimit != Int32.MaxValue) {
if (finalTarget is Func<CodeContext, CodeContext> ||
if (finalTarget is Func<CodeContext, CodeContext> ||
finalTarget is Func<FunctionCode, object> ||
finalTarget is LookupCompilationDelegate) {
// no recursion enforcement on classes or modules
Expand Down Expand Up @@ -1185,7 +1184,6 @@ public Dictionary<int, bool> LoopOrFinallyIds {
return _loopIds;
}
}

}
}
}
7 changes: 4 additions & 3 deletions Src/IronPython/Runtime/ModuleDictionaryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

using IronPython.Compiler;
using IronPython.Runtime.Types;

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

namespace IronPython.Runtime {
/// <summary>
Expand Down Expand Up @@ -162,7 +163,7 @@ private bool TryGetLazyValue(string name, bool publish, out object value) {

var propInfo = (PropertyInfo)members[0];
if ((propInfo.GetGetMethod() ?? propInfo.GetSetMethod()).IsStatic) {
value = ((PropertyInfo)members[0]).GetValue(null, ArrayUtils.EmptyObjects);
value = ((PropertyInfo)members[0]).GetValue(null, []);
} else {
throw new InvalidOperationException("instance property declared on module. Propreties should be declared as static, marked as PythonHidden, or you should use a PythonGlobal.");
}
Expand Down Expand Up @@ -217,7 +218,7 @@ public override bool TryGetValue(object key, out object value) {
return value != Uninitialized.Instance;
}

if(key is string strKey) {
if (key is string strKey) {
return TryGetLazyValue(strKey, out value);
}

Expand Down
Loading

0 comments on commit e970303

Please sign in to comment.