jaraco.context

raw JSON →
6.1.2 verified Tue May 12 auth: no python install: verified quickstart: stale

jaraco.context is a Python library providing useful decorators and context managers for common programming patterns. It offers tools like `ExceptionTrap` for exception handling, `pushd` for temporary directory changes, `tarball` for extracting archives, and `temp_dir` for managing temporary file system resources. The current version is 6.1.2, and it maintains an active release schedule with several updates in the past year.

pip install jaraco-context
error ModuleNotFoundError: No module named 'jaraco.context'
cause The `jaraco.context` package is not installed in the current Python environment or is not accessible on the Python path.
fix
Ensure the package is correctly installed using pip: pip install jaraco.context
error AttributeError: 'ExceptionTrap' object has no attribute 'passes'
cause This usually indicates an attempt to use an `ExceptionTrap` instance with a method (`.passes()` or `.raises()`) that is only available as a decorator on a function, or an older version of `jaraco.context` is installed that lacks this specific API.
fix
Update jaraco.context to the latest version (pip install --upgrade jaraco.context) and ensure ExceptionTrap is used as a decorator (e.g., @ExceptionTrap(ValueError).passes) or as a context manager (e.g., with ExceptionTrap(ValueError) as trap:).
error TypeError: 'function' object is not a context manager
cause A function that is intended to be a context manager (e.g., created with `@contextlib.contextmanager` or similar internal mechanisms in `jaraco.context`) is being called directly without a `with` statement, or the `@contextmanager` decorator was omitted.
fix
Ensure that functions designed as context managers are always used within a with statement: with some_context_manager(): or, if using a decorator, that the decorator itself is correctly applied to the function.
error ImportError: The 'jaraco' package is required
cause This error typically occurs in bundled applications (e.g., created with PyInstaller) where `setuptools`'s `pkg_resources` module struggles to correctly locate the `jaraco` namespace package, even if `jaraco.context` is present.
fix
When bundling with tools like PyInstaller, ensure the jaraco namespace is correctly included. This might involve adding --hidden-import=jaraco or --collect-all jaraco to your PyInstaller command, or explicitly including the jaraco parent directory in your build process.
deprecated The `null` context manager, which provides a no-op context, has been deprecated. While still available, users should consider refactoring to avoid its use or using `contextlib.nullcontext` for newer Python versions (3.7+) as an alternative if truly needed for symmetry.
fix Migrate to `contextlib.nullcontext` (Python 3.7+) or refactor code to remove the need for a no-op context.
gotcha When using the `tarball` context manager, a conditional dependency on `backports.tarfile` exists for Python versions prior to 3.12. If you target Python 3.10 or 3.11 and use `tarball`, ensure `backports-tarfile` is installed.
fix For Python 3.10-3.11, explicitly install `pip install backports-tarfile`. For Python 3.12+, it's part of the standard library.
gotcha Older versions of `jaraco.context` (prior to unspecified fixes) had a vulnerability related to handling certain archive file paths in `tarball`, which could lead to directory traversal and overwriting files outside the intended extraction directory.
fix Always use the latest stable version of `jaraco.context` to ensure security patches are applied. As of 6.1.2, this issue should be addressed.
breaking A temporary directory created and managed by a context manager (likely `tempdir` from `jaraco.context`) failed to be cleaned up upon exiting its context, resulting in an `AssertionError` when checking for its non-existence. This indicates a resource leak where temporary files or directories persist beyond their intended scope.
fix Ensure the library version is up-to-date. If the issue persists, verify that the temporary directory context manager's implementation correctly removes the directory and its contents upon context exit. This may require checking for platform-specific cleanup issues or examining the library's bug tracker for known regressions related to resource management.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.10s 18.1M
3.10 slim (glibc) - - 0.14s 19M
3.11 alpine (musl) - - 0.19s 20.0M
3.11 slim (glibc) - - 0.17s 20M
3.12 alpine (musl) - - 0.12s 11.5M
3.12 slim (glibc) - - 0.18s 12M
3.13 alpine (musl) - - 0.10s 11.2M
3.13 slim (glibc) - - 0.12s 12M
3.9 alpine (musl) - - 0.10s 17.6M
3.9 slim (glibc) - - 0.09s 18M

This quickstart demonstrates the `temp_dir` context manager to create and automatically clean up a temporary directory, and the `pushd` context manager to temporarily change and restore the current working directory.

import os
from jaraco.context import temp_dir, pushd

with temp_dir() as tmp_dir_path:
    print(f"Temporary directory created at: {tmp_dir_path}")
    assert os.path.isdir(tmp_dir_path)

    # The temporary directory is automatically removed here
    assert not os.path.exists(tmp_dir_path)

# Example with pushd
current_cwd = os.getcwd()
with temp_dir() as tmp_path_for_pushd:
    with pushd(tmp_path_for_pushd):
        print(f"Current working directory changed to: {os.getcwd()}")
        assert os.getcwd() == os.fspath(tmp_path_for_pushd)
    print(f"Current working directory restored to: {os.getcwd()}")
    assert os.getcwd() == current_cwd