linecache.cache and linecache.getlines() are not documented
@picnixz ya está trabajando en esto.
Desde el 2/9/2026.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
Documentation
linecache.cache is not underscored, and is widely used throughout the python ecosystem to provide source context for dynamically generated Python functions. Examples include attrs
as well as ipython:
a code search on github reports over 700 files accessing this attribute directly and assuming it's a dict:
https://github.com/search?q=linecache.cache+language%3APython&type=code&p=1
the utility of linecache.cache is significant, in that it allows us to provide real source lines for generated code that comes up in stacktraces, and which is also walkable in pdb. The linecache.lazycache() documented API almost provides this functionality, but is explicitly blocked for string-oriented "filenames" like <string>.
Without linecache.cache:
broken_function="""\
def go():
print("step1")
print("step2")
x = 1 / 0
print("step3")
"""
code = compile(broken_function, "<custom go() function>", "exec")
exec(code, env := {})
env["go"]()
this code produces a stack trace that sees the generated function as opaque:
$ python test3.py
step1
step2
Traceback (most recent call last):
File "/home/classic/dev/sqlalchemy/test3.py", line 14, in <module>
env["go"]()
~~~~~~~~~^^
File "<custom go() function>", line 4, in go
ZeroDivisionError: division by zero
however add linecache and suddenly we get much more detail:
import linecache
broken_function = """\
def go():
print("step1")
print("step2")
x = 1 / 0
print("step3")
"""
filename = "<custom go() function>"
code = compile(broken_function, filename, "exec")
entry = (
len(broken_function),
None,
broken_function.splitlines(True),
filename,
)
linecache.cache[filename] = entry
exec(code, env := {})
env["go"]()
now we get introspection into the generated function itself:
$ python test3.py
step1
step2
Traceback (most recent call last):
File "/home/classic/dev/sqlalchemy/test3.py", line 24, in <module>
env["go"]()
~~~~~~~~~^^
File "<custom go() function>", line 4, in go
x = 1 / 0
~~^~~
ZeroDivisionError: division by zero
The None used as the second entry of the tuple is so that checkcache() skips asserting that the filename is actually a file; this is commented by the ipython project in the link above.
There is one documented API that is close to this use case linecache.lazycache(), but it's explicitly disallowed from working with the universal convention for generated code, a name in angle brackets ( see https://github.com/python/cpython/blob/af4ee512e97a33d34359695b64ec1e31e8ce44d1/Lib/linecache.py#L224-L226), and also for this use case would require us to fabricate a loader object with a get_source() method and a module_globals dict carrying __name__.
from my perspective it seems that the behavior of compile() could be improved directly by providing options to populate the linecache, or alternatively that lazycache() could accommodate the angle-bracket names that generated code actually uses, however neither seems to be an option today. Hence we see widespread direct manipulation of linecache.cache, and this technique has by now escaped containment and is being recommended by robots such as Claude.
I am seeking clarification on why this attribute has remained undocumented for so long, if this is in fact public API, or if it's in some kind of "public, but we really don't like people doing this" sort of limbo (and if so, is there an opening for actual features to be proposed).
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 36k
- Merge medio
- 1 d 9 h
- PR fusionados (30 d)
- 558
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python/cpython
-
docs pending
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
stdlib type-feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
stdlib type-feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
build type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
stdlib topic-email type-feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Todos los issues de python/cpython
Issues similares
-
from:qa priority:P2 reliability tech-debt
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
spec-kitty/spec-kitty#4874 ·
-
fix: inaccuracy ⚠️
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
uabrc/uabrc.github.io#1255 · 1 comentario ·
-
kind:bug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
-
docs
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
ethereum-optimism/factory#64 ·