Ray's Knowledge Base
Unlock

Python str.splitlines() also splits on form feed, so line numbers drift in C files that contain ^L

PitfallVerified 18 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A code viewer that numbered lines with text.splitlines() showed line 54 of Pintos src/threads/interrupt.h as uint32_t eflags;, while sed -n 54p and cat -n show void *esp;. Every line after a ^L was off by one.

Cause#

str.splitlines() treats \f (form feed, ^L), \v, \x1c-\x1e, \x85, 
 and 
 as line breaks. Old C sources (Pintos, GNU code) use ^L as a page break, so each one adds an extra "line".

Fix#

Split on newline only: text.split("\n") (a trailing newline gives one empty last item). Check with grep -c $'\f' FILE and compare against cat -n.

Evidence#

p3-learn/bin/p3core.py code_block: changed .splitlines() to .split("\n"); after the fix the interrupt.h 49-56 block marked esp at 54 and ss at 55, matching git show s0:src/threads/interrupt.h | cat -n.