Skip to content

Commit bc4cfb9

Browse files
author
nyuszika7h
committed
Unix: Fix FD leaks and Windows compatibility
1 parent a3c79d6 commit bc4cfb9

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

plugins/Unix/plugin.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,11 @@ def fortune(self, irc, msg, args):
210210
args.append('-a')
211211
args.extend(self.registryValue('fortune.files'))
212212
try:
213-
inst = subprocess.Popen(args, close_fds=True,
214-
stdout=subprocess.PIPE,
215-
stderr=subprocess.PIPE,
216-
stdin=open(os.devnull))
213+
with open(os.devnull) as null:
214+
inst = subprocess.Popen(args,
215+
stdout=subprocess.PIPE,
216+
stderr=subprocess.PIPE,
217+
stdin=null)
217218
except OSError as e:
218219
irc.error(_('It seems the configured fortune command was '
219220
'not available.'), Raise=True)
@@ -241,10 +242,11 @@ def wtf(self, irc, msg, args, foo, something):
241242
if wtfCmd:
242243
something = something.rstrip('?')
243244
try:
244-
inst = subprocess.Popen([wtfCmd, something], close_fds=True,
245-
stdout=subprocess.PIPE,
246-
stderr=open(os.devnull),
247-
stdin=open(os.devnull))
245+
with open(os.devnull, 'r+') as null:
246+
inst = subprocess.Popen([wtfCmd, something],
247+
stdout=subprocess.PIPE,
248+
stderr=null,
249+
stdin=null)
248250
except OSError:
249251
irc.error(_('It seems the configured wtf command was not '
250252
'available.'), Raise=True)
@@ -291,9 +293,11 @@ def ping(self, irc, msg, args, optlist, host):
291293
args.append('5')
292294
args.append(host)
293295
try:
294-
inst = subprocess.Popen(args, stdout=subprocess.PIPE,
295-
stderr=subprocess.PIPE,
296-
stdin=open(os.devnull))
296+
with open(os.devnull) as null:
297+
inst = subprocess.Popen(args,
298+
stdout=subprocess.PIPE,
299+
stderr=subprocess.PIPE,
300+
stdin=null)
297301
except OSError as e:
298302
irc.error('It seems the configured ping command was '
299303
'not available (%s).' % e, Raise=True)
@@ -323,10 +327,11 @@ def sysuptime(self, irc, msg, args):
323327
if uptimeCmd:
324328
args = [uptimeCmd]
325329
try:
326-
inst = subprocess.Popen(args, close_fds=True,
330+
with open(os.devnull) as null:
331+
inst = subprocess.Popen(args,
327332
stdout=subprocess.PIPE,
328333
stderr=subprocess.PIPE,
329-
stdin=open(os.devnull))
334+
stdin=null)
330335
except OSError as e:
331336
irc.error('It seems the configured uptime command was '
332337
'not available.', Raise=True)
@@ -351,10 +356,11 @@ def sysuname(self, irc, msg, args):
351356
if unameCmd:
352357
args = [unameCmd, '-a']
353358
try:
354-
inst = subprocess.Popen(args, close_fds=True,
355-
stdout=subprocess.PIPE,
356-
stderr=subprocess.PIPE,
357-
stdin=open(os.devnull))
359+
with open(os.devnull) as null:
360+
inst = subprocess.Popen(args,
361+
stdout=subprocess.PIPE,
362+
stderr=subprocess.PIPE,
363+
stdin=null)
358364
except OSError as e:
359365
irc.error('It seems the configured uptime command was '
360366
'not available.', Raise=True)
@@ -381,9 +387,10 @@ def call(self, irc, msg, args, text):
381387
"""
382388
args = shlex.split(text)
383389
try:
384-
inst = subprocess.Popen(args, stdout=subprocess.PIPE,
385-
stderr=subprocess.PIPE,
386-
stdin=open(os.devnull))
390+
with open(os.devnull) as null:
391+
inst = subprocess.Popen(args, stdout=subprocess.PIPE,
392+
stderr=subprocess.PIPE,
393+
stdin=null)
387394
except OSError as e:
388395
irc.error('It seems the requested command was '
389396
'not available (%s).' % e, Raise=True)

0 commit comments

Comments
 (0)