0

I have a class "MyClass" with a class attribute and its methods.

class MyClass:
    def __init__(self):
        self._my_attr = None

    @property
    def my_attr(self):
        return self._my_attr

    @my_attr.setter
    def my_attr(self, value):
        self._my_attr = value

    def set_my_attr(self, value):
        self._my_attr = value

When I try to assign a value in Zope (in a Python Script, which uses Restricted Python), I get a TypeError attribute-less object (assign or del) when I try to assign the value with =.

It also says in the traceback: AttributeError: 'MyClass' object has no attribute '__guarded_setattr__'

obj = MyClass()
obj.my_attr = 42  # error in Restricted Python
obj.set_my_attr(value=42)  # does work in Restricted Python

What does the error mean? Is there a way to allow setter methods for direct assignment in Restricted Python or do I have to implement a normal method to set the value?

Georg Pfolz
  • 1,416
  • 2
  • 12
  • 12
  • Side note: why do you have a `set_my_attr()` in addition to a proper setter? Just for the example? – Andras Deak -- Слава Україні Jul 16 '23 at 09:36
  • I found the documentation a bit hard to follow, but what kind of guard are you using? https://restrictedpython.readthedocs.io/en/latest/usage/policy.html#guards The "attribute-less object" message seems expected for certain guards: https://restrictedpython.readthedocs.io/en/latest/usage/policy.html#guards-1 – slothrop Jul 16 '23 at 09:38
  • 1
    @AndrasDeak--СлаваУкраїні the `set_my_attr()` method is there as an alternative that's working in Restricted Python, that's its sole purpose. – Georg Pfolz Jul 16 '23 at 10:01
  • @slothrop As I use Zope which in turn uses Restricted Python for its "Python Scripts" in the ZODB, I have not configured Restricted Python myself, so I don't know which guard is used. I also would be curious if the Restricted Python settings can be changed for a Zope instance. – Georg Pfolz Jul 16 '23 at 10:03

0 Answers0