python / python/cpython

ctypes: subclassed swapped endianness doesn't swap

オープン
#95,384 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic-ctypes type-bug
主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、c_ushort.ctype_be とサブクラスを使った最小限の ctypes の例を再現し、次に、バイトスワップされた基底型とサブクラスの from_buffer_copy の動作を比較します。ctypes におけるバイトスワップされた単純型の処理を追跡し、サブクラスの経路がどのように異なるかを特定します。サブクラスが基底型と同じバイトスワップ済みの値を生成し、回帰がテストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。