From 2cf604384b5e795321968b716d2bb3759779797b Mon Sep 17 00:00:00 2001 From: Yu Feng Date: Mon, 9 Jun 2025 08:53:48 -0700 Subject: [PATCH 1/2] Fix assert Crash when code size is 0 I encountered a crash from the asertion line (e.g. 1240) when running a Cython generated extension module (grpc-python, which creates async def functions) under PYTHONDEBUGMODE. It appears that the co object created by Cython in this case has a zero length code bytes -- which probably made sense because there is no python bytecode. Assuming Cython is correct in reporting a non-zero sized co object in this case, I think we can short circuit the Addr* functions for zero sized co objects. --- Objects/codeobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index ee869d991d93cd..5e1c79ed7f03c4 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1015,7 +1015,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) int PyCode_Addr2Line(PyCodeObject *co, int addrq) { - if (addrq < 0) { + if (addrq < 0 || _PyCode_NBYTES(co)) { return co->co_firstlineno; } if (co->_co_monitoring && co->_co_monitoring->lines) { @@ -1232,7 +1232,7 @@ PyCode_Addr2Location(PyCodeObject *co, int addrq, int *start_line, int *start_column, int *end_line, int *end_column) { - if (addrq < 0) { + if (addrq < 0 || _PyCode_NBYTES(co) == 0) { *start_line = *end_line = co->co_firstlineno; *start_column = *end_column = 0; return 1; From 2e2d81aceb2b229cdd4f7fe7f5f3546c61636aac Mon Sep 17 00:00:00 2001 From: Yu Feng Date: Mon, 9 Jun 2025 09:05:31 -0700 Subject: [PATCH 2/2] Actually the size is 8 bytes. --- Objects/codeobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 5e1c79ed7f03c4..8fda0a666f6e6f 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1015,7 +1015,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) int PyCode_Addr2Line(PyCodeObject *co, int addrq) { - if (addrq < 0 || _PyCode_NBYTES(co)) { + if (addrq < 0 || addrq == _PyCode_NBYTES(co)) { return co->co_firstlineno; } if (co->_co_monitoring && co->_co_monitoring->lines) { @@ -1232,7 +1232,7 @@ PyCode_Addr2Location(PyCodeObject *co, int addrq, int *start_line, int *start_column, int *end_line, int *end_column) { - if (addrq < 0 || _PyCode_NBYTES(co) == 0) { + if (addrq < 0 || addrq == _PyCode_NBYTES(co)) { *start_line = *end_line = co->co_firstlineno; *start_column = *end_column = 0; return 1;