Python: several fixes (cherry picked from commit 67651d80bc)
67651d80bc
(cherry picked from commit 489bfcfa0f)
489bfcfa0f