pycharm下getpass.getpass()卡住


pycharm下getpass.getpass()卡住不運行是什么問題

首先聲明 下面這幾行代碼在命令行和eclipse下都能正常運行

import getpass
print "nihao1"
d = getpass.getpass()
print "nihao2"
print "d=", d
運行程序只是打印nihao1,然后可以不停的輸入,就是不繼續往下運行了,程序一直卡在這里,我是windows系統

然后我查看getpass模塊的源碼,發現getpass.getpass()的調用的是getpass.win_getpass(), 在這個方法中有一行c=msvcrt,getch().程序就是卡在這里不動了。
我能找到的問題就到這了,,接下來也不知道該怎么解決 ,求大神啊!!!

PS:
剛才又研究了一下源碼,win_getpass()方法源碼:里面的print語句是我自己調的時候加上的。
def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
print "hahaha i am in "
if sys.stdin is not sys.__stdin__:
print "if sys.stdin is not sys.__stdin__:"
return fallback_getpass(prompt, stream)
import msvcrt
print "import msvcrt"
for c in prompt:
print "for c in prompt:",c
msvcrt.putch(c)
pw = ""
while 1:
print "while 1:"
c = msvcrt.getch()
print "is here "
print c
if c == '\r' or c == '\n':
print "if c == '\r' or c == '\n':"
break
if c == '\003':
print "if c == '\003':"
raise KeyboardInterrupt
if c == '\b':
print "if c == '\b':"
pw = pw[:-1]
else:
print "else:"
pw = pw + c
msvcrt.putch('\r')
msvcrt.putch('\n')
print "pw:", pw
return pw


第四行有一句 if sys.stdin is not sys.__stdin__:
在windows下 pycharm中,運行源碼時這一句返回的是false,所以程序跳過if語句繼續往下執行了,這就進入了while 1:循環, 然后就卡在了剛開始說的c=msvcrt.getch()這里了。我把上面的那個if語句改了一下,把is not 改成 is了,竟然可以運行了。只是多了這么幾行輸出:
D:\Python27\lib\getpass.py:94: GetPassWarning: Can not control echo on the terminal.
return fallback_getpass(prompt, stream)
Warning: Password input may be echoed.

我只能做到這個程度了,至於為什么會是這樣,為什么那個If語句在起作用,這和pycharm有什么關系,我都不知道了,所以求大神指點啊,說一下所以然,其中的原理是什么,應該怎么弄才能在pycharm中正常運行,因為源碼在命令行和eclipse中都是正常的。
謝謝啦!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM