py: Clean up edge cases of malloc/realloc/free.