Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uname and other os functions should return a struct sequence instead of a tuple #59323

Closed
larryhastings opened this issue Jun 20, 2012 · 12 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@larryhastings
Copy link
Contributor

BPO 15118
Nosy @birkenfeld, @terryjreedy, @jcea, @larryhastings, @merwok, @hynek, @serhiy-storchaka
Files
  • larry.uname.and.times.structseq.1.diff: First patch, adding structseq for os.uname() and os.times().
  • larry.uname.and.times.structseq.2.diff
  • larry.uname.and.times.structseq.3.diff: Third patch, chock full of nutritious vitamins. Just the thing a growing snake needs.
  • 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/larryhastings'
    closed_at = <Date 2012-06-24.11:35:14.793>
    created_at = <Date 2012-06-20.20:12:40.609>
    labels = ['type-bug']
    title = 'uname and other os functions should return a struct sequence instead of a tuple'
    updated_at = <Date 2012-06-24.11:35:14.792>
    user = 'https://github.com/larryhastings'

    bugs.python.org fields:

    activity = <Date 2012-06-24.11:35:14.792>
    actor = 'larry'
    assignee = 'larry'
    closed = True
    closed_date = <Date 2012-06-24.11:35:14.793>
    closer = 'larry'
    components = []
    creation = <Date 2012-06-20.20:12:40.609>
    creator = 'larry'
    dependencies = []
    files = ['26065', '26121', '26123']
    hgrepos = []
    issue_num = 15118
    keywords = ['patch']
    message_count = 12.0
    messages = ['163295', '163301', '163302', '163304', '163317', '163537', '163609', '163698', '163714', '163715', '163774', '163775']
    nosy_count = 9.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'jcea', 'larry', 'eric.araujo', 'Arfrever', 'python-dev', 'hynek', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue15118'
    versions = ['Python 3.3', 'Python 3.4']

    @larryhastings
    Copy link
    Contributor Author

    The trend in the standard library is to get rid of awkward Python-1-style tuple return values and switch to struct sequences. (And perhaps, in the fullness of time, to deprecate the iterability of such objects. But that's for the future.)

    os.stat is a good example; it's much better to say s = os.stat() then refer to s.st_mtime than s[5] (or whatever the offset is). And doing destructuring assignment... ptui!

    I just noticed that the following functions in Modules/posixmodule.c still use BuildValue to build raw tuples:

    _getdiskusage
    _getfileinformation
    forkpty
    getloadavg
    getresgid
    getresuid
    openpty
    os2_error (can't get excited about this one)
    pipe, pipe2
    times
    uname
    wait, waitpid
    wait3, wait4 (but one of the values is a struct sequence)

    I think it'd be worthwhile to change all of these to struct sequences, sooner or later.

    I realize we're almost out of time for 3.3, but perhaps we could hit the important ones (uname! times!) and get to the rest for 3.4?

    @jcea
    Copy link
    Member

    jcea commented Jun 21, 2012

    +1.

    @merwok
    Copy link
    Member

    merwok commented Jun 21, 2012

    os._getdiskusage is an internal helper for shutil.getdiskusage on Windows, which does return a named tuple. The private C function can continue returning a tuple IMO.

    +1 on changing uname for 3.3! No opinion on times; I don’t use it and don’t understand the member names anyway :)

    @merwok merwok changed the title uname &c should return a struct sequence instead of a tuple uname and other os functions should return a struct sequence instead of a tuple Jun 21, 2012
    @serhiy-storchaka
    Copy link
    Member

    Agree for changing times() and uname(). But functions that return a 2- or 3-element tuple, which immediately will be unpacked, no need to change. Keep it simple.

    @larryhastings
    Copy link
    Contributor Author

    Patch implementing struct sequences for os.uname() and os.times(). Those two are a slam dunk, so let's try and get 'em into 3.3.

    Patch includes docs! Maybe it's ready! Who knows!

    @terryjreedy
    Copy link
    Member

    (OT, but since you brought it up: In my opinion, deprecating the iterability of any builtin class is a horrible idea. It is a Python feature, especially in 3.x, that all *are* iterable. However, I would agree that named tuples should be iterable by name-object pairs, just like dicts. Position is not the real key.)

    @larryhastings
    Copy link
    Contributor Author

    OT, but since you brought it up: In my opinion, deprecating the
    iterability of any builtin class is a horrible idea. It is a
    Python feature, especially in 3.x, that all *are* iterable.

    As you say, OT. But I don't see how it's a feature. Destructuring assignment is opaque (what was the order of fields again?), and with named attributes almost always unnecessary. And I find it hard to believe that there's a good use case for iterating over the values in a loop.

    I don't propose deprecating the iterability of these structures simply because I think it's inappropriate in a point release. But I hope to remove that misfeature in Python 4.

    (If you wish to continue the discussion, perhaps we should take it somewhere else?)

    @larryhastings
    Copy link
    Contributor Author

    Okay, this patch is definitely ready. Changes from the last patch:

    • The two return types are now exposed in the os module (os.uname_result
      and os.times_result) and are permanently on even if os.uname() or
      os.times() are not available.
    • I updated the library to use the field names where appropriate.
      Fun trivia: there are no uses of os.times() in the library--not even
      in the regression test suite.

    Of course, with the patch applied python passes the entire regression test suite. (Of course!)

    Can I get a review?

    @larryhastings
    Copy link
    Contributor Author

    Attached is patch #3. Ned Deily advised me not to touch distutils, and packaging is in the process of being removed. So I backed out of my changes to those two packages. Apart from that the patch is the same as #2.

    Georg: mind if this goes in before freeze?

    @larryhastings
    Copy link
    Contributor Author

    Whoops, here's the patch.

    @larryhastings larryhastings self-assigned this Jun 24, 2012
    @larryhastings larryhastings added the type-bug An unexpected behavior, bug, or error label Jun 24, 2012
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 24, 2012

    New changeset 460407f35aa9 by Larry Hastings in branch 'default':
    Issue bpo-15118: Change return value of os.uname() and os.times() from
    http://hg.python.org/cpython/rev/460407f35aa9

    @larryhastings
    Copy link
    Contributor Author

    Yummy!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants