-
Notifications
You must be signed in to change notification settings - Fork 187
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
Moving from Python 2 -> 3 and hit snag with webob and JSON serializer, pickle works #431
Comments
It sounds like you've written a custom serializer and it's not returning bytes. |
@mmerickel thanks for the rapid call out, here is another snippet: from pyramid.config import Configurator
# Cookie only session, not encrypted but signed to prevent tampering.
from pyramid.session import SignedCookieSessionFactory
from miscutils import get_children_settings
def session_serializer(serializer_name):
if serializer_name == "json":
import json
return json
import pickle
return pickle
def main(global_config, **settings):
"""This function returns a Pyramid WSGI application."""
# setup session factory to use unencrypted but signed cookies
session_settings = get_children_settings(settings, "session")
session_settings["serializer"] = session_serializer(
session_settings.get("serializer")
)
session_factory = SignedCookieSessionFactory(**session_settings)
with Configurator(settings=settings, session_factory=session_factory) as config:
# all of the models for persisting data into our database.
config.include(".models")
# all of the web application routes.
config.include(".routes")
# all of the data we attach to each inbound request.
config.include(".request_methods")
# enable jinja2 templating engine.
config.include(".config_jinja2")
# scan each of these includes for additional configuration.
config.scan()
return config.make_wsgi_app() |
|
Thanks, I figured as much. Here is a first pass at making First I created a new file / module which I may import and pass to the session configurator: import json
loads = json.loads
def dumps(obj, *args, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw):
result = json.dumps(obj, *args, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
return result.encode("utf-8") and then in my def session_serializer(serializer_name):
if serializer_name == "json":
import make_post_sell.lib.json_session_serializer as json_serializer
return json_serializer
import pickle
return pickle This appears to work so I will close this issue. Please feel free to comment after closing if my approach is bad. |
Actually looks like we already have a batteries included solution, see: https://docs.pylonsproject.org/projects/pyramid/en/latest/api/session.html#pyramid.session.JSONSerializer def session_serializer(serializer_name):
if serializer_name == "json":
from pyramid.session import JSONSerializer
return JSONSerializer()
from pyramid.session import PickleSerializer
return PickleSerializer() |
I'm taking the time to try to upgrade some of my Pyramid projects from Python 2.7 to 3.9, things are going well but I hit a snag with
webob
, and I'm wondering if it is a defect or something obvious I'm doing wrong.I have a work around by falling back the the pickle serializer so there is no rush but I would prefer to use the JSON serializer for signed cookie based sessions.
Here is a snippet of my code in question and the full traceback.
Thanks again for anything you can do to nudge me in the right direction. This code works in Python 2 with JSON serializer.
The text was updated successfully, but these errors were encountered: