| 1 |
import contextlib |
| 2 |
import importlib |
| 3 |
import importlib.util |
| 4 |
import itertools |
| 5 |
import sys |
| 6 |
import traceback |
| 7 |
import types |
| 8 |
import unittest |
| 9 |
import warnings |
| 10 |
|
| 11 |
from test import support |
| 12 |
from test.support import import_helper |
| 13 |
|
| 14 |
_testinternalcapi = import_helper.import_module('_testinternalcapi') |
| 15 |
_interpreters = import_helper.import_module('_interpreters') |
| 16 |
from _interpreters import NotShareableError |
| 17 |
|
| 18 |
from test import _code_definitions as code_defs |
| 19 |
from test import _crossinterp_definitions as defs |
| 20 |
|
| 21 |
|
| 22 |
@contextlib.contextmanager |
| 23 |
def ignore_byteswarning(): |
| 24 |
with warnings.catch_warnings(): |
| 25 |
warnings.filterwarnings('ignore', category=BytesWarning) |
| 26 |
yield |
| 27 |
|
| 28 |
|
| 29 |
# builtin types |
| 30 |
|
| 31 |
BUILTINS_TYPES = [o for _, o in __builtins__.items() if isinstance(o, type)] |
| 32 |
EXCEPTION_TYPES = [cls for cls in BUILTINS_TYPES |
| 33 |
if issubclass(cls, BaseException)] |
| 34 |
OTHER_TYPES = [o for n, o in vars(types).items() |
| 35 |
if (isinstance(o, type) and |
| 36 |
n not in ('DynamicClassAttribute', '_GeneratorWrapper'))] |
| 37 |
BUILTIN_TYPES = [ |
| 38 |
*BUILTINS_TYPES, |
| 39 |
*OTHER_TYPES, |
| 40 |
] |
| 41 |
|
| 42 |
# builtin exceptions |
| 43 |
|
| 44 |
try: |
| 45 |
raise Exception |
| 46 |
except Exception as exc: |
| 47 |
CAUGHT = exc |
| 48 |
EXCEPTIONS_WITH_SPECIAL_SIG = { |
| 49 |
BaseExceptionGroup: (lambda msg: (msg, [CAUGHT])), |
| 50 |
ExceptionGroup: (lambda msg: (msg, [CAUGHT])), |
| 51 |
UnicodeError: (lambda msg: (None, msg, None, None, None)), |
| 52 |
UnicodeEncodeError: (lambda msg: ('utf-8', '', 1, 3, msg)), |
| 53 |
UnicodeDecodeError: (lambda msg: ('utf-8', b'', 1, 3, msg)), |
| 54 |
UnicodeTranslateError: (lambda msg: ('', 1, 3, msg)), |
| 55 |
} |
| 56 |
BUILTIN_EXCEPTIONS = [ |
| 57 |
*(cls(*sig('error!')) for cls, sig in EXCEPTIONS_WITH_SPECIAL_SIG.items()), |
| 58 |
*(cls('error!') for cls in EXCEPTION_TYPES |
| 59 |
if cls not in EXCEPTIONS_WITH_SPECIAL_SIG), |
| 60 |
] |
| 61 |
|
| 62 |
# other builtin objects |
| 63 |
|
| 64 |
METHOD = defs.SpamOkay().okay |
| 65 |
BUILTIN_METHOD = [].append |
| 66 |
METHOD_DESCRIPTOR_WRAPPER = str.join |
| 67 |
METHOD_WRAPPER = object().__str__ |
| 68 |
WRAPPER_DESCRIPTOR = object.__init__ |
| 69 |
BUILTIN_WRAPPERS = { |
| 70 |
METHOD: types.MethodType, |
| 71 |
BUILTIN_METHOD: types.BuiltinMethodType, |
| 72 |
dict.__dict__['fromkeys']: types.ClassMethodDescriptorType, |
| 73 |
types.FunctionType.__code__: types.GetSetDescriptorType, |
| 74 |
types.FunctionType.__globals__: types.MemberDescriptorType, |
| 75 |
METHOD_DESCRIPTOR_WRAPPER: types.MethodDescriptorType, |
| 76 |
METHOD_WRAPPER: types.MethodWrapperType, |
| 77 |
WRAPPER_DESCRIPTOR: types.WrapperDescriptorType, |
| 78 |
staticmethod(defs.SpamOkay.okay): None, |
| 79 |
classmethod(defs.SpamOkay.okay): None, |
| 80 |
property(defs.SpamOkay.okay): None, |
| 81 |
} |
| 82 |
BUILTIN_FUNCTIONS = [ |
| 83 |
# types.BuiltinFunctionType |
| 84 |
len, |
| 85 |
sys.is_finalizing, |
| 86 |
sys.exit, |
| 87 |
_testinternalcapi.get_crossinterp_data, |
| 88 |
] |
| 89 |
assert 'emptymod' not in sys.modules |
| 90 |
with import_helper.ready_to_import('emptymod', ''): |
| 91 |
import emptymod as EMPTYMOD |
| 92 |
MODULES = [ |
| 93 |
sys, |
| 94 |
defs, |
| 95 |
unittest, |
| 96 |
EMPTYMOD, |
| 97 |
] |
| 98 |
OBJECT = object() |
| 99 |
EXCEPTION = Exception() |
| 100 |
LAMBDA = (lambda: None) |
| 101 |
BUILTIN_SIMPLE = [ |
| 102 |
OBJECT, |
| 103 |
# singletons |
| 104 |
None, |
| 105 |
True, |
| 106 |
False, |
| 107 |
Ellipsis, |
| 108 |
NotImplemented, |
| 109 |
# bytes |
| 110 |
*(i.to_bytes(2, 'little', signed=True) |
| 111 |
for i in range(-1, 258)), |
| 112 |
# str |
| 113 |
'hello world', |
| 114 |
'你好世界', |
| 115 |
'', |
| 116 |
# int |
| 117 |
sys.maxsize + 1, |
| 118 |
sys.maxsize, |
| 119 |
-sys.maxsize - 1, |
| 120 |
-sys.maxsize - 2, |
| 121 |
*range(-1, 258), |
| 122 |
2**1000, |
| 123 |
# float |
| 124 |
0.0, |
| 125 |
1.1, |
| 126 |
-1.0, |
| 127 |
0.12345678, |
| 128 |
-0.12345678, |
| 129 |
] |
| 130 |
TUPLE_EXCEPTION = (0, 1.0, EXCEPTION) |
| 131 |
TUPLE_OBJECT = (0, 1.0, OBJECT) |
| 132 |
TUPLE_NESTED_EXCEPTION = (0, 1.0, (EXCEPTION,)) |
| 133 |
TUPLE_NESTED_OBJECT = (0, 1.0, (OBJECT,)) |
| 134 |
MEMORYVIEW_EMPTY = memoryview(b'') |
| 135 |
MEMORYVIEW_NOT_EMPTY = memoryview(b'spam'*42) |
| 136 |
MAPPING_PROXY_EMPTY = types.MappingProxyType({}) |
| 137 |
BUILTIN_CONTAINERS = [ |
| 138 |
# tuple (flat) |
| 139 |
(), |
| 140 |
(1,), |
| 141 |
("hello", "world", ), |
| 142 |
(1, True, "hello"), |
| 143 |
TUPLE_EXCEPTION, |
| 144 |
TUPLE_OBJECT, |
| 145 |
# tuple (nested) |
| 146 |
((1,),), |
| 147 |
((1, 2), (3, 4)), |
| 148 |
((1, 2), (3, 4), (5, 6)), |
| 149 |
TUPLE_NESTED_EXCEPTION, |
| 150 |
TUPLE_NESTED_OBJECT, |
| 151 |
# buffer |
| 152 |
MEMORYVIEW_EMPTY, |
| 153 |
MEMORYVIEW_NOT_EMPTY, |
| 154 |
# list |
| 155 |
[], |
| 156 |
[1, 2, 3], |
| 157 |
[[1], (2,), {3: 4}], |
| 158 |
# dict |
| 159 |
{}, |
| 160 |
{1: 7, 2: 8, 3: 9}, |
| 161 |
{1: [1], 2: (2,), 3: {3: 4}}, |
| 162 |
# set |
| 163 |
set(), |
| 164 |
{1, 2, 3}, |
| 165 |
{frozenset({1}), (2,)}, |
| 166 |
# frozenset |
| 167 |
frozenset([]), |
| 168 |
frozenset({frozenset({1}), (2,)}), |
| 169 |
# bytearray |
| 170 |
bytearray(b''), |
| 171 |
# other |
| 172 |
MAPPING_PROXY_EMPTY, |
| 173 |
types.SimpleNamespace(), |
| 174 |
] |
| 175 |
ns = {} |
| 176 |
exec(""" |
| 177 |
try: |
| 178 |
raise Exception |
| 179 |
except Exception as exc: |
| 180 |
TRACEBACK = exc.__traceback__ |
| 181 |
FRAME = TRACEBACK.tb_frame |
| 182 |
""", ns, ns) |
| 183 |
BUILTIN_OTHER = [ |
| 184 |
# types.CellType |
| 185 |
types.CellType(), |
| 186 |
# types.FrameType |
| 187 |
ns['FRAME'], |
| 188 |
# types.TracebackType |
| 189 |
ns['TRACEBACK'], |
| 190 |
] |
| 191 |
del ns |
| 192 |
|
| 193 |
# user-defined objects |
| 194 |
|
| 195 |
USER_TOP_INSTANCES = [c(*a) for c, a in defs.TOP_CLASSES.items()] |
| 196 |
USER_NESTED_INSTANCES = [c(*a) for c, a in defs.NESTED_CLASSES.items()] |
| 197 |
USER_INSTANCES = [ |
| 198 |
*USER_TOP_INSTANCES, |
| 199 |
*USER_NESTED_INSTANCES, |
| 200 |
] |
| 201 |
USER_EXCEPTIONS = [ |
| 202 |
defs.MimimalError('error!'), |
| 203 |
] |
| 204 |
|
| 205 |
# shareable objects |
| 206 |
|
| 207 |
TUPLES_WITHOUT_EQUALITY = [ |
| 208 |
TUPLE_EXCEPTION, |
| 209 |
TUPLE_OBJECT, |
| 210 |
TUPLE_NESTED_EXCEPTION, |
| 211 |
TUPLE_NESTED_OBJECT, |
| 212 |
] |
| 213 |
_UNSHAREABLE_SIMPLE = [ |
| 214 |
Ellipsis, |
| 215 |
NotImplemented, |
| 216 |
OBJECT, |
| 217 |
sys.maxsize + 1, |
| 218 |
-sys.maxsize - 2, |
| 219 |
2**1000, |
| 220 |
] |
| 221 |
with ignore_byteswarning(): |
| 222 |
_SHAREABLE_SIMPLE = [o for o in BUILTIN_SIMPLE |
| 223 |
if o not in _UNSHAREABLE_SIMPLE] |
| 224 |
_SHAREABLE_CONTAINERS = [ |
| 225 |
*(o for o in BUILTIN_CONTAINERS if type(o) is memoryview), |
| 226 |
*(o for o in BUILTIN_CONTAINERS |
| 227 |
if type(o) is tuple and o not in TUPLES_WITHOUT_EQUALITY), |
| 228 |
] |
| 229 |
_UNSHAREABLE_CONTAINERS = [o for o in BUILTIN_CONTAINERS |
| 230 |
if o not in _SHAREABLE_CONTAINERS] |
| 231 |
SHAREABLE = [ |
| 232 |
*_SHAREABLE_SIMPLE, |
| 233 |
*_SHAREABLE_CONTAINERS, |
| 234 |
] |
| 235 |
NOT_SHAREABLE = [ |
| 236 |
*_UNSHAREABLE_SIMPLE, |
| 237 |
*_UNSHAREABLE_CONTAINERS, |
| 238 |
*BUILTIN_TYPES, |
| 239 |
*BUILTIN_WRAPPERS, |
| 240 |
*BUILTIN_EXCEPTIONS, |
| 241 |
*BUILTIN_FUNCTIONS, |
| 242 |
*MODULES, |
| 243 |
*BUILTIN_OTHER, |
| 244 |
# types.CodeType |
| 245 |
*(f.__code__ for f in defs.FUNCTIONS), |
| 246 |
*(f.__code__ for f in defs.FUNCTION_LIKE), |
| 247 |
# types.FunctionType |
| 248 |
*defs.FUNCTIONS, |
| 249 |
defs.SpamOkay.okay, |
| 250 |
LAMBDA, |
| 251 |
*defs.FUNCTION_LIKE, |
| 252 |
# coroutines and generators |
| 253 |
*defs.FUNCTION_LIKE_APPLIED, |
| 254 |
# user classes |
| 255 |
*defs.CLASSES, |
| 256 |
*USER_INSTANCES, |
| 257 |
# user exceptions |
| 258 |
*USER_EXCEPTIONS, |
| 259 |
] |
| 260 |
|
| 261 |
# pickleable objects |
| 262 |
|
| 263 |
PICKLEABLE = [ |
| 264 |
*BUILTIN_SIMPLE, |
| 265 |
*(o for o in BUILTIN_CONTAINERS if o not in [ |
| 266 |
MEMORYVIEW_EMPTY, |
| 267 |
MEMORYVIEW_NOT_EMPTY, |
| 268 |
MAPPING_PROXY_EMPTY, |
| 269 |
] or type(o) is dict), |
| 270 |
*BUILTINS_TYPES, |
| 271 |
*BUILTIN_EXCEPTIONS, |
| 272 |
*BUILTIN_FUNCTIONS, |
| 273 |
*defs.TOP_FUNCTIONS, |
| 274 |
defs.SpamOkay.okay, |
| 275 |
*defs.FUNCTION_LIKE, |
| 276 |
*defs.TOP_CLASSES, |
| 277 |
*USER_TOP_INSTANCES, |
| 278 |
*USER_EXCEPTIONS, |
| 279 |
# from OTHER_TYPES |
| 280 |
types.NoneType, |
| 281 |
types.EllipsisType, |
| 282 |
types.NotImplementedType, |
| 283 |
types.GenericAlias, |
| 284 |
types.UnionType, |
| 285 |
types.SimpleNamespace, |
| 286 |
# from BUILTIN_WRAPPERS |
| 287 |
METHOD, |
| 288 |
BUILTIN_METHOD, |
| 289 |
METHOD_DESCRIPTOR_WRAPPER, |
| 290 |
METHOD_WRAPPER, |
| 291 |
WRAPPER_DESCRIPTOR, |
| 292 |
] |
| 293 |
assert not any(isinstance(o, types.MappingProxyType) for o in PICKLEABLE) |
| 294 |
|
| 295 |
|
| 296 |
# helpers |
| 297 |
|
| 298 |
DEFS = defs |
| 299 |
with open(code_defs.__file__) as infile: |
| 300 |
_code_defs_text = infile.read() |
| 301 |
with open(DEFS.__file__) as infile: |
| 302 |
_defs_text = infile.read() |
| 303 |
_defs_text = _defs_text.replace('from ', '# from ') |
| 304 |
DEFS_TEXT = f""" |
| 305 |
####################################### |
| 306 |
# from {code_defs.__file__} |
| 307 |
|
| 308 |
{_code_defs_text} |
| 309 |
|
| 310 |
####################################### |
| 311 |
# from {defs.__file__} |
| 312 |
|
| 313 |
{_defs_text} |
| 314 |
""" |
| 315 |
del infile, _code_defs_text, _defs_text |
| 316 |
|
| 317 |
|
| 318 |
def load_defs(module=None): |
| 319 |
"""Return a new copy of the test._crossinterp_definitions module. |
| 320 |
|
| 321 |
The module's __name__ matches the "module" arg, which is either |
| 322 |
a str or a module. |
| 323 |
|
| 324 |
If the "module" arg is a module then the just-loaded defs are also |
| 325 |
copied into that module. |
| 326 |
|
| 327 |
Note that the new module is not added to sys.modules. |
| 328 |
""" |
| 329 |
if module is None: |
| 330 |
modname = DEFS.__name__ |
| 331 |
elif isinstance(module, str): |
| 332 |
modname = module |
| 333 |
module = None |
| 334 |
else: |
| 335 |
modname = module.__name__ |
| 336 |
# Create the new module and populate it. |
| 337 |
defs = import_helper.create_module(modname) |
| 338 |
defs.__file__ = DEFS.__file__ |
| 339 |
exec(DEFS_TEXT, defs.__dict__) |
| 340 |
# Copy the defs into the module arg, if any. |
| 341 |
if module is not None: |
| 342 |
for name, value in defs.__dict__.items(): |
| 343 |
if name.startswith('_'): |
| 344 |
continue |
| 345 |
assert not hasattr(module, name), (name, getattr(module, name)) |
| 346 |
setattr(module, name, value) |
| 347 |
return defs |
| 348 |
|
| 349 |
|
| 350 |
@contextlib.contextmanager |
| 351 |
def using___main__(): |
| 352 |
"""Make sure __main__ module exists (and clean up after).""" |
| 353 |
modname = '__main__' |
| 354 |
if modname not in sys.modules: |
| 355 |
with import_helper.isolated_modules(): |
| 356 |
yield import_helper.add_module(modname) |
| 357 |
else: |
| 358 |
with import_helper.module_restored(modname) as mod: |
| 359 |
yield mod |
| 360 |
|
| 361 |
|
| 362 |
@contextlib.contextmanager |
| 363 |
def temp_module(modname): |
| 364 |
"""Create the module and add to sys.modules, then remove it after.""" |
| 365 |
assert modname not in sys.modules, (modname,) |
| 366 |
with import_helper.isolated_modules(): |
| 367 |
yield import_helper.add_module(modname) |
| 368 |
|
| 369 |
|
| 370 |
@contextlib.contextmanager |
| 371 |
def missing_defs_module(modname, *, prep=False): |
| 372 |
assert modname not in sys.modules, (modname,) |
| 373 |
if prep: |
| 374 |
with import_helper.ready_to_import(modname, DEFS_TEXT): |
| 375 |
yield modname |
| 376 |
else: |
| 377 |
with import_helper.isolated_modules(): |
| 378 |
yield modname |
| 379 |
|
| 380 |
|
| 381 |
class _GetXIDataTests(unittest.TestCase): |
| 382 |
|
| 383 |
MODE = None |
| 384 |
|
| 385 |
def assert_functions_equal(self, func1, func2): |
| 386 |
assert type(func1) is types.FunctionType, repr(func1) |
| 387 |
assert type(func2) is types.FunctionType, repr(func2) |
| 388 |
self.assertEqual(func1.__name__, func2.__name__) |
| 389 |
self.assertEqual(func1.__code__, func2.__code__) |
| 390 |
self.assertEqual(func1.__defaults__, func2.__defaults__) |
| 391 |
self.assertEqual(func1.__kwdefaults__, func2.__kwdefaults__) |
| 392 |
# We don't worry about __globals__ for now. |
| 393 |
|
| 394 |
def assert_exc_args_equal(self, exc1, exc2): |
| 395 |
args1 = exc1.args |
| 396 |
args2 = exc2.args |
| 397 |
if isinstance(exc1, ExceptionGroup): |
| 398 |
self.assertIs(type(args1), type(args2)) |
| 399 |
self.assertEqual(len(args1), 2) |
| 400 |
self.assertEqual(len(args1), len(args2)) |
| 401 |
self.assertEqual(args1[0], args2[0]) |
| 402 |
group1 = args1[1] |
| 403 |
group2 = args2[1] |
| 404 |
self.assertEqual(len(group1), len(group2)) |
| 405 |
for grouped1, grouped2 in zip(group1, group2): |
| 406 |
# Currently the "extra" attrs are not preserved |
| 407 |
# (via __reduce__). |
| 408 |
self.assertIs(type(exc1), type(exc2)) |
| 409 |
self.assert_exc_equal(grouped1, grouped2) |
| 410 |
else: |
| 411 |
self.assertEqual(args1, args2) |
| 412 |
|
| 413 |
def assert_exc_equal(self, exc1, exc2): |
| 414 |
self.assertIs(type(exc1), type(exc2)) |
| 415 |
|
| 416 |
if type(exc1).__eq__ is not object.__eq__: |
| 417 |
self.assertEqual(exc1, exc2) |
| 418 |
|
| 419 |
self.assert_exc_args_equal(exc1, exc2) |
| 420 |
# XXX For now we do not preserve tracebacks. |
| 421 |
if exc1.__traceback__ is not None: |
| 422 |
self.assertEqual(exc1.__traceback__, exc2.__traceback__) |
| 423 |
self.assertEqual( |
| 424 |
getattr(exc1, '__notes__', None), |
| 425 |
getattr(exc2, '__notes__', None), |
| 426 |
) |
| 427 |
# We assume there are no cycles. |
| 428 |
if exc1.__cause__ is None: |
| 429 |
self.assertIs(exc1.__cause__, exc2.__cause__) |
| 430 |
else: |
| 431 |
self.assert_exc_equal(exc1.__cause__, exc2.__cause__) |
| 432 |
if exc1.__context__ is None: |
| 433 |
self.assertIs(exc1.__context__, exc2.__context__) |
| 434 |
else: |
| 435 |
self.assert_exc_equal(exc1.__context__, exc2.__context__) |
| 436 |
|
| 437 |
def assert_equal_or_equalish(self, obj, expected): |
| 438 |
cls = type(expected) |
| 439 |
if cls.__eq__ is not object.__eq__: |
| 440 |
self.assertEqual(obj, expected) |
| 441 |
elif cls is types.FunctionType: |
| 442 |
self.assert_functions_equal(obj, expected) |
| 443 |
elif isinstance(expected, BaseException): |
| 444 |
self.assert_exc_equal(obj, expected) |
| 445 |
elif cls is types.MethodType: |
| 446 |
raise NotImplementedError(cls) |
| 447 |
elif cls is types.BuiltinMethodType: |
| 448 |
raise NotImplementedError(cls) |
| 449 |
elif cls is types.MethodWrapperType: |
| 450 |
raise NotImplementedError(cls) |
| 451 |
elif cls.__bases__ == (object,): |
| 452 |
self.assertEqual(obj.__dict__, expected.__dict__) |
| 453 |
else: |
| 454 |
raise NotImplementedError(cls) |
| 455 |
|
| 456 |
def get_xidata(self, obj, *, mode=None): |
| 457 |
mode = self._resolve_mode(mode) |
| 458 |
return _testinternalcapi.get_crossinterp_data(obj, mode) |
| 459 |
|
| 460 |
def get_roundtrip(self, obj, *, mode=None): |
| 461 |
mode = self._resolve_mode(mode) |
| 462 |
return self._get_roundtrip(obj, mode) |
| 463 |
|
| 464 |
def _get_roundtrip(self, obj, mode): |
| 465 |
xid = _testinternalcapi.get_crossinterp_data(obj, mode) |
| 466 |
return _testinternalcapi.restore_crossinterp_data(xid) |
| 467 |
|
| 468 |
def assert_roundtrip_identical(self, values, *, mode=None): |
| 469 |
mode = self._resolve_mode(mode) |
| 470 |
for obj in values: |
| 471 |
with self.subTest(repr(obj)): |
| 472 |
got = self._get_roundtrip(obj, mode) |
| 473 |
self.assertIs(got, obj) |
| 474 |
|
| 475 |
def assert_roundtrip_equal(self, values, *, mode=None, expecttype=None): |
| 476 |
mode = self._resolve_mode(mode) |
| 477 |
for obj in values: |
| 478 |
with self.subTest(repr(obj)): |
| 479 |
got = self._get_roundtrip(obj, mode) |
| 480 |
if got is obj: |
| 481 |
continue |
| 482 |
self.assertIs(type(got), |
| 483 |
type(obj) if expecttype is None else expecttype) |
| 484 |
self.assert_equal_or_equalish(got, obj) |
| 485 |
|
| 486 |
def assert_roundtrip_equal_not_identical(self, values, *, |
| 487 |
mode=None, expecttype=None): |
| 488 |
mode = self._resolve_mode(mode) |
| 489 |
for obj in values: |
| 490 |
with self.subTest(repr(obj)): |
| 491 |
got = self._get_roundtrip(obj, mode) |
| 492 |
self.assertIsNot(got, obj) |
| 493 |
self.assertIs(type(got), |
| 494 |
type(obj) if expecttype is None else expecttype) |
| 495 |
self.assert_equal_or_equalish(got, obj) |
| 496 |
|
| 497 |
def assert_roundtrip_not_equal(self, values, *, |
| 498 |
mode=None, expecttype=None): |
| 499 |
mode = self._resolve_mode(mode) |
| 500 |
for obj in values: |
| 501 |
with self.subTest(repr(obj)): |
| 502 |
got = self._get_roundtrip(obj, mode) |
| 503 |
self.assertIsNot(got, obj) |
| 504 |
self.assertIs(type(got), |
| 505 |
type(obj) if expecttype is None else expecttype) |
| 506 |
self.assertNotEqual(got, obj) |
| 507 |
|
| 508 |
def assert_not_shareable(self, values, exctype=None, *, mode=None): |
| 509 |
mode = self._resolve_mode(mode) |
| 510 |
for obj in values: |
| 511 |
with self.subTest(repr(obj)): |
| 512 |
with self.assertRaises(NotShareableError) as cm: |
| 513 |
_testinternalcapi.get_crossinterp_data(obj, mode) |
| 514 |
if exctype is not None: |
| 515 |
self.assertIsInstance(cm.exception.__cause__, exctype) |
| 516 |
|
| 517 |
def _resolve_mode(self, mode): |
| 518 |
if mode is None: |
| 519 |
mode = self.MODE |
| 520 |
assert mode |
| 521 |
return mode |
| 522 |
|
| 523 |
|
| 524 |
class PickleTests(_GetXIDataTests): |
| 525 |
|
| 526 |
MODE = 'pickle' |
| 527 |
|
| 528 |
def test_shareable(self): |
| 529 |
with ignore_byteswarning(): |
| 530 |
for obj in SHAREABLE: |
| 531 |
if obj in PICKLEABLE: |
| 532 |
self.assert_roundtrip_equal([obj]) |
| 533 |
else: |
| 534 |
self.assert_not_shareable([obj]) |
| 535 |
|
| 536 |
def test_not_shareable(self): |
| 537 |
with ignore_byteswarning(): |
| 538 |
for obj in NOT_SHAREABLE: |
| 539 |
if type(obj) is types.MappingProxyType: |
| 540 |
self.assert_not_shareable([obj]) |
| 541 |
elif obj in PICKLEABLE: |
| 542 |
with self.subTest(repr(obj)): |
| 543 |
# We don't worry about checking the actual value. |
| 544 |
# The other tests should cover that well enough. |
| 545 |
got = self.get_roundtrip(obj) |
| 546 |
self.assertIs(type(got), type(obj)) |
| 547 |
else: |
| 548 |
self.assert_not_shareable([obj]) |
| 549 |
|
| 550 |
def test_list(self): |
| 551 |
self.assert_roundtrip_equal_not_identical([ |
| 552 |
[], |
| 553 |
[1, 2, 3], |
| 554 |
[[1], (2,), {3: 4}], |
| 555 |
]) |
| 556 |
|
| 557 |
def test_dict(self): |
| 558 |
self.assert_roundtrip_equal_not_identical([ |
| 559 |
{}, |
| 560 |
{1: 7, 2: 8, 3: 9}, |
| 561 |
{1: [1], 2: (2,), 3: {3: 4}}, |
| 562 |
]) |
| 563 |
|
| 564 |
def test_set(self): |
| 565 |
self.assert_roundtrip_equal_not_identical([ |
| 566 |
set(), |
| 567 |
{1, 2, 3}, |
| 568 |
{frozenset({1}), (2,)}, |
| 569 |
]) |
| 570 |
|
| 571 |
# classes |
| 572 |
|
| 573 |
def assert_class_defs_same(self, defs): |
| 574 |
# Unpickle relative to the unchanged original module. |
| 575 |
self.assert_roundtrip_identical(defs.TOP_CLASSES) |
| 576 |
|
| 577 |
instances = [] |
| 578 |
for cls, args in defs.TOP_CLASSES.items(): |
| 579 |
if cls in defs.CLASSES_WITHOUT_EQUALITY: |
| 580 |
continue |
| 581 |
instances.append(cls(*args)) |
| 582 |
self.assert_roundtrip_equal_not_identical(instances) |
| 583 |
|
| 584 |
# these don't compare equal |
| 585 |
instances = [] |
| 586 |
for cls, args in defs.TOP_CLASSES.items(): |
| 587 |
if cls not in defs.CLASSES_WITHOUT_EQUALITY: |
| 588 |
continue |
| 589 |
instances.append(cls(*args)) |
| 590 |
self.assert_roundtrip_equal(instances) |
| 591 |
|
| 592 |
def assert_class_defs_other_pickle(self, defs, mod): |
| 593 |
# Pickle relative to a different module than the original. |
| 594 |
for cls in defs.TOP_CLASSES: |
| 595 |
assert not hasattr(mod, cls.__name__), (cls, getattr(mod, cls.__name__)) |
| 596 |
self.assert_not_shareable(defs.TOP_CLASSES) |
| 597 |
|
| 598 |
instances = [] |
| 599 |
for cls, args in defs.TOP_CLASSES.items(): |
| 600 |
instances.append(cls(*args)) |
| 601 |
self.assert_not_shareable(instances) |
| 602 |
|
| 603 |
def assert_class_defs_other_unpickle(self, defs, mod, *, fail=False): |
| 604 |
# Unpickle relative to a different module than the original. |
| 605 |
for cls in defs.TOP_CLASSES: |
| 606 |
assert not hasattr(mod, cls.__name__), (cls, getattr(mod, cls.__name__)) |
| 607 |
|
| 608 |
instances = [] |
| 609 |
for cls, args in defs.TOP_CLASSES.items(): |
| 610 |
with self.subTest(repr(cls)): |
| 611 |
setattr(mod, cls.__name__, cls) |
| 612 |
xid = self.get_xidata(cls) |
| 613 |
inst = cls(*args) |
| 614 |
instxid = self.get_xidata(inst) |
| 615 |
instances.append( |
| 616 |
(cls, xid, inst, instxid)) |
| 617 |
|
| 618 |
for cls, xid, inst, instxid in instances: |
| 619 |
with self.subTest(repr(cls)): |
| 620 |
delattr(mod, cls.__name__) |
| 621 |
if fail: |
| 622 |
with self.assertRaises(NotShareableError): |
| 623 |
_testinternalcapi.restore_crossinterp_data(xid) |
| 624 |
continue |
| 625 |
got = _testinternalcapi.restore_crossinterp_data(xid) |
| 626 |
self.assertIsNot(got, cls) |
| 627 |
self.assertNotEqual(got, cls) |
| 628 |
|
| 629 |
gotcls = got |
| 630 |
got = _testinternalcapi.restore_crossinterp_data(instxid) |
| 631 |
self.assertIsNot(got, inst) |
| 632 |
self.assertIs(type(got), gotcls) |
| 633 |
if cls in defs.CLASSES_WITHOUT_EQUALITY: |
| 634 |
self.assertNotEqual(got, inst) |
| 635 |
elif cls in defs.BUILTIN_SUBCLASSES: |
| 636 |
self.assertEqual(got, inst) |
| 637 |
else: |
| 638 |
self.assertNotEqual(got, inst) |
| 639 |
|
| 640 |
def assert_class_defs_not_shareable(self, defs): |
| 641 |
self.assert_not_shareable(defs.TOP_CLASSES) |
| 642 |
|
| 643 |
instances = [] |
| 644 |
for cls, args in defs.TOP_CLASSES.items(): |
| 645 |
instances.append(cls(*args)) |
| 646 |
self.assert_not_shareable(instances) |
| 647 |
|
| 648 |
def test_user_class_normal(self): |
| 649 |
self.assert_class_defs_same(defs) |
| 650 |
|
| 651 |
def test_user_class_in___main__(self): |
| 652 |
with using___main__() as mod: |
| 653 |
defs = load_defs(mod) |
| 654 |
self.assert_class_defs_same(defs) |
| 655 |
|
| 656 |
def test_user_class_not_in___main___with_filename(self): |
| 657 |
with using___main__() as mod: |
| 658 |
defs = load_defs('__main__') |
| 659 |
assert defs.__file__ |
| 660 |
mod.__file__ = defs.__file__ |
| 661 |
self.assert_class_defs_not_shareable(defs) |
| 662 |
|
| 663 |
def test_user_class_not_in___main___without_filename(self): |
| 664 |
with using___main__() as mod: |
| 665 |
defs = load_defs('__main__') |
| 666 |
defs.__file__ = None |
| 667 |
mod.__file__ = None |
| 668 |
self.assert_class_defs_not_shareable(defs) |
| 669 |
|
| 670 |
def test_user_class_not_in___main___unpickle_with_filename(self): |
| 671 |
with using___main__() as mod: |
| 672 |
defs = load_defs('__main__') |
| 673 |
assert defs.__file__ |
| 674 |
mod.__file__ = defs.__file__ |
| 675 |
self.assert_class_defs_other_unpickle(defs, mod) |
| 676 |
|
| 677 |
def test_user_class_not_in___main___unpickle_without_filename(self): |
| 678 |
with using___main__() as mod: |
| 679 |
defs = load_defs('__main__') |
| 680 |
defs.__file__ = None |
| 681 |
mod.__file__ = None |
| 682 |
self.assert_class_defs_other_unpickle(defs, mod, fail=True) |
| 683 |
|
| 684 |
def test_user_class_in_module(self): |
| 685 |
with temp_module('__spam__') as mod: |
| 686 |
defs = load_defs(mod) |
| 687 |
self.assert_class_defs_same(defs) |
| 688 |
|
| 689 |
def test_user_class_not_in_module_with_filename(self): |
| 690 |
with temp_module('__spam__') as mod: |
| 691 |
defs = load_defs(mod.__name__) |
| 692 |
assert defs.__file__ |
| 693 |
# For now, we only address this case for __main__. |
| 694 |
self.assert_class_defs_not_shareable(defs) |
| 695 |
|
| 696 |
def test_user_class_not_in_module_without_filename(self): |
| 697 |
with temp_module('__spam__') as mod: |
| 698 |
defs = load_defs(mod.__name__) |
| 699 |
defs.__file__ = None |
| 700 |
self.assert_class_defs_not_shareable(defs) |
| 701 |
|
| 702 |
def test_user_class_module_missing_then_imported(self): |
| 703 |
with missing_defs_module('__spam__', prep=True) as modname: |
| 704 |
defs = load_defs(modname) |
| 705 |
# For now, we only address this case for __main__. |
| 706 |
self.assert_class_defs_not_shareable(defs) |
| 707 |
|
| 708 |
def test_user_class_module_missing_not_available(self): |
| 709 |
with missing_defs_module('__spam__') as modname: |
| 710 |
defs = load_defs(modname) |
| 711 |
self.assert_class_defs_not_shareable(defs) |
| 712 |
|
| 713 |
def test_nested_class(self): |
| 714 |
eggs = defs.EggsNested() |
| 715 |
with self.assertRaises(NotShareableError): |
| 716 |
self.get_roundtrip(eggs) |
| 717 |
|
| 718 |
# functions |
| 719 |
|
| 720 |
def assert_func_defs_same(self, defs): |
| 721 |
# Unpickle relative to the unchanged original module. |
| 722 |
self.assert_roundtrip_identical(defs.TOP_FUNCTIONS) |
| 723 |
|
| 724 |
def assert_func_defs_other_pickle(self, defs, mod): |
| 725 |
# Pickle relative to a different module than the original. |
| 726 |
for func in defs.TOP_FUNCTIONS: |
| 727 |
assert not hasattr(mod, func.__name__), (getattr(mod, func.__name__),) |
| 728 |
self.assert_not_shareable(defs.TOP_FUNCTIONS) |
| 729 |
|
| 730 |
def assert_func_defs_other_unpickle(self, defs, mod, *, fail=False): |
| 731 |
# Unpickle relative to a different module than the original. |
| 732 |
for func in defs.TOP_FUNCTIONS: |
| 733 |
assert not hasattr(mod, func.__name__), (getattr(mod, func.__name__),) |
| 734 |
|
| 735 |
captured = [] |
| 736 |
for func in defs.TOP_FUNCTIONS: |
| 737 |
with self.subTest(func): |
| 738 |
setattr(mod, func.__name__, func) |
| 739 |
xid = self.get_xidata(func) |
| 740 |
captured.append( |
| 741 |
(func, xid)) |
| 742 |
|
| 743 |
for func, xid in captured: |
| 744 |
with self.subTest(func): |
| 745 |
delattr(mod, func.__name__) |
| 746 |
if fail: |
| 747 |
with self.assertRaises(NotShareableError): |
| 748 |
_testinternalcapi.restore_crossinterp_data(xid) |
| 749 |
continue |
| 750 |
got = _testinternalcapi.restore_crossinterp_data(xid) |
| 751 |
self.assertIsNot(got, func) |
| 752 |
self.assertNotEqual(got, func) |
| 753 |
|
| 754 |
def assert_func_defs_not_shareable(self, defs): |
| 755 |
self.assert_not_shareable(defs.TOP_FUNCTIONS) |
| 756 |
|
| 757 |
def test_user_function_normal(self): |
| 758 |
self.assert_roundtrip_equal(defs.TOP_FUNCTIONS) |
| 759 |
self.assert_func_defs_same(defs) |
| 760 |
|
| 761 |
def test_user_func_in___main__(self): |
| 762 |
with using___main__() as mod: |
| 763 |
defs = load_defs(mod) |
| 764 |
self.assert_func_defs_same(defs) |
| 765 |
|
| 766 |
def test_user_func_not_in___main___with_filename(self): |
| 767 |
with using___main__() as mod: |
| 768 |
defs = load_defs('__main__') |
| 769 |
assert defs.__file__ |
| 770 |
mod.__file__ = defs.__file__ |
| 771 |
self.assert_func_defs_not_shareable(defs) |
| 772 |
|
| 773 |
def test_user_func_not_in___main___without_filename(self): |
| 774 |
with using___main__() as mod: |
| 775 |
defs = load_defs('__main__') |
| 776 |
defs.__file__ = None |
| 777 |
mod.__file__ = None |
| 778 |
self.assert_func_defs_not_shareable(defs) |
| 779 |
|
| 780 |
def test_user_func_not_in___main___unpickle_with_filename(self): |
| 781 |
with using___main__() as mod: |
| 782 |
defs = load_defs('__main__') |
| 783 |
assert defs.__file__ |
| 784 |
mod.__file__ = defs.__file__ |
| 785 |
self.assert_func_defs_other_unpickle(defs, mod) |
| 786 |
|
| 787 |
def test_user_func_not_in___main___unpickle_without_filename(self): |
| 788 |
with using___main__() as mod: |
| 789 |
defs = load_defs('__main__') |
| 790 |
defs.__file__ = None |
| 791 |
mod.__file__ = None |
| 792 |
self.assert_func_defs_other_unpickle(defs, mod, fail=True) |
| 793 |
|
| 794 |
def test_user_func_in_module(self): |
| 795 |
with temp_module('__spam__') as mod: |
| 796 |
defs = load_defs(mod) |
| 797 |
self.assert_func_defs_same(defs) |
| 798 |
|
| 799 |
def test_user_func_not_in_module_with_filename(self): |
| 800 |
with temp_module('__spam__') as mod: |
| 801 |
defs = load_defs(mod.__name__) |
| 802 |
assert defs.__file__ |
| 803 |
# For now, we only address this case for __main__. |
| 804 |
self.assert_func_defs_not_shareable(defs) |
| 805 |
|
| 806 |
def test_user_func_not_in_module_without_filename(self): |
| 807 |
with temp_module('__spam__') as mod: |
| 808 |
defs = load_defs(mod.__name__) |
| 809 |
defs.__file__ = None |
| 810 |
self.assert_func_defs_not_shareable(defs) |
| 811 |
|
| 812 |
def test_user_func_module_missing_then_imported(self): |
| 813 |
with missing_defs_module('__spam__', prep=True) as modname: |
| 814 |
defs = load_defs(modname) |
| 815 |
# For now, we only address this case for __main__. |
| 816 |
self.assert_func_defs_not_shareable(defs) |
| 817 |
|
| 818 |
def test_user_func_module_missing_not_available(self): |
| 819 |
with missing_defs_module('__spam__') as modname: |
| 820 |
defs = load_defs(modname) |
| 821 |
self.assert_func_defs_not_shareable(defs) |
| 822 |
|
| 823 |
def test_nested_function(self): |
| 824 |
self.assert_not_shareable(defs.NESTED_FUNCTIONS) |
| 825 |
|
| 826 |
# exceptions |
| 827 |
|
| 828 |
def test_user_exception_normal(self): |
| 829 |
self.assert_roundtrip_equal([ |
| 830 |
defs.MimimalError('error!'), |
| 831 |
]) |
| 832 |
self.assert_roundtrip_equal_not_identical([ |
| 833 |
defs.RichError('error!', 42), |
| 834 |
]) |
| 835 |
|
| 836 |
def test_builtin_exception(self): |
| 837 |
msg = 'error!' |
| 838 |
try: |
| 839 |
raise Exception |
| 840 |
except Exception as exc: |
| 841 |
caught = exc |
| 842 |
special = { |
| 843 |
BaseExceptionGroup: (msg, [caught]), |
| 844 |
ExceptionGroup: (msg, [caught]), |
| 845 |
UnicodeError: (None, msg, None, None, None), |
| 846 |
UnicodeEncodeError: ('utf-8', '', 1, 3, msg), |
| 847 |
UnicodeDecodeError: ('utf-8', b'', 1, 3, msg), |
| 848 |
UnicodeTranslateError: ('', 1, 3, msg), |
| 849 |
} |
| 850 |
exceptions = [] |
| 851 |
for cls in EXCEPTION_TYPES: |
| 852 |
args = special.get(cls) or (msg,) |
| 853 |
exceptions.append(cls(*args)) |
| 854 |
|
| 855 |
self.assert_roundtrip_equal(exceptions) |
| 856 |
|
| 857 |
|
| 858 |
class MarshalTests(_GetXIDataTests): |
| 859 |
|
| 860 |
MODE = 'marshal' |
| 861 |
|
| 862 |
def test_simple_builtin_singletons(self): |
| 863 |
self.assert_roundtrip_identical([ |
| 864 |
True, |
| 865 |
False, |
| 866 |
None, |
| 867 |
Ellipsis, |
| 868 |
]) |
| 869 |
self.assert_not_shareable([ |
| 870 |
NotImplemented, |
| 871 |
]) |
| 872 |
|
| 873 |
def test_simple_builtin_objects(self): |
| 874 |
self.assert_roundtrip_equal([ |
| 875 |
# int |
| 876 |
*range(-1, 258), |
| 877 |
sys.maxsize + 1, |
| 878 |
sys.maxsize, |
| 879 |
-sys.maxsize - 1, |
| 880 |
-sys.maxsize - 2, |
| 881 |
2**1000, |
| 882 |
# complex |
| 883 |
1+2j, |
| 884 |
# float |
| 885 |
0.0, |
| 886 |
1.1, |
| 887 |
-1.0, |
| 888 |
0.12345678, |
| 889 |
-0.12345678, |
| 890 |
# bytes |
| 891 |
*(i.to_bytes(2, 'little', signed=True) |
| 892 |
for i in range(-1, 258)), |
| 893 |
b'hello world', |
| 894 |
# str |
| 895 |
'hello world', |
| 896 |
'你好世界', |
| 897 |
'', |
| 898 |
]) |
| 899 |
self.assert_not_shareable([ |
| 900 |
OBJECT, |
| 901 |
types.SimpleNamespace(), |
| 902 |
]) |
| 903 |
|
| 904 |
def test_bytearray(self): |
| 905 |
# bytearray is special because it unmarshals to bytes, not bytearray. |
| 906 |
self.assert_roundtrip_equal([ |
| 907 |
bytearray(), |
| 908 |
bytearray(b'hello world'), |
| 909 |
], expecttype=bytes) |
| 910 |
|
| 911 |
def test_compound_immutable_builtin_objects(self): |
| 912 |
self.assert_roundtrip_equal([ |
| 913 |
# tuple |
| 914 |
(), |
| 915 |
(1,), |
| 916 |
("hello", "world"), |
| 917 |
(1, True, "hello"), |
| 918 |
# frozenset |
| 919 |
frozenset([1, 2, 3]), |
| 920 |
]) |
| 921 |
# nested |
| 922 |
self.assert_roundtrip_equal([ |
| 923 |
# tuple |
| 924 |
((1,),), |
| 925 |
((1, 2), (3, 4)), |
| 926 |
((1, 2), (3, 4), (5, 6)), |
| 927 |
# frozenset |
| 928 |
frozenset([frozenset([1]), frozenset([2]), frozenset([3])]), |
| 929 |
]) |
| 930 |
|
| 931 |
def test_compound_mutable_builtin_objects(self): |
| 932 |
self.assert_roundtrip_equal([ |
| 933 |
# list |
| 934 |
[], |
| 935 |
[1, 2, 3], |
| 936 |
# dict |
| 937 |
{}, |
| 938 |
{1: 7, 2: 8, 3: 9}, |
| 939 |
# set |
| 940 |
set(), |
| 941 |
{1, 2, 3}, |
| 942 |
]) |
| 943 |
# nested |
| 944 |
self.assert_roundtrip_equal([ |
| 945 |
[[1], [2], [3]], |
| 946 |
{1: {'a': True}, 2: {'b': False}}, |
| 947 |
{(1, 2, 3,)}, |
| 948 |
]) |
| 949 |
|
| 950 |
def test_compound_builtin_objects_with_bad_items(self): |
| 951 |
bogus = object() |
| 952 |
self.assert_not_shareable([ |
| 953 |
(bogus,), |
| 954 |
frozenset([bogus]), |
| 955 |
[bogus], |
| 956 |
{bogus: True}, |
| 957 |
{True: bogus}, |
| 958 |
{bogus}, |
| 959 |
]) |
| 960 |
|
| 961 |
def test_builtin_code(self): |
| 962 |
self.assert_roundtrip_equal([ |
| 963 |
*(f.__code__ for f in defs.FUNCTIONS), |
| 964 |
*(f.__code__ for f in defs.FUNCTION_LIKE), |
| 965 |
]) |
| 966 |
|
| 967 |
def test_builtin_type(self): |
| 968 |
shareable = [ |
| 969 |
StopIteration, |
| 970 |
] |
| 971 |
types = BUILTIN_TYPES |
| 972 |
self.assert_not_shareable(cls for cls in types |
| 973 |
if cls not in shareable) |
| 974 |
self.assert_roundtrip_identical(cls for cls in types |
| 975 |
if cls in shareable) |
| 976 |
|
| 977 |
def test_builtin_function(self): |
| 978 |
functions = [ |
| 979 |
len, |
| 980 |
sys.is_finalizing, |
| 981 |
sys.exit, |
| 982 |
_testinternalcapi.get_crossinterp_data, |
| 983 |
] |
| 984 |
for func in functions: |
| 985 |
assert type(func) is types.BuiltinFunctionType, func |
| 986 |
|
| 987 |
self.assert_not_shareable(functions) |
| 988 |
|
| 989 |
def test_builtin_exception(self): |
| 990 |
msg = 'error!' |
| 991 |
try: |
| 992 |
raise Exception |
| 993 |
except Exception as exc: |
| 994 |
caught = exc |
| 995 |
special = { |
| 996 |
BaseExceptionGroup: (msg, [caught]), |
| 997 |
ExceptionGroup: (msg, [caught]), |
| 998 |
# UnicodeError: (None, msg, None, None, None), |
| 999 |
UnicodeEncodeError: ('utf-8', '', 1, 3, msg), |
| 1000 |
UnicodeDecodeError: ('utf-8', b'', 1, 3, msg), |
| 1001 |
UnicodeTranslateError: ('', 1, 3, msg), |
| 1002 |
} |
| 1003 |
exceptions = [] |
| 1004 |
for cls in EXCEPTION_TYPES: |
| 1005 |
args = special.get(cls) or (msg,) |
| 1006 |
exceptions.append(cls(*args)) |
| 1007 |
|
| 1008 |
self.assert_not_shareable(exceptions) |
| 1009 |
# Note that StopIteration (the type) can be marshalled, |
| 1010 |
# but its instances cannot. |
| 1011 |
|
| 1012 |
def test_module(self): |
| 1013 |
assert type(sys) is types.ModuleType, type(sys) |
| 1014 |
assert type(defs) is types.ModuleType, type(defs) |
| 1015 |
assert type(unittest) is types.ModuleType, type(defs) |
| 1016 |
|
| 1017 |
assert 'emptymod' not in sys.modules |
| 1018 |
with import_helper.ready_to_import('emptymod', ''): |
| 1019 |
import emptymod |
| 1020 |
|
| 1021 |
self.assert_not_shareable([ |
| 1022 |
sys, |
| 1023 |
defs, |
| 1024 |
unittest, |
| 1025 |
emptymod, |
| 1026 |
]) |
| 1027 |
|
| 1028 |
def test_user_class(self): |
| 1029 |
self.assert_not_shareable(defs.TOP_CLASSES) |
| 1030 |
|
| 1031 |
instances = [] |
| 1032 |
for cls, args in defs.TOP_CLASSES.items(): |
| 1033 |
instances.append(cls(*args)) |
| 1034 |
self.assert_not_shareable(instances) |
| 1035 |
|
| 1036 |
def test_user_function(self): |
| 1037 |
self.assert_not_shareable(defs.TOP_FUNCTIONS) |
| 1038 |
|
| 1039 |
def test_user_exception(self): |
| 1040 |
self.assert_not_shareable([ |
| 1041 |
defs.MimimalError('error!'), |
| 1042 |
defs.RichError('error!', 42), |
| 1043 |
]) |
| 1044 |
|
| 1045 |
|
| 1046 |
class CodeTests(_GetXIDataTests): |
| 1047 |
|
| 1048 |
MODE = 'code' |
| 1049 |
|
| 1050 |
def test_function_code(self): |
| 1051 |
self.assert_roundtrip_equal_not_identical([ |
| 1052 |
*(f.__code__ for f in defs.FUNCTIONS), |
| 1053 |
*(f.__code__ for f in defs.FUNCTION_LIKE), |
| 1054 |
]) |
| 1055 |
|
| 1056 |
def test_functions(self): |
| 1057 |
self.assert_not_shareable([ |
| 1058 |
*defs.FUNCTIONS, |
| 1059 |
*defs.FUNCTION_LIKE, |
| 1060 |
]) |
| 1061 |
|
| 1062 |
def test_other_objects(self): |
| 1063 |
self.assert_not_shareable([ |
| 1064 |
None, |
| 1065 |
True, |
| 1066 |
False, |
| 1067 |
Ellipsis, |
| 1068 |
NotImplemented, |
| 1069 |
9999, |
| 1070 |
'spam', |
| 1071 |
b'spam', |
| 1072 |
(), |
| 1073 |
[], |
| 1074 |
{}, |
| 1075 |
object(), |
| 1076 |
]) |
| 1077 |
|
| 1078 |
|
| 1079 |
class ShareableFuncTests(_GetXIDataTests): |
| 1080 |
|
| 1081 |
MODE = 'func' |
| 1082 |
|
| 1083 |
def test_stateless(self): |
| 1084 |
self.assert_roundtrip_equal([ |
| 1085 |
*defs.STATELESS_FUNCTIONS, |
| 1086 |
# Generators can be stateless too. |
| 1087 |
*defs.FUNCTION_LIKE, |
| 1088 |
]) |
| 1089 |
|
| 1090 |
def test_not_stateless(self): |
| 1091 |
self.assert_not_shareable([ |
| 1092 |
*(f for f in defs.FUNCTIONS |
| 1093 |
if f not in defs.STATELESS_FUNCTIONS), |
| 1094 |
]) |
| 1095 |
|
| 1096 |
def test_other_objects(self): |
| 1097 |
self.assert_not_shareable([ |
| 1098 |
None, |
| 1099 |
True, |
| 1100 |
False, |
| 1101 |
Ellipsis, |
| 1102 |
NotImplemented, |
| 1103 |
9999, |
| 1104 |
'spam', |
| 1105 |
b'spam', |
| 1106 |
(), |
| 1107 |
[], |
| 1108 |
{}, |
| 1109 |
object(), |
| 1110 |
]) |
| 1111 |
|
| 1112 |
|
| 1113 |
class PureShareableScriptTests(_GetXIDataTests): |
| 1114 |
|
| 1115 |
MODE = 'script-pure' |
| 1116 |
|
| 1117 |
VALID_SCRIPTS = [ |
| 1118 |
'', |
| 1119 |
'spam', |
| 1120 |
'# a comment', |
| 1121 |
'print("spam")', |
| 1122 |
'raise Exception("spam")', |
| 1123 |
"""if True: |
| 1124 |
do_something() |
| 1125 |
""", |
| 1126 |
"""if True: |
| 1127 |
def spam(x): |
| 1128 |
return x |
| 1129 |
class Spam: |
| 1130 |
def eggs(self): |
| 1131 |
return 42 |
| 1132 |
x = Spam().eggs() |
| 1133 |
raise ValueError(spam(x)) |
| 1134 |
""", |
| 1135 |
] |
| 1136 |
INVALID_SCRIPTS = [ |
| 1137 |
' pass', # IndentationError |
| 1138 |
'----', # SyntaxError |
| 1139 |
"""if True: |
| 1140 |
def spam(): |
| 1141 |
# no body |
| 1142 |
spam() |
| 1143 |
""", # IndentationError |
| 1144 |
] |
| 1145 |
|
| 1146 |
def test_valid_str(self): |
| 1147 |
self.assert_roundtrip_not_equal([ |
| 1148 |
*self.VALID_SCRIPTS, |
| 1149 |
], expecttype=types.CodeType) |
| 1150 |
|
| 1151 |
def test_invalid_str(self): |
| 1152 |
self.assert_not_shareable([ |
| 1153 |
*self.INVALID_SCRIPTS, |
| 1154 |
]) |
| 1155 |
|
| 1156 |
def test_valid_bytes(self): |
| 1157 |
self.assert_roundtrip_not_equal([ |
| 1158 |
*(s.encode('utf8') for s in self.VALID_SCRIPTS), |
| 1159 |
], expecttype=types.CodeType) |
| 1160 |
|
| 1161 |
def test_invalid_bytes(self): |
| 1162 |
self.assert_not_shareable([ |
| 1163 |
*(s.encode('utf8') for s in self.INVALID_SCRIPTS), |
| 1164 |
]) |
| 1165 |
|
| 1166 |
def test_pure_script_code(self): |
| 1167 |
self.assert_roundtrip_equal_not_identical([ |
| 1168 |
*(f.__code__ for f in defs.PURE_SCRIPT_FUNCTIONS), |
| 1169 |
]) |
| 1170 |
|
| 1171 |
def test_impure_script_code(self): |
| 1172 |
self.assert_not_shareable([ |
| 1173 |
*(f.__code__ for f in defs.SCRIPT_FUNCTIONS |
| 1174 |
if f not in defs.PURE_SCRIPT_FUNCTIONS), |
| 1175 |
]) |
| 1176 |
|
| 1177 |
def test_other_code(self): |
| 1178 |
self.assert_not_shareable([ |
| 1179 |
*(f.__code__ for f in defs.FUNCTIONS |
| 1180 |
if f not in defs.SCRIPT_FUNCTIONS), |
| 1181 |
*(f.__code__ for f in defs.FUNCTION_LIKE), |
| 1182 |
]) |
| 1183 |
|
| 1184 |
def test_pure_script_function(self): |
| 1185 |
self.assert_roundtrip_not_equal([ |
| 1186 |
*defs.PURE_SCRIPT_FUNCTIONS, |
| 1187 |
], expecttype=types.CodeType) |
| 1188 |
|
| 1189 |
def test_impure_script_function(self): |
| 1190 |
self.assert_not_shareable([ |
| 1191 |
*(f for f in defs.SCRIPT_FUNCTIONS |
| 1192 |
if f not in defs.PURE_SCRIPT_FUNCTIONS), |
| 1193 |
]) |
| 1194 |
|
| 1195 |
def test_other_function(self): |
| 1196 |
self.assert_not_shareable([ |
| 1197 |
*(f for f in defs.FUNCTIONS |
| 1198 |
if f not in defs.SCRIPT_FUNCTIONS), |
| 1199 |
*defs.FUNCTION_LIKE, |
| 1200 |
]) |
| 1201 |
|
| 1202 |
def test_other_objects(self): |
| 1203 |
self.assert_not_shareable([ |
| 1204 |
None, |
| 1205 |
True, |
| 1206 |
False, |
| 1207 |
Ellipsis, |
| 1208 |
NotImplemented, |
| 1209 |
(), |
| 1210 |
[], |
| 1211 |
{}, |
| 1212 |
object(), |
| 1213 |
]) |
| 1214 |
|
| 1215 |
|
| 1216 |
class ShareableScriptTests(PureShareableScriptTests): |
| 1217 |
|
| 1218 |
MODE = 'script' |
| 1219 |
|
| 1220 |
def test_impure_script_code(self): |
| 1221 |
self.assert_roundtrip_equal_not_identical([ |
| 1222 |
*(f.__code__ for f in defs.SCRIPT_FUNCTIONS |
| 1223 |
if f not in defs.PURE_SCRIPT_FUNCTIONS), |
| 1224 |
]) |
| 1225 |
|
| 1226 |
def test_impure_script_function(self): |
| 1227 |
self.assert_roundtrip_not_equal([ |
| 1228 |
*(f for f in defs.SCRIPT_FUNCTIONS |
| 1229 |
if f not in defs.PURE_SCRIPT_FUNCTIONS), |
| 1230 |
], expecttype=types.CodeType) |
| 1231 |
|
| 1232 |
|
| 1233 |
class ShareableFallbackTests(_GetXIDataTests): |
| 1234 |
|
| 1235 |
MODE = 'fallback' |
| 1236 |
|
| 1237 |
def test_shareable(self): |
| 1238 |
self.assert_roundtrip_equal(SHAREABLE) |
| 1239 |
|
| 1240 |
def test_not_shareable(self): |
| 1241 |
okay = [ |
| 1242 |
*PICKLEABLE, |
| 1243 |
*defs.STATELESS_FUNCTIONS, |
| 1244 |
LAMBDA, |
| 1245 |
] |
| 1246 |
ignored = [ |
| 1247 |
*TUPLES_WITHOUT_EQUALITY, |
| 1248 |
OBJECT, |
| 1249 |
METHOD, |
| 1250 |
BUILTIN_METHOD, |
| 1251 |
METHOD_WRAPPER, |
| 1252 |
] |
| 1253 |
with ignore_byteswarning(): |
| 1254 |
self.assert_roundtrip_equal([ |
| 1255 |
*(o for o in NOT_SHAREABLE |
| 1256 |
if o in okay and o not in ignored |
| 1257 |
and o is not MAPPING_PROXY_EMPTY), |
| 1258 |
]) |
| 1259 |
self.assert_roundtrip_not_equal([ |
| 1260 |
*(o for o in NOT_SHAREABLE |
| 1261 |
if o in ignored and o is not MAPPING_PROXY_EMPTY), |
| 1262 |
]) |
| 1263 |
self.assert_not_shareable([ |
| 1264 |
*(o for o in NOT_SHAREABLE if o not in okay), |
| 1265 |
MAPPING_PROXY_EMPTY, |
| 1266 |
]) |
| 1267 |
|
| 1268 |
|
| 1269 |
class ShareableTypeTests(_GetXIDataTests): |
| 1270 |
|
| 1271 |
MODE = 'xidata' |
| 1272 |
|
| 1273 |
def test_shareable(self): |
| 1274 |
self.assert_roundtrip_equal(SHAREABLE) |
| 1275 |
|
| 1276 |
def test_singletons(self): |
| 1277 |
self.assert_roundtrip_identical([ |
| 1278 |
None, |
| 1279 |
True, |
| 1280 |
False, |
| 1281 |
]) |
| 1282 |
self.assert_not_shareable([ |
| 1283 |
Ellipsis, |
| 1284 |
NotImplemented, |
| 1285 |
]) |
| 1286 |
|
| 1287 |
def test_types(self): |
| 1288 |
self.assert_roundtrip_equal([ |
| 1289 |
b'spam', |
| 1290 |
9999, |
| 1291 |
]) |
| 1292 |
|
| 1293 |
def test_bytes(self): |
| 1294 |
values = (i.to_bytes(2, 'little', signed=True) |
| 1295 |
for i in range(-1, 258)) |
| 1296 |
self.assert_roundtrip_equal(values) |
| 1297 |
|
| 1298 |
def test_strs(self): |
| 1299 |
self.assert_roundtrip_equal([ |
| 1300 |
'hello world', |
| 1301 |
'你好世界', |
| 1302 |
'', |
| 1303 |
]) |
| 1304 |
|
| 1305 |
def test_int(self): |
| 1306 |
bounds = [sys.maxsize, -sys.maxsize - 1] |
| 1307 |
values = itertools.chain(range(-1, 258), bounds) |
| 1308 |
self.assert_roundtrip_equal(values) |
| 1309 |
|
| 1310 |
def test_non_shareable_int(self): |
| 1311 |
ints = [ |
| 1312 |
sys.maxsize + 1, |
| 1313 |
-sys.maxsize - 2, |
| 1314 |
2**1000, |
| 1315 |
] |
| 1316 |
self.assert_not_shareable(ints, OverflowError) |
| 1317 |
|
| 1318 |
def test_float(self): |
| 1319 |
self.assert_roundtrip_equal([ |
| 1320 |
0.0, |
| 1321 |
1.1, |
| 1322 |
-1.0, |
| 1323 |
0.12345678, |
| 1324 |
-0.12345678, |
| 1325 |
]) |
| 1326 |
|
| 1327 |
def test_tuple(self): |
| 1328 |
self.assert_roundtrip_equal([ |
| 1329 |
(), |
| 1330 |
(1,), |
| 1331 |
("hello", "world", ), |
| 1332 |
(1, True, "hello"), |
| 1333 |
]) |
| 1334 |
# Test nesting |
| 1335 |
self.assert_roundtrip_equal([ |
| 1336 |
((1,),), |
| 1337 |
((1, 2), (3, 4)), |
| 1338 |
((1, 2), (3, 4), (5, 6)), |
| 1339 |
]) |
| 1340 |
|
| 1341 |
def test_tuples_containing_non_shareable_types(self): |
| 1342 |
non_shareables = [ |
| 1343 |
EXCEPTION, |
| 1344 |
OBJECT, |
| 1345 |
] |
| 1346 |
for s in non_shareables: |
| 1347 |
value = tuple([0, 1.0, s]) |
| 1348 |
with self.subTest(repr(value)): |
| 1349 |
with self.assertRaises(NotShareableError): |
| 1350 |
self.get_xidata(value) |
| 1351 |
# Check nested as well |
| 1352 |
value = tuple([0, 1., (s,)]) |
| 1353 |
with self.subTest("nested " + repr(value)): |
| 1354 |
with self.assertRaises(NotShareableError): |
| 1355 |
self.get_xidata(value) |
| 1356 |
|
| 1357 |
# The rest are not shareable. |
| 1358 |
|
| 1359 |
def test_not_shareable(self): |
| 1360 |
self.assert_not_shareable(NOT_SHAREABLE) |
| 1361 |
|
| 1362 |
def test_object(self): |
| 1363 |
self.assert_not_shareable([ |
| 1364 |
object(), |
| 1365 |
]) |
| 1366 |
|
| 1367 |
def test_code(self): |
| 1368 |
# types.CodeType |
| 1369 |
self.assert_not_shareable([ |
| 1370 |
*(f.__code__ for f in defs.FUNCTIONS), |
| 1371 |
*(f.__code__ for f in defs.FUNCTION_LIKE), |
| 1372 |
]) |
| 1373 |
|
| 1374 |
def test_function_object(self): |
| 1375 |
for func in defs.FUNCTIONS: |
| 1376 |
assert type(func) is types.FunctionType, func |
| 1377 |
assert type(defs.SpamOkay.okay) is types.FunctionType, func |
| 1378 |
assert type(LAMBDA) is types.LambdaType |
| 1379 |
|
| 1380 |
self.assert_not_shareable([ |
| 1381 |
*defs.FUNCTIONS, |
| 1382 |
defs.SpamOkay.okay, |
| 1383 |
LAMBDA, |
| 1384 |
]) |
| 1385 |
|
| 1386 |
def test_builtin_function(self): |
| 1387 |
functions = [ |
| 1388 |
len, |
| 1389 |
sys.is_finalizing, |
| 1390 |
sys.exit, |
| 1391 |
_testinternalcapi.get_crossinterp_data, |
| 1392 |
] |
| 1393 |
for func in functions: |
| 1394 |
assert type(func) is types.BuiltinFunctionType, func |
| 1395 |
|
| 1396 |
self.assert_not_shareable(functions) |
| 1397 |
|
| 1398 |
def test_function_like(self): |
| 1399 |
self.assert_not_shareable(defs.FUNCTION_LIKE) |
| 1400 |
self.assert_not_shareable(defs.FUNCTION_LIKE_APPLIED) |
| 1401 |
|
| 1402 |
def test_builtin_wrapper(self): |
| 1403 |
_wrappers = { |
| 1404 |
defs.SpamOkay().okay: types.MethodType, |
| 1405 |
[].append: types.BuiltinMethodType, |
| 1406 |
dict.__dict__['fromkeys']: types.ClassMethodDescriptorType, |
| 1407 |
types.FunctionType.__code__: types.GetSetDescriptorType, |
| 1408 |
types.FunctionType.__globals__: types.MemberDescriptorType, |
| 1409 |
str.join: types.MethodDescriptorType, |
| 1410 |
object().__str__: types.MethodWrapperType, |
| 1411 |
object.__init__: types.WrapperDescriptorType, |
| 1412 |
} |
| 1413 |
for obj, expected in _wrappers.items(): |
| 1414 |
assert type(obj) is expected, (obj, expected) |
| 1415 |
|
| 1416 |
self.assert_not_shareable([ |
| 1417 |
*_wrappers, |
| 1418 |
staticmethod(defs.SpamOkay.okay), |
| 1419 |
classmethod(defs.SpamOkay.okay), |
| 1420 |
property(defs.SpamOkay.okay), |
| 1421 |
]) |
| 1422 |
|
| 1423 |
def test_module(self): |
| 1424 |
assert type(sys) is types.ModuleType, type(sys) |
| 1425 |
assert type(defs) is types.ModuleType, type(defs) |
| 1426 |
assert type(unittest) is types.ModuleType, type(defs) |
| 1427 |
|
| 1428 |
assert 'emptymod' not in sys.modules |
| 1429 |
with import_helper.ready_to_import('emptymod', ''): |
| 1430 |
import emptymod |
| 1431 |
|
| 1432 |
self.assert_not_shareable([ |
| 1433 |
sys, |
| 1434 |
defs, |
| 1435 |
unittest, |
| 1436 |
emptymod, |
| 1437 |
]) |
| 1438 |
|
| 1439 |
def test_class(self): |
| 1440 |
self.assert_not_shareable(defs.CLASSES) |
| 1441 |
|
| 1442 |
instances = [] |
| 1443 |
for cls, args in defs.CLASSES.items(): |
| 1444 |
instances.append(cls(*args)) |
| 1445 |
self.assert_not_shareable(instances) |
| 1446 |
|
| 1447 |
def test_builtin_type(self): |
| 1448 |
self.assert_not_shareable(BUILTIN_TYPES) |
| 1449 |
|
| 1450 |
def test_exception(self): |
| 1451 |
self.assert_not_shareable([ |
| 1452 |
defs.MimimalError('error!'), |
| 1453 |
]) |
| 1454 |
|
| 1455 |
def test_builtin_exception(self): |
| 1456 |
msg = 'error!' |
| 1457 |
try: |
| 1458 |
raise Exception |
| 1459 |
except Exception as exc: |
| 1460 |
caught = exc |
| 1461 |
special = { |
| 1462 |
BaseExceptionGroup: (msg, [caught]), |
| 1463 |
ExceptionGroup: (msg, [caught]), |
| 1464 |
# UnicodeError: (None, msg, None, None, None), |
| 1465 |
UnicodeEncodeError: ('utf-8', '', 1, 3, msg), |
| 1466 |
UnicodeDecodeError: ('utf-8', b'', 1, 3, msg), |
| 1467 |
UnicodeTranslateError: ('', 1, 3, msg), |
| 1468 |
} |
| 1469 |
exceptions = [] |
| 1470 |
for cls in EXCEPTION_TYPES: |
| 1471 |
args = special.get(cls) or (msg,) |
| 1472 |
exceptions.append(cls(*args)) |
| 1473 |
|
| 1474 |
self.assert_not_shareable(exceptions) |
| 1475 |
|
| 1476 |
def test_builtin_objects(self): |
| 1477 |
ns = {} |
| 1478 |
exec("""if True: |
| 1479 |
try: |
| 1480 |
raise Exception |
| 1481 |
except Exception as exc: |
| 1482 |
TRACEBACK = exc.__traceback__ |
| 1483 |
FRAME = TRACEBACK.tb_frame |
| 1484 |
""", ns, ns) |
| 1485 |
|
| 1486 |
self.assert_not_shareable([ |
| 1487 |
MAPPING_PROXY_EMPTY, |
| 1488 |
types.SimpleNamespace(), |
| 1489 |
# types.CellType |
| 1490 |
types.CellType(), |
| 1491 |
# types.FrameType |
| 1492 |
ns['FRAME'], |
| 1493 |
# types.TracebackType |
| 1494 |
ns['TRACEBACK'], |
| 1495 |
]) |
| 1496 |
|
| 1497 |
|
| 1498 |
class CaptureExceptionTests(unittest.TestCase): |
| 1499 |
|
| 1500 |
# Prevent crashes with incompatible TracebackException.format(). |
| 1501 |
# Regression test for https://github.com/python/cpython/issues/143377. |
| 1502 |
|
| 1503 |
def capture_with_formatter(self, exc, formatter): |
| 1504 |
with support.swap_attr(traceback.TracebackException, "format", formatter): |
| 1505 |
return _interpreters.capture_exception(exc) |
| 1506 |
|
| 1507 |
def test_capture_exception(self): |
| 1508 |
captured = _interpreters.capture_exception(ValueError("hello")) |
| 1509 |
|
| 1510 |
self.assertEqual(captured.type.__name__, "ValueError") |
| 1511 |
self.assertEqual(captured.type.__qualname__, "ValueError") |
| 1512 |
self.assertEqual(captured.type.__module__, "builtins") |
| 1513 |
|
| 1514 |
self.assertEqual(captured.msg, "hello") |
| 1515 |
self.assertEqual(captured.formatted, "ValueError: hello") |
| 1516 |
|
| 1517 |
def test_capture_exception_custom_format(self): |
| 1518 |
exc = ValueError("good bye!") |
| 1519 |
formatter = lambda self: ["hello\n", "world\n"] |
| 1520 |
captured = self.capture_with_formatter(exc, formatter) |
| 1521 |
self.assertEqual(captured.msg, "good bye!") |
| 1522 |
self.assertEqual(captured.formatted, "ValueError: good bye!") |
| 1523 |
self.assertEqual(captured.errdisplay, "hello\nworld") |
| 1524 |
|
| 1525 |
@support.subTests("exc_lines", ([], ["x-no-nl"], ["x-no-nl", "y-no-nl"])) |
| 1526 |
def test_capture_exception_invalid_format(self, exc_lines): |
| 1527 |
formatter = lambda self: exc_lines |
| 1528 |
captured = self.capture_with_formatter(ValueError(), formatter) |
| 1529 |
self.assertEqual(captured.msg, "") |
| 1530 |
self.assertEqual(captured.formatted, "ValueError: ") |
| 1531 |
self.assertEqual(captured.errdisplay, "".join(exc_lines)) |
| 1532 |
|
| 1533 |
@unittest.skipUnless( |
| 1534 |
support.Py_DEBUG, |
| 1535 |
"printing subinterpreter unraisable exceptions requires DEBUG build", |
| 1536 |
) |
| 1537 |
def test_capture_exception_unraisable_exception(self): |
| 1538 |
formatter = lambda self: 1 |
| 1539 |
with support.catch_unraisable_exception() as cm: |
| 1540 |
captured = self.capture_with_formatter(ValueError(), formatter) |
| 1541 |
self.assertNotHasAttr(captured, "errdisplay") |
| 1542 |
self.assertEqual(cm.unraisable.exc_type, TypeError) |
| 1543 |
self.assertEqual(str(cm.unraisable.exc_value), |
| 1544 |
"can only join an iterable") |
| 1545 |
|
| 1546 |
|
| 1547 |
if __name__ == '__main__': |
| 1548 |
unittest.main() |