Skip to content

Commit 4a3906e

Browse files
committed
Cleanup unrepr function and related code
There was some cruft left around from the old implemention of unrepr, which can now be deleted. Not sure if the lazy import of ast is worthwhile but the implemention of that is now pretty harmless. Note this does remove the exported but unused, and only ever internally propogated, UnknownType exception type.
1 parent 7a0f1e0 commit 4a3906e

1 file changed

Lines changed: 14 additions & 31 deletions

File tree

src/configobj/__init__.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
import six
2929
from ._version import __version__
3030

31-
# imported lazily to avoid startup performance hit if it isn't used
32-
compiler = None
33-
3431
# A dictionary mapping BOM to
3532
# the encoding to decode with, and what to set the
3633
# encoding attribute to.
@@ -102,7 +99,6 @@ def match_utf8(encoding):
10299
'RepeatSectionError',
103100
'ReloadError',
104101
'UnreprError',
105-
'UnknownType',
106102
'flatten_errors',
107103
'get_extra_values'
108104
)
@@ -127,30 +123,23 @@ def match_utf8(encoding):
127123
'write_empty_values': False,
128124
}
129125

130-
# this could be replaced if six is used for compatibility, or there are no
131-
# more assertions about items being a string
132-
133-
134-
def getObj(s):
135-
global compiler
136-
if compiler is None:
137-
import compiler
138-
s = "a=" + s
139-
p = compiler.parse(s)
140-
return p.getChildren()[1].getChildren()[0].getChildren()[1]
141126

127+
_literal_eval = None
142128

143-
class UnknownType(Exception):
144-
pass
145129

130+
def unrepr(string):
131+
"""Return given string evaluated to a Python literal."""
132+
if not string:
133+
return string
146134

147-
def unrepr(s):
148-
if not s:
149-
return s
135+
# Lazy import of ast - may not really be needed as much lighter than old
136+
# compile module was, but unrepr is only required by some configobj users.
137+
global _literal_eval
138+
if _literal_eval is None:
139+
import ast
140+
_literal_eval = ast.literal_eval
150141

151-
# this is supposed to be safe
152-
import ast
153-
return ast.literal_eval(s)
142+
return _literal_eval(string)
154143

155144

156145
class ConfigObjError(SyntaxError):
@@ -1625,10 +1614,7 @@ def _parse(self, infile):
16251614
try:
16261615
value = unrepr(value)
16271616
except Exception as cause:
1628-
if isinstance(cause, UnknownType):
1629-
msg = 'Unknown name or type in value'
1630-
else:
1631-
msg = 'Parse error from unrepr-ing multiline value'
1617+
msg = 'Parse error from unrepr-ing multiline value'
16321618
self._handle_error(msg, UnreprError, infile, cur_index)
16331619
continue
16341620
else:
@@ -1637,10 +1623,7 @@ def _parse(self, infile):
16371623
try:
16381624
value = unrepr(value)
16391625
except Exception as cause:
1640-
if isinstance(cause, UnknownType):
1641-
msg = 'Unknown name or type in value'
1642-
else:
1643-
msg = 'Parse error from unrepr-ing value'
1626+
msg = 'Parse error from unrepr-ing value'
16441627
self._handle_error(msg, UnreprError, infile, cur_index)
16451628
continue
16461629
else:

0 commit comments

Comments
 (0)