Merge pull request #4842 from nicoddemus/disable-bytecode-writing

Document how to disable caching rewritten .pyc files to disk
This commit is contained in:
Daniel Hahler 2019-02-27 12:11:20 +01:00 committed by GitHub
commit 05bb5ffb65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 21 deletions

View File

@ -252,8 +252,8 @@ the conftest file:
.. _assert-details: .. _assert-details:
.. _`assert introspection`: .. _`assert introspection`:
Advanced assertion introspection Assertion introspection details
---------------------------------- -------------------------------
.. versionadded:: 2.1 .. versionadded:: 2.1
@ -266,13 +266,36 @@ supporting modules which are not themselves test modules will not be rewritten**
You can manually enable assertion rewriting for an imported module by calling You can manually enable assertion rewriting for an imported module by calling
`register_assert_rewrite <https://docs.pytest.org/en/latest/writing_plugins.html#assertion-rewriting>`_ `register_assert_rewrite <https://docs.pytest.org/en/latest/writing_plugins.html#assertion-rewriting>`_
before you import it (a good place to do that is in ``conftest.py``). before you import it (a good place to do that is in your root ``conftest.py``).
.. note:: For further information, Benjamin Peterson wrote up `Behind the scenes of pytest's new assertion rewriting <http://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html>`_.
Assertion rewriting caches files on disk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``pytest`` will write back the rewritten modules to disk for caching. You can disable
this behavior (for example to avoid leaving stale ``.pyc`` files around in projects that
move files around a lot) by adding this to the top of your ``conftest.py`` file:
.. code-block:: python
import sys
sys.dont_write_bytecode = True
Note that you still get the benefits of assertion introspection, the only change is that
the ``.pyc`` files won't be cached on disk.
Additionally, rewriting will silently skip caching if it cannot write new ``.pyc`` files,
i.e. in a read-only filesystem or a zipfile.
Disabling assert rewriting
~~~~~~~~~~~~~~~~~~~~~~~~~~
``pytest`` rewrites test modules on import by using an import ``pytest`` rewrites test modules on import by using an import
hook to write new ``pyc`` files. Most of the time this works transparently. hook to write new ``pyc`` files. Most of the time this works transparently.
However, if you are messing with import yourself, the import hook may However, if you are working with the import machinery yourself, the import hook may
interfere. interfere.
If this is the case you have two options: If this is the case you have two options:
@ -282,11 +305,6 @@ before you import it (a good place to do that is in ``conftest.py``).
* Disable rewriting for all modules by using ``--assert=plain``. * Disable rewriting for all modules by using ``--assert=plain``.
Additionally, rewriting will fail silently if it cannot write new ``.pyc`` files,
i.e. in a read-only filesystem or a zipfile.
For further information, Benjamin Peterson wrote up `Behind the scenes of pytest's new assertion rewriting <http://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html>`_.
.. versionadded:: 2.1 .. versionadded:: 2.1
Add assert rewriting as an alternate introspection technique. Add assert rewriting as an alternate introspection technique.