| 1 |
|
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
class _C: |
| 6 |
def _m(self): pass |
| 7 |
MethodType = type(_C()._m) |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class CPPScopeMeta(type): |
| 25 |
def __getattr__(self, name): |
| 26 |
try: |
| 27 |
return get_scoped_pycppitem(self, name) |
| 28 |
except Exception as e: |
| 29 |
raise AttributeError("%s object has no attribute '%s' (details: %s)" % |
| 30 |
(self, name, str(e))) |
| 31 |
|
| 32 |
class CPPNamespaceMeta(CPPScopeMeta): |
| 33 |
def __dir__(self): |
| 34 |
|
| 35 |
values = set(self.__dict__.keys()) |
| 36 |
values.update(object.__dict__.keys()) |
| 37 |
values.update(type(self).__dict__.keys()) |
| 38 |
|
| 39 |
|
| 40 |
values.update(self.__cppdecl__.__dir__()) |
| 41 |
return list(values) |
| 42 |
|
| 43 |
class CPPClassMeta(CPPScopeMeta): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
def with_metaclass(meta, *bases): |
| 68 |
"""Create a base class with a metaclass.""" |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
class metaclass(type): |
| 73 |
|
| 74 |
def __new__(cls, name, this_bases, d): |
| 75 |
return meta(name, bases, d) |
| 76 |
|
| 77 |
@classmethod |
| 78 |
def __prepare__(cls, name, this_bases): |
| 79 |
return meta.__prepare__(name, bases) |
| 80 |
return type.__new__(metaclass, 'temporary_class', (), {}) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
class CPPNamespace(with_metaclass(CPPNamespaceMeta, object)): |
| 85 |
def __init__(self): |
| 86 |
raise TypeError("cannot instantiate namespace '%s'", self.__cpp_name__) |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class CPPTemplate(object): |
| 92 |
def __init__(self, name, scope=None): |
| 93 |
self._name = name |
| 94 |
if scope is None: |
| 95 |
self._scope = gbl |
| 96 |
else: |
| 97 |
self._scope = scope |
| 98 |
|
| 99 |
def _arg_to_str(self, arg): |
| 100 |
|
| 101 |
if type(arg) == str: |
| 102 |
return arg |
| 103 |
elif hasattr(arg, '__cpp_name__'): |
| 104 |
return arg.__cpp_name__ |
| 105 |
elif arg == str: |
| 106 |
import _cppyy |
| 107 |
return _cppyy._std_string_name() |
| 108 |
elif isinstance(arg, type): |
| 109 |
return arg.__name__ |
| 110 |
return str(arg) |
| 111 |
|
| 112 |
def __call__(self, *args): |
| 113 |
fullname = ''.join( |
| 114 |
[self._name, '<', ','.join(map(self._arg_to_str, args))]) |
| 115 |
fullname += '>' |
| 116 |
try: |
| 117 |
return self._scope.__dict__[fullname] |
| 118 |
except KeyError: |
| 119 |
pass |
| 120 |
result = get_scoped_pycppitem(self._scope, fullname, True) |
| 121 |
if not result: |
| 122 |
raise TypeError("%s does not exist" % fullname) |
| 123 |
return result |
| 124 |
|
| 125 |
def __getitem__(self, *args): |
| 126 |
if args and type(args[0]) == tuple: |
| 127 |
return self.__call__(*(args[0])) |
| 128 |
return self.__call__(*args) |
| 129 |
|
| 130 |
|
| 131 |
def scope_splitter(name): |
| 132 |
is_open_template, scope = 0, "" |
| 133 |
for c in name: |
| 134 |
if c == ':' and not is_open_template: |
| 135 |
if scope: |
| 136 |
yield scope |
| 137 |
scope = "" |
| 138 |
continue |
| 139 |
elif c == '<': |
| 140 |
is_open_template += 1 |
| 141 |
elif c == '>': |
| 142 |
is_open_template -= 1 |
| 143 |
scope += c |
| 144 |
yield scope |
| 145 |
|
| 146 |
def get_pycppitem(final_scoped_name): |
| 147 |
|
| 148 |
|
| 149 |
scope = gbl |
| 150 |
for name in scope_splitter(final_scoped_name): |
| 151 |
scope = getattr(scope, name) |
| 152 |
return scope |
| 153 |
get_pycppclass = get_pycppitem |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
def clgen_callback(final_scoped_name): |
| 160 |
return get_pycppclass(final_scoped_name) |
| 161 |
|
| 162 |
def fngen_callback(func, npar): |
| 163 |
if npar == 0: |
| 164 |
def wrapper(a0, a1): |
| 165 |
la0 = [a0[0], a0[1], a0[2], a0[3]] |
| 166 |
return func(la0) |
| 167 |
return wrapper |
| 168 |
else: |
| 169 |
def wrapper(a0, a1): |
| 170 |
la0 = [a0[0], a0[1], a0[2], a0[3]] |
| 171 |
la1 = [a1[i] for i in range(npar)] |
| 172 |
return func(la0, la1) |
| 173 |
return wrapper |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
def make_module_name(scope): |
| 178 |
if scope: |
| 179 |
return scope.__module__ + '.' + scope.__name__ |
| 180 |
return 'cppyy' |
| 181 |
|
| 182 |
def make_cppnamespace(scope, name, decl): |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
import _cppyy |
| 187 |
ns_meta = type(CPPNamespace)(name+'_meta', (CPPNamespaceMeta,), {}) |
| 188 |
|
| 189 |
|
| 190 |
d = {"__cppdecl__" : decl, |
| 191 |
"__module__" : make_module_name(scope), |
| 192 |
"__cpp_name__" : decl.__cpp_name__ } |
| 193 |
pyns = ns_meta(name, (CPPNamespace,), d) |
| 194 |
if scope: |
| 195 |
setattr(scope, name, pyns) |
| 196 |
|
| 197 |
|
| 198 |
sys.modules[make_module_name(pyns)] = pyns |
| 199 |
return pyns |
| 200 |
|
| 201 |
def _drop_cycles(bases): |
| 202 |
|
| 203 |
for b1 in bases: |
| 204 |
for b2 in bases: |
| 205 |
if not (b1 is b2) and issubclass(b2, b1): |
| 206 |
bases.remove(b1) |
| 207 |
break |
| 208 |
return tuple(bases) |
| 209 |
|
| 210 |
def make_new(decl): |
| 211 |
def __new__(cls, *args): |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
import _cppyy |
| 216 |
instance = _cppyy._bind_object(0, decl, True) |
| 217 |
if not instance.__class__ is cls: |
| 218 |
instance.__class__ = cls |
| 219 |
return instance |
| 220 |
return __new__ |
| 221 |
|
| 222 |
def make_cppclass(scope, cl_name, decl): |
| 223 |
import _cppyy |
| 224 |
|
| 225 |
|
| 226 |
bases = [get_pycppclass(base) for base in decl.get_base_names()] |
| 227 |
if not bases: |
| 228 |
bases = [CPPClass,] |
| 229 |
else: |
| 230 |
|
| 231 |
|
| 232 |
try: |
| 233 |
return scope.__dict__[cl_name] |
| 234 |
except KeyError: |
| 235 |
pass |
| 236 |
|
| 237 |
|
| 238 |
d_meta = {} |
| 239 |
|
| 240 |
|
| 241 |
def dispatch(self, m_name, signature): |
| 242 |
cppol = decl.__dispatch__(m_name, signature) |
| 243 |
return MethodType(cppol, self, type(self)) |
| 244 |
d_class = {"__cppdecl__" : decl, |
| 245 |
"__new__" : make_new(decl), |
| 246 |
"__module__" : make_module_name(scope), |
| 247 |
"__cpp_name__" : decl.__cpp_name__, |
| 248 |
"__dispatch__" : dispatch,} |
| 249 |
|
| 250 |
|
| 251 |
for m_name in decl.get_method_names(): |
| 252 |
cppol = decl.get_overload(m_name) |
| 253 |
d_class[m_name] = cppol |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
for d_name in decl.get_datamember_names(): |
| 258 |
cppdm = decl.get_datamember(d_name) |
| 259 |
d_class[d_name] = cppdm |
| 260 |
if _cppyy._is_static_data(cppdm): |
| 261 |
d_meta[d_name] = cppdm |
| 262 |
|
| 263 |
|
| 264 |
metabases = [type(base) for base in bases] |
| 265 |
cl_meta = type(CPPClassMeta)(cl_name+'_meta', _drop_cycles(metabases), d_meta) |
| 266 |
|
| 267 |
|
| 268 |
pycls = cl_meta(cl_name, _drop_cycles(bases), d_class) |
| 269 |
|
| 270 |
|
| 271 |
setattr(scope, cl_name, pycls) |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
import _cppyy |
| 276 |
_cppyy._register_class(pycls) |
| 277 |
_pythonize(pycls, pycls.__cpp_name__) |
| 278 |
return pycls |
| 279 |
|
| 280 |
def make_cpptemplatetype(scope, template_name): |
| 281 |
return CPPTemplate(template_name, scope) |
| 282 |
|
| 283 |
|
| 284 |
def get_scoped_pycppitem(scope, name, type_only=False): |
| 285 |
import _cppyy |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
scoped_name = (scope == gbl) and name or (scope.__cpp_name__+'::'+name) |
| 290 |
final_scoped_name = _cppyy._resolve_name(scoped_name) |
| 291 |
if final_scoped_name != scoped_name: |
| 292 |
pycppitem = get_pycppitem(final_scoped_name) |
| 293 |
|
| 294 |
setattr(scope, name, pycppitem) |
| 295 |
return pycppitem |
| 296 |
|
| 297 |
pycppitem = None |
| 298 |
|
| 299 |
|
| 300 |
cppitem = _cppyy._scope_byname(final_scoped_name) |
| 301 |
if cppitem: |
| 302 |
if cppitem.is_namespace(): |
| 303 |
pycppitem = make_cppnamespace(scope, name, cppitem) |
| 304 |
else: |
| 305 |
pycppitem = make_cppclass(scope, name, cppitem) |
| 306 |
|
| 307 |
if type_only: |
| 308 |
return pycppitem |
| 309 |
|
| 310 |
|
| 311 |
if not cppitem: |
| 312 |
cppitem = _cppyy._is_template(final_scoped_name) |
| 313 |
if cppitem: |
| 314 |
pycppitem = make_cpptemplatetype(scope, name) |
| 315 |
setattr(scope, name, pycppitem) |
| 316 |
|
| 317 |
|
| 318 |
if not cppitem: |
| 319 |
try: |
| 320 |
cppitem = scope.__cppdecl__.get_overload(name) |
| 321 |
setattr(scope, name, cppitem) |
| 322 |
pycppitem = getattr(scope, name) |
| 323 |
except AttributeError: |
| 324 |
pass |
| 325 |
|
| 326 |
|
| 327 |
if not cppitem: |
| 328 |
try: |
| 329 |
cppdm = scope.__cppdecl__.get_datamember(name) |
| 330 |
setattr(scope, name, cppdm) |
| 331 |
if _cppyy._is_static_data(cppdm): |
| 332 |
setattr(scope.__class__, name, cppdm) |
| 333 |
pycppitem = getattr(scope, name) |
| 334 |
except AttributeError: |
| 335 |
pass |
| 336 |
|
| 337 |
|
| 338 |
if not cppitem: |
| 339 |
if scope.__cppdecl__.has_enum(name): |
| 340 |
pycppitem = int |
| 341 |
|
| 342 |
if pycppitem is not None: |
| 343 |
return pycppitem |
| 344 |
|
| 345 |
raise AttributeError("'%s' has no attribute '%s'" % (str(scope), name)) |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
def extract_namespace(name): |
| 350 |
|
| 351 |
tpl_open = 0 |
| 352 |
for pos in range(len(name)-1, 1, -1): |
| 353 |
c = name[pos] |
| 354 |
|
| 355 |
|
| 356 |
if c == '>': |
| 357 |
tpl_open += 1 |
| 358 |
elif c == '<': |
| 359 |
tpl_open -= 1 |
| 360 |
|
| 361 |
|
| 362 |
elif tpl_open == 0 and c == ':' and name[pos-1] == ':': |
| 363 |
|
| 364 |
return name[:pos-1], name[pos+1:] |
| 365 |
|
| 366 |
|
| 367 |
return '', name |
| 368 |
|
| 369 |
|
| 370 |
def python_style_getitem(self, _idx): |
| 371 |
|
| 372 |
sz = len(self) |
| 373 |
idx = _idx |
| 374 |
if isinstance(idx, int): |
| 375 |
if idx < 0: idx = sz + idx |
| 376 |
if 0 <= idx < sz: |
| 377 |
return self._getitem__unchecked(idx) |
| 378 |
else: |
| 379 |
raise IndexError( |
| 380 |
'index out of range: %s requested for %s of size %d' % (str(idx), str(self), sz)) |
| 381 |
|
| 382 |
return self._getitem__unchecked(_idx) |
| 383 |
|
| 384 |
def python_style_sliceable_getitem(self, slice_or_idx): |
| 385 |
if type(slice_or_idx) == slice: |
| 386 |
nseq = self.__class__() |
| 387 |
nseq += [python_style_getitem(self, i) \ |
| 388 |
for i in range(*slice_or_idx.indices(len(self)))] |
| 389 |
return nseq |
| 390 |
return python_style_getitem(self, slice_or_idx) |
| 391 |
|
| 392 |
def _pythonize(pyclass, name): |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
import _cppyy |
| 397 |
|
| 398 |
|
| 399 |
if '__eq__' in pyclass.__dict__: |
| 400 |
def __eq__(self, other): |
| 401 |
if other is None: return not self |
| 402 |
if not self and not other: return True |
| 403 |
try: |
| 404 |
return self._cxx_eq(other) |
| 405 |
except TypeError: |
| 406 |
return NotImplemented |
| 407 |
pyclass._cxx_eq = pyclass.__dict__['__eq__'] |
| 408 |
pyclass.__eq__ = __eq__ |
| 409 |
|
| 410 |
if '__ne__' in pyclass.__dict__: |
| 411 |
def __ne__(self, other): |
| 412 |
if other is None: return not not self |
| 413 |
if type(self) is not type(other): return True |
| 414 |
return self._cxx_ne(other) |
| 415 |
pyclass._cxx_ne = pyclass.__dict__['__ne__'] |
| 416 |
pyclass.__ne__ = __ne__ |
| 417 |
|
| 418 |
|
| 419 |
if 'size' in pyclass.__dict__ and not '__len__' in pyclass.__dict__ \ |
| 420 |
and callable(pyclass.size): |
| 421 |
pyclass.__len__ = pyclass.size |
| 422 |
|
| 423 |
|
| 424 |
if 'push_back' in pyclass.__dict__ and not '__iadd__' in pyclass.__dict__: |
| 425 |
if 'reserve' in pyclass.__dict__: |
| 426 |
def iadd(self, ll): |
| 427 |
self.reserve(len(ll)) |
| 428 |
for x in ll: self.push_back(x) |
| 429 |
return self |
| 430 |
else: |
| 431 |
def iadd(self, ll): |
| 432 |
for x in ll: self.push_back(x) |
| 433 |
return self |
| 434 |
pyclass.__iadd__ = iadd |
| 435 |
|
| 436 |
is_vector = name.find('std::vector', 0, 11) == 0 |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
add_checked_item = False |
| 442 |
if not is_vector: |
| 443 |
if 'begin' in pyclass.__dict__ and 'end' in pyclass.__dict__: |
| 444 |
if _cppyy._scope_byname(name+'::iterator') or \ |
| 445 |
_cppyy._scope_byname(name+'::const_iterator'): |
| 446 |
def __iter__(self): |
| 447 |
i = self.begin() |
| 448 |
end = self.size() |
| 449 |
count = 0 |
| 450 |
while count != end: |
| 451 |
yield i.__deref__() |
| 452 |
i.__preinc__() |
| 453 |
count += 1 |
| 454 |
i.__destruct__() |
| 455 |
raise StopIteration |
| 456 |
pyclass.__iter__ = __iter__ |
| 457 |
else: |
| 458 |
|
| 459 |
add_checked_item = True |
| 460 |
|
| 461 |
|
| 462 |
else: |
| 463 |
pyclass.__real_init__ = pyclass.__init__ |
| 464 |
def vector_init(self, *args): |
| 465 |
if len(args) == 1 and isinstance(args[0], (tuple, list)): |
| 466 |
ll = args[0] |
| 467 |
self.__real_init__() |
| 468 |
self.reserve(len(ll)) |
| 469 |
for item in ll: |
| 470 |
self.push_back(item) |
| 471 |
return |
| 472 |
return self.__real_init__(*args) |
| 473 |
pyclass.__init__ = vector_init |
| 474 |
|
| 475 |
|
| 476 |
if hasattr(pyclass, 'data'): |
| 477 |
pyclass.__real_data = pyclass.data |
| 478 |
def data_with_len(self): |
| 479 |
arr = self.__real_data() |
| 480 |
arr.reshape((len(self),)) |
| 481 |
return arr |
| 482 |
pyclass.data = data_with_len |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if add_checked_item or is_vector or \ |
| 487 |
name.find('std::array', 0, 11) == 0 or name.find('std::deque', 0, 10) == 0: |
| 488 |
|
| 489 |
if '__getitem__' in pyclass.__dict__ and '__len__' in pyclass.__dict__: |
| 490 |
pyclass._getitem__unchecked = pyclass.__getitem__ |
| 491 |
if '__setitem__' in pyclass.__dict__ and '__iadd__' in pyclass.__dict__: |
| 492 |
pyclass.__getitem__ = python_style_sliceable_getitem |
| 493 |
else: |
| 494 |
pyclass.__getitem__ = python_style_getitem |
| 495 |
|
| 496 |
|
| 497 |
if name == _cppyy._std_string_name(): |
| 498 |
def eq(self, other): |
| 499 |
if type(other) == pyclass: |
| 500 |
return self.c_str() == other.c_str() |
| 501 |
else: |
| 502 |
return self.c_str() == other |
| 503 |
pyclass.__eq__ = eq |
| 504 |
pyclass.__str__ = pyclass.c_str |
| 505 |
|
| 506 |
|
| 507 |
elif name.find('std::pair', 0, 9) == 0: |
| 508 |
def getitem(self, idx): |
| 509 |
if idx == 0: return self.first |
| 510 |
if idx == 1: return self.second |
| 511 |
raise IndexError("out of bounds") |
| 512 |
def return2(self): |
| 513 |
return 2 |
| 514 |
pyclass.__getitem__ = getitem |
| 515 |
pyclass.__len__ = return2 |
| 516 |
|
| 517 |
|
| 518 |
elif name.find('std::complex', 0, 12) == 0: |
| 519 |
def getreal(obj): |
| 520 |
return obj.__cpp_real() |
| 521 |
def setreal(obj, val): |
| 522 |
obj.__cpp_real(val) |
| 523 |
pyclass.__cpp_real = pyclass.real |
| 524 |
pyclass.real = property(getreal, setreal) |
| 525 |
|
| 526 |
def getimag(obj): |
| 527 |
return obj.__cpp_imag() |
| 528 |
def setimag(obj, val): |
| 529 |
obj.__cpp_imag(val) |
| 530 |
pyclass.__cpp_imag = pyclass.imag |
| 531 |
pyclass.imag = property(getimag, setimag) |
| 532 |
|
| 533 |
def cmplx(self): |
| 534 |
return self.real+self.imag*1.j |
| 535 |
pyclass.__complex__ = cmplx |
| 536 |
|
| 537 |
def cmplx_repr(self): |
| 538 |
return repr(self.__complex__()) |
| 539 |
pyclass.__repr__ = cmplx_repr |
| 540 |
|
| 541 |
|
| 542 |
try: |
| 543 |
ns_name, cl_name = extract_namespace(name) |
| 544 |
pythonizors = _pythonizations[ns_name] |
| 545 |
name = cl_name |
| 546 |
except KeyError: |
| 547 |
pythonizors = _pythonizations[''] |
| 548 |
|
| 549 |
for p in pythonizors: |
| 550 |
p(pyclass, name) |
| 551 |
|
| 552 |
cppyyIsInitialized = False |
| 553 |
def _post_import_startup(): |
| 554 |
|
| 555 |
global cppyyIsInitialized |
| 556 |
if cppyyIsInitialized: |
| 557 |
return |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
import _cppyy |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
global CPPClass |
| 568 |
class CPPClass(with_metaclass(CPPClassMeta, _cppyy.CPPInstance)): |
| 569 |
pass |
| 570 |
|
| 571 |
|
| 572 |
_cppyy._set_class_generator(clgen_callback) |
| 573 |
|
| 574 |
|
| 575 |
_cppyy._set_function_generator(fngen_callback) |
| 576 |
|
| 577 |
|
| 578 |
global gbl |
| 579 |
gbl = make_cppnamespace(None, 'gbl', _cppyy._scope_byname('')) |
| 580 |
gbl.__module__ = 'cppyy' |
| 581 |
gbl.__doc__ = 'Global C++ namespace.' |
| 582 |
|
| 583 |
|
| 584 |
gbl.std = make_cppnamespace(gbl, 'std', _cppyy._scope_byname('std')) |
| 585 |
|
| 586 |
|
| 587 |
gbl.std.move = _cppyy.move |
| 588 |
|
| 589 |
|
| 590 |
setattr(gbl, 'internal_enum_type_t', int) |
| 591 |
setattr(gbl, 'unsigned int', int) |
| 592 |
|
| 593 |
|
| 594 |
_cppyy.gbl = gbl |
| 595 |
|
| 596 |
|
| 597 |
_cppyy.nullptr = _cppyy._get_nullptr() |
| 598 |
|
| 599 |
|
| 600 |
cppyyIsInitialized = True |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
_pythonizations = {'' : list()} |
| 605 |
def add_pythonization(pythonizor, scope = ''): |
| 606 |
"""<pythonizor> should be a callable taking two arguments: a class proxy, |
| 607 |
and its C++ name. It is called on each time a named class from <scope> |
| 608 |
(the global one by default, but a relevant C++ namespace is recommended) |
| 609 |
is bound. |
| 610 |
""" |
| 611 |
if not callable(pythonizor): |
| 612 |
raise TypeError("given '%s' object is not callable" % str(pythonizor)) |
| 613 |
try: |
| 614 |
_pythonizations[scope].append(pythonizor) |
| 615 |
except KeyError: |
| 616 |
_pythonizations[scope] = list() |
| 617 |
_pythonizations[scope].append(pythonizor) |
| 618 |
|
| 619 |
def remove_pythonization(pythonizor, scope = ''): |
| 620 |
"""Remove previously registered <pythonizor> from <scope>. |
| 621 |
""" |
| 622 |
try: |
| 623 |
_pythonizations[scope].remove(pythonizor) |
| 624 |
return True |
| 625 |
except (KeyError, ValueError): |
| 626 |
return False |