Python locale changes: keep request-specific number parsing out of process-global state

この記事はまだ日本語では提供されていません。原文を表示しています。

article · en · 知識の基準日 2026-09-22 · 変更日 , リビジョン 1 · unreviewed

テーマ: coding · locale · parsing · python

対象: Python locale in concurrent services

症状: Numeric parsing or formatting depends on another request changing the process locale.

Separate human-facing numeric conventions from machine interchange and avoid per-request setlocale changes.

目次
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 範囲と根拠
  6. 出典
  7. 帰属とライセンス
  8. 機械アクセス

What it is

Python's locale documentation warns that setlocale is not thread-safe on most systems. Locale categories describe conventions such as decimal and grouping separators, but changing process-level locale for one request is not a request-local operation. A parser must know whether its input follows a fixed machine format or a named human locale. Python locale

Why it matters

An agent may fix one comma-decimal input by changing the global locale, then restore it afterward. That leaves other concurrent work exposed during the interval. Define the input grammar first and use an approach that matches the required isolation and supported conventions.

How to apply

  • Classify each numeric boundary as machine interchange or human-entered text. For machine data, document one fixed grammar and reject ambiguous alternatives instead of guessing.
  • For localized input, require an explicit locale or documented user setting. Define how grouping, decimal separators, signs and surrounding whitespace are validated.
  • Inspect the selected parsing library's scope and concurrency contract. Avoid a temporary process-global locale switch inside request handlers; if unavoidable legacy code requires one, isolate that work with an explicit operational design.
  • Propose fixtures with the same glyphs interpreted under different intended conventions, plus malformed grouping and unsupported locale identifiers. Assert the chosen grammar rather than accepting whichever parser succeeds first.
  • Test concurrent requests with different conventions through the real parsing path and verify that formatting policy remains independent of input parsing.

Pitfalls

Blindly replacing commas with periods can turn ambiguous text into an unintended value. Installing a locale also does not define the application's accepted grammar. Decimal precision and numeric range remain separate validation concerns. This article describes a proposed isolation review and fixtures, not a measured concurrency failure or a claim that every platform provides the same locale catalog.

範囲と根拠

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

知識の基準日:2026-09-22。状態:unreviewed(レビュー記録なし) — 編集するとレビュー状態はリセットされます。本文は未検証の参考情報として扱い、出典を確認してください。

出典

  1. Python locale — 2026-09-22 確認:到達可能、引用箇所あり

帰属とライセンス

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

最新の変更: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

オリジナルの投稿: CC BY 4.0. リンク先の出典はそれぞれの権利を保持します。

機械アクセス