Skip to content

Add shutil.open #47427

Closed as not planned
Closed as not planned

Description

@ganadist
mannequin
BPO 3177
Nosy @ronaldoussoren, @tebeka, @pitrou, @larryhastings, @giampaolo, @benjaminp, @mcepl, @merwok, @bitdancer, @smarnach, @Fak3, @danpla
Files
  • os_startfile.diff: patch that implemented os.startfile()
  • shutil_open.py: implementation sketch
  • shutil_launch.py: The last 1/3rd of Rebert's implementation of shutil.open()
  • shutil_open.patch: Implementation of shutil_open feature, untested on Windows
  • shutil_open.patch
  • shutil_open.patch: provides shutil.launch() to do os.startfile() cross-platform
  • shutil_open.patch: another round of improvement
  • shutil_open.patch: revised patch
  • issue3177-os_startfile_macosx.txt
  • 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 = None
    created_at = <Date 2008-06-23.11:38:23.901>
    labels = ['type-feature', 'library']
    title = 'Add shutil.open'
    updated_at = <Date 2018-03-02.17:06:15.586>
    user = 'https://bugs.python.org/ganadist'

    bugs.python.org fields:

    activity = <Date 2018-03-02.17:06:15.586>
    actor = 'Socob'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2008-06-23.11:38:23.901>
    creator = 'ganadist'
    dependencies = []
    files = ['10715', '25310', '25311', '25312', '25315', '25317', '25319', '25332', '26624']
    hgrepos = []
    issue_num = 3177
    keywords = ['patch']
    message_count = 62.0
    messages = ['68623', '68650', '68664', '140059', '140275', '140276', '140280', '140283', '140415', '140416', '140418', '140419', '157216', '157246', '157247', '157258', '159000', '159001', '159004', '159005', '159006', '159007', '159008', '159009', '159010', '159017', '159020', '159029', '159030', '159034', '159037', '159038', '159039', '159043', '159044', '159051', '159056', '159058', '159059', '159062', '159063', '159064', '159065', '159068', '159069', '159121', '159505', '159507', '161569', '161613', '161631', '161687', '161688', '161837', '161852', '161853', '161854', '161858', '167011', '310214', '311549', '311552']
    nosy_count = 19.0
    nosy_names = ['ronaldoussoren', 'tebeka', 'pitrou', 'larry', 'giampaolo.rodola', 'benjamin.peterson', 'mcepl', 'eric.araujo', 'ganadist', 'Arfrever', 'r.david.murray', 'cvrebert', 'rosslagerwall', 'smarnach', 'Roman.Evstifeev', 'Hobson.Lane', 'plakhotich', 'Socob', 'amirjn']
    pr_nums = []
    priority = 'low'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue3177'
    versions = ['Python 3.3']

    Activity

    1. ganadist commented on Jun 23, 2008

      ganadistmannequin
      MannequinAuthor

      Currently, os.startfile is implemented in Win32 only, but there are
      command line tools in Unix and MacOSX that have same behavior.

      As a result of http://portland.freedesktop.org, unix desktop has command
      line tool named "xdg-open"
      (http://portland.freedesktop.org/xdg-utils-1.0/xdg-open.html).

      And MacOSX has 'open'
      (http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/open.1.html)
      command, and it can be used in same condition.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Jun 23, 2008
    3. benjaminp commented on Jun 23, 2008

      @benjaminp
      Contributor

      Those commands can be easily used through the subprocess module.
      However, if a patch is provided, it could be accepted.

    4. changed the title [-]os.startfile implement in posix and MacOSX[/-] [+]implement os.startfile on posix and MacOSX[/+] on Jun 23, 2008
    5. ganadist commented on Jun 24, 2008

      ganadistmannequin
      MannequinAuthor

      I implemented os.startfile on posix and MacOSX as you said.

      but it need more work to handle error.

    6. rosslagerwall commented on Jul 9, 2011

      rosslagerwallmannequin
      Mannequin

      Closed bpo-12522 as a duplicate.

      It contains a link to a discussion on python-ideas requesting the feature.

    7. merwok commented on Jul 13, 2011

      @merwok
      Member

      I’m not sure we want to copy the Windows startfile call for other OSes. The os module is designed to wrap system-level calls, masking OS differences (for sendfile, for example) but not going further; it’s up to other modules (like shutil) to build more convenient APIs on top of what os provides. xdg-open is a program that’s used to open files or URIs, but it does not provide other actions like Windows’ startfile does (edit, print, etc.), nor is it backed by a system call. For these reasons, I think it’s inappropriate to implement os.startfile for non-Windows systems. People can use subprocess to run open or xdg-open.

    8. giampaolo commented on Jul 13, 2011

      @giampaolo
      Contributor

      +1 on what Eric just said.
      See bpo-10882 and msg 126049

    9. merwok commented on Jul 13, 2011

      @merwok
      Member

      So, unless someone wants to turn this request into “Add shutil.open”, I’ll close it in a few days. (I haven’t found the original discussion; if someone could add a link to the archives on mail.python.org or copy relevant quotes, it could help.)

    10. cool-RR commented on Jul 13, 2011

      cool-RRmannequin
      Mannequin

      Eric, I have no problem with this function being placed in shutil instead of os, as long as it's implemented and it's in the standard library, and people don't have to use subprocess to run open or xdg-open themselves as I currently do.

      So I have no problem with renaming this bug to "Add shutil.open".

    11. merwok commented on Jul 15, 2011

      @merwok
      Member

      as long as it's implemented and it's in the standard library, and
      people don't have to use subprocess to run open or xdg-open themselves
      as I currently do.

      This new function would call os.startfile on Windows, xdg-open where applicable, and open on Mac OS X (using a subprocess for these last two).

      I found the python-ideas thread: http://mail.python.org/pipermail/python-ideas/2011-July/010674.html
      There was only limited support.

    12. changed the title [-]implement os.startfile on posix and MacOSX[/-] [+]Add shutil.open[/+] on Jul 15, 2011
    13. vstinner commented on Jul 15, 2011

      @vstinner
      Member

      dependencies: + Finding programs in PATH, adding shutil.which

      Why did you add this dependency? For example, subprocess is able to locate a program if the program has no full path. We don't need a which function.

    14. merwok commented on Jul 15, 2011

      @merwok
      Member

      See also lengthy discussion on bpo-1301512.

    15. 43 remaining items

    16. merwok commented on May 29, 2012

      @merwok
      Member

      The latest discussion on this thread does not match my UNIX user model.

      tl;dr: shutil.whatever should call startfile(..., 'edit') or xdg-open; other actions implemented by startfile are not cross-platform.

      Well, so how about on UNIX shutil.launch (or whatever it's called) first checks to see if we're
      referring to a file. If we are, check to see if it's marked executable. If it is, execute it
      under a shell. Failing that we could run xdg-open where available.

      This makes sense from an os.startfile perspective, but not for an xdg-open user. xdg-open (and shutil.whatever in my eyes) only starts an application with the passed argument; that’s it. There is no execute action because that’s done otherwise (in Python, with subprocess and os.exec*; in the shell, with program invocation (i.e. “program” or “/full/path/to/file”) using shebangs or binfmt-format). So in my mental model, using xdg-open to have a file or URI opened by a configured application (which can be graphical or command-line) has nothing to do with executing a file.

      (There was an older system on UNIX named mailcap where you could register console and GUI actions for different actions, like edit and print, but it’s not used by current graphical mail clients like Thunderbird or by the xdg-open system, so I would not consider it at all.)

    17. merwok commented on May 29, 2012

      @merwok
      Member

      Note that my suggestion has an inconvenient on Windows. Take the example of HTML files; on an UNIX system I would decide whether I want them to be opened with my text editor or my web browser; I’d choose web browser because I launch my text editor from a shell. On Windows though you would register your web browser for default action and your text editor for the edit action; this deliberate configuration would not be respected if we implement shutil.whatever with os.startfile(..., 'edit').

    18. merwok commented on May 29, 2012

      @merwok
      Member

      (A last, very minor thing: please trim or remove quoted text when replying with the email interface (or on mailing lists); it puts unneeded wall of texts that reader waste time scanning, in case there were inline replies. Thanks in advance.)

    19. HobsonLane commented on May 29, 2012

      HobsonLanemannequin
      Mannequin

      shutil.whatever with os.startfile(..., 'edit').

      That's why I thought an 'auto' option might be useful--to allow the caller
      to indicate that they want to respect the OS settings for open/run, if
      possible.

    20. ronaldoussoren commented on Jul 31, 2012

      @ronaldoussoren
      Contributor

      MacOSX provides functionality simular to os.startfile in the LaunchServices framework.

      I've tried to prototype a startfile implementation using this framework, (see bpo-3177-os_startfile_macosx.txt) but I'm not too happy about it because the APIs don't actually work as expected.

      In particular:

      • The patch implements "open" (default), "edit", "print" and
        "explore" actions.

      • "open" and "explore" work fine

      • "edit" does not work as I'd expect because the function used
        to retrieve the default editor for a file returns the default
        application that can open the file, not necessarily one that
        claims to be an editor for the file-type. As an example,
        'os.startfile("file.html", "edit")' starts Safari instead of
        an editor.

      • "print" works for some file type, but not others (depending on
        whether or not the target application implements the required
        AppleScript API)

      • "execute" is untested, I haven't found an application yet that claims
        this role.

      Other notes:

      • This patch links Python with the ApplicationServices framework
        (which contains the LaunchServices framework)

      • Because the API uses Apple's application frameworks users might get
        annoying crashes when calling this function in a subprocess.

      • The code attached code is a prototype, it should be correct w.r.t.
        resource management, but isn't fully baked yet. One example of that
        is the use of a generic RuntimeError exception to signal problems,
        others are the lack of documentation and tests.

    21. danpla commented on Jan 17, 2018

      danplamannequin
      Mannequin

      tl;dr: shutil.whatever should call startfile(..., 'edit') or xdg-open; other actions implemented by startfile are not cross-platform.

      xdg-open is just a shell script that calls desktop-specific launchers. The behavior of these launchers is very different when it comes to executables. There is no guarantee that calling xdg-open for a script will open it for editing.

      For example, there are 3 launchers on my XFCE machine: exo-open (native for XFCE), gvfs-open (GNOME), and kde-open. When called with a .py script with shebang line and executable bit set, exo-open and kde-open open text editor, but gvfs-open launches the script. When called for a real exe, exo-open and gvfs-open launch it, but kde-open brings a message box saying that the exe will not be started for safety reasons.


      The patch raises NoAssociatedApplicationError when xdg-open returns 3, but this error code actually means that a launcher was not found.

    22. amirjn commented on Feb 3, 2018

      amirjnmannequin
      Mannequin

      same problem here

    23. amirjn commented on Feb 3, 2018

      amirjnmannequin
    24. transferred this issue fromon Apr 10, 2022
    25. dbarnett commented on Sep 15, 2024

      @dbarnett

      Hello from 2024! 👋

      Any hope of getting a shutil.open implementation landed still? It looks like this issue stalled out after 2012...

      It's been discussed on a popular StackOverflow question, and there's now a universal-startfile package on PyPI attempting to offer the same thing.

    26. merwok commented on Sep 16, 2024

      @merwok
      Member

      I don’t think there is a well thought-out proposal, or a big need for this.
      Third-party modules can fill the need.

    27. dbarnett commented on Sep 16, 2024

      @dbarnett

      What's the rationale for "not a big need"? To me the popular StackOverflow questions (+ lots of dupes of it) suggest otherwise.

      I was actually considering to publish my own third-party module for it, but would prefer to make it a drop-in polyfill with whatever's planned for standard library here (and the half-working os.startfile) instead of trying to freehand a signature myself.

      @ALL could anyone help direct me to min viable behavior if I were volunteering to implement it in a module?

    28. dbarnett commented on Sep 22, 2024

      @dbarnett

      K, I've implemented a quick https://pypi.org/project/shopen/ with my understanding of what a shutil.open should offer. IMO it'd still be great to have those batteries come included with python, but for now I can iterate there on ironing out the wrinkles.

    29. zed commented on Nov 8, 2024

      @zed

      Often shutil_open = webbrowser.open works.

    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/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions