python / cpython

The Python programming language
https://www.python.org/
Other
61.15k stars 29.51k forks source link

IDLE: Option to warn user on "Run Module" if file is not Python source #85606

Open ff014e15-a125-4b8d-b08d-d53768e316b2 opened 3 years ago

ff014e15-a125-4b8d-b08d-d53768e316b2 commented 3 years ago
BPO 41434
Nosy @terryjreedy, @roseman, @wyz23x2, @E-Paine

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = 'https://github.com/terryjreedy' closed_at = None created_at = labels = ['expert-IDLE', 'type-feature', '3.8', '3.9', '3.10'] title = 'IDLE: Option to warn user on "Run Module" if file is not Python source' updated_at = user = 'https://github.com/wyz23x2' ``` bugs.python.org fields: ```python activity = actor = 'markroseman' assignee = 'terry.reedy' closed = False closed_date = None closer = None components = ['IDLE'] creation = creator = 'wyz23x2' dependencies = [] files = [] hgrepos = [] issue_num = 41434 keywords = [] message_count = 5.0 messages = ['374561', '374562', '374568', '374571', '379448'] nosy_count = 4.0 nosy_names = ['terry.reedy', 'markroseman', 'wyz23x2', 'epaine'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue41434' versions = ['Python 3.8', 'Python 3.9', 'Python 3.10'] ```

ff014e15-a125-4b8d-b08d-d53768e316b2 commented 3 years ago

It would be great if IDLE shows a note when a non-Python file is attempted to run.

ff014e15-a125-4b8d-b08d-d53768e316b2 commented 3 years ago

It should be able to turn on/off this feature.

919475b3-36a2-4cd7-997c-9c38f05f93c7 commented 3 years ago

I think this sounds like a very good idea. I would recommend using the ispythonsource method (in the editor class) as it already has all the logic you need while also checking for a start line like "#!/usr/bin/env python" (rather than relying solely on the extension).

terryjreedy commented 3 years ago

I disagree. IDLE already shows a note -- in the editor window -- with the first noted divergence from python code marked. We are trying to keep IDLE relatively simple and not clutter it with trivial options.

ispythonsource is just a guess. The decision is whether turn on the colorizer and do python indents. For a new file, assume that it will be python code. Returning True for a directory is an error that is never hit as directories are not opened. Python files do not have to have a .py extensions, and files that have a .py extension do not have to be python files. But IDLE encourages use of the convention.

I would consider adding a note to the SyntaxError box when the error is on the first line. Something like "Is this really a Python code file?"

The only actual running of non-Python files I have seen reported, and I believe at least twice, is very beginner beginners running the saved Shell log, which begins "Python 3.x...", with the 3 highlighted. If this were recognized, the message might be "If this is a log of an interactive session, you cannot run it."

But I am not sure if this would really solve the problem of extreme ignorance. And anything added would have to have a test, and would be a possible maintenance burden.

f4315b71-14d3-4c16-bb7a-6534e5fa1d04 commented 3 years ago

I like Terry's idea of providing a better error message than just "invalid syntax" when we run something that likely isn't a Python file. There doesn't seem to be any great danger in trying to run any file that would justify a warning beforehand.