Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/core/IronPython.Modules/_ctypes/_ctypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>(), 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<object>(), 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(
Expand Down
62 changes: 30 additions & 32 deletions src/core/IronPython.Modules/_thread.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.

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

using IronPython.Runtime;
Expand All @@ -14,7 +15,6 @@

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

using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute;

Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -138,8 +142,6 @@ public static object _set_sentinel(CodeContext context) {

#endregion

#nullable enable

[PythonType, PythonHidden]
public sealed class @lock {
private AutoResetEvent? blockEvent;
Expand Down Expand Up @@ -321,8 +323,6 @@ private void CreateBlockEvent() {
}
}

#nullable restore

#region Internal Implementation details

private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start) {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -428,21 +426,21 @@ public PythonDictionary/*!*/ __dict__ {
private class ThreadLocalDictionaryStorage : DictionaryStorage {
private readonly Microsoft.Scripting.Utils.ThreadLocal<CommonDictionaryStorage> _storage = new Microsoft.Scripting.Utils.ThreadLocal<CommonDictionaryStorage>();

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);
}

Expand All @@ -454,7 +452,7 @@ public override void Clear(ref DictionaryStorage storage) {
GetStorage().Clear(ref storage);
}

public override List<KeyValuePair<object, object>>/*!*/ GetItems() {
public override List<KeyValuePair<object?, object?>>/*!*/ GetItems() {
return GetStorage().GetItems();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/core/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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());
Expand Down