Title: __package__ not set to None under pdb in Python 3
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Keith Prussing, eric.snow, iritkatriel, ncoghlan
Priority: normal Keywords:

Created on 2015-11-20 16:02 by Keith Prussing, last changed 2021-06-15 04:48 by iritkatriel. This issue is now closed.

File name Uploaded Description Edit Keith Prussing, 2015-11-20 16:02 Minimum working example
Messages (4)
msg254989 - (view) Author: Keith Prussing (Keith Prussing) Date: 2015-11-20 16:02
When a module is run under pdb in Python 3, __package__ is set to the empty string instead of None.  The attached minimum working example depicts this behavior.  The results are summarized in the following table.

=============== ====== ======
    Command      Py 2   Py 3
=============== ====== ======
 python          None   None
 python -m        ''     ''
 python -m pdb   None    ''
 pdb             None    ''
=============== ====== ======

The behavior I expected was for the Python executable to treat the input file as a script and not a module.  My rationale is that pdb is loaded as the executing module that accepts a script as its argument.  Per the help, only one -m option is allowed when launching python.
msg395812 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-14 17:00
pdb imports the module with importlib, and populates __main__ with data from its spec, including the package. This doesn't contradict the fact that the python command line can have only one "-m".

What is the actual problem here?
msg395858 - (view) Author: Keith Prussing (Keith Prussing) Date: 2021-06-15 02:16
Caveat: It's been a few years so I'm trying to recall what I was doing at the time and what my original problem was.

I understand and expect pdb to populate __package__ and __main__ to be what it expects when run as a module with -m.  However, I believe my expectation was for it to not alter the relevant __package__ or __main__ with respect to the script being debugged.  This was the behavior in Python 2.7 which changed (unexpectedly to me) in Python 3.4 (or earlier.  I obviously didn't look too hard).  I must have been trying to debug a script that expected to run using the -m option but could be run as a script too.

A few things have changed since the original report:

1.  I have forgotten which script/tool I was working on so I wouldn't be able to find it if I tried.
2.  I was trying to support Python 2.7 and Python 3 (which I have 0 interest in doing ever again),
3.  This small gotcha apparently hasn't tripped up anyone else.

At this point, I suspect this has been overcome by events and can be closed.
msg395859 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-15 04:48
Thanks Keith. Closing and we can look into it if someone else has an issue.
Date User Action Args
2021-06-15 04:48:49iritkatrielsetstatus: open -> closed

messages: + msg395859
stage: resolved
2021-06-15 02:16:17Keith Prussingsetmessages: + msg395858
2021-06-14 17:00:45iritkatrielsetnosy: + iritkatriel
messages: + msg395812
2020-09-11 23:12:04brett.cannonsetnosy: - brett.cannon
components: + Library (Lib), - Extension Modules
2016-01-14 23:01:15SilentGhostsetnosy: + brett.cannon, ncoghlan, eric.snow
2015-11-20 16:02:19Keith Prussingcreate