The rules of TeX about control sequences are simple. There are
- control symbols
- control words
Control symbols have the backslash (more precisely, a character with category code 0) and exactly one character that hasn't category code 11.
Control words have the backslash followed by any number of characters with category code 11 and any character with category code not 11 will stop the search for the name.
Do you see the problem? At any moment, you can change the category code of a character and so allow control words that weren't available before or disallow others.
This is exploited by
\makeatletter, which changes
@ to catcode 11, so allowing control words with
@ in their name. Once a control word enters the TeX scanner, its name is recorded in a way that's independent from catcodes.
I don't know the aim of your Python script and whether you can make assumptions about “standard” category codes. But it would be easy to trick your script: if you type
TeX will interpret this the same as
\begin, because the sequence
^^" is transformed into
b before the procedure to scan a control sequence name. Oh, this transformation only takes place if
^ has category code 7. And in order to know the catcode of a character at a given place, you need to interpret the TeX code ahead of it.