Skip to content

IDLE: warn if save-as name matches stdlib name #69708

Open

Description

@terryjreedy
BPO 25522
Nosy @terryjreedy, @roseman, @vedgar, @ZackerySpytz
PRs
  • gh-69708: IDLE "Save As" warnings #17051
  • 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:

    assignee = 'https://github.com/terryjreedy'
    closed_at = None
    created_at = <Date 2015-10-31.05:41:42.825>
    labels = ['expert-IDLE', 'type-feature', '3.7']
    title = 'IDLE: warn if save-as name matches stdlib name'
    updated_at = <Date 2019-11-05.00:40:06.357>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2019-11-05.00:40:06.357>
    actor = 'ZackerySpytz'
    assignee = 'terry.reedy'
    closed = False
    closed_date = None
    closer = None
    components = ['IDLE']
    creation = <Date 2015-10-31.05:41:42.825>
    creator = 'terry.reedy'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 25522
    keywords = ['patch']
    message_count = 8.0
    messages = ['253775', '253777', '253829', '253830', '333944', '333952', '334279', '355988']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'markroseman', 'THRlWiTi', 'veky', 'lac', 'ZackerySpytz']
    pr_nums = ['17051']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue25522'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. terryjreedy commented on Oct 31, 2015

      @terryjreedy
      MemberAuthor

      When users 'saveas', warn if name matches that of a stdlib modules. Note that shadowing is opposite for lib and binary (builtin) modules, so message needs to be different. I am not worrying about 3rd party modules on the search path (ie, site-packages), which would be shadowed like lib modules. Rough outline of code to add to IOBinding.

      from os,path import dirname
      import sys

      # this part on initiallzation or first saveas call
      def libmodules():
          with open(dirname(dirname(__file__))) as lib:
              for f in lib:
                  if <directory containing __init__.py#> or f.endswith(.py):
                      yield f
      # test.test__all__ has this code
      
      islibmodule = frozenset(libmodules()).__contains__
      isbinmodule = frozenset(sys.builtin_module_names).__contains__
      
      # this part after saveas dialog returns
      if islibmodule(name):
          go = warn("This name matches a stdlib name in /Lib. If you run python in this directory, you will not be able to import the stdlib module.  Continue?".)
      elif isbinmodule(name):
          go = warn("This name matches a builtin stdlib name.  You will not be able to import this file.  Continue?".)
      else:
          go = True
      if go:
          <save as requested>
      else:
         (get name again>  # or put in while not go loop
    2. terryjreedy commented on Oct 31, 2015

      @terryjreedy
      MemberAuthor

      The lib messages should say "Neither you nor python will be able to import the stdlib module". Example of python-imported lib names that a beginner might write are abc, io, os, nt, and stat.

    3. lac commented on Nov 1, 2015

      lacmannequin
      Mannequin

      I'm not sure about "Neither you nor Python will be able to import
      the stdlib." Depending on what the file is, you may be able to import
      it later. I'd prefer:

      "If you run Python in this directory, your version of <stdlibname>
      will be used instead of the one in the standard library. This may cause Idle to behave strangely, or possibly not run at all."

    4. lac commented on Nov 1, 2015

      lacmannequin
      Mannequin

      Do we need a full path name here as well? Probably not.

    5. self-assigned this
      on Jun 19, 2017
    6. terryjreedy commented on Jan 18, 2019

      @terryjreedy
      MemberAuthor

      Also check if the name is not an identifier and therefore could not be imported. Example:

      >>> exec('import abc--bb')
          import abc--bb
                    ^
      SyntaxError: invalid syntax
    7. vedgar commented on Jan 18, 2019

      vedgarmannequin
      Mannequin

      Many beginners don't write files in order to import them. For them, these name-checking is simply adding noise. If you want to do something in this area, I think a much more useful (and difficult) course of action would be to check on the opposite end. If an AttributeError "module blah has no attribute foo" is raised, then check whether a file in the current directory (or simply the current file) is named blah, and there is also a stdlib blah that has fool.

    8. terryjreedy commented on Jan 24, 2019

      @terryjreedy
      MemberAuthor

      Beginners import stdlib files such as random. And they save scripts with such name, and accidentally import the script when not desired.

      Beginners should learn how to test a script by running a test file provided by an instructor or written themselves. In either case, they *will* have to import the script.

    9. ZackerySpytz commented on Nov 5, 2019

      ZackerySpytzmannequin
      Mannequin

      I have created a pull request for this issue.

    10. transferred this issue fromon Apr 10, 2022
    11. moved this to In Progress in IDLE Issueson Jul 27, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions