Creating a request copy?

29 views
Skip to first unread message

Thierry Florac

unread,
Jan 15, 2019, 5:09:18 AM1/15/19
to pylons-...@googlegroups.com
Hi,
Simple question: I have a use case where I need to create an exact copy (a clone) of a request, to update it's properties and make "simulations" (actually by providing custom marker interfaces) without modifying the original request.
Is there a good way to do this?
Actually I just tried to do a "request.copy()", but then I get errors like "AttributeError: 'Request' object has no attribute 'root'" when trying to get access to "root" attribute...

Thanks for any help,
Thierry

Mike Orr

unread,
Jan 15, 2019, 11:23:21 AM1/15/19
to pylons-...@googlegroups.com
I don't have a complete answer to this, but the Request docstring says
that 'root', 'registry', and a few other attributes are added by the
router. (Commit 7bd9da, lines 160-166). The Router source assigns it
in line 122. ('attrs["root"])' is "request.__dict__["root"]').

https://github.com/Pylons/pyramid/blob/master/src/pyramid/request.py
https://github.com/Pylons/pyramid/blob/master/src/pyramid/router.py

I'd think 'copy' would still find it but there may be another
complication in the request/router/interface machinery. If you only
need the instance attributes, you could do 'myobject.__dict__ =
request.__dict__.copy()'. You might still get the same exception with
that.
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWAWwAbA6MAWHP_2C-3PxfKiXktt1mecJHp5aokeZVOwdA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Mike Orr <slugg...@gmail.com>

Michael Merickel

unread,
Jan 15, 2019, 1:17:56 PM1/15/19
to Pylons
There is no supported way to deep copy a general request object. Instead I would suggest copying the headers/body directly and replaying the request through the router as part of your simulations.

Bobby

unread,
Jan 15, 2019, 3:22:45 PM1/15/19
to pylons-discuss
The following works for me whenever celery needs a dup request:

import plaster
from pyramid.request import Request, apply_request_extensions

def make_request():
    loader = plaster.get_loader('development.ini', protocols=['wsgi'])
    app = loader.get_wsgi_app()
    r = Request.blank('/localhost:6543')
    r.registry = app.registry
    apply_request_extensions(r)
    return r

Thierry Florac

unread,
Jan 16, 2019, 4:04:45 AM1/16/19
to pylons-...@googlegroups.com
Hi,
My use case implies that I don't want to create a custom new request but a clone of the initial request!
I investigated a little more and found that "request.copy()" duplicates most of the initial query attributes, including properties added via "add_request_method()" (reifyed or not), except "root" and "registry".
So finally I use a function like this:

def copy_request(request):
    """Create clone of given request, keeping registry and root as well"""
    root = request.root
    request = request.copy()
    if not hasattr(request, 'registry'):
        registry = get_current_registry()
        if registry is None:
            registry = get_global_registry()
        request.registry = registry
    request.root = root
    return request

Any advise is still welcome!  ;)

Best regards,
Thierry


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages