MCPcopy Create free account
hub / github.com/python/cpython / __init__

Method __init__

Tools/clinic/libclinic/converter.py:174–255  ·  view source on GitHub ↗
(self,
             # Positional args:
             name: str,
             py_name: str,
             function: Function,
             default: object = unspecified,
             *,  # Keyword only args:
             c_default: str | None = None,
             py_default: str | None = None,
             annotation: str | Literal[Sentinels.unspecified] = unspecified,
             unused: bool = False,
             **kwargs: Any
    )

Source from the content-addressed store, hash-verified

172
173 # keep in sync with self_converter.__init__!
174 def __init__(self,
175 # Positional args:
176 name: str,
177 py_name: str,
178 function: Function,
179 default: object = unspecified,
180 *, # Keyword only args:
181 c_default: str | None = None,
182 py_default: str | None = None,
183 annotation: str | Literal[Sentinels.unspecified] = unspecified,
184 unused: bool = False,
185 **kwargs: Any
186 ) -> None:
187 self.name = libclinic.ensure_legal_c_identifier(name)
188 self.py_name = py_name
189 self.unused = unused
190 self._includes: list[Include] = []
191
192 if c_default:
193 self.c_default = c_default
194 if py_default:
195 self.py_default = py_default
196
197 if annotation is not unspecified:
198 fail("The 'annotation' parameter is not currently permitted.")
199
200 # Make sure not to set self.function until after converter_init() has been called.
201 # This prevents you from caching information
202 # about the function in converter_init().
203 # (That breaks if we get cloned.)
204 self.converter_init(**kwargs)
205
206 if default is not unspecified:
207 if self.default_type == ():
208 conv_name = self.__class__.__name__.removesuffix('_converter')
209 fail(f"A '{conv_name}' parameter cannot be marked optional.")
210 if (default is not unknown
211 and not isinstance(default, self.default_type)
212 ):
213 if isinstance(self.default_type, type):
214 types_str = self.default_type.__name__
215 else:
216 names = [cls.__name__ for cls in self.default_type]
217 types_str = ', '.join(names)
218 cls_name = self.__class__.__name__
219 fail(f"{cls_name}: default value {default!r} for field "
220 f"{name!r} is not of type {types_str!r}")
221 self.default = default
222
223 if not self.c_default:
224 if default is unspecified:
225 if self.c_init_default:
226 self.c_default = self.c_init_default
227 elif default is NULL:
228 self.c_default = self.c_ignored_default or self.c_init_default
229 if not self.c_default:
230 cls_name = self.__class__.__name__
231 fail(f"{cls_name}: c_default is required for "

Callers

nothing calls this directly

Calls 5

converter_initMethod · 0.95
c_default_initMethod · 0.95
failFunction · 0.90
removesuffixMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected