@@ -1056,6 +1056,7 @@ public PyList Dir()
1056
1056
/// </remarks>
1057
1057
public override string ? ToString ( )
1058
1058
{
1059
+ using var _ = Py . GIL ( ) ;
1059
1060
using var strval = Runtime . PyObject_Str ( obj ) ;
1060
1061
return Runtime . GetManagedString ( strval . BorrowOrThrow ( ) ) ;
1061
1062
}
@@ -1072,7 +1073,11 @@ public PyList Dir()
1072
1073
/// Return true if this object is equal to the given object. This
1073
1074
/// method is based on Python equality semantics.
1074
1075
/// </remarks>
1075
- public override bool Equals ( object o ) => Equals ( o as PyObject ) ;
1076
+ public override bool Equals ( object o )
1077
+ {
1078
+ using var _ = Py . GIL ( ) ;
1079
+ return Equals ( o as PyObject ) ;
1080
+ }
1076
1081
1077
1082
public virtual bool Equals ( PyObject ? other )
1078
1083
{
@@ -1101,6 +1106,7 @@ public virtual bool Equals(PyObject? other)
1101
1106
/// </remarks>
1102
1107
public override int GetHashCode ( )
1103
1108
{
1109
+ using var _ = Py . GIL ( ) ;
1104
1110
nint pyHash = Runtime . PyObject_Hash ( obj ) ;
1105
1111
if ( pyHash == - 1 && Exceptions . ErrorOccurred ( ) )
1106
1112
{
@@ -1135,12 +1141,14 @@ public long Refcount
1135
1141
1136
1142
public override bool TryGetMember ( GetMemberBinder binder , out object ? result )
1137
1143
{
1144
+ using var _ = Py . GIL ( ) ;
1138
1145
result = CheckNone ( this . GetAttr ( binder . Name ) ) ;
1139
1146
return true ;
1140
1147
}
1141
1148
1142
1149
public override bool TrySetMember ( SetMemberBinder binder , object ? value )
1143
1150
{
1151
+ using var _ = Py . GIL ( ) ;
1144
1152
using var newVal = Converter . ToPythonDetectType ( value ) ;
1145
1153
int r = Runtime . PyObject_SetAttrString ( obj , binder . Name , newVal . Borrow ( ) ) ;
1146
1154
if ( r < 0 )
@@ -1234,6 +1242,7 @@ private static NewReference GetPythonObject(object? target)
1234
1242
1235
1243
public override bool TryInvokeMember ( InvokeMemberBinder binder , object ? [ ] args , out object ? result )
1236
1244
{
1245
+ using var _ = Py . GIL ( ) ;
1237
1246
if ( this . HasAttr ( binder . Name ) && this . GetAttr ( binder . Name ) . IsCallable ( ) )
1238
1247
{
1239
1248
PyTuple ? pyargs = null ;
@@ -1258,6 +1267,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object?[] args,
1258
1267
1259
1268
public override bool TryInvoke ( InvokeBinder binder , object ? [ ] args , out object ? result )
1260
1269
{
1270
+ using var _ = Py . GIL ( ) ;
1261
1271
if ( this . IsCallable ( ) )
1262
1272
{
1263
1273
PyTuple ? pyargs = null ;
@@ -1282,6 +1292,7 @@ public override bool TryInvoke(InvokeBinder binder, object?[] args, out object?
1282
1292
1283
1293
public override bool TryConvert ( ConvertBinder binder , out object ? result )
1284
1294
{
1295
+ using var _ = Py . GIL ( ) ;
1285
1296
// always try implicit conversion first
1286
1297
if ( Converter . ToManaged ( this . obj , binder . Type , out result , false ) )
1287
1298
{
@@ -1307,6 +1318,7 @@ public override bool TryConvert(ConvertBinder binder, out object? result)
1307
1318
1308
1319
public override bool TryBinaryOperation ( BinaryOperationBinder binder , object arg , out object ? result )
1309
1320
{
1321
+ using var _ = Py . GIL ( ) ;
1310
1322
NewReference res ;
1311
1323
if ( ! ( arg is PyObject ) )
1312
1324
{
@@ -1419,6 +1431,7 @@ public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg
1419
1431
1420
1432
public override bool TryUnaryOperation ( UnaryOperationBinder binder , out object ? result )
1421
1433
{
1434
+ using var _ = Py . GIL ( ) ;
1422
1435
int r ;
1423
1436
NewReference res ;
1424
1437
switch ( binder . Operation )
@@ -1463,10 +1476,8 @@ public override bool TryUnaryOperation(UnaryOperationBinder binder, out object?
1463
1476
/// <returns>A sequence that contains dynamic member names.</returns>
1464
1477
public override IEnumerable < string > GetDynamicMemberNames ( )
1465
1478
{
1466
- foreach ( PyObject pyObj in Dir ( ) )
1467
- {
1468
- yield return pyObj . ToString ( ) ! ;
1469
- }
1479
+ using var _ = Py . GIL ( ) ;
1480
+ return Dir ( ) . Select ( pyObj => pyObj . ToString ( ) ! ) . ToArray ( ) ;
1470
1481
}
1471
1482
1472
1483
void ISerializable . GetObjectData ( SerializationInfo info , StreamingContext context )
0 commit comments