Edgewall Software

Opened 9 years ago

Closed 9 years ago

#11867 closed enhancement (fixed)

Support @cached decorator on properties of the environment class — at Version 6

Reported by: Ryan J Ollos Owned by: Ryan J Ollos
Priority: normal Milestone: 1.0.3
Component: general Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:
API Changes:

The Environment class has an env property, which allows the cached decorator to be used on other properties of the class.

Internal Changes:

Description

The proposed change should allow @cached to be used on properties of the Environment class.

Change History (6)

comment:1 by Ryan J Ollos, 9 years ago

I tried:

from trac.env import Environment
env = instance if isinstance(instance, Environment) else instance.env

The local import is required to avoid a circular import since trac.env imports CacheManager.

Or we could test whether the object is a ComponentManager instance: log:rjollos.git:t11867.

comment:2 by Peter Suter, 9 years ago

Testing for ComponentManager sounds good.

(Another possibility might be to add an env property returning self to Environment, or would that be too confusing?)

Last edited 9 years ago by Peter Suter (previous) (diff)

in reply to:  2 comment:3 by Ryan J Ollos, 9 years ago

Replying to psuter:

(Another possibility might be to add an env property returning self to Environment, or would that be too confusing?)

While working on #7339 and encountering this issue, as a workaround I initially added self.env = self at tags/trac-1.0.2/trac/env.py@:276#L265. Is that what you had in mind?

comment:4 by Jun Omae, 9 years ago

Sounds a good idea. But I think that adding self.env = self would be a circular reference. Instead, it would be good to add env method with property decorator like this.

    @property
    def env(self):
        return self

comment:5 by Ryan J Ollos, 9 years ago

Do you have a particular use-case in mind where self.env = self causes a problem that is not seen with the property definition? There are similar references in tags/trac-1.0.2/trac/config.py@:573#L555. That's a class attribute that contains a dictionary, but otherwise looks like a similar pattern.

At least for this simple case, the property x and instance attribute y appear to behave the same:

>>> class A(object):
...   @property
...   def x(self):
...     return self
...   def __init__(self):
...     self.y = self
...     self.z = 1
... 
>>> a = A()
>>> a
<__main__.A object at 0x7f5b22038450>
>>> a.y
<__main__.A object at 0x7f5b22038450>
>>> a.y.y
<__main__.A object at 0x7f5b22038450>
>>> a.y.y.z
1
>>> a.x
<__main__.A object at 0x7f5b22038450>
>>> a.x.x
<__main__.A object at 0x7f5b22038450>
>>> a.x.x.z
1
Last edited 9 years ago by Ryan J Ollos (previous) (diff)

comment:6 by Ryan J Ollos, 9 years ago

API Changes: modified (diff)
Resolution: fixed
Status: newclosed

Committed to 1.0-stable in [13476], merged to trunk in [13477].

Note: See TracTickets for help on using tickets.