Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed assignment to HasStrictTraits instance modifies the class. #58

Open
mdickinson opened this issue May 17, 2013 · 3 comments
Open

Comments

@mdickinson
Copy link
Member

I encountered the following behaviour while (foolishly) trying to monkeypatch a HasStrictTraits subclass. The fact that the monkeypatching doesn't work isn't really surprising. What is suprising is that the attempt to monkeypatch one instance of the class breaks the class itself.

>>> from traits.api import *
>>> class A(HasStrictTraits):
...     def start(self): print "Starting"
... 
>>> try:
...     A().start = 3.2  # Monkey-patching fails;  too bad.
... except Exception:
...     pass
... 
>>> A().start  # Surprise: the failure above affects new A instances!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'start'

What appears to be happening is that the assignment to A().start creates a new class trait start for A and puts it into the A.__class_traits__ dictionary, before the failure occurs. After the failure, that new class trait still exists.

@mdickinson
Copy link
Member Author

It looks like get_prefix_trait in ctraits.c could be the place to fix this. I don't have time to produce a fix right now; I may come back to this later.

@mdickinson
Copy link
Member Author

Closely related to #30.

@mdickinson
Copy link
Member Author

Another team ran into this issue recently: see #1375.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant