Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python getpass encoding

Imagine I have the following code:

  # -*- coding: utf-8 -*-
  from getpass import getpass

  #print u'Введите пароль!'.encode('cp866')
  passwd = getpass (u'Введите пароль!'.encode('cp866'))

This is for asking user to enter his password in Windows console (thus encoding is 'cp866'). The user sees the following prompt: "???¤?a? ? aR?i!" But if you uncomment the line with print, you will see the correct text. I already have a workaround, first make a print statement, then issue getpass with empty prompt, but I just want to know what exactly is wrong with my code and why do I get this result? One hint if it make it clearer: getpass uses msvcrt.putch (char) to put characters on the console.

like image 988
Graf Avatar asked Nov 14 '22 06:11

Graf


1 Answers

putch() might be doing its own translation from your ANSI codepage (cp1251) to your OEM codepage (cp866). Try encoding with cp1251 instead.

like image 65
kichik Avatar answered Dec 02 '22 12:12

kichik