MCPcopy
hub / github.com/psycopg/psycopg / register_dumper

Method register_dumper

psycopg/psycopg/_adapters_map.py:114–163  ·  view source on GitHub ↗

Configure the context to use `!dumper` to convert objects of type `!cls`. If two dumpers with different `~Dumper.format` are registered for the same type, the last one registered will be chosen when the query doesn't specify a format (i.e. when the value is used wit

(self, cls: type | str | None, dumper: type[Dumper])

Source from the content-addressed store, hash-verified

112 return None
113
114 def register_dumper(self, cls: type | str | None, dumper: type[Dumper]) -> None:
115 """
116 Configure the context to use `!dumper` to convert objects of type `!cls`.
117
118 If two dumpers with different `~Dumper.format` are registered for the
119 same type, the last one registered will be chosen when the query
120 doesn't specify a format (i.e. when the value is used with a ``%s``
121 "`~PyFormat.AUTO`" placeholder).
122
123 :param cls: The type to manage.
124 :param dumper: The dumper to register for `!cls`.
125
126 If `!cls` is specified as string it will be lazy-loaded, so that it
127 will be possible to register it without importing it before. In this
128 case it should be the fully qualified name of the object (e.g.
129 ``"uuid.UUID"``).
130
131 If `!cls` is None, only use the dumper when looking up using
132 `get_dumper_by_oid()`, which happens when we know the Postgres type to
133 adapt to, but not the Python type that will be adapted (e.g. in COPY
134 after using `~psycopg.Copy.set_types()`).
135
136 """
137 if not (cls is None or isinstance(cls, (str, type))):
138 raise TypeError(
139 f"dumpers should be registered on classes, got {cls} instead"
140 )
141
142 if _psycopg:
143 dumper = self._get_optimised(dumper)
144
145 # Register the dumper both as its format and as auto
146 # so that the last dumper registered is used in auto (%s) format
147 if cls:
148 for fmt in (PyFormat.from_pq(dumper.format), PyFormat.AUTO):
149 if not self._own_dumpers[fmt]:
150 self._dumpers[fmt] = self._dumpers[fmt].copy()
151 self._own_dumpers[fmt] = True
152
153 self._dumpers[fmt][cls] = dumper
154
155 # Register the dumper by oid, if the oid of the dumper is fixed
156 if dumper.oid:
157 if not self._own_dumpers_by_oid[dumper.format]:
158 self._dumpers_by_oid[dumper.format] = self._dumpers_by_oid[
159 dumper.format
160 ].copy()
161 self._own_dumpers_by_oid[dumper.format] = True
162
163 self._dumpers_by_oid[dumper.format][dumper.oid] = dumper
164
165 def register_loader(self, oid: int | str, loader: type[Loader]) -> None:
166 """

Callers 15

test_subclass_adapterFunction · 0.80
test_copy_in_text_pinnedFunction · 0.80
test_text_literalMethod · 0.80
test_invalid_nameMethod · 0.80
test_as_string_contextFunction · 0.80
test_as_bytes_contextFunction · 0.80

Calls 3

_get_optimisedMethod · 0.95
from_pqMethod · 0.80
copyMethod · 0.45

Tested by 15

test_subclass_adapterFunction · 0.64
test_copy_in_text_pinnedFunction · 0.64
test_text_literalMethod · 0.64
test_invalid_nameMethod · 0.64
test_as_string_contextFunction · 0.64
test_as_bytes_contextFunction · 0.64