| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d833779 commit 482fe04
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -511,11 +511,37 @@ def value(self): | |||
| 511 | 511 | """The value of the Enum member.""" | |
| 512 | 512 | return self._value_ | |
| 513 | 513 | ||
| 514 | + @classmethod | ||
| 515 | + def _convert(cls, name, module, filter, source=None): | ||
| 516 | + """ | ||
| 517 | + Create a new Enum subclass that replaces a collection of global constants | ||
| 518 | + """ | ||
| 519 | + # convert all constants from source (or module) that pass filter() to | ||
| 520 | + # a new Enum called name, and export the enum and its members back to | ||
| 521 | + # module; | ||
| 522 | + # also, replace the __reduce_ex__ method so unpickling works in | ||
| 523 | + # previous Python versions | ||
| 524 | + module_globals = vars(sys.modules[module]) | ||
| 525 | + if source: | ||
| 526 | + source = vars(source) | ||
| 527 | + else: | ||
| 528 | + source = module_globals | ||
| 529 | + members = {name: value for name, value in source.items() | ||
| 530 | + if filter(name)} | ||
| 531 | + cls = cls(name, members, module=module) | ||
| 532 | + cls.__reduce_ex__ = _reduce_ex_by_name | ||
| 533 | + module_globals.update(cls.__members__) | ||
| 534 | + module_globals[name] = cls | ||
| 535 | + return cls | ||
| 536 | + | ||
| 514 | 537 | ||
| 515 | 538 | class IntEnum(int, Enum): | |
| 516 | 539 | """Enum where members are also (and must be) ints""" | |
| 517 | 540 | ||
| 518 | 541 | ||
| 542 | + def _reduce_ex_by_name(self, proto): | ||
| 543 | + return self.name | ||
| 544 | + | ||
| 519 | 545 | def unique(enumeration): | |
| 520 | 546 | """Class decorator for enumerations ensuring unique member values.""" | |
| 521 | 547 | duplicates = [] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,15 +69,15 @@ | |||
| 69 | 69 | # Note that _socket only knows about the integer values. The public interface | |
| 70 | 70 | # in this module understands the enums and translates them back from integers | |
| 71 | 71 | # where needed (e.g. .family property of a socket object). | |
| 72 | - AddressFamily = IntEnum('AddressFamily', | ||
| 73 | - {name: value for name, value in globals().items() | ||
| 74 | - if name.isupper() and name.startswith('AF_')}) | ||
| 75 | - globals().update(AddressFamily.__members__) | ||
| 76 | - | ||
| 77 | - SocketKind = IntEnum('SocketKind', | ||
| 78 | - {name: value for name, value in globals().items() | ||
| 79 | - if name.isupper() and name.startswith('SOCK_')}) | ||
| 80 | - globals().update(SocketKind.__members__) | ||
| 72 | + IntEnum._convert( | ||
| 73 | + 'AddressFamily', | ||
| 74 | + __name__, | ||
| 75 | + lambda C: C.isupper() and C.startswith('AF_')) | ||
| 76 | + | ||
| 77 | + IntEnum._convert( | ||
| 78 | + 'SocketKind', | ||
| 79 | + __name__, | ||
| 80 | + lambda C: C.isupper() and C.startswith('SOCK_')) | ||
| 81 | 81 | ||
| 82 | 82 | def _intenum_converter(value, enum_klass): | |
| 83 | 83 | """Convert a numeric family value to an IntEnum member. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -581,6 +581,14 @@ class NestedEnum(Enum): | |||
| 581 | 581 | test_pickle_dump_load(self.assertIs, self.NestedEnum.twigs, | |
| 582 | 582 | protocol=(4, HIGHEST_PROTOCOL)) | |
| 583 | 583 | ||
| 584 | + def test_pickle_by_name(self): | ||
| 585 | + class ReplaceGlobalInt(IntEnum): | ||
| 586 | + ONE = 1 | ||
| 587 | + TWO = 2 | ||
| 588 | + ReplaceGlobalInt.__reduce_ex__ = enum._reduce_ex_by_name | ||
| 589 | + for proto in range(HIGHEST_PROTOCOL): | ||
| 590 | + self.assertEqual(ReplaceGlobalInt.TWO.__reduce_ex__(proto), 'TWO') | ||
| 591 | + | ||
| 584 | 592 | def test_exploding_pickle(self): | |
| 585 | 593 | BadPickle = Enum( | |
| 586 | 594 | 'BadPickle', 'dill sweet bread-n-butter', module=__name__) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1375,6 +1375,11 @@ def test_pickle(self): | |||
| 1375 | 1375 | with sock: | |
| 1376 | 1376 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): | |
| 1377 | 1377 | self.assertRaises(TypeError, pickle.dumps, sock, protocol) | |
| 1378 | + for protocol in range(pickle.HIGHEST_PROTOCOL + 1): | ||
| 1379 | + family = pickle.loads(pickle.dumps(socket.AF_INET, protocol)) | ||
| 1380 | + self.assertEqual(family, socket.AF_INET) | ||
| 1381 | + type = pickle.loads(pickle.dumps(socket.SOCK_STREAM, protocol)) | ||
| 1382 | + self.assertEqual(type, socket.SOCK_STREAM) | ||
| 1378 | 1383 | ||
| 1379 | 1384 | def test_listen_backlog(self): | |
| 1380 | 1385 | for backlog in 0, -1: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments