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

Skip to content

Commit b7e55ae

Browse files
committed
Make sure file handlers get closed
Also lazily import json only when needed.
1 parent 7e095b5 commit b7e55ae

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

ua_parser/user_agent_parser.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
__author__ = 'Lindsey Simon <[email protected]>'
2020

21-
import json
2221
import os
2322
import re
2423

@@ -451,15 +450,15 @@ def GetFilters(user_agent_string, js_user_agent_string=None,
451450
# falling back to yaml format
452451
if regexes is None:
453452
try:
454-
json_file = open(json_path)
455-
regexes = json.loads(json_file.read())
456-
json_file.close()
453+
import json
454+
455+
with open(json_path) as fp:
456+
regexes = json.load(fp)
457457
except IOError:
458458
import yaml
459459

460-
yamlFile = open(yamlPath)
461-
regexes = yaml.load(yamlFile)
462-
yamlFile.close()
460+
with open(yamlPath) as fp:
461+
regexes = yaml.load(fp)
463462

464463

465464
USER_AGENT_PARSERS = []

0 commit comments

Comments
 (0)