8000 Further refinements to setattr logic on ModuleObjects · pythonnet/pythonnet@d821c0f · GitHub
[go: up one dir, main page]

Skip to content

Commit d821c0f

Browse files
committed
Further refinements to setattr logic on ModuleObjects
1 parent afffc18 commit d821c0f

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/runtime/moduleobject.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ internal class ModuleObject : ExtensionType
2222
protected string _namespace;
2323
private IntPtr __all__ = IntPtr.Zero;
2424

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+
2530
public ModuleObject(string name)
2631
{
2732
if (name == string.Empty)
@@ -352,22 +357,15 @@ protected override void Clear()
352357
[ForbidPythonThreads]
353358
public new static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
354359
{
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)) )
364363
{
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);
368366
}
369367

370-
return Runtime.PyDict_SetItem(dict, key, val);
368+
return ExtensionType.tp_setattro(ob, key, val);
371369
}
372370

373371
protected override void OnSave(InterDomainContext context)

0 commit comments

Comments
 (0)
0