GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pdb mangles sys.path when run with -P or ._pth · Issue #99367 · python/cpython

pdb mangles sys.path when run with -P or ._pth #99367

New issue
Copy link
New issue
Copy link
Closed
Closed
pdb mangles sys.path when run with -P or ._pth#99367
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@cwalther
cwalther
opened on Nov 11, 2022
Issue body actions

Bug report

When running a script, pdb indiscriminately replaces sys.path[0], which it assumes to be the path where pdb itself was found, with the path where the script was found. That assumption may not be correct: it is not when the interpreter runs in “safe path” mode due to a -P command line option or the presence of a ._pth file. In that case sys.path[0] may point to the standard library, and overwriting it may break the script’s ability to import standard library modules.

This is easily reproduced in the embeddable package for Windows, which has the standard library in python311.zip, refers to it in sys.path[0], and has a python311._pth file, but should also be reproducible (using -P) on other platforms in any other installation that finds the standard library in sys.path[0]. (This is not the case in a standard installation for Windows, which has '…\\python311.zip' in sys.path[0], but that file doesn’t actually exist and the standard library is located in files in Lib/ and is found through later entries in sys.path.)

Steps to reproduce

script.py:

import sys
print(sys.path)
import random
print(random.random())

Actual result

C:\Users\Walther\pdbtest>python-3.11.0-embed-amd64\python.exe -m pdb script.py
> c:\users\walther\pdbtest\script.py(1)<module>()
-> import sys
(Pdb) c
['C:\\Users\\Walther\\pdbtest', 'C:\\Users\\Walther\\pdbtest\\python-3.11.0-embed-amd64']
Traceback (most recent call last):
  File "pdb.py", line 1768, in main
  File "pdb.py", line 1646, in _run
  File "bdb.py", line 597, in run
  File "<string>", line 1, in <module>
  File "C:\Users\Walther\pdbtest\script.py", line 3, in <module>
    import random
ModuleNotFoundError: No module named 'random'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\users\walther\pdbtest\script.py(3)<module>()
-> import random
(Pdb) q
Post mortem debugger finished. The C:\Users\Walther\pdbtest\script.py will be restarted
> c:\users\walther\pdbtest\script.py(1)<module>()
-> import sys
(Pdb) q

Expected result

C:\Users\Walther\pdbtest>python-3.11.0-embed-amd64\python.exe -m pdb script.py
> c:\users\walther\pdbtest\script.py(1)<module>()
-> import sys
(Pdb) c
['C:\\Users\\Walther\\pdbtest\\python-3.11.0-embed-amd64\\python311.zip', 'C:\\Users\\Walther\\pdbtest\\python-3.11.0-embed-amd64']
0.6351821708383135
The program finished and will be restarted
> c:\users\walther\pdbtest\script.py(1)<module>()
-> import sys
(Pdb) q

It seems to me this could be fixed as follows (which is what I used to get the “expected result”), or maybe by comparing sys.path[0] with __file__ to check the assumption directly:

--- pdb-orig.py 2022-11-11 10:51:02.717413700 +0100
+++ pdb.py      2022-11-11 10:10:19.737092700 +0100
@@ -147,7 +147,8 @@
             sys.exit(1)

         # Replace pdb's dir with script's dir in front of module search path.
-        sys.path[0] = os.path.dirname(self)
+        if not (hasattr(sys.flags, 'safe_path') and sys.flags.safe_path):
+            sys.path[0] = os.path.dirname(self)

     @property
     def filename(self):

Any opinions, should I make a PR with that?

Your environment

  • CPython versions tested on: 3.11.0
  • Operating system and architecture: Windows 10+11 AMD64+ARM64 (but shouldn’t matter)

Linked PRs

  • gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set #111762

Activity

  1. cwalther
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 11, 2022
  2. gaogaotiantian commented on Nov 4, 2023

    @gaogaotiantian
    gaogaotiantian
    on Nov 4, 2023
    Member
    More actions

    Hi @cwalther , do you still work on this? You can make a PR with a regression test. If you don't want to work on the test, I can make the PR and list you as co-author.

  3. cwalther commented on Nov 5, 2023

    @cwalther
    cwalther
    on Nov 5, 2023
    ContributorAuthor
    More actions

    Thank you for the feedback! Sure, I can try to make a test and submit a PR. I am not currently working on CPython however, and it will probably take a few weeks until I have time to set up a development environment and figure out how the testing works. So, if you can do it more quickly than that, that would be nice. But I have no urgent need for a fix.

  4. gaogaotiantian commented on Nov 5, 2023

    @gaogaotiantian
    gaogaotiantian
    on Nov 5, 2023
    Member
    More actions

    Don't worry about that, it's not a super complicated change. I can do it and list you as the co-author.

    Reacted by Christian Walther
  5. iritkatriel
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 26, 2023
  6. iritkatriel
    added a commit that references this issue on Nov 27, 2023

    gh-99367: Do not mangle sys.path[0] in pdb if safe_path is set (#111762)

    b90a5cf
  7. gaogaotiantian commented on Feb 5, 2024

    @gaogaotiantian
    gaogaotiantian
    on Feb 5, 2024
    Member
    More actions

    This is fixed.

  8. gaogaotiantian
    closed this as completedon Feb 5, 2024
  9. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-99367: Do not mangle sys.path[0] in pdb if safe_path is set (p…

    6244c0b
  10. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-99367: Do not mangle sys.path[0] in pdb if safe_path is set (p…

    41623d1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.