python / python/cpython

datetime: Stop Exposing Process-Global Objects in the datetime C-API

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

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

extension-modules type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

The datetime module has its own C-API which is enabled with PyDateTime_IMPORT. From the docs:

Before using any of these functions, the header file datetime.h must be included in your source
(note that this is not included by Python.h), and the macro PyDateTime_IMPORT must be invoked,
usually as part of the module initialisation function. The macro puts a pointer to a C structure into
a static variable, PyDateTimeAPI, that is used by the following macros.

My main concern is that the PyDateTimeAPI struct is a process-global value, but it exposes object pointers (which should always be per-interpreter). We have worked around this in 3.13+, but it would be better if we could make the objects per-interpreter.

FTR, here are the objects exposed directly by PyDateTimeAPI:

  • (static type) PyDateTime_DateType
  • (static type) PyDateTime_DateTimeType
  • (static type) PyDateTime_TimeType
  • (static type) PyDateTime_DeltaType
  • (static type) PyDateTime_TZInfoType
  • (singleton) utc_timezone (an instance of PyDateTime_TimeZoneType)

exposed indirectly:

  • (static type) PyDateTime_TimeZoneType

In order to make these objects per-interpreter, it would require changes to the datetime C-API. [^1] I expect we would leave PyDateTime_IMPORT alone. Instead, we'd need to update the macros in datetime.h to get the objects from the module associated with the current interpreter. [^2]

[^1]: These would be ABI-incompatible changes, which wouldn't be a problem unless the datetime C-API is part of the limited API).
[^2]: Anyone who is accessing the PyDateTimeAPI struct directly would have to change their code. It might make sense to provide a getter function/macro for each of the objects.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

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

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

はじめの一歩

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

調査の方向性

まず datetime C-API のドキュメントと、提案で説明されている datetime.h マクロを確認し、次に PyDateTimeAPI が列挙された型と utc_timezone をどのように公開しているかを調べます。これらのオブジェクトがインタープリターごとに存在し、PyDateTime_IMPORT は変更されないまま、必要な getter または直接アクセスの変更が文書化され、テストされていれば完了です。

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

評価

技術スタック
c, python
領域
api
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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