python / python/cpython

Don't rely on errno values, coming from platform's math functions

未關閉
#156,145 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

extension-modules interpreter-core type-feature
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

Proposal:

In the cmath module most functions don't check errno values. See cosh() example below, which set nonzero errno in some cases, but don't read errno (e.g. to detect overflow in libm's cosh/sinh()). Here we essentially assume, that platform implements Annex F of the C standard:
https://github.com/python/cpython/blob/04242c027feeff726acb15b6463422897b489bcf/Modules/cmathmodule.c#L441-L490

What if we extend this approach to the rest of CPython's floating-point arithmetics? Now this seems natural, as we require that platforms C double has IEEE 754 binary64 format. On practice, that means much more, i.e. that platform supports Annex F (modulo bugs). And we can utilize that, assuming that for invalid input - NaN's produced, for overflows - infinities.

For example, instead of current code of float_pow() that handle finite input
https://github.com/python/cpython/blob/04242c027feeff726acb15b6463422897b489bcf/Objects/floatobject.c#L788-L801
we could use something much more simple:

    ix = pow(iv, iw);
    if (negate_result)
        ix = -ix;
    if (isinf(ix)) {
        PyErr_SetString(PyExc_OverflowError,
                        "float exponentiation result out of range");
        return NULL;
    }

(_Py_ADJUST_ERANGE1 and _Py_ADJUST_ERANGE2 helpers will be removed.)

See recent d.p.o thread for illustration of subtle issues, that can be introduced trying to fix errno, coming from platforms functions. We also have occurring bugreports, when platform libm set errno wrongly, e.g. https://github.com/python/cpython/issues/153144.

Disclaimer: this issue filled, based on my humble understanding of the proposal by @mdickinson. It seems, there are volunteers to work on this (CC @hpkfft), hence issue was opened.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/108539/

Linked PRs
  • gh-156551

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先閱讀提案和連結的 Discourse 討論,然後檢查 Modules/cmathmodule.c、Objects/floatobject.c 和 Include/internal/pycore_pymath.h。將現有的 errno 處理和調整輔助函式與連結的 platform-libm 錯誤報告進行比較;完成這項工作需要針對所有受影響的算術運算,決定一種一致且可測試的浮點錯誤處理方案。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
c, python
領域
operating-systems
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。