Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit37f1235

Browse files
authored
Merge pull request#1760 from filmor/misc-fixes
Miscellaneous fixes and cleanups
2 parentsbbfa252 +aa5b54b commit37f1235

File tree

54 files changed

+540
-676
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+540
-676
lines changed

‎src/runtime/AssemblyManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ internal class AssemblyManager
2323
// than it can end up referring to assemblies that are already unloaded (default behavior after unload appDomain -
2424
// unless LoaderOptimization.MultiDomain is used);
2525
// So for multidomain support it is better to have the dict. recreated for each app-domain initialization
26-
privatestaticConcurrentDictionary<string,ConcurrentDictionary<Assembly,string>>namespaces=
27-
newConcurrentDictionary<string,ConcurrentDictionary<Assembly,string>>();
26+
privatestaticreadonlyConcurrentDictionary<string,ConcurrentDictionary<Assembly,string>>namespaces=
27+
new();
2828

2929
#pragma warning disableCS8618// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
3030
// domain-level handlers are initialized in Initialize
@@ -33,7 +33,7 @@ internal class AssemblyManager
3333
#pragma warning restoreCS8618// Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
3434

3535
// updated only under GIL?
36-
privatestaticDictionary<string,int>probed=newDictionary<string,int>(32);
36+
privatestaticreadonlyDictionary<string,int>probed=new(32);
3737

3838
// modified from event handlers below, potentially triggered from different .NET threads
3939
privatestaticreadonlyConcurrentQueue<Assembly>assemblies=new();

‎src/runtime/ClassManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,9 @@ internal static void InitClassBase(Type type, ClassBase impl, ReflectedClrType p
247247
Runtime.PyDict_SetItem(dict,PyIdentifier.__doc__,doc.Borrow());
248248
}
249249

250-
varco=implasClassObject;
251250
// If this is a ClassObject AND it has constructors, generate a __doc__ attribute.
252251
// required that the ClassObject.ctors be changed to internal
253-
if(co!=null)
252+
if(implisClassObjectco)
254253
{
255254
if(co.NumCtors>0&&!co.HasCustomNew())
256255
{

‎src/runtime/Codecs/DecoderGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Python.Runtime.Codecs
1010
/// </summary>
1111
publicsealedclassDecoderGroup:IPyObjectDecoder,IEnumerable<IPyObjectDecoder>,IDisposable
1212
{
13-
readonlyList<IPyObjectDecoder>decoders=newList<IPyObjectDecoder>();
13+
readonlyList<IPyObjectDecoder>decoders=new();
1414

1515
/// <summary>
1616
/// Add specified decoder to the group

‎src/runtime/Codecs/EncoderGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Python.Runtime.Codecs
1010
/// </summary>
1111
publicsealedclassEncoderGroup:IPyObjectEncoder,IEnumerable<IPyObjectEncoder>,IDisposable
1212
{
13-
readonlyList<IPyObjectEncoder>encoders=newList<IPyObjectEncoder>();
13+
readonlyList<IPyObjectEncoder>encoders=new();
1414

1515
/// <summary>
1616
/// Add specified encoder to the group

‎src/runtime/Codecs/PyObjectConversions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace Python.Runtime
1515
/// </summary>
1616
publicstaticclassPyObjectConversions
1717
{
18-
staticreadonlyDecoderGroupdecoders=newDecoderGroup();
19-
staticreadonlyEncoderGroupencoders=newEncoderGroup();
18+
staticreadonlyDecoderGroupdecoders=new();
19+
staticreadonlyEncoderGroupencoders=new();
2020

2121
/// <summary>
2222
/// Registers specified encoder (marshaller)
@@ -62,7 +62,7 @@ public static void RegisterDecoder(IPyObjectDecoder decoder)
6262
}
6363

6464
staticreadonlyConcurrentDictionary<Type,IPyObjectEncoder[]>
65-
clrToPython=newConcurrentDictionary<Type,IPyObjectEncoder[]>();
65+
clrToPython=new();
6666
staticIPyObjectEncoder[]GetEncoders(Typetype)
6767
{
6868
lock(encoders)

‎src/runtime/Converter.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ private Converter()
1818
{
1919
}
2020

21-
privatestaticTypeobjectType;
22-
privatestaticTypestringType;
23-
privatestaticTypesingleType;
24-
privatestaticTypedoubleType;
25-
privatestaticTypeint16Type;
26-
privatestaticTypeint32Type;
27-
privatestaticTypeint64Type;
28-
privatestaticTypeboolType;
29-
privatestaticTypetypeType;
21+
privatestaticreadonlyTypeobjectType;
22+
privatestaticreadonlyTypestringType;
23+
privatestaticreadonlyTypesingleType;
24+
privatestaticreadonlyTypedoubleType;
25+
privatestaticreadonlyTypeint16Type;
26+
privatestaticreadonlyTypeint32Type;
27+
privatestaticreadonlyTypeint64Type;
28+
privatestaticreadonlyTypeboolType;
29+
privatestaticreadonlyTypetypeType;
3030

3131
staticConverter()
3232
{
@@ -151,8 +151,7 @@ internal static NewReference ToPython(object? value, Type type)
151151

152152
// it the type is a python subclass of a managed type then return the
153153
// underlying python object rather than construct a new wrapper object.
154-
varpyderived=valueasIPythonDerivedType;
155-
if(null!=pyderived)
154+
if(valueisIPythonDerivedTypepyderived)
156155
{
157156
if(!IsTransparentProxy(pyderived))
158157
returnClassDerivedObject.ToPython(pyderived);
@@ -161,7 +160,7 @@ internal static NewReference ToPython(object? value, Type type)
161160
// ModuleObjects are created in a way that their wrapping them as
162161
// a CLRObject fails, the ClassObject has no tpHandle. Return the
163162
// pyHandle as is, do not convert.
164-
if(valueisModuleObjectmodobj)
163+
if(valueisModuleObject)
165164
{
166165
thrownewNotImplementedException();
167166
}

‎src/runtime/DelegateManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ namespace Python.Runtime
1515
/// </summary>
1616
internalclassDelegateManager
1717
{
18-
privatereadonlyDictionary<Type,Type>cache=newDictionary<Type,Type>();
18+
privatereadonlyDictionary<Type,Type>cache=new();
1919
privatereadonlyTypebasetype=typeof(Dispatcher);
2020
privatereadonlyTypearrayType=typeof(object[]);
2121
privatereadonlyTypevoidtype=typeof(void);
2222
privatereadonlyTypetypetype=typeof(Type);
2323
privatereadonlyTypepyobjType=typeof(PyObject);
24-
privatereadonlyCodeGeneratorcodeGenerator=newCodeGenerator();
24+
privatereadonlyCodeGeneratorcodeGenerator=new();
2525
privatereadonlyConstructorInfoarrayCtor;
2626
privatereadonlyMethodInfodispatch;
2727

@@ -309,7 +309,7 @@ protected Dispatcher(PyObject target, Type dtype)
309309
{
310310
tpName+=$" of size{Runtime.PyTuple_Size(op)}";
311311
}
312-
StringBuildersb=newStringBuilder();
312+
varsb=newStringBuilder();
313313
if(!isVoid)sb.Append(rtype.FullName);
314314
for(inti=0;i<pi.Length;i++)
315315
{

‎src/runtime/Exceptions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ public static bool SetError(Exception e)
196196
// might get a managed exception raised that is a wrapper for a
197197
// Python exception. In that case we'd rather have the real thing.
198198

199-
varpe=easPythonException;
200-
if(pe!=null)
199+
if(eisPythonExceptionpe)
201200
{
202201
pe.Restore();
203202
returntrue;

‎src/runtime/Finalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ErrorArgs(Exception error)
4141
[DefaultValue(true)]
4242
publicboolEnable{get;set;}=true;
4343

44-
privateConcurrentQueue<PendingFinalization>_objQueue=new();
44+
privatereadonlyConcurrentQueue<PendingFinalization>_objQueue=new();
4545
privatereadonlyConcurrentQueue<PendingFinalization>_derivedQueue=new();
4646
privatereadonlyConcurrentQueue<Py_buffer>_bufferQueue=new();
4747
privateint_throttled;

‎src/runtime/InternString.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public static void Shutdown()
6161

6262
publicstaticstring?GetManagedString(BorrowedReferenceop)
6363
{
64-
strings;
65-
if(TryGetInterned(op,outs))
64+
if(TryGetInterned(op,outstrings))
6665
{
6766
returns;
6867
}

‎src/runtime/Interop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ internal static Type GetPrototype(MethodInfo method)
139139
}
140140

141141

142-
internalstaticDictionary<IntPtr,Delegate>allocatedThunks=newDictionary<IntPtr,Delegate>();
142+
internalstaticDictionary<IntPtr,Delegate>allocatedThunks=new();
143143

144144
internalstaticThunkInfoGetThunk(MethodInfomethod)
145145
{

‎src/runtime/InteropConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Python.Runtime
99
publicsealedclassInteropConfiguration:IDisposable
1010
{
1111
internalreadonlyPythonBaseTypeProviderGrouppythonBaseTypeProviders
12-
=newPythonBaseTypeProviderGroup();
12+
=new();
1313

1414
/// <summary>Enables replacing base types of CLR types as seen from Python</summary>
1515
publicIList<IPythonBaseTypeProvider>PythonBaseTypeProviders=>this.pythonBaseTypeProviders;

‎src/runtime/Loader.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,8 @@ public unsafe static int Initialize(IntPtr data, int size)
2323
PythonDLL=null;
2424
}
2525

26-
vargs=PyGILState_Ensure();
27-
28-
try
29-
{
30-
// Console.WriteLine("Startup thread");
31-
PythonEngine.InitExt();
32-
// Console.WriteLine("Startup finished");
33-
}
34-
finally
35-
{
36-
PyGILState_Release(gs);
37-
}
26+
usingvar_=Py.GIL();
27+
PythonEngine.InitExt();
3828
}
3929
catch(Exceptionexc)
4030
{
@@ -55,15 +45,8 @@ public unsafe static int Shutdown(IntPtr data, int size)
5545

5646
if(command=="full_shutdown")
5747
{
58-
vargs=PyGILState_Ensure();
59-
try
60-
{
61-
PythonEngine.Shutdown();
62-
}
63-
finally
64-
{
65-
PyGILState_Release(gs);
66-
}
48+
usingvar_=Py.GIL();
49+
PythonEngine.Shutdown();
6750
}
6851
}
6952
catch(Exceptionexc)

‎src/runtime/MethodBinder.cs

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -242,52 +242,24 @@ internal static int ArgPrecedence(Type t)
242242

243243
TypeCodetc=Type.GetTypeCode(t);
244244
// TODO: Clean up
245-
switch(tc)
245+
returntcswitch
246246
{
247-
caseTypeCode.Object:
248-
return1;
249-
250-
caseTypeCode.UInt64:
251-
return10;
252-
253-
caseTypeCode.UInt32:
254-
return11;
255-
256-
caseTypeCode.UInt16:
257-
return12;
258-
259-
caseTypeCode.Int64:
260-
return13;
261-
262-
caseTypeCode.Int32:
263-
return14;
264-
265-
caseTypeCode.Int16:
266-
return15;
267-
268-
caseTypeCode.Char:
269-
return16;
270-
271-
caseTypeCode.SByte:
272-
return17;
273-
274-
caseTypeCode.Byte:
275-
return18;
276-
277-
caseTypeCode.Single:
278-
return20;
279-
280-
caseTypeCode.Double:
281-
return21;
282-
283-
caseTypeCode.String:
284-
return30;
285-
286-
caseTypeCode.Boolean:
287-
return40;
288-
}
289-
290-
return2000;
247+
TypeCode.Object=>1,
248+
TypeCode.UInt64=>10,
249+
TypeCode.UInt32=>11,
250+
TypeCode.UInt16=>12,
251+
TypeCode.Int64=>13,
252+
TypeCode.Int32=>14,
253+
TypeCode.Int16=>15,
254+
TypeCode.Char=>16,
255+
TypeCode.SByte=>17,
256+
TypeCode.Byte=>18,
257+
TypeCode.Single=>20,
258+
TypeCode.Double=>21,
259+
TypeCode.String=>30,
260+
TypeCode.Boolean=>40,
261+
_=>2000,
262+
};
291263
}
292264

293265
/// <summary>
@@ -410,18 +382,14 @@ public MismatchedMethod(Exception exception, MethodBase mb)
410382
isGeneric=true;
411383
}
412384
ParameterInfo[]pi=mi.GetParameters();
413-
ArrayList?defaultArgList;
414-
boolparamsArray;
415-
intkwargsMatched;
416-
intdefaultsNeeded;
417385
boolisOperator=OperatorMethod.IsOperatorMethod(mi);
418386
// Binary operator methods will have 2 CLR args but only one Python arg
419387
// (unary operators will have 1 less each), since Python operator methods are bound.
420388
isOperator=isOperator&&pynargs==pi.Length-1;
421389
boolisReverse=isOperator&&OperatorMethod.IsReverse((MethodInfo)mi);// Only cast if isOperator.
422390
if(isReverse&&OperatorMethod.IsComparisonOp((MethodInfo)mi))
423391
continue;// Comparison operators in Python have no reverse mode.
424-
if(!MatchesArgumentCount(pynargs,pi,kwargDict,outparamsArray,outdefaultArgList,outkwargsMatched,outdefaultsNeeded)&&!isOperator)
392+
if(!MatchesArgumentCount(pynargs,pi,kwargDict,outboolparamsArray,outArrayList?defaultArgList,outintkwargsMatched,outintdefaultsNeeded)&&!isOperator)
425393
{
426394
continue;
427395
}
@@ -436,8 +404,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
436404
// We need to take the first CLR argument.
437405
pi=pi.Take(1).ToArray();
438406
}
439-
intouts;
440-
varmargs=TryConvertArguments(pi,paramsArray,args,pynargs,kwargDict,defaultArgList,outs:outouts);
407+
varmargs=TryConvertArguments(pi,paramsArray,args,pynargs,kwargDict,defaultArgList,outs:outintouts);
441408
if(margs==null)
442409
{
443410
varmismatchCause=PythonException.FetchCurrent();
@@ -495,7 +462,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
495462
{
496463
// Best effort for determining method to match on gives multiple possible
497464
// matches and we need at least one default argument - bail from this point
498-
StringBuilderstringBuilder=newStringBuilder("Not enough arguments provided to disambiguate the method. Found:");
465+
varstringBuilder=newStringBuilder("Not enough arguments provided to disambiguate the method. Found:");
499466
foreach(varmatchedMethodinargMatchedMethods)
500467
{
501468
stringBuilder.AppendLine();
@@ -523,18 +490,20 @@ public MismatchedMethod(Exception exception, MethodBase mb)
523490
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
524491
// InvalidCastException: Unable to cast object of type
525492
// 'Python.Runtime.ClassObject' to type 'Python.Runtime.CLRObject'
526-
varco=ManagedType.GetManagedObject(inst)asCLRObject;
527493

528494
// Sanity check: this ensures a graceful exit if someone does
529495
// something intentionally wrong like call a non-static method
530496
// on the class rather than on an instance of the class.
531497
// XXX maybe better to do this before all the other rigmarole.
532-
if(co==null)
498+
if(ManagedType.GetManagedObject(inst)isCLRObjectco)
499+
{
500+
target=co.inst;
501+
}
502+
else
533503
{
534504
Exceptions.SetError(Exceptions.TypeError,"Invoked a non-static method with an invalid instance");
535505
returnnull;
536506
}
537-
target=co.inst;
538507
}
539508

540509
returnnewBinding(mi,target,margs,outs);
@@ -623,7 +592,7 @@ static BorrowedReference HandleParamsArray(BorrowedReference args, int arrayStar
623592
for(intparamIndex=0;paramIndex<pi.Length;paramIndex++)
624593
{
625594
varparameter=pi[paramIndex];
626-
boolhasNamedParam=parameter.Name!=null?kwargDict.ContainsKey(parameter.Name):false;
595+
boolhasNamedParam=parameter.Name!=null&&kwargDict.ContainsKey(parameter.Name);
627596

628597
if(paramIndex>=pyArgCount&&!(hasNamedParam||(paramsArray&&paramIndex==arrayStart)))
629598
{
@@ -658,8 +627,7 @@ static BorrowedReference HandleParamsArray(BorrowedReference args, int arrayStar
658627
}
659628
}
660629

661-
boolisOut;
662-
if(!TryConvertArgument(op,parameter.ParameterType,outmargs[paramIndex],outisOut))
630+
if(!TryConvertArgument(op,parameter.ParameterType,outmargs[paramIndex],outboolisOut))
663631
{
664632
tempObject.Dispose();
665633
returnnull;
@@ -789,7 +757,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
789757
{
790758
defaultArgList=null;
791759
varmatch=false;
792-
paramsArray=parameters.Length>0?Attribute.IsDefined(parameters[parameters.Length-1],typeof(ParamArrayAttribute)):false;
760+
paramsArray=parameters.Length>0&&Attribute.IsDefined(parameters[parameters.Length-1],typeof(ParamArrayAttribute));
793761
kwargsMatched=0;
794762
defaultsNeeded=0;
795763
if(positionalArgumentCount==parameters.Length&&kwargDict.Count==0)

‎src/runtime/Native/BorrowedReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public IntPtr DangerousGetAddress()
1919
/// <summary>Gets a raw pointer to the Python object</summary>
2020
publicIntPtrDangerousGetAddressOrNull()=>this.pointer;
2121

22-
publicstaticBorrowedReferenceNull=>newBorrowedReference();
22+
publicstaticBorrowedReferenceNull=>new();
2323

2424
/// <summary>
2525
/// Creates new instance of <see cref="BorrowedReference"/> from raw pointer. Unsafe.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp