mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
This commit is contained in:
parent
afbb7a371f
commit
172bb39452
27 changed files with 249 additions and 259 deletions
|
@ -78,9 +78,9 @@ def test():
|
|||
continue
|
||||
else:
|
||||
f = open(filename, 'r')
|
||||
if debug: print('processing', filename, '...')
|
||||
text = f.read()
|
||||
f.close()
|
||||
with f:
|
||||
if debug: print('processing', filename, '...')
|
||||
text = f.read()
|
||||
paralist = text.split('\n\n')
|
||||
for para in paralist:
|
||||
if debug > 1: print('feeding ...')
|
||||
|
|
|
@ -22,17 +22,16 @@ def main():
|
|||
port = int(port[i+1:])
|
||||
host = host[:i]
|
||||
command = ' '.join(sys.argv[2:])
|
||||
s = socket(AF_INET, SOCK_STREAM)
|
||||
s.connect((host, port))
|
||||
s.send(command.encode())
|
||||
s.shutdown(SHUT_WR)
|
||||
reply = b''
|
||||
while True:
|
||||
data = s.recv(BUFSIZE)
|
||||
if not data:
|
||||
break
|
||||
reply += data
|
||||
print(reply.decode(), end=' ')
|
||||
s.close()
|
||||
with socket(AF_INET, SOCK_STREAM) as s:
|
||||
s.connect((host, port))
|
||||
s.send(command.encode())
|
||||
s.shutdown(SHUT_WR)
|
||||
reply = b''
|
||||
while True:
|
||||
data = s.recv(BUFSIZE)
|
||||
if not data:
|
||||
break
|
||||
reply += data
|
||||
print(reply.decode(), end=' ')
|
||||
|
||||
main()
|
||||
|
|
|
@ -26,16 +26,16 @@ def main():
|
|||
s.listen(1)
|
||||
while True:
|
||||
conn, (remotehost, remoteport) = s.accept()
|
||||
print('connection from', remotehost, remoteport)
|
||||
request = b''
|
||||
while 1:
|
||||
data = conn.recv(BUFSIZE)
|
||||
if not data:
|
||||
break
|
||||
request += data
|
||||
reply = execute(request.decode())
|
||||
conn.send(reply.encode())
|
||||
conn.close()
|
||||
with conn:
|
||||
print('connection from', remotehost, remoteport)
|
||||
request = b''
|
||||
while 1:
|
||||
data = conn.recv(BUFSIZE)
|
||||
if not data:
|
||||
break
|
||||
request += data
|
||||
reply = execute(request.decode())
|
||||
conn.send(reply.encode())
|
||||
|
||||
def execute(request):
|
||||
stdout = sys.stdout
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue