Force exception on attribute write access only one object

  • Thread starter Thomas Guettler
  • Start date
T

Thomas Guettler

Hi,

for debugging I want to raise an exception if an attribute is
changed on an object. Since it is only for debugging I don't want
to change the integer attribute to a property.

This should raise an exception:

myobj.foo=1

Background:
Somewhere this value gets changed. But I don't now where.
 
C

Chris Rebert

Hi,

for debugging I want to raise an exception if an attribute is
changed on an object. Since it is only for debugging I don't want
to change the integer attribute to a property.

This should raise an exception:

myobj.foo=1

Background:
Somewhere this value gets changed. But I don't now where.

Completely untested:

class Protector(object):
def __init__(self, delegate, *forbidden):
self.__delegate = delegate
self.__forbidden = set(forbidden)
def __getattr__(self, name):
return getattr(self.__delegate, name)
def __setattr__(self, name, value):
if name in self.__forbidden:
raise TypeError("attempt to assign to forbidden attribute
'%s'" % name)
setattr(self.__delegate, name, value)

x = Foo()
x = Protector(x, "bar")
x.bar = 6 #should raise TypeError

Cheers,
Chris
 
P

Peter Otten

Thomas said:
for debugging I want to raise an exception if an attribute is
changed on an object. Since it is only for debugging I don't want
to change the integer attribute to a property.

Why?
This should raise an exception:

myobj.foo=1

Background:
Somewhere this value gets changed. But I don't now where.

If you change your mind:

class A(object):
def __init__(self):
self.foo = 42

a = A()
b = A()

class B(A):
@property
def foo(self):
return self.__dict__["foo"]

b.__class__ = B

a.foo = "whatever"
print b.foo
b.foo = "whatever"

Peter
 
T

Thomas Guettler

Hi Peter and others,

your idea was good, but it does not work with Django ORM Models:

Traceback (most recent call last):
File "/localhome/modw/django/core/handlers/base.py", line 87, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/localhome/modw/foo/views/filter.py", line 473, in add
return edit(request, 'add')
File "/localhome/modw/foo/views/filter.py", line 493, in edit
filter=form.save()
File "/localhome/modw/foo/views/filter.py", line 457, in save
action=form.save()
File "/localhome/modw/django/forms/models.py", line 315, in save
if self.instance.pk is None:
File "/localhome/modw/django/db/models/base.py", line 292, in _get_pk_val
return getattr(self, meta.pk.attname)
AttributeError: 'MyAction' object has no attribute 'filter_action_ptr_id'


Peter said:
Thomas said:
for debugging I want to raise an exception if an attribute is
changed on an object. Since it is only for debugging I don't want
to change the integer attribute to a property.
Why?

This should raise an exception:

myobj.foo=1

Background:
Somewhere this value gets changed. But I don't now where.

If you change your mind:

class A(object):
def __init__(self):
self.foo = 42

a = A()
b = A()

class B(A):
@property
def foo(self):
return self.__dict__["foo"]

b.__class__ = B

a.foo = "whatever"
print b.foo
b.foo = "whatever"

Peter
 
P

Peter Otten

Thomas said:
Peter said:
Thomas said:
for debugging I want to raise an exception if an attribute is
changed on an object. Since it is only for debugging I don't want
to change the integer attribute to a property.
class A(object):
def __init__(self):
self.foo = 42

a = A()
b = A()

class B(A):
@property
def foo(self):
return self.__dict__["foo"]

b.__class__ = B

a.foo = "whatever"
print b.foo
b.foo = "whatever"
your idea was good, but it does not work with Django ORM Models:

Traceback (most recent call last):
File "/localhome/modw/django/core/handlers/base.py", line 87, in
get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/localhome/modw/foo/views/filter.py", line 473, in add
return edit(request, 'add')
File "/localhome/modw/foo/views/filter.py", line 493, in edit
filter=form.save()
File "/localhome/modw/foo/views/filter.py", line 457, in save
action=form.save()
File "/localhome/modw/django/forms/models.py", line 315, in save
if self.instance.pk is None:
File "/localhome/modw/django/db/models/base.py", line 292, in
_get_pk_val
return getattr(self, meta.pk.attname)
AttributeError: 'MyAction' object has no attribute 'filter_action_ptr_id'

I can't tell what's happening from the traceback alone.
Is "filter_action_ptr_id"" your actual "foo" and "MyAction" your "B"?
Maybe the relevant setup code would help...

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,299
Messages
2,571,546
Members
48,300
Latest member
Markwen49

Latest Threads

Top