Open and yield a file for libpq client/server communication traces if --pq-tracefile option is set.
(request)
| 123 | |
| 124 | @pytest.fixture(scope="session") |
| 125 | def tracefile(request): |
| 126 | """Open and yield a file for libpq client/server communication traces if |
| 127 | --pq-tracefile option is set. |
| 128 | """ |
| 129 | if not (tracefile := request.config.getoption("--pq-trace")): |
| 130 | yield None |
| 131 | return |
| 132 | |
| 133 | if tracefile.lower() == "stderr": |
| 134 | try: |
| 135 | sys.stderr.fileno() |
| 136 | except io.UnsupportedOperation: |
| 137 | raise pytest.UsageError( |
| 138 | "cannot use stderr for --pq-trace (in-memory file?)" |
| 139 | ) from None |
| 140 | |
| 141 | yield sys.stderr |
| 142 | return |
| 143 | |
| 144 | with open(tracefile, "w") as f: |
| 145 | yield f |
| 146 | |
| 147 | |
| 148 | @contextmanager |