{"id":520,"library":"jaraco-context","title":"jaraco.context","description":"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.","status":"active","version":"6.1.2","language":"python","source_language":"en","source_url":"https://github.com/jaraco/jaraco.context","tags":["context-manager","decorator","utility","files","temporary","filesystem"],"install":[{"cmd":"pip install jaraco-context","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for `tarball` context manager on Python versions < 3.12.","package":"backports.tarfile","optional":true}],"imports":[{"symbol":"ExceptionTrap","correct":"from jaraco.context import ExceptionTrap"},{"symbol":"pushd","correct":"from jaraco.context import pushd"},{"symbol":"temp_dir","correct":"from jaraco.context import temp_dir"},{"symbol":"tarball","correct":"from jaraco.context import tarball"},{"symbol":"on_interrupt","correct":"from jaraco.context import on_interrupt"},{"note":"A decorator version of contextlib.suppress.","symbol":"suppress","correct":"from jaraco.context import suppress"},{"note":"While directly importable, it's generally preferred to import specific symbols. The `null` context manager is also deprecated.","wrong":"import jaraco.context.null","symbol":"null","correct":"from jaraco.context import null"}],"quickstart":{"code":"import os\nfrom jaraco.context import temp_dir, pushd\n\nwith temp_dir() as tmp_dir_path:\n    print(f\"Temporary directory created at: {tmp_dir_path}\")\n    assert os.path.isdir(tmp_dir_path)\n\n    # The temporary directory is automatically removed here\n    assert not os.path.exists(tmp_dir_path)\n\n# Example with pushd\ncurrent_cwd = os.getcwd()\nwith temp_dir() as tmp_path_for_pushd:\n    with pushd(tmp_path_for_pushd):\n        print(f\"Current working directory changed to: {os.getcwd()}\")\n        assert os.getcwd() == os.fspath(tmp_path_for_pushd)\n    print(f\"Current working directory restored to: {os.getcwd()}\")\n    assert os.getcwd() == current_cwd\n","lang":"python","description":"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."},"warnings":[{"fix":"Migrate to `contextlib.nullcontext` (Python 3.7+) or refactor code to remove the need for a no-op context.","message":"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.","severity":"deprecated","affected_versions":">=6.0.0 (deprecation noted in documentation)"},{"fix":"For Python 3.10-3.11, explicitly install `pip install backports-tarfile`. For Python 3.12+, it's part of the standard library.","message":"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.","severity":"gotcha","affected_versions":"<3.12"},{"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.","message":"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.","severity":"gotcha","affected_versions":"<6.x (exact versions unspecified, but fixed in later releases)"},{"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.","message":"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.","severity":"breaking","affected_versions":">=6.0.0 (versions relying on `tempdir` for cleanup)"}],"env_vars":null,"last_verified":"2026-05-12T14:36:48.002Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure the package is correctly installed using pip: `pip install jaraco.context`","cause":"The `jaraco.context` package is not installed in the current Python environment or is not accessible on the Python path.","error":"ModuleNotFoundError: No module named 'jaraco.context'"},{"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:`).","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.","error":"AttributeError: 'ExceptionTrap' object has no attribute 'passes'"},{"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.","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.","error":"TypeError: 'function' object is not a context manager"},{"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.","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.","error":"ImportError: The 'jaraco' package is required"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":4.4,"disk_size":"18.1M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":4.4,"disk_size":"19M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.19,"mem_mb":5.1,"disk_size":"20.0M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":5.1,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":4.8,"disk_size":"11.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.18,"mem_mb":4.8,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":5,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":5,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":4.4,"disk_size":"17.6M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":4.4,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}