Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
MMCV
Commits
61f9e91c
Unverified
Commit
61f9e91c
authored
Jun 10, 2020
by
Jerry Jiarui XU
Committed by
GitHub
Jun 10, 2020
Browse files
Add syntax check in .py config (#330)
* Add syntax check in .py config * rename validate
parent
2aa7712c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
0 deletions
+26
-0
mmcv/utils/config.py
mmcv/utils/config.py
+12
-0
tests/test_config.py
tests/test_config.py
+14
-0
No files found.
mmcv/utils/config.py
View file @
61f9e91c
# Copyright (c) Open-MMLab. All rights reserved.
# Copyright (c) Open-MMLab. All rights reserved.
import
ast
import
os.path
as
osp
import
os.path
as
osp
import
shutil
import
shutil
import
sys
import
sys
...
@@ -80,6 +81,16 @@ class Config(object):
...
@@ -80,6 +81,16 @@ class Config(object):
"""
"""
@
staticmethod
def
_validate_py_syntax
(
filename
):
with
open
(
filename
)
as
f
:
content
=
f
.
read
()
try
:
ast
.
parse
(
content
)
except
SyntaxError
:
raise
SyntaxError
(
'There are syntax errors in config '
f
'file
{
filename
}
'
)
@
staticmethod
@
staticmethod
def
_file2dict
(
filename
):
def
_file2dict
(
filename
):
filename
=
osp
.
abspath
(
osp
.
expanduser
(
filename
))
filename
=
osp
.
abspath
(
osp
.
expanduser
(
filename
))
...
@@ -93,6 +104,7 @@ class Config(object):
...
@@ -93,6 +104,7 @@ class Config(object):
osp
.
join
(
temp_config_dir
,
temp_config_name
))
osp
.
join
(
temp_config_dir
,
temp_config_name
))
temp_module_name
=
osp
.
splitext
(
temp_config_name
)[
0
]
temp_module_name
=
osp
.
splitext
(
temp_config_name
)[
0
]
sys
.
path
.
insert
(
0
,
temp_config_dir
)
sys
.
path
.
insert
(
0
,
temp_config_dir
)
Config
.
_validate_py_syntax
(
filename
)
mod
=
import_module
(
temp_module_name
)
mod
=
import_module
(
temp_module_name
)
sys
.
path
.
pop
(
0
)
sys
.
path
.
pop
(
0
)
cfg_dict
=
{
cfg_dict
=
{
...
...
tests/test_config.py
View file @
61f9e91c
...
@@ -272,3 +272,17 @@ def test_reserved_key():
...
@@ -272,3 +272,17 @@ def test_reserved_key():
cfg_file
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/config/g.py'
)
cfg_file
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'data/config/g.py'
)
with
pytest
.
raises
(
KeyError
):
with
pytest
.
raises
(
KeyError
):
Config
.
fromfile
(
cfg_file
)
Config
.
fromfile
(
cfg_file
)
def
test_syntax_error
():
temp_cfg_file
=
tempfile
.
NamedTemporaryFile
(
suffix
=
'.py'
)
temp_cfg_path
=
temp_cfg_file
.
name
# write a file with syntax error
with
open
(
temp_cfg_path
,
'w'
)
as
f
:
f
.
write
(
'a=0b=dict(c=1)'
)
with
pytest
.
raises
(
SyntaxError
,
match
=
'There are syntax errors in config '
f
'file
{
temp_cfg_path
}
'
):
Config
.
fromfile
(
temp_cfg_path
)
temp_cfg_file
.
close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment