Python 2.7 data handling
- Handle
unicode, byte strings, and UTF-8 explicitly. - For source-embedded multilingual UI text, use a UTF-8 coding declaration and
from __future__ import unicode_literals. Resolve the client language throughhelpers.getClientLanguage(), normalize case and-/_, map known aliases such as Ukrainian, Russian-family, Brazilian Portuguese, and Chinese variants, then fall back from regional code to base language and finally English. - Keep every translation dictionary structurally identical to English and validate that all format placeholders can be expanded. Pass already-styled values such as hotkeys, counts, and item names through named placeholders so translations can reorder them naturally.
- For binary JSON writes in dual Python 2/3 code, encode text to UTF-8; decode bytes before parsing reads.
- Python 2 networking commonly uses
urllib2,httplib,BaseHTTPServer, andurlparse. Python 3 fallbacks may be useful for offline tooling, but must not break the client runtime. - Avoid mutable defaults such as
config_files=[]; useNoneand allocate a new collection per call. - When a zero-argument function captured from another class must be stored on a new-style
owner class, wrap it with
staticmethod(...). Storing the plain function makes later class access apply Python 2 method binding and can inject an unexpected owner argument. Restore it with the same descriptor shape.
Text limits have different units
The external string helpers implement different boundaries. normalized_unicode_trim decodes UTF-8 when needed, applies normalization (NFKC by default), slices the resulting Unicode string, then encodes it. Its length parameter is not a UTF-8 byte budget. truncate_utf8 instead expects already encoded UTF-8 and trims to a byte budget without cutting a valid multibyte sequence; it does not normalize or validate the complete input.
| Input and limit | Normalized string trim | Encoded byte trim |
|---|---|---|
UTF-8 for éé, limit 2 | éé: four bytes | é: two bytes |
UTF-8 for the ligature ffi, limit 2 | ff, after normalization expands the ligature | Empty prefix: the encoded ligature needs three bytes |
The outgoing text filter uses normalized trimming and then collapses whitespace; a request context uses the encoded-byte helper for its comment field. Preserve the chosen unit and processing order when adapting a boundary. Neither helper promises grapheme-based trimming or establishes server acceptance.
Selected readable helpers were checked under Python 3 using synthetic encoded strings and an adapter for Python 2 byte-index values. Valid multibyte prefixes, normalization-before-slicing and the differing output sizes were checked. No messages, requests, native APIs, installed bytecode or Python 2.7 client runtime were executed. Unicode-string slicing must not be generalized into one codepoint-count rule across narrow and wide Python 2 builds.