Skip to content

Commit f4a61f8

Browse files
add Py_UNREACHABLE() CPython macro Easter egg
Add "Unreachable State" entry documenting some funny error messages in CPython's Py_UNREACHABLE() macro, including the `RANDALL_WAS_HERE` build flag referencing xkcd author Randall Munroe.
1 parent 91491ed commit f4a61f8

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,32 @@ Hello World!
213213
>>> import __phello__.ham.eggs
214214
```
215215

216+
### 17. Unreachable State[^unreachable-state]
217+
This is a macro in CPython (Python's reference implementation) source code used to mark code paths that should never execute.
218+
219+
```c
220+
#if defined(RANDALL_WAS_HERE)
221+
# define Py_UNREACHABLE() \
222+
Py_FatalError( \
223+
"If you're seeing this, the code is in what I thought was\n" \
224+
"an unreachable state.\n\n" \
225+
"I could give you advice for what to do, but honestly, why\n" \
226+
"should you trust me? I clearly screwed this up. I'm writing\n" \
227+
"a message that should never appear, yet I know it will\n" \
228+
"probably appear someday.\n\n" \
229+
"On a deep level, I know I'm not up to this task.\n" \
230+
"I'm so sorry.\n" \
231+
"https://xkcd.com/2200")
232+
#elif defined(Py_DEBUG)
233+
# define Py_UNREACHABLE() \
234+
Py_FatalError( \
235+
"We've reached an unreachable state. Anything is possible.\n" \
236+
"The limits were in our heads all along. Follow your dreams.\n" \
237+
"https://xkcd.com/2200")
238+
// ...
239+
#endif
240+
```
241+
216242
[^hello-world]: Easiest hello world program in a language without calling any function
217243
[^the-classic]: Each and every line is the philosophy of Python's design and is a supreme holy guide
218244
[^missing-zen]: Maybe just to show that there always should be a new line at the end of a file!
@@ -228,6 +254,7 @@ Hello World!
228254
[^inpynite]: [Source](https://www.reddit.com/r/Python/comments/6wrd8t/nice_lil_easter_egg_i_suppose/).
229255
[^peg-parser]: See [this answer](https://stackoverflow.com/a/65487013/14362510)
230256
[^breakfast-of-champions]: [Frozen modules](https://github.com/python/cpython/blob/main/Python/frozen.c) used for testing and is a reference to Monty Python's [SPAM sketch](https://cs.stanford.edu/people/eroberts/cs181/projects/spam/sketch.HTML)
257+
[^unreachable-state]: `Py_UNREACHABLE()` is a macro in CPython's [Include/pymacro.h](https://github.com/python/cpython/blob/main/Include/pymacro.h#L243-L270) used to mark code paths that should never execute. The `RANDALL_WAS_HERE` build flag refers to Randall Munroe, the xkcd webcomic author. The error messages that should never be printed are a reference to this [xkcd comic](https://xkcd.com/2200).
231258

232259
## Add more
233260

0 commit comments

Comments
 (0)