Type-agnostic reducing callback for function and classes. For performance reasons, subclasses of the C `pickle.Pickler` class cannot register custom reducers for functions and classes in the dispatch_table attribute. Reducers for such types must instead
(self, obj)
| 1353 | # not be the case anymore when pypy implements protocol 5. |
| 1354 | |
| 1355 | def reducer_override(self, obj): |
| 1356 | """Type-agnostic reducing callback for function and classes. |
| 1357 | |
| 1358 | For performance reasons, subclasses of the C `pickle.Pickler` class |
| 1359 | cannot register custom reducers for functions and classes in the |
| 1360 | dispatch_table attribute. Reducers for such types must instead |
| 1361 | implemented via the special `reducer_override` method. |
| 1362 | |
| 1363 | Note that this method will be called for any object except a few |
| 1364 | builtin-types (int, lists, dicts etc.), which differs from reducers |
| 1365 | in the Pickler's dispatch_table, each of them being invoked for |
| 1366 | objects of a specific type only. |
| 1367 | |
| 1368 | This property comes in handy for classes: although most classes are |
| 1369 | instances of the ``type`` metaclass, some of them can be instances |
| 1370 | of other custom metaclasses (such as enum.EnumMeta for example). In |
| 1371 | particular, the metaclass will likely not be known in advance, and |
| 1372 | thus cannot be special-cased using an entry in the dispatch_table. |
| 1373 | reducer_override, among other things, allows us to register a |
| 1374 | reducer that will be called for any class, independently of its |
| 1375 | type. |
| 1376 | |
| 1377 | Notes: |
| 1378 | |
| 1379 | * reducer_override has the priority over dispatch_table-registered |
| 1380 | reducers. |
| 1381 | * reducer_override can be used to fix other limitations of |
| 1382 | cloudpickle for other types that suffered from type-specific |
| 1383 | reducers, such as Exceptions. See |
| 1384 | https://github.com/cloudpipe/cloudpickle/issues/248 |
| 1385 | """ |
| 1386 | t = type(obj) |
| 1387 | try: |
| 1388 | is_anyclass = issubclass(t, type) |
| 1389 | except TypeError: # t is not a class (old Boost; see SF #502085) |
| 1390 | is_anyclass = False |
| 1391 | |
| 1392 | if is_anyclass: |
| 1393 | return _class_reduce(obj) |
| 1394 | elif isinstance(obj, types.FunctionType): |
| 1395 | return self._function_reduce(obj) |
| 1396 | else: |
| 1397 | # fallback to save_global, including the Pickler's |
| 1398 | # dispatch_table |
| 1399 | return NotImplemented |
| 1400 | |
| 1401 | else: |
| 1402 | # When reducer_override is not available, hack the pure-Python |
nothing calls this directly
no test coverage detected