Skip to content

Commit 4758db7

Browse files
authored
Obsolete PythonOps.CallWithArgsTuple (#1950)
* Obsolete PythonOps.CallWithArgsTuple * Revert CallWithArgsTuple signature
1 parent 604d5f1 commit 4758db7

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

src/core/IronPython.Modules/_ctypes/_ctypes.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,41 +361,45 @@ public static object byref(CData instance, int offset = 0) {
361361
return new NativeArgument(instance, "P");
362362
}
363363

364-
public static object call_cdeclfunction(CodeContext context, int address, PythonTuple args) {
364+
#nullable enable
365+
366+
public static object? call_cdeclfunction(CodeContext context, int address, [NotNone] PythonTuple args) {
365367
return call_cdeclfunction(context, new IntPtr(address), args);
366368
}
367369

368-
public static object call_cdeclfunction(CodeContext context, BigInteger address, PythonTuple args) {
370+
public static object? call_cdeclfunction(CodeContext context, BigInteger address, [NotNone] PythonTuple args) {
369371
return call_cdeclfunction(context, new IntPtr((long)address), args);
370372
}
371373

372-
public static object call_cdeclfunction(CodeContext context, IntPtr address, PythonTuple args) {
374+
public static object? call_cdeclfunction(CodeContext context, IntPtr address, [NotNone] PythonTuple args) {
373375
CFuncPtrType funcType = GetFunctionType(context, FUNCFLAG_CDECL);
374376

375377
_CFuncPtr func = (_CFuncPtr)funcType.CreateInstance(context, address);
376378

377-
return PythonOps.CallWithArgsTuple(func, System.Array.Empty<object>(), args);
379+
return PythonCalls.Call(context, func, args.ToArray());
378380
}
379381

380382
public static void call_commethod() {
381383
}
382384

383-
public static object call_function(CodeContext context, int address, PythonTuple args) {
385+
public static object? call_function(CodeContext context, int address, [NotNone] PythonTuple args) {
384386
return call_function(context, new IntPtr(address), args);
385387
}
386388

387-
public static object call_function(CodeContext context, BigInteger address, PythonTuple args) {
389+
public static object? call_function(CodeContext context, BigInteger address, [NotNone] PythonTuple args) {
388390
return call_function(context, new IntPtr((long)address), args);
389391
}
390392

391-
public static object call_function(CodeContext context, IntPtr address, PythonTuple args) {
393+
public static object? call_function(CodeContext context, IntPtr address, [NotNone] PythonTuple args) {
392394
CFuncPtrType funcType = GetFunctionType(context, FUNCFLAG_STDCALL);
393395

394396
_CFuncPtr func = (_CFuncPtr)funcType.CreateInstance(context, address);
395397

396-
return PythonOps.CallWithArgsTuple(func, System.Array.Empty<object>(), args);
398+
return PythonCalls.Call(context, func, args.ToArray());
397399
}
398400

401+
#nullable restore
402+
399403
private static CFuncPtrType GetFunctionType(CodeContext context, int flags) {
400404
// Ideally we should cache these...
401405
SimpleType resType = new SimpleType(

src/core/IronPython.Modules/_thread.cs

Lines changed: 30 additions & 32 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+
#nullable enable
6+
57
using System;
68
using System.Collections.Generic;
7-
using System.Diagnostics;
89
using System.Threading;
910

1011
using IronPython.Runtime;
@@ -14,7 +15,6 @@
1415

1516
using Microsoft.Scripting;
1617
using Microsoft.Scripting.Runtime;
17-
using Microsoft.Scripting.Utils;
1818

1919
using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute;
2020

@@ -25,7 +25,7 @@ public static class PythonThread {
2525

2626
private static readonly object _stackSizeKey = new object();
2727
private static object _threadCountKey = new object();
28-
[ThreadStatic] private static List<@lock> _sentinelLocks;
28+
[ThreadStatic] private static List<@lock>? _sentinelLocks;
2929

3030
[SpecialName]
3131
public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
@@ -40,21 +40,20 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction
4040
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
4141
public static readonly PythonType LockType = DynamicHelpers.GetPythonTypeFromType(typeof(@lock));
4242

43-
[Documentation("start_new_thread(function, [args, [kwDict]]) -> thread id\nCreates a new thread running the given function")]
44-
public static object start_new_thread(CodeContext/*!*/ context, object function, object args, object kwDict) {
45-
PythonTuple tupArgs = args as PythonTuple;
46-
if (tupArgs == null) throw PythonOps.TypeError("2nd arg must be a tuple");
43+
[Documentation("start_new_thread(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")]
44+
public static object start_new_thread(CodeContext/*!*/ context, object? function, object? args, object? kwDict) {
45+
if (args is not PythonTuple tupArgs) throw PythonOps.TypeError("2nd arg must be a tuple");
46+
if (kwDict is not PythonDictionary dict) throw PythonOps.TypeError("optional 3rd arg must be a dictionary");
4747

48-
Thread t = CreateThread(context, new ThreadObj(context, function, tupArgs, kwDict).Start);
48+
Thread t = CreateThread(context, new ThreadObj(context, function, tupArgs, dict).Start);
4949
t.Start();
5050

5151
return t.ManagedThreadId;
5252
}
5353

5454
[Documentation("start_new_thread(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")]
55-
public static object start_new_thread(CodeContext/*!*/ context, object function, object args) {
56-
PythonTuple tupArgs = args as PythonTuple;
57-
if (tupArgs == null) throw PythonOps.TypeError("2nd arg must be a tuple");
55+
public static object start_new_thread(CodeContext/*!*/ context, object? function, object? args) {
56+
if (args is not PythonTuple tupArgs) throw PythonOps.TypeError("2nd arg must be a tuple");
5857

5958
Thread t = CreateThread(context, new ThreadObj(context, function, tupArgs, null).Start);
6059
t.IsBackground = true;
@@ -109,8 +108,13 @@ public static int stack_size(CodeContext/*!*/ context, int size) {
109108
}
110109

111110
// deprecated synonyms, wrappers over preferred names...
112-
[Documentation("start_new(function, [args, [kwDict]]) -> thread id\nCreates a new thread running the given function")]
113-
public static object start_new(CodeContext context, object function, object args) {
111+
[Documentation("start_new(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")]
112+
public static object start_new(CodeContext context, object? function, object? args, object? kwDict) {
113+
return start_new_thread(context, function, args, kwDict);
114+
}
115+
116+
[Documentation("start_new(function, args, [kwDict]) -> thread id\nCreates a new thread running the given function")]
117+
public static object start_new(CodeContext context, object? function, object? args) {
114118
return start_new_thread(context, function, args);
115119
}
116120

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

139143
#endregion
140144

141-
#nullable enable
142-
143145
[PythonType, PythonHidden]
144146
public sealed class @lock {
145147
private AutoResetEvent? blockEvent;
@@ -321,8 +323,6 @@ private void CreateBlockEvent() {
321323
}
322324
}
323325

324-
#nullable restore
325-
326326
#region Internal Implementation details
327327

328328
private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start) {
@@ -331,12 +331,12 @@ private static Thread CreateThread(CodeContext/*!*/ context, ThreadStart start)
331331
}
332332

333333
private class ThreadObj {
334-
private readonly object _func, _kwargs;
334+
private readonly object? _func;
335+
private readonly PythonDictionary? _kwargs;
335336
private readonly PythonTuple _args;
336337
private readonly CodeContext _context;
337338

338-
public ThreadObj(CodeContext context, object function, PythonTuple args, object kwargs) {
339-
Debug.Assert(args != null);
339+
public ThreadObj(CodeContext context, object? function, PythonTuple args, PythonDictionary? kwargs) {
340340
_func = function;
341341
_kwargs = kwargs;
342342
_args = args;
@@ -349,13 +349,11 @@ public void Start() {
349349
_context.LanguageContext.SetModuleState(_threadCountKey, startCount + 1);
350350
}
351351
try {
352-
#pragma warning disable 618 // TODO: obsolete
353352
if (_kwargs != null) {
354-
PythonOps.CallWithArgsTupleAndKeywordDictAndContext(_context, _func, [], [], _args, _kwargs);
353+
PythonCalls.CallWithKeywordArgs(_context, _func, _args.ToArray(), new PythonDictionary(_kwargs));
355354
} else {
356-
PythonOps.CallWithArgsTuple(_func, [], _args);
355+
PythonCalls.Call(_context, _func, _args.ToArray());
357356
}
358-
#pragma warning restore 618
359357
} catch (SystemExitException) {
360358
// ignore and quit
361359
} catch (Exception e) {
@@ -397,17 +395,17 @@ public class _local {
397395
#region Custom Attribute Access
398396

399397
[SpecialName]
400-
public object GetCustomMember(string name) {
398+
public object GetCustomMember([NotNone] string name) {
401399
return _dict.get(name, OperationFailed.Value);
402400
}
403401

404402
[SpecialName]
405-
public void SetMemberAfter(string name, object value) {
403+
public void SetMemberAfter([NotNone] string name, object? value) {
406404
_dict[name] = value;
407405
}
408406

409407
[SpecialName]
410-
public void DeleteMember(string name) {
408+
public void DeleteMember([NotNone] string name) {
411409
_dict.__delitem__(name);
412410
}
413411

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

431-
public override void Add(ref DictionaryStorage storage, object key, object value) {
429+
public override void Add(ref DictionaryStorage storage, object? key, object? value) {
432430
GetStorage().Add(key, value);
433431
}
434432

435-
public override bool Contains(object key) {
433+
public override bool Contains(object? key) {
436434
return GetStorage().Contains(key);
437435
}
438436

439-
public override bool Remove(ref DictionaryStorage storage, object key) {
437+
public override bool Remove(ref DictionaryStorage storage, object? key) {
440438
return GetStorage().Remove(ref storage, key);
441439
}
442440

443441
public override DictionaryStorage AsMutable(ref DictionaryStorage storage) => this;
444442

445-
public override bool TryGetValue(object key, out object value) {
443+
public override bool TryGetValue(object? key, out object? value) {
446444
return GetStorage().TryGetValue(key, out value);
447445
}
448446

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

457-
public override List<KeyValuePair<object, object>>/*!*/ GetItems() {
455+
public override List<KeyValuePair<object?, object?>>/*!*/ GetItems() {
458456
return GetStorage().GetItems();
459457
}
460458

src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte
776776
if (PythonOps.IsInstance(value, type)) {
777777
pyEx = value;
778778
} else if (value is PythonTuple pt) {
779-
pyEx = PythonOps.CallWithArgsTuple(type, [], pt);
779+
pyEx = PythonCalls.Call(context, type, pt.ToArray());
780780
} else if (value != null) {
781781
pyEx = PythonCalls.Call(context, type, value);
782782
} else {

src/core/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,8 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence,
10251025
}
10261026
}
10271027

1028-
public static object? CallWithArgsTuple(object func, object?[] args, IEnumerable argsTuple) {
1028+
[Obsolete("Use PythonCalls.Call")]
1029+
public static object? CallWithArgsTuple(object func, object?[] args, object argsTuple) {
10291030
if (argsTuple is PythonTuple tp) {
10301031
object?[] nargs = new object[args.Length + tp.__len__()];
10311032
for (int i = 0; i < args.Length; i++) nargs[i] = args[i];
@@ -1035,7 +1036,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence,
10351036

10361037
PythonList allArgs = new PythonList(args.Length + 10);
10371038
allArgs.AddRange(args);
1038-
IEnumerator e = argsTuple.GetEnumerator();
1039+
IEnumerator e = PythonOps.GetEnumerator(DefaultContext.Default, argsTuple);
10391040
while (e.MoveNext()) allArgs.AddNoLock(e.Current);
10401041

10411042
return PythonCalls.Call(func, allArgs.GetObjectArray());

0 commit comments

Comments
 (0)