ctypes: subclassed swapped endianness doesn't swap

未關閉
#95,384 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
python
領域
backend

研究方向

首先使用 c_ushort.ctype_be 和一個子類別重現最小 ctypes 範例,然後比較位元組交換後的基底型別與子類別的 from_buffer_copy 行為。追蹤 ctypes 對位元組交換簡單型別的處理,並判斷子類別路徑有何不同;當子類別產生與基底型別相同的位元組交換值,且回歸問題由測試涵蓋時,即表示完成。

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

描述

topic-ctypes type-bug

Bug report

Subclassing a swapped simple type and calling from_buffer_copy doesn't swap bytes. Here is a minimal non-working example.

import ctypes

cshort_be = ctypes.c_ushort.__ctype_be__
class Short(cshort_be):
    pass

print(cshort_be.from_buffer_copy(b"\x00\x01").value) # prints 1
print(Short.from_buffer_copy(b"\x00\x01").value) # prints 256

However, there is a simple workaround:

print(Short.__ctype_be__.from_buffer_copy(b"\x00\x01").value) # prints 1

I am not sure what happens, but ctypes behave kinda weird when subclassing native types. So this bug might be related to issue #73456.

My environment

  • CPython versions tested on: 3.10.5
  • Operating system and architecture: Linux Celap 5.18.0-1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 5.18.2-1 (2022-06-06) x86_64 GNU/Linux
主要語言
Python
星號
77.2k
分支
36k
平均合併
1 天 9 小時
30 天內合併 PR
558

貢獻指南

開啟貢獻指南

從這裡開始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

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

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