fix(task-16): custom keys follow AutoCAD's simple case compare, and on COM AutoCAD routes the write

4fe2d96 matched custom keys with str.casefold(), but AutoCAD's key compare
is a simple one-to-one case compare, not Unicode full case folding.
Measured on AutoCAD 2026 (AddCustomInfo of the second spelling over the
first on a scratch document, Close(False)): Project/PROJECT, Grün/GRÜN,
é/É, ÿ/Ÿ, ł/Ł, σ/Σ, я/Я and dž/DŽ are one key ('Duplicate key'), while
Straße/STRASSE, ẞ/ß, kelvin/Kelvin-sign, µ/Μ, σς/ΣΣ and Dž/DŽ are two keys
(both stored) -- every one of those six is casefold()-equal. So with
'Straße' present, `custom={'BEFORE': 'b', 'STRASSE': 's2', 'AFTER': ...}`
was routed to SetCustomByKey, AutoCAD answered 'Key not found', and the
request raised with subject and BEFORE applied and AFTER never written --
the half-write class the fix claimed to close; a delete of 'STRASSE' over
'Straße' raised the same way; headlessly 'STRASSE' overwrote the 'Straße'
value instead of becoming a second tag; and the validator refused
{'Straße', 'STRASSE'} with a reason AutoCAD contradicts.

COM no longer routes from a Python approximation. An exact spelling goes
to SetCustomByKey; anything else is AddCustomInfo, and its 'Duplicate key'
(excepinfo scode -2145386475, measured) falls back to SetCustomByKey. A
delete is RemoveCustomByKey, and its 'Key not found' (scode -2145386476)
is reported as not deleted. Any other com_error keeps propagating. The
scode is matched before the English description because the HRESULT text
is localised.

The headless engine and the validator match on `custom_key_fold`: a
per-character fold to lowercase only when lower()/upper() are single
characters that round-trip and the character is one of the two; it never
changes string length and reproduces every measured pair (ß, ς, µ, the
Kelvin sign, Dž, ı and İ stay as they are). Docstrings in settings.py and
server.py now state that rule instead of 'case-insensitive'.

Tests: parametrized fold over the measured SAME / DISTINCT table (plus a
pin that casefold() merges the six DISTINCT pairs, which is why 4fe2d96
regressed), validator acceptance of each DISTINCT pair, headless
side-by-side storage and deletion of each pair, the fake SummaryInfo now
matches by the same fold and raises pywintypes.com_error with the
measured excepinfo tuples, the COM half-write scenario for each pair
(no SetCustomByKey guess, 'Key not found' delete tolerated, rest of the
request lands), the 'Duplicate key' fallback, and an unrelated
SummaryInfo failure still raising. The scodes were measured live before
the change; AutoCAD stopped answering COM (blocked in another agent's
command or a modal dialog) before the post-change re-run, so that run
is recorded as not done.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
This commit is contained in:
Umut CAN
2026-09-21 22:04:22 +03:00
co-authored by Claude Opus 5
parent 4fe2d963c0
commit dda11755cf
5 changed files with 297 additions and 55 deletions
+47 -14
View File
@@ -199,6 +199,27 @@ _BUILTIN_LINETYPES = {"continuous", "bylayer", "byblock"}
#: `drawing_properties_*` field → `IAcadSummaryInfo` property.
_SUMMARY_ATTRS = {field: field.capitalize() for field in SUMMARY_FIELDS}
#: `IAcadSummaryInfo` failure codes, as the `scode` in a `com_error`'s
#: excepinfo (measured on AutoCAD 2026; the description next to them is
#: English while the HRESULT text is localised, so the code is matched first).
_SUMMARYINFO_DUPLICATE_KEY = -2145386475 # AddCustomInfo: 'Duplicate key'
_SUMMARYINFO_KEY_NOT_FOUND = -2145386476 # Set/RemoveCustomByKey: 'Key not found'
def _summaryinfo_error_is(exc: BaseException, scode: int, description: str) -> bool:
"""True when `exc` is the `IAcadSummaryInfo` failure `scode` / `description`.
`com_error.args[2]` is the excepinfo tuple ``(wCode, source, description,
helpfile, helpcontext, scode)``; a missing or foreign excepinfo is never a
match, so an unrelated COM failure keeps propagating.
"""
info = exc.args[2] if len(exc.args) > 2 else None
if not isinstance(info, tuple):
return False
code = info[5] if len(info) > 5 else None
desc = str(info[2] or "") if len(info) > 2 else ""
return code == scode or desc.strip().casefold() == description.casefold()
def _ensure_linetype_loaded(name: str) -> None:
"""If `name` is not already loaded, load it via -LINETYPE behind FILEDIA=0.
@@ -4293,31 +4314,43 @@ class ComBackend(AutoCADBackend):
def _sync():
info = _acad_doc().SummaryInfo
# AutoCAD's custom keys are case-insensitive (measured on 2026:
# AddCustomInfo("PROJECT") over an existing "Project" raises
# 'Duplicate key'; SetCustomByKey / RemoveCustomByKey match either
# spelling and the stored spelling is kept), so existence is
# checked casefolded — a case-sensitive check routed "PROJECT" to
# AddCustomInfo and failed mid-write after the summary fields and
# the earlier keys were already applied.
# AutoCAD matches custom keys by its own simple per-character case
# compare (measured on 2026: AddCustomInfo("PROJECT") over an
# existing "Project" is 'Duplicate key', yet "Straße" and
# "STRASSE" are two keys -- Python's casefold() merges them, and a
# routing built on it sent "STRASSE" to SetCustomByKey, which
# failed 'Key not found' after the summary fields and the earlier
# keys were already applied). So only an exact spelling is routed
# from here; for everything else AutoCAD decides: AddCustomInfo's
# 'Duplicate key' means the key is there under another spelling
# and it is set instead, RemoveCustomByKey's 'Key not found' means
# it is not there and the delete is reported as not done. No
# routing guess can then half-write.
existing: set[str] = set()
for index in range(int(info.NumCustomInfo())):
key, _value = info.GetCustomByIndex(index)
existing.add(str(key).casefold())
existing.add(str(key))
for field, value in written.items():
setattr(info, _SUMMARY_ATTRS[field], value)
for key, value in to_write.items():
# AddCustomInfo on an existing key raises in AutoCAD; SetCustomByKey
# on a missing one does too — route by what is really there.
if key.casefold() in existing:
if key in existing:
info.SetCustomByKey(key, value)
else:
continue
try:
info.AddCustomInfo(key, value)
except _COM_ERROR as exc:
if not _summaryinfo_error_is(exc, _SUMMARYINFO_DUPLICATE_KEY, "Duplicate key"):
raise
info.SetCustomByKey(key, value)
deleted = []
for key in to_delete:
if key.casefold() in existing:
try:
info.RemoveCustomByKey(key)
deleted.append(key)
except _COM_ERROR as exc:
if not _summaryinfo_error_is(exc, _SUMMARYINFO_KEY_NOT_FOUND, "Key not found"):
raise
continue
deleted.append(key)
return {
"ok": True,
"summary_written": sorted(written),