33
33
34
34
from ._version import __version__
35
35
36
- # imported lazily to avoid startup performance hit if it isn't used
37
- compiler = None
38
-
39
36
# A dictionary mapping BOM to
40
37
# the encoding to decode with, and what to set the
41
38
# encoding attribute to.
@@ -107,7 +104,6 @@ def match_utf8(encoding):
107
104
'RepeatSectionError' ,
108
105
'ReloadError' ,
109
106
'UnreprError' ,
110
- 'UnknownType' ,
111
107
'flatten_errors' ,
112
108
'get_extra_values'
113
109
)
@@ -132,30 +128,23 @@ def match_utf8(encoding):
132
128
'write_empty_values' : False ,
133
129
}
134
130
135
- # this could be replaced if six is used for compatibility, or there are no
136
- # more assertions about items being a string
137
-
138
-
139
- def getObj (s ):
140
- global compiler
141
- if compiler is None :
142
- import compiler
143
- s = "a=" + s
144
- p = compiler .parse (s )
145
- return p .getChildren ()[1 ].getChildren ()[0 ].getChildren ()[1 ]
146
131
132
+ _literal_eval = None
147
133
148
- class UnknownType (Exception ):
149
- pass
150
134
135
+ def unrepr (string ):
136
+ """Return given string evaluated to a Python literal."""
137
+ if not string :
138
+ return string
151
139
152
- def unrepr (s ):
153
- if not s :
154
- return s
140
+ # Lazy import of ast - may not really be needed as much lighter than old
141
+ # compile module was, but unrepr is only required by some configobj users.
142
+ global _literal_eval
143
+ if _literal_eval is None :
144
+ import ast
145
+ _literal_eval = ast .literal_eval
155
146
156
- # this is supposed to be safe
157
- import ast
158
- return ast .literal_eval (s )
147
+ return _literal_eval (string )
159
148
160
149
161
150
class ConfigObjError (SyntaxError ):
@@ -1623,10 +1612,7 @@ def _parse(self, infile):
1623
1612
try :
1624
1613
value = unrepr (value )
1625
1614
except Exception as cause :
1626
- if isinstance (cause , UnknownType ):
1627
- msg = 'Unknown name or type in value'
1628
- else :
1629
- msg = 'Parse error from unrepr-ing multiline value'
1615
+ msg = 'Parse error from unrepr-ing multiline value'
1630
1616
self ._handle_error (msg , UnreprError , infile , cur_index )
1631
1617
continue
1632
1618
else :
@@ -1635,10 +1621,7 @@ def _parse(self, infile):
1635
1621
try :
1636
1622
value = unrepr (value )
1637
1623
except Exception as cause :
1638
- if isinstance (cause , UnknownType ):
1639
- msg = 'Unknown name or type in value'
1640
- else :
1641
- msg = 'Parse error from unrepr-ing value'
1624
+ msg = 'Parse error from unrepr-ing value'
1642
1625
self ._handle_error (msg , UnreprError , infile , cur_index )
1643
1626
continue
1644
1627
else :
0 commit comments