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