python/cpython

Add PyFloat_(Pack|Unpack)(2|4|8) to the public C API

Closed this issue · 12 comments

BPO 46906
Nosy @malemburg, @rhettinger, @mdickinson, @vstinner, @methane
PRs
  • #31649
  • #31657
  • #31832
  • #31866
  • 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 2022-03-14.15:58:36.669>
    created_at = <Date 2022-03-03.03:09:32.087>
    labels = ['expert-C-API', '3.11']
    title = 'Add PyFloat_(Pack|Unpack)(2|4|8) to the public C API'
    updated_at = <Date 2022-03-14.15:58:36.668>
    user = 'https://github.com/methane'

    bugs.python.org fields:

    activity = <Date 2022-03-14.15:58:36.668>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-03-14.15:58:36.669>
    closer = 'vstinner'
    components = ['C API']
    creation = <Date 2022-03-03.03:09:32.087>
    creator = 'methane'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46906
    keywords = ['patch']
    message_count = 12.0
    messages = ['414401', '414410', '414411', '414416', '414419', '414427', '414944', '414952', '414953', '414954', '415151', '415152']
    nosy_count = 6.0
    nosy_names = ['lemburg', 'rhettinger', 'mark.dickinson', 'vstinner', 'stutzbach', 'methane']
    pr_nums = ['31649', '31657', '31832', '31866']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue46906'
    versions = ['Python 3.11']

    Original issue. msgpack/msgpack-python#497

    _PyFloat_(Pack|Unpack)(4|8) is very nice API for serializers like msgpack.
    Converting double and float into char[] is not trivial and these APIs do it in very efficient way.

    And these APIs don't reveal CPython internal strucutre. It just convert double and float into char[].

    So please keep these APIs public for libraries like msgpack.

    I disagree. A function should be either fully public: tested, documented. Or fully internal.

    In the past, many functions were in both, in the gray area.

    If these functions are useful, please make them *public* and document them. I'm +1 to make these functions public.

    OK. By quick grepping, I found only msgpack and bitstruct use these API.
    It is not enough number to make them public.

    I reopen the issue, I would like to make these functions public :-) I searched for the 6 functions moved to the internal C API and I found 6 projets using it in the top 5000 PyPI projects (at 2022-01-26):

    • msgpack
    • ddtrace
    • bitstruct
    • pickle5
    • line_profiler
    • zodbpickle

    The 6 functions are used:

    • _PyFloat_Pack2()
    • _PyFloat_Pack4()
    • _PyFloat_Pack8()
    • _PyFloat_Unpack2()
    • _PyFloat_Unpack4()
    • _PyFloat_Unpack8()

    The pack/unpack functions have been moved to the internal C API by this change:

    commit 0a883a7
    Author: Victor Stinner <vstinner@python.org>
    Date: Thu Oct 14 23:41:06 2021 +0200

    bpo-35134: Add [Include/cpython/floatobject.h](https://github.com/python/cpython/blob/main/Include/cpython/floatobject.h) (GH-28957)
    
    Split [Include/floatobject.h](https://github.com/python/cpython/blob/main/Include/floatobject.h) into sub-files: add
    [Include/cpython/floatobject.h](https://github.com/python/cpython/blob/main/Include/cpython/floatobject.h) and
    [Include/internal/pycore_floatobject.h](https://github.com/python/cpython/blob/main/Include/internal/pycore_floatobject.h).
    

    I prepared a pythoncapi_compat PR to provide these functions to Python 3.10 and older:
    python/pythoncapi-compat#26

    Note: bpo-11734 (commit 7c4e409) added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6.0b1.

    New changeset 882d809 by Victor Stinner in branch 'main':
    bpo-46906: Add PyFloat_Pack8() to the C API (GH-31657)
    882d809

    PR for bitstruct: eerimoq/bitstruct#26

    New changeset 11c25b8 by Victor Stinner in branch 'main':
    bpo-46906: Mention native endian in PyFloat_Pack8() doc (GH-31866)
    11c25b8

    msgpack and bitstruct use the newly added functions: my two PRs got merged. msgpack was my main motivation to add these functions :-)

    Thanks to great reviews, the functions got a new better documentation!

    I close the issue. Thanks again for reviews!