Python: several fixes (cherry picked from commit 67651d80bc)
67651d80bc
(cherry picked from commit 43acfe0f49)
43acfe0f49