Skip to content

Mac: Local modules can shadow builtins (e.g. a local math.py can shadow math) #77026

Closed

Description

@EricCousineau-TRI
BPO 32845
Nosy @ronaldoussoren, @ericvsmith, @ned-deily, @EricCousineau-TRI
Files
  • test.sh
  • 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 = None
    closed_at = <Date 2018-02-15.01:08:51.154>
    created_at = <Date 2018-02-14.23:11:46.841>
    labels = ['interpreter-core', 'type-bug', 'invalid']
    title = 'Mac: Local modules can shadow builtins (e.g. a local `math.py` can shadow `math`)'
    updated_at = <Date 2018-02-15.17:49:54.435>
    user = 'https://github.com/EricCousineau-TRI'

    bugs.python.org fields:

    activity = <Date 2018-02-15.17:49:54.435>
    actor = 'ned.deily'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-02-15.01:08:51.154>
    closer = 'ned.deily'
    components = ['Interpreter Core']
    creation = <Date 2018-02-14.23:11:46.841>
    creator = 'Eric Cousineau'
    dependencies = []
    files = ['47442']
    hgrepos = []
    issue_num = 32845
    keywords = []
    message_count = 7.0
    messages = ['312183', '312186', '312189', '312190', '312210', '312211', '312217']
    nosy_count = 4.0
    nosy_names = ['ronaldoussoren', 'eric.smith', 'ned.deily', 'Eric Cousineau']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32845'
    versions = ['Python 2.7']

    Activity

    1. EricCousineau-TRI commented on Feb 14, 2018

      EricCousineau-TRImannequin
      MannequinAuthor

      We ran into an issue with our library; we build a submodule named math.so, and have a test math_test which is ran in the same directory as this module (since this is how we've written it with Bazel's generated Python binaries):
      RobotLocomotion/drake#8041

      This happens for us on HighSierra / Sierra MacOS (for *.py and *.so modules); Ubuntu Linux seems fine.

      I have a reproduction script just for *.py files here:
      https://github.com/EricCousineau-TRI/repro/blob/b08bc47/bug/mac_builtin_shadow/test.sh#L38
      (attached this as test.sh)

      The module is exposed on the path given that it neighbors the first execution of the import_test.py script. The second execution (where the import_test.py script is moved up one level) succeeds on Mac (s.t. the math.py is not in sys.path).

      This behavior seems to violate the documentation:
      https://docs.python.org/2/tutorial/modules.html#the-module-search-path

    2. added
      type-bug
      on Feb 14, 2018
    3. ericvsmith commented on Feb 15, 2018

      @ericvsmith
      Member

      This is the expected behavior, fortunately or not. "math" is not builtin in the sense that is used in that paragraph.

    4. ericvsmith commented on Feb 15, 2018

      @ericvsmith
      Member

      Removing macOS since this isn't Mac specific.

    5. added
      interpreter-core
      and removed on Feb 15, 2018
    6. ned-deily commented on Feb 15, 2018

      @ned-deily
      Member

      This had me confused for a while. But eric.smith's comment is the clue to what's going on here:

      This is the expected behavior, fortunately or not. "math" is not builtin in the sense that is used in that paragraph.

      The difference in behavior that you are seeing seems to be due to the fact that the Debian/Ubuntu uses a non-standard build to build-in the standard lib "math" module, whereas the Mac version you are using undoubtedly does not. You can see this behavior yourself if you build your own version of python (either 2.7 or 3.x) on your Ubuntu system. You should now see the same behavior you see on the Mac. For example:

      # on a current Debian system
      $ /usr/bin/python2.7 -c 'import sys,math;print(sys.modules["math"])'
      <module 'math' (built-in)>

      # on a current Mac system using the python.org 2.7.14
      $ /usr/local/bin/python2.7 -c 'import sys,math;print(sys.modules["math"])'
      <module 'math' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/math.so'>

      So, I don't think this is an issue at all. Please reopen if you think there is actually a problem here.

    7. ned-deily commented on Feb 15, 2018

      @ned-deily
      Member

      P.S. This issue points out once again why it is generally a bad idea to shadow or mix-and-match standard library module names.

    8. EricCousineau-TRI commented on Feb 15, 2018

      EricCousineau-TRImannequin
      MannequinAuthor

      P.S. This issue points out once again why it is generally a bad idea to shadow or mix-and-match standard library module names.

      Duly noted! And thank y'all for the explanations!

      Can I ask if it's bad practice to use a standard library module name as a submodule, e.g. example_module.math?

      TBH, this all arises because we use Bazel and wanted to modularize our tests - we place them under test/{name}.py neighboring the target (sub)module, and Bazel presently generates a wrapper script which is executed in the same directory as the (sub)module, which is why this (unintended) shadowing occurs.

      I would like still like to have a submodule named math, so I can just teach our Bazel rules to not run the test in the same directory -- if having submodules named math is not frowned upon.

    9. ned-deily commented on Feb 15, 2018

      @ned-deily
      Member

      The example you gave caused problems because of the use of the ambiguous unqualified name "math". If you are careful to use qualified names, and/or perhaps use alias names ("as") to increase readability, there shouldn't be any problems.

    10. transferred this issue fromon Apr 10, 2022
    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

      interpreter-coretype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions