Skip to content

Commit

Permalink
SSSDConfig: Fix ResourceWarning unclosed file
Browse files Browse the repository at this point in the history
/usr/lib64/python3.7/unittest/case.py:763:
    ResourceWarning: unclosed file <_io.TextIOWrapper name='src/config/testconfigs/sssd-invalid.conf'
                                    mode='r' encoding='UTF-8'>
  context = None
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/usr/lib64/python3.7/unittest/case.py:763:
    ResourceWarning: unclosed file <_io.TextIOWrapper name='src/config/testconfigs/noparse.api.conf'
                                    mode='r' encoding='UTF-8'>
  context = None
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Merges: https://pagure.io/SSSD/sssd/pull-request/3927

Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
  • Loading branch information
Lukas Slebodnik authored and jhrozek committed Jan 29, 2019
1 parent c4db34c commit 769dc24
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/config/SSSDConfig/__init__.py.in
Expand Up @@ -500,16 +500,14 @@ class SSSDConfigSchema(SSSDChangeConf):
schemaplugindir = '@datadir@/sssd/sssd.api.d'

try:
#Read the primary config file
fd = open(schemafile, 'r')
self.readfp(fd)
fd.close()
# Read the primary config file
with open(schemafile, 'r') as fd:
self.readfp(fd)
# Read in the provider files
for file in filter(lambda f: re.search(r'^sssd-.*\.conf$', f),
os.listdir(schemaplugindir)):
fd = open(schemaplugindir+ "/" + file)
self.readfp(fd)
fd.close()
with open(schemaplugindir+ "/" + file) as fd:
self.readfp(fd)
except IOError:
raise
except SyntaxError: # can be raised with readfp
Expand Down Expand Up @@ -1452,14 +1450,12 @@ class SSSDConfig(SSSDChangeConf):
#TODO: get this from a global setting
configfile = '@sysconfdir@/sssd/sssd.conf'
# open will raise an IOError if it fails
fd = open(configfile, 'r')

try:
self.readfp(fd)
except:
raise ParsingError
with open(configfile, 'r') as fd:
try:
self.readfp(fd)
except:
raise ParsingError

fd.close()
self.configfile = configfile
self.initialized = True

Expand Down Expand Up @@ -1523,10 +1519,9 @@ class SSSDConfig(SSSDChangeConf):

# open() will raise IOError if it fails
old_umask = os.umask(0o177)
of = open(outputfile, "wb")
output = self.dump(self.opts).encode('utf-8')
of.write(output)
of.close()
with open(outputfile, "wb") as of:
output = self.dump(self.opts).encode('utf-8')
of.write(output)
os.umask(old_umask)

def list_active_services(self):
Expand Down

0 comments on commit 769dc24

Please sign in to comment.