Setting get_localizer as a property of request causes infinite loop. #508

Closed
thusly opened this Issue Mar 22, 2012 · 3 comments

Comments

Projects
None yet
2 participants
@thusly

thusly commented Mar 22, 2012

With the introduction of set_property in 1.3, I realized that I could get away with no longer fully initializing request.localizer for every single request. As the get_localizer(request) function's signature already takes a request object as its argument, I thought it would be a simple matter of:

@subscriber(NewRequest)
def request_i18n(event):
    request = event.request
    request.set_property(get_localizer, 'localizer', reify=True)

Unfortunately, this causes an infinite recursive loop, because get_localizer() begins with:

def get_localizer(request):
    """ Retrieve a :class:`pyramid.i18n.Localizer` object
    corresponding to the current request's locale name. """
    localizer =  getattr(request, 'localizer', None)

    ...

Which of course results in the function calling itself over and over. With the codebase as it is, the current options seem to be:

  • Implement your own version of the function without the attribute lookup.
  • Use a different name for the localizer property.
  • Continue as you were (in my case, initializing on each request).

(Or wrap in a function that passes a dummy request, and then return the localizer from that, but that's awfully hackish.)

In tracking this down I noticed that the TODO file has this listed in the nice-to-haves:

- Make "localizer" a property of request (instead of requiring
  "get_localizer(request)"?

Which I absolutely agree would be nice, as it looks like it is not currently possible to do this cleanly on the user side of the framework, with the most seemingly reasonable path towards it resulting in an infinite loop.

@ghost ghost assigned witsch Aug 17, 2013

witsch added a commit to witsch/pyramid that referenced this issue Aug 17, 2013

witsch added a commit to witsch/pyramid that referenced this issue Aug 17, 2013

@witsch

This comment has been minimized.

Show comment
Hide comment
@witsch

witsch Aug 17, 2013

Member

with #1098 you no longer need to use set_property (in a subscriber or such). instead the request now contains a (reified) localizer property in the first place...

Member

witsch commented Aug 17, 2013

with #1098 you no longer need to use set_property (in a subscriber or such). instead the request now contains a (reified) localizer property in the first place...

@witsch

This comment has been minimized.

Show comment
Hide comment
@witsch

witsch Aug 17, 2013

Member

this should have been fixed by #1098 — @thusly, could you verify (and close the issue), please?

Member

witsch commented Aug 17, 2013

this should have been fixed by #1098 — @thusly, could you verify (and close the issue), please?

@witsch

This comment has been minimized.

Show comment
Hide comment
@witsch

witsch Aug 17, 2013

Member

ok, the policy is to close the ticket — you might reopen, of course, should the solution not work for you... :)

Member

witsch commented Aug 17, 2013

ok, the policy is to close the ticket — you might reopen, of course, should the solution not work for you... :)

@witsch witsch closed this Aug 17, 2013

@pyup-bot pyup-bot referenced this issue in nicfit/MishMash Feb 26, 2017

Merged

Pin pyramid to latest version 1.8.2 #35

@pyup-bot pyup-bot referenced this issue in nicfit/MishMash Apr 30, 2017

Merged

Pin pyramid to latest version 1.8.3 #94

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