Move module constants into cdef

This also has two unrelated commits (f12e4a2 and f1ff790), but those are too tiny for their own pull request.