1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
# Copyright © 2015-2024 Jakub Wilk <jwilk@jwilk.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
'''
string format checks: Python's %-formatting
'''
import collections
import io
class _info:
flags = '#0- +'
lengths = 'hlL'
oct_cvt = 'o'
hex_cvt = 'xX'
int_cvt = oct_cvt + hex_cvt + 'diu'
float_cvt = 'eEfFgG'
other_cvt = 'csra'
all_cvt = int_cvt + float_cvt + other_cvt + '%'
SSIZE_MAX = (1 << 31) - 1 # on 32-bit architectures
# --------------------------------------------------------------------------
class Error(Exception):
message = 'invalid conversion specification'
class ObsoleteConversion(Error):
message = 'obsolete conversion specifier'
# errors in argument indexing:
class ForbiddenArgumentKey(Error):
message = 'argument key not allowed'
class ArgumentIndexingMixture(Error):
message = 'mixed named and unnamed argument specifications'
class ArgumentTypeMismatch(Error):
message = 'argument type mismatch'
# errors is flags:
class RedundantFlag(Error):
message = 'redundant flag character'
# errors in field width:
class WidthRangeError(Error):
message = 'field width too large'
# errors in precision:
class RedundantPrecision(Error):
message = 'redundant precision'
class PrecisionRangeError(Error):
message = 'precision too large'
# errors in length modifiers:
class RedundantLength(Error):
message = 'redundant length modifier'
# --------------------------------------------------------------------------
class VariableWidth:
type = 'int'
def __init__(self, parent):
self.parent = parent
class VariablePrecision:
type = 'int'
def __init__(self, parent):
self.parent = parent
# --------------------------------------------------------------------------
class FormatString:
def __init__(self, s):
self._items = items = []
self._seq_arguments = []
self._map_arguments = collections.defaultdict(list)
self.warnings = []
text = io.StringIO()
si = enumerate(s)
i = 0
def next_si():
try:
return next(si)
except StopIteration:
raise Error(s[i:])
while True:
try:
i, ch = next(si)
except StopIteration:
if text.tell():
items += [text.getvalue()]
break
if ch != '%':
text.write(ch)
continue
j, ch = next_si()
key = None
if ch == '(':
# XXX Python skips over balanced parentheses, but this is not documented.
pcount = 1
while True:
j, ch = next_si()
if ch == '(':
pcount += 1
elif ch == ')':
pcount -= 1
if pcount == 0:
j, ch = next_si()
break
key = s[i+2:j-1]
flags = collections.Counter()
while ch in _info.flags:
flags[ch] += 1
j, ch = next_si()
width = None
var_width = False
if ch == '*':
var_width = True
j, ch = next_si()
else:
width = 0
while '0' <= ch <= '9':
width *= 10
width += int(ch)
j, ch = next_si()
prec = None
var_prec = False
if ch == '.':
j, ch = next_si()
if ch == '*':
var_prec = True
j, ch = next_si()
else:
prec = 0
while '0' <= ch <= '9':
prec *= 10
prec += int(ch)
j, ch = next_si()
length = None
if ch in _info.lengths:
length = ch
j, ch = next_si()
if ch in _info.all_cvt:
conv = ch
else:
raise Error(s[i:])
if text.tell():
items += [text.getvalue()]
text.seek(0)
text.truncate()
items += [Conversion(self,
s[i:j+1],
key=key,
flags=flags,
width=width,
var_width=var_width,
prec=prec,
var_prec=var_prec,
length=length,
conv=conv,
)]
self.seq_arguments = self._seq_arguments
self.seq_conversions = [
arg
for arg in self._seq_arguments
if isinstance(arg, Conversion)
]
for key, args in self._map_arguments.items():
types = frozenset(a.type for a in args)
if len(types) > 1:
raise ArgumentTypeMismatch(s, key, types)
self.map_arguments = self._map_arguments
self._map_arguments = self._seq_arguments = None
def add_argument(self, key, arg):
if self._map_arguments is None:
raise RuntimeError('arguments already initialized')
# Python accepts mixed named and unnamed arguments
# in some corner cases:
# https://bugs.python.org/issue1467929
# We follow the documentation and reject such mixtures.
if key is None:
if self._map_arguments:
raise IndexError
self._seq_arguments += [arg]
else:
if self._seq_arguments:
raise IndexError
self._map_arguments[key] += [arg]
def warn(self, exc_type, *args, **kwargs):
self.warnings += [exc_type(*args, **kwargs)]
def __iter__(self):
return iter(self._items)
def __len__(self):
return len(self._items)
# --------------------------------------------------------------------------
class Conversion:
def __init__(self, parent, s, *, key, flags, width, var_width, prec, var_prec, length, conv):
assert s[-1] == conv, f'{s[-1]} != {conv}'
i = _info
for flag, count in flags.items():
if count != 1:
parent.warn(RedundantFlag, s, flag, flag)
if flag == '#':
if conv not in i.oct_cvt + i.hex_cvt + i.float_cvt:
parent.warn(RedundantFlag, s, flag)
elif flag == '-':
pass
else:
assert flag in '0 +'
if conv not in i.int_cvt + i.float_cvt:
parent.warn(RedundantFlag, s, flag)
for f1, f2 in [('-', '0'), ('+', ' ')]:
if (f1 in flags) and (f2 in flags):
parent.warn(RedundantFlag, s, f1, f2)
if var_width:
assert width is None
try:
parent.add_argument(None, VariableWidth(self))
except IndexError:
raise ArgumentIndexingMixture(s)
width = ...
elif width > SSIZE_MAX:
# The limit is INT_MAX in Python 2.X and SSIZE_MAX in Python 3.X;
# but INT_MAX == SSIZE_MAX on mainstream 32-bit architectures.
raise WidthRangeError(s, width)
if var_prec:
assert prec is None
try:
parent.add_argument(None, VariablePrecision(self))
except IndexError:
raise ArgumentIndexingMixture(s)
prec = ...
elif prec is not None:
if prec > SSIZE_MAX:
raise PrecisionRangeError(s, width)
if prec is not None:
if (conv in i.int_cvt) and ('0' in flags):
parent.warn(RedundantFlag, s, '0')
if conv in 'c%':
parent.warn(RedundantPrecision, s, prec)
if length is not None:
parent.warn(RedundantLength, s, length)
if conv in i.int_cvt:
tp = 'int'
if conv == 'u':
parent.warn(ObsoleteConversion, s, '%u', '%d')
elif conv in i.float_cvt:
tp = 'float'
elif conv == 'c':
tp = 'chr'
elif conv == 's':
tp = 'str'
elif conv in 'ra':
tp = 'object'
elif conv == '%':
tp = 'None'
else:
assert False # no coverage
self.type = tp
if tp == 'None':
if key is not None:
raise ForbiddenArgumentKey(s)
else:
try:
parent.add_argument(key, self)
except IndexError:
raise ArgumentIndexingMixture(s)
# vim:ts=4 sts=4 sw=4 et
|