python / python/cpython

repr(PermissionError) lacks filename information

Open
#92,921 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

repr(PermissionError) looses the information about the affected filename, because it only returns PermissionError(13, 'Permission denied'). Example code to demonstrate that (run as non-root):

#!/usr/bin/python3
try:
    open("/path-with-no-write-access", "w")
except PermissionError as error:
    print(f"error = {error}")
    print(f"eval(repr(error)) = {eval(repr(error))}")

Output of this code:

error = [Errno 13] Permission denied: '/path-with-no-write-access'
eval(repr(error)) = [Errno 13] Permission denied

I also failed to figure out how to raise a PermissionError with a filename specified. PermissionError takes no keyword arguments.

Rational

apport uses repr(error) in UserInterface.collect_info() in case the thread_collect_info thread crashes to present this information to the user. One test case failed with PermissionError(13, 'Permission denied') which did not help at all to trouble shoot the failure. If the representation of the exception had included the filename, it would have saved me time.

Your environment

  • CPython versions tested on: 3.10.4
  • Operating system and architecture: Ubuntu 22.04 LTS (jammy)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported PermissionError example on a non-root system and inspect how PermissionError stores and formats its filename. Trace the exception representation and existing tests for OSError subclasses, then add coverage showing the desired filename information in repr while preserving eval(repr(error)) behavior where applicable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.