diff --git a/src/core/IronPython.Modules/_ctypes/_ctypes.cs b/src/core/IronPython.Modules/_ctypes/_ctypes.cs index aed9c5941..e0580c137 100644 --- a/src/core/IronPython.Modules/_ctypes/_ctypes.cs +++ b/src/core/IronPython.Modules/_ctypes/_ctypes.cs @@ -361,41 +361,45 @@ public static object byref(CData instance, int offset = 0) { return new NativeArgument(instance, "P"); } - public static object call_cdeclfunction(CodeContext context, int address, PythonTuple args) { +#nullable enable + + public static object? call_cdeclfunction(CodeContext context, int address, [NotNone] PythonTuple args) { return call_cdeclfunction(context, new IntPtr(address), args); } - public static object call_cdeclfunction(CodeContext context, BigInteger address, PythonTuple args) { + public static object? call_cdeclfunction(CodeContext context, BigInteger address, [NotNone] PythonTuple args) { return call_cdeclfunction(context, new IntPtr((long)address), args); } - public static object call_cdeclfunction(CodeContext context, IntPtr address, PythonTuple args) { + public static object? call_cdeclfunction(CodeContext context, IntPtr address, [NotNone] PythonTuple args) { CFuncPtrType funcType = GetFunctionType(context, FUNCFLAG_CDECL); _CFuncPtr func = (_CFuncPtr)funcType.CreateInstance(context, address); - return PythonOps.CallWithArgsTuple(func, System.Array.Empty(), args); + return PythonCalls.Call(context, func, args.ToArray()); } public static void call_commethod() { } - public static object call_function(CodeContext context, int address, PythonTuple args) { + public static object? call_function(CodeContext context, int address, [NotNone] PythonTuple args) { return call_function(context, new IntPtr(address), args); } - public static object call_function(CodeContext context, BigInteger address, PythonTuple args) { + public static object? call_function(CodeContext context, BigInteger address, [NotNone] PythonTuple args) { return call_function(context, new IntPtr((long)address), args); } - public static object call_function(CodeContext context, IntPtr address, PythonTuple args) { + public static object? call_function(CodeContext context, IntPtr address, [NotNone] PythonTuple args) { CFuncPtrType funcType = GetFunctionType(context, FUNCFLAG_STDCALL); _CFuncPtr func = (_CFuncPtr)funcType.CreateInstance(context, address); - return PythonOps.CallWithArgsTuple(func, System.Array.Empty(), args); + return PythonCalls.Call(context, func, args.ToArray()); } +#nullable restore + private static CFuncPtrType GetFunctionType(CodeContext context, int flags) { // Ideally we should cache these... SimpleType resType = new SimpleType( diff --git a/src/core/IronPython.Modules/_thread.cs b/src/core/IronPython.Modules/_thread.cs index 469da057c..a5517a3c5 100644 --- a/src/core/IronPython.Modules/_thread.cs +++ b/src/core/IronPython.Modules/_thread.cs @@ -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. +#nullable enable + using System; using System.Collections.Generic; -using System.Diagnostics; using System.Threading; using IronPython.Runtime; @@ -14,7 +15,6 @@ using Microsoft.Scripting; using Microsoft.Scripting.Runtime; -using Microsoft.Scripting.Utils; using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute; @@ -25,7 +25,7 @@ public static class PythonThread { private static readonly object _stackSizeKey = new object(); private static object _threadCountKey = new object(); - [ThreadStatic] private static List<@lock> _sentinelLocks; + [ThreadStatic] private static List<@lock>? _sentinelLocks; [SpecialName] public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) { @@ -40,21 +40,20 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] public static readonly PythonType LockType = DynamicHelpers.GetPythonTypeFromType(typeof(@lock)); - [Documentation("start_new_thread(function, [args, [kwDict]]) -> thread id\nCreates a new thread running the given function")] - public static object start_new_thread(CodeContext/*!*/ context, object function, object args, object kwDict) { - PythonTuple tupArgs = args as PythonTuple; - if (tupArgs == null) throw PythonOps.TypeError("2nd arg must be a tuple"); + [Documentation("start_new_thread(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")] + public static object start_new_thread(CodeContext/*!*/ context, object? function, object? args, object? kwDict) { + if (args is not PythonTuple tupArgs) throw PythonOps.TypeError("2nd arg must be a tuple"); + if (kwDict is not PythonDictionary dict) throw PythonOps.TypeError("optional 3rd arg must be a dictionary"); - Thread t = CreateThread(context, new ThreadObj(context, function, tupArgs, kwDict).Start); + Thread t = CreateThread(context, new ThreadObj(context, function, tupArgs, dict).Start); t.Start(); return t.ManagedThreadId; } [Documentation("start_new_thread(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")] - public static object start_new_thread(CodeContext/*!*/ context, object function, object args) { - PythonTuple tupArgs = args as PythonTuple; - if (tupArgs == null) throw PythonOps.TypeError("2nd arg must be a tuple"); + public static object start_new_thread(CodeContext/*!*/ context, object? function, object? args) { + if (args is not PythonTuple tupArgs) throw PythonOps.TypeError("2nd arg must be a tuple"); Thread t = CreateThread(context, new ThreadObj(context, function, tupArgs, null).Start); t.IsBackground = true; @@ -109,8 +108,13 @@ public static int stack_size(CodeContext/*!*/ context, int size) { } // deprecated synonyms, wrappers over preferred names... - [Documentation("start_new(function, [args, [kwDict]]) -> thread id\nCreates a new thread running the given function")] - public static object start_new(CodeContext context, object function, object args) { + [Documentation("start_new(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")] + public static object start_new(CodeContext context, object? function, object? args, object? kwDict) { + return start_new_thread(context, function, args, kwDict); + } + + [Documentation("start_new(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")] + public static object start_new(CodeContext context, object? function, object? args) { return start_new_thread(context, function, args); } @@ -138,8 +142,6 @@ public static object _set_sentinel(CodeContext context) { #endregion -#nullable enable - [PythonType, PythonHidden] public sealed class @lock { private AutoResetEvent? blockEvent; @@ -321,8 +323,6 @@ private void CreateBlockEvent() { } } -#nullable restore - #region Internal Implementation details private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start) { @@ -331,12 +331,12 @@ private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start) } private class ThreadObj { - private readonly object _func, _kwargs; + private readonly object? _func; + private readonly PythonDictionary? _kwargs; private readonly PythonTuple _args; private readonly CodeContext _context; - public ThreadObj(CodeContext context, object function, PythonTuple args, object kwargs) { - Debug.Assert(args != null); + public ThreadObj(CodeContext context, object? function, PythonTuple args, PythonDictionary? kwargs) { _func = function; _kwargs = kwargs; _args = args; @@ -349,13 +349,11 @@ public void Start() { _context.LanguageContext.SetModuleState(_threadCountKey, startCount + 1); } try { -#pragma warning disable 618 // TODO: obsolete if (_kwargs != null) { - PythonOps.CallWithArgsTupleAndKeywordDictAndContext(_context, _func, [], [], _args, _kwargs); + PythonCalls.CallWithKeywordArgs(_context, _func, _args.ToArray(), new PythonDictionary(_kwargs)); } else { - PythonOps.CallWithArgsTuple(_func, [], _args); + PythonCalls.Call(_context, _func, _args.ToArray()); } -#pragma warning restore 618 } catch (SystemExitException) { // ignore and quit } catch (Exception e) { @@ -397,17 +395,17 @@ public class _local { #region Custom Attribute Access [SpecialName] - public object GetCustomMember(string name) { + public object GetCustomMember([NotNone] string name) { return _dict.get(name, OperationFailed.Value); } [SpecialName] - public void SetMemberAfter(string name, object value) { + public void SetMemberAfter([NotNone] string name, object? value) { _dict[name] = value; } [SpecialName] - public void DeleteMember(string name) { + public void DeleteMember([NotNone] string name) { _dict.__delitem__(name); } @@ -428,21 +426,21 @@ public PythonDictionary/*!*/ __dict__ { private class ThreadLocalDictionaryStorage : DictionaryStorage { private readonly Microsoft.Scripting.Utils.ThreadLocal _storage = new Microsoft.Scripting.Utils.ThreadLocal(); - public override void Add(ref DictionaryStorage storage, object key, object value) { + public override void Add(ref DictionaryStorage storage, object? key, object? value) { GetStorage().Add(key, value); } - public override bool Contains(object key) { + public override bool Contains(object? key) { return GetStorage().Contains(key); } - public override bool Remove(ref DictionaryStorage storage, object key) { + public override bool Remove(ref DictionaryStorage storage, object? key) { return GetStorage().Remove(ref storage, key); } public override DictionaryStorage AsMutable(ref DictionaryStorage storage) => this; - public override bool TryGetValue(object key, out object value) { + public override bool TryGetValue(object? key, out object? value) { return GetStorage().TryGetValue(key, out value); } @@ -454,7 +452,7 @@ public override void Clear(ref DictionaryStorage storage) { GetStorage().Clear(ref storage); } - public override List>/*!*/ GetItems() { + public override List>/*!*/ GetItems() { return GetStorage().GetItems(); } diff --git a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs index c2e425015..f4dd89fab 100644 --- a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs +++ b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs @@ -776,7 +776,7 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte if (PythonOps.IsInstance(value, type)) { pyEx = value; } else if (value is PythonTuple pt) { - pyEx = PythonOps.CallWithArgsTuple(type, [], pt); + pyEx = PythonCalls.Call(context, type, pt.ToArray()); } else if (value != null) { pyEx = PythonCalls.Call(context, type, value); } else { diff --git a/src/core/IronPython/Runtime/Operations/PythonOps.cs b/src/core/IronPython/Runtime/Operations/PythonOps.cs index 07ef3abed..d4dcb62df 100644 --- a/src/core/IronPython/Runtime/Operations/PythonOps.cs +++ b/src/core/IronPython/Runtime/Operations/PythonOps.cs @@ -1025,7 +1025,8 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence, } } - public static object? CallWithArgsTuple(object func, object?[] args, IEnumerable argsTuple) { + [Obsolete("Use PythonCalls.Call")] + public static object? CallWithArgsTuple(object func, object?[] args, object argsTuple) { if (argsTuple is PythonTuple tp) { object?[] nargs = new object[args.Length + tp.__len__()]; for (int i = 0; i < args.Length; i++) nargs[i] = args[i]; @@ -1035,7 +1036,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence, PythonList allArgs = new PythonList(args.Length + 10); allArgs.AddRange(args); - IEnumerator e = argsTuple.GetEnumerator(); + IEnumerator e = PythonOps.GetEnumerator(DefaultContext.Default, argsTuple); while (e.MoveNext()) allArgs.AddNoLock(e.Current); return PythonCalls.Call(func, allArgs.GetObjectArray());