Is there a way to determine whether code is toplevel?

Yes, you can set a gensym variable and test if it is indeed set on __module__.

2 Likes