python / python/cpython

object.c inconsistently truncates tp_name in exception error messages, hampering debugging

未关闭
#101,964 4 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@gpshead 已经在做这个了。

开始于 2023年2月16日。

interpreter-core type-feature
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

描述

Objects/object.c places tp_name into TypeError and AttributeError exception messages via an inconsistent mess of %.50s, %.100s, and %.200s PyErr_FromFormat format string specifiers.

We butt heads with this at work routinely. Especially on the mere 50 character limit cases. When wrapping C++ classes within a deep directory tree or namespace the .tp_name of the class your pybind11 or otherwise generated extension module code will use can easily be longer than that.

We're going ahead with a local patch to make these consistent with a limit of 200 internally for now, which I'd like to apply here.

But the question arises: What practical reason do we limit the rendered size of tp_name for at all?

Truncating is always the wrong thing to do for information within an error message, the beginning and end of a name string are usually the most unique deterministic parts useful as a debugging aid. If we are to truncate at all rather than just use an unbounded %s format for tp_name we should elide characters from the middle with a ... placeholder. If we don't have a format specifier that can do that for PyUnicodeObject, one would need to be added.

Is there ever a situation where a C PyTypeObject's tp_name can be assigned to an arbitrary length string at runtime?

(There may be places outside of object.c doing this with tp_name as well, I consider finding those and being consistent in approach there part of this issue)


This theme has come up in different areas in the past, but those were for general str/repr of things which are larger cans of worms. Not the tp_name field. For example #57558 and other things generally mentioned via #51579 which added our length specifiers to PyUnicode formatting.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。