Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc57789

Browse files
committed
Improve example for crypt module. No string exceptions..
1 parent 7f9b37b commit bc57789

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

Doc/library/crypt.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,20 @@ Examples
131131

132132
A simple example illustrating typical use::
133133

134-
import crypt, getpass, pwd
134+
import pwd
135+
import crypt
136+
import getpass
135137

136138
def login():
137-
username = input('Python login:')
139+
username = input('Python login: ')
138140
cryptedpasswd = pwd.getpwnam(username)[1]
139141
if cryptedpasswd:
140142
if cryptedpasswd == 'x' or cryptedpasswd == '*':
141-
raise "Sorry, currently no support for shadow passwords"
143+
raise ValueError('no support for shadow passwords')
142144
cleartext = getpass.getpass()
143145
return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
144146
else:
145-
return 1
147+
return True
146148

147149
To generate a hash of a password using the strongest available method and
148150
check it against the original::
@@ -151,4 +153,4 @@ check it against the original::
151153

152154
hashed = crypt.crypt(plaintext)
153155
if hashed != crypt.crypt(plaintext, hashed):
154-
raise "Hashed version doesn't validate against original"
156+
raise ValueError("hashed version doesn't validate against original")

0 commit comments

Comments
 (0)