@@ -22,6 +22,11 @@ internal class ModuleObject : ExtensionType
22
22
protected string _namespace ;
23
23
private IntPtr __all__ = IntPtr . Zero ;
24
24
25
+ // Attributes to be set on the module according to PEP302 and 451
26
+ // by the import machinery.
27
+ static readonly HashSet < string > settableAttributes =
28
+ new HashSet < string > { "__spec__" , "__file__" , "__name__" , "__path__" , "__loader__" , "__package__" } ;
29
+
25
30
public ModuleObject ( string name )
26
31
{
27
32
if ( name == string . Empty )
@@ -352,22 +357,15 @@ protected override void Clear()
352
357
[ ForbidPythonThreads ]
353
358
public new static int tp_setattro ( IntPtr ob , IntPtr key , IntPtr val )
354
359
{
355
- var self = ( ModuleObject ) ManagedType . GetManagedObject ( ob ) ;
356
- var dict = self . dict ;
357
-
358
- var current = Runtime . PyDict_GetItem ( dict , key ) ;
359
- if ( current == val )
360
- {
361
- return 0 ;
362
- }
363
- else if ( ManagedType . GetManagedObject ( current ) != null )
360
+ var managedKey = Runtime . GetManagedString ( key ) ;
361
+ if ( ( settableAttributes . Contains ( managedKey ) ) ||
362
+ ( ManagedType . GetManagedObject ( val ) ? . GetType ( ) == typeof ( ModuleObject ) ) )
364
363
{
365
- var message = "Can't override a .NET object" ;
366
- Exceptions . SetError ( Exceptions . AttributeError , message ) ;
367
- return - 1 ;
364
+ var self = ( ModuleObject ) ManagedType . GetManagedObject ( ob ) ;
365
+ return Runtime . PyDict_SetItem ( self . dict , key , val ) ;
368
366
}
369
367
370
- return Runtime . PyDict_SetItem ( dict , key , val ) ;
368
+ return ExtensionType . tp_setattro ( ob , key , val ) ;
371
369
}
372
370
373
371
protected override void OnSave ( InterDomainContext context )
0 commit comments