Skip to content

Commit 7d2c2ec

Browse files
author
Burak Yigit Kaya
committed
Safer config and interface loading mechanisms using with open() as f:
1 parent 5e8fc81 commit 7d2c2ec

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

phabricator/__init__.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,45 @@
3131
__all__ = ['Phabricator']
3232

3333

34-
# Default phabricator interfaces
35-
INTERFACES = json.loads(open(os.path.join(os.path.dirname(__file__), 'interfaces.json'), 'r').read())
34+
ON_WINDOWS = os.name == 'nt'
35+
CURRENT_DIR = os.getcwd()
36+
37+
38+
# Default Phabricator interfaces
39+
INTERFACES = {}
40+
with open(os.path.join(os.path.dirname(__file__), 'interfaces.json')) as fobj:
41+
INTERFACES = json.load(fobj)
42+
3643

3744
# Load arc config
38-
ARC_CONFIGS = [
45+
ARC_CONFIGS = (
3946
# System config
40-
os.path.join(os.environ['ProgramData'], 'Phabricator', 'Arcanist', 'config') if os.name == 'nt' else
41-
os.path.join('/etc', 'arcconfig'),
47+
os.path.join(
48+
os.environ['ProgramData'],
49+
'Phabricator',
50+
'Arcanist',
51+
'config'
52+
) if ON_WINDOWS else os.path.join('/etc', 'arcconfig'),
4253

4354
# User config
44-
os.path.join(os.environ['AppData'] if os.name == 'nt' else os.path.expanduser('~'), '.arcrc'),
55+
os.path.join(
56+
os.environ['AppData'] if ON_WINDOWS else os.path.expanduser('~'),
57+
'.arcrc'
58+
),
4559

4660
# Project config
47-
os.path.join(os.getcwd(), '.arcconfig'),
61+
os.path.join(CURRENT_DIR, '.arcconfig'),
4862

4963
# Local project config
50-
os.path.join(os.getcwd(), '.git', 'arc', 'config'),
51-
]
64+
os.path.join(CURRENT_DIR, '.git', 'arc', 'config'),
65+
)
5266

5367
ARCRC = {}
5468
for conf in ARC_CONFIGS:
5569
if os.path.exists(conf):
56-
ARCRC.update(json.load(open(conf, 'r')))
70+
with open(conf, 'r') as fobj:
71+
ARCRC.update(json.load(fobj))
72+
5773

5874
# Map Phabricator types to Python types
5975
PARAM_TYPE_MAP = {

0 commit comments

Comments
 (0)