py.py
Overview
`py.py` serves as a compatibility shim for the `pylib` package, which is being deprecated or removed. Its main purpose is to provide seamless backward compatibility for projects or modules that depend on `py.error` and `py.path` submodules.
If `pylib` is installed and available with higher priority, this shim file will be bypassed in favor of the actual `py` package implementation (typically located at `py/__init__.py`). When the shim is active, it redirects import requests for `py.error` and `py.path` to corresponding internal `_pytest._py` modules, ensuring that existing code relying on these modules continues to function without modification.
This approach allows a smooth transition away from `pylib` while maintaining compatibility and avoiding import errors in legacy codebases or during migration.
Detailed Explanation
Module-Level Behavior
Imports
sys: Used to manipulate the module import system._pytest._py.error as error: Imports the error handling module from the internal_pytestpackage._pytest._py.path as path: Imports the path handling module from the internal_pytestpackage.
Module Replacement in
sys.modulesThe two key lines:
sys.modules["py.error"] = error sys.modules["py.path"] = pathperform dynamic patching of the Python import system such that any subsequent import of
py.errororpy.pathwill reference these imported internal modules instead of looking for the originalpypackage submodules.__all__SpecificationDeclares the public API for this module as:
__all__ = ["error", "path"]meaning only
errorandpathare intended to be accessible when usingfrom py import *.
Usage Examples
This file itself is not intended to be directly imported by developers. Instead, it transparently hooks into the import mechanism.
For example, with this shim in place, the following code:
import py.error
import py.path
try:
raise py.error.Exception("Error occurred")
except py.error.Exception as e:
print("Caught py error:", e)
p = py.path.local("/tmp")
print("Local path:", p)
will work correctly by using the `_pytest._py.error` and `_pytest._py.path` implementations, even if the original `pylib` is not installed.
Important Implementation Details
Shim Strategy
This file acts purely as a shim or proxy to maintain compatibility. It does not implement any functionality itself but leverages internal
_pytest._pymodules.Module Precedence
It is important that if the
pylibpackage is installed, this shim file is skipped because the actualpypackage inpy/__init__.pywill take precedence due to Python's module resolution order.No Classes or Functions
This file contains no class or function definitions. Its entire logic is at the module level for import redirection.
Interaction with the System
Dependency on
_pytest._pyThis shim depends on the internal
_pytest._py.errorand_pytest._py.pathmodules provided by thepytesttesting framework.Impact on Downstream Code
Code that imports
py.errororpy.pathwill transparently receive the corresponding_pytestimplementations, ensuring compatibility without code changes.Part of Migration Path
This file is part of a transition strategy removing the deprecated
pylibin favor of internal or alternative implementations.
Mermaid Diagram: Module Structure
flowchart TD
A[py.py (shim)] --> B[_pytest._py.error]
A --> C[_pytest._py.path]
subgraph sys.modules
direction LR
D["py.error"] -->|redirects to| B
E["py.path"] -->|redirects to| C
end
Summary
py.pyis a shim file redirectingpy.errorandpy.pathimports to internal_pytest._pymodules.It enables compatibility for projects relying on
pylibduring its deprecation.Implements import redirection by patching
sys.modules.No classes or functions; purely a module-level proxy.
Used internally in the system and skipped if the real
pypackage is present.Supports legacy code and smooth migration.
This design ensures minimal disruption and a clean path forward as the `pylib` package is phased out.