Coverage for an_website/fake_orjson.py: 0.000%

54 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-10 18:56 +0000

1# This program is free software: you can redistribute it and/or modify 

2# it under the terms of the GNU Affero General Public License as 

3# published by the Free Software Foundation, either version 3 of the 

4# License, or (at your option) any later version. 

5# 

6# This program is distributed in the hope that it will be useful, 

7# but WITHOUT ANY WARRANTY; without even the implied warranty of 

8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

9# GNU Affero General Public License for more details. 

10# 

11# You should have received a copy of the GNU Affero General Public License 

12# along with this program. If not, see <https://www.gnu.org/licenses/>. 

13# pylint: disable=preferred-module 

14 

15"""Nobody inspects the spammish repetition.""" 

16 

17from collections.abc import Callable 

18from dataclasses import fields, is_dataclass 

19from datetime import date, datetime, time, timezone 

20from enum import Enum 

21from functools import partial 

22from json import JSONDecodeError, dumps as _dumps, loads as _loads 

23from typing import Any 

24from uuid import UUID 

25 

26__version__ = "3.8.14" # TODO: orjson.Fragment 

27 

28__all__ = ( 

29 "dumps", 

30 "loads", 

31 "JSONDecodeError", 

32 "JSONEncodeError", 

33 "OPT_APPEND_NEWLINE", 

34 "OPT_INDENT_2", 

35 "OPT_NAIVE_UTC", 

36 "OPT_NON_STR_KEYS", 

37 "OPT_OMIT_MICROSECONDS", 

38 "OPT_PASSTHROUGH_DATACLASS", 

39 "OPT_PASSTHROUGH_DATETIME", 

40 "OPT_PASSTHROUGH_SUBCLASS", 

41 "OPT_SERIALIZE_DATACLASS", 

42 "OPT_SERIALIZE_NUMPY", 

43 "OPT_SERIALIZE_UUID", 

44 "OPT_SORT_KEYS", 

45 "OPT_STRICT_INTEGER", 

46 "OPT_UTC_Z", 

47) 

48 

49OPT_APPEND_NEWLINE = 1 << 10 

50OPT_INDENT_2 = 1 << 0 

51OPT_NAIVE_UTC = 1 << 1 

52OPT_NON_STR_KEYS = 1 << 2 # TODO: require for non-string keys 

53OPT_OMIT_MICROSECONDS = 1 << 3 

54OPT_PASSTHROUGH_DATACLASS = 1 << 11 

55OPT_PASSTHROUGH_DATETIME = 1 << 9 

56OPT_PASSTHROUGH_SUBCLASS = 1 << 8 # TODO 

57OPT_SERIALIZE_DATACLASS = 0 

58OPT_SERIALIZE_NUMPY = 1 << 4 # TODO 

59OPT_SERIALIZE_UUID = 0 

60OPT_SORT_KEYS = 1 << 5 

61OPT_STRICT_INTEGER = 1 << 6 # TODO 

62OPT_UTC_Z = 1 << 7 

63 

64 

65class JSONEncodeError(TypeError): 

66 """Nobody inspects the spammish repetition.""" 

67 

68 

69def _default( # pylint: disable=too-complex 

70 default: None | Callable[[Any], Any], option: int, spam: object 

71) -> Any: 

72 serialize_dataclass = not option & OPT_PASSTHROUGH_DATACLASS 

73 serialize_datetime = not option & OPT_PASSTHROUGH_DATETIME 

74 

75 if serialize_dataclass and is_dataclass(spam): 

76 return { 

77 field.name: getattr(spam, field.name) 

78 for field in fields(spam) 

79 if not field.name.startswith("_") 

80 } 

81 if serialize_datetime and type(spam) in (date, datetime, time): 

82 if type(spam) in (datetime, time): 

83 if option & OPT_OMIT_MICROSECONDS: 

84 spam = spam.replace(microsecond=0) # type: ignore[attr-defined] 

85 if type(spam) is datetime: # pylint: disable=unidiomatic-typecheck 

86 if option & OPT_NAIVE_UTC and not spam.tzinfo: 

87 spam = spam.replace(tzinfo=timezone.utc) 

88 if ( 

89 option & OPT_UTC_Z 

90 and spam.tzinfo 

91 and not spam.tzinfo.utcoffset(spam) 

92 ): 

93 return f"{spam.isoformat()[:-6]}Z" 

94 return spam.isoformat() # type: ignore[attr-defined] 

95 if isinstance(spam, Enum): 

96 return spam.value 

97 if type(spam) is UUID: # pylint: disable=unidiomatic-typecheck 

98 return str(spam) 

99 if default: 

100 return default(spam) 

101 

102 raise TypeError 

103 

104 

105def dumps( 

106 spam: Any, 

107 /, 

108 default: None | Callable[[Any], Any] = None, 

109 option: None | int = None, 

110) -> bytes: 

111 """Nobody inspects the spammish repetition.""" 

112 # TODO: only serialize subclasses of str, int, dict, list and Enum 

113 option = option or 0 

114 return ( 

115 _dumps( 

116 spam, 

117 allow_nan=False, 

118 default=partial(_default, default, option or 0), 

119 ensure_ascii=False, 

120 indent=(option & OPT_INDENT_2) * 2 or None, 

121 separators=None if option & OPT_INDENT_2 else (",", ":"), 

122 sort_keys=bool(option & OPT_SORT_KEYS), 

123 ) 

124 + ("\n" if option & OPT_APPEND_NEWLINE else "") 

125 ).encode("UTF-8") 

126 

127 

128def loads(spam: bytes | bytearray | memoryview | str, /) -> Any: 

129 """Nobody inspects the spammish repetition.""" 

130 if isinstance(spam, memoryview): 

131 spam = bytes(spam) 

132 return _loads(spam)