What's New in Pyramid 1.6¶
This article explains the new features in Pyramid version 1.6 as compared to its predecessor, Pyramid 1.5. It also documents backwards incompatibilities between the two versions and deprecations added to Pyramid 1.6, as well as software dependency changes and notable documentation additions.
Backwards Incompatibilities¶
IPython and BPython support have been removed from pshell in the core. To continue using them on Pyramid 1.6+, you must install the binding packages explicitly. One way to do this is by adding
pyramid_ipython(orpyramid_bpython) to theinstall_requiressection of your package'ssetup.pyfile, then re-runningsetup.py develop:setup( #... install_requires=[ 'pyramid_ipython', # new dependency 'pyramid', #... ], )
request.responsewill no longer be mutated when using therender_to_response()API. It is now necessary to pass in aresponse=argument torender_to_response()if you wish to supply the renderer with a custom response object. If you do not pass one, then a response object will be created using the current response factory. Almost all renderers mutate therequest.responseresponse object (for example, the JSON renderer setsrequest.response.content_typetoapplication/json). However, when invokingrender_to_response, it is not expected that the response object being returned would be the same one used later in the request. The response object returned fromrender_to_responseis now explicitly different fromrequest.response. This does not change the API of a renderer. See https://github.com/Pylons/pyramid/pull/1563In an effort to combat a common issue it is now a
ConfigurationErrorto register a view callable that is actually an unbound method when using the default view mapper. As unbound methods do not exist in PY3+ possible errors are detected by checking if the first parameter is namedself. For example, config.add_view(ViewClass.some_method, ...) should actually be config.add_view(ViewClass, attr='some_method)'. This was always an issue in Pyramid on PY2 but the backward incompatibility is on PY3+ where you may not use a function with the first parameter namedself. In this case it looks too much like a common error and the exception will be raised. See https://github.com/Pylons/pyramid/pull/1498
Feature Additions¶
Python 3.5 and pypy3 compatibility.
pserve --reloadwill no longer crash on syntax errors. See https://github.com/Pylons/pyramid/pull/2044Cache busting for static resources has been added and is available via a new
pyramid.config.Configurator.add_cache_buster()API. Core APIs are shipped for both cache busting via query strings and via asset manifests for integrating into custom asset pipelines. See https://github.com/Pylons/pyramid/pull/1380 and https://github.com/Pylons/pyramid/pull/1583 and https://github.com/Pylons/pyramid/pull/2171Assets can now be overidden by an absolute path on the filesystem when using the
override_asset()API. This makes it possible to fully support serving up static content from a mutable directory while still being able to use thestatic_url()API andadd_static_view(). Previously it was not possible to useadd_static_view()with an absolute path and generate urls to the content. This change replaces the call,config.add_static_view('/abs/path', 'static'), withconfig.add_static_view('myapp:static', 'static')andconfig.override_asset(to_override='myapp:static/', override_with='/abs/path/'). Themyapp:staticasset spec is completely made up and does not need to exist—it is used for generating URLs viarequest.static_url('myapp:static/foo.png'). See https://github.com/Pylons/pyramid/issues/1252Added
set_response_factory()and theresponse_factorykeyword argument to the constructor ofConfiguratorfor defining a factory that will return a customResponseclass. See https://github.com/Pylons/pyramid/pull/1499Added
pyramid.config.Configurator.root_packageattribute and init parameter to assist with includible packages that wish to resolve resources relative to the package in which the configurator was created. This is especially useful for add-ons that need to load asset specs from settings, in which case it may be natural for a developer to define imports or assets relative to the top-level package. See https://github.com/Pylons/pyramid/pull/1337Overall improvements for the
proutescommand. Added--formatand--globarguments to the command, introduced themethodcolumn for displaying available request methods, and improved theviewoutput by showing the module instead of just__repr__. See https://github.com/Pylons/pyramid/pull/1488pservecan now take a-bor--browseroption to open the server URL in a web browser. See https://github.com/Pylons/pyramid/pull/1533Support keyword-only arguments and function annotations in views in Python 3. See https://github.com/Pylons/pyramid/pull/1556
The
append_slashargument ofadd_notfound_view()will now accept anything that implements theIResponseinterface and will use that as the response class instead of the defaultHTTPFound. See https://github.com/Pylons/pyramid/pull/1610The
Configuratorhas grown the ability to allow actions to call other actions during a commit cycle. This enables much more logic to be placed into actions, such as the ability to invoke other actions or group them for improved conflict detection. We have also exposed and documented the configuration phases that Pyramid uses in order to further assist in building conforming add-ons. See https://github.com/Pylons/pyramid/pull/1513Allow an iterator to be returned from a renderer. Previously it was only possible to return bytes or unicode. See https://github.com/Pylons/pyramid/pull/1417
Improve robustness to timing attacks in the
AuthTktCookieHelperand theSignedCookieSessionFactoryclasses by using the stdlib'shmac.compare_digestif it is available (such as Python 2.7.7+ and 3.3+). See https://github.com/Pylons/pyramid/pull/1457Improve the readability of the
pcreateshell script output. See https://github.com/Pylons/pyramid/pull/1453Make it simple to define
notfoundandforbiddenviews that wish to use the default exception-response view, but with altered predicates and other configuration options. Theviewargument is now optional inadd_notfound_view()andadd_forbidden_view()See https://github.com/Pylons/pyramid/issues/494The
pshellscript will now load aPYTHONSTARTUPfile if one is defined in the environment prior to launching the interpreter. See https://github.com/Pylons/pyramid/pull/1448Add new HTTP exception objects for status codes
428 Precondition Required,429 Too Many Requestsand431 Request Header Fields Too Largeinpyramid.httpexceptions. See https://github.com/Pylons/pyramid/pull/1372/filespcreatewhen run without a scaffold argument will now print information on the missing flag, as well as a list of available scaffolds. See https://github.com/Pylons/pyramid/pull/1566 and https://github.com/Pylons/pyramid/issues/1297pcreatewill now ask for confirmation if invoked with an argument for a project name that already exists or is importable in the current environment. See https://github.com/Pylons/pyramid/issues/1357 and https://github.com/Pylons/pyramid/pull/1837Add
pyramid.request.apply_request_extensions()function which can be used in testing to apply any request extensions configured viaconfig.add_request_method. Previously it was only possible to test the extensions by going through Pyramid's router. See https://github.com/Pylons/pyramid/pull/1581Make it possible to subclass
pyramid.request.Requestand also usepyramid.request.Request.add_request.method. See https://github.com/Pylons/pyramid/issues/1529Additional shells for
pshellcan now be registered as entry points. See https://github.com/Pylons/pyramid/pull/1891 and https://github.com/Pylons/pyramid/pull/2012The variables injected into
pshellare now displayed with their docstrings instead of the defaultstr(obj)when possible. See https://github.com/Pylons/pyramid/pull/1929
Deprecations¶
The
pservecommand's daemonization features, as well as--monitor-restart, have been deprecated. This includes the[start,stop,restart,status]subcommands, as well as the--daemon,--stop-daemon,--pid-file,--status,--user, and--groupflags. See https://github.com/Pylons/pyramid/pull/2120 and https://github.com/Pylons/pyramid/pull/2189 and https://github.com/Pylons/pyramid/pull/1641Please use a real process manager in the future instead of relying on
pserveto daemonize itself. Many options exist, including your operating system's services, such as Systemd or Upstart, as well as Python-based solutions like Circus and Supervisor.See https://github.com/Pylons/pyramid/pull/1641 and https://github.com/Pylons/pyramid/pull/2120
The
principalargument topyramid.security.remember()was renamed touserid. Usingprincipalas the argument name still works and will continue to work for the next few releases, but a deprecation warning is printed.
Scaffolding Enhancements¶
Added line numbers to the log formatters in the scaffolds to assist with debugging. See https://github.com/Pylons/pyramid/pull/1326
Updated scaffold generating machinery to return the version of Pyramid and its documentation for use in scaffolds. Updated
starter,alchemyandzodbtemplates to have links to correctly versioned documentation, and to reflect which Pyramid was used to generate the scaffold.Removed non-ASCII copyright symbol from templates, as this was causing the scaffolds to fail for project generation.
Documentation Enhancements¶
Removed logging configuration from Quick Tutorial
inifiles, except for scaffolding- and logging-related chapters, to avoid needing to explain it too early.Improve and clarify the documentation on what Pyramid defines as a
principaland auseridin its security APIs. See https://github.com/Pylons/pyramid/pull/1399Moved the documentation for
acceptonpyramid.config.Configurator.add_view()to no longer be part of the predicate list. See https://github.com/Pylons/pyramid/issues/1391 for a bug report statingnot_was failing onaccept. Discussion with @mcdonc led to the conclusion that it should not be documented as a predicate. See https://github.com/Pylons/pyramid/pull/1487 for this PR.Clarify a previously-implied detail of the
ISession.invalidateAPI documentation.Add documentation of command line programs (
p*scripts). See https://github.com/Pylons/pyramid/pull/2191