wrapt 2.4.0.dev2__cp315-cp315t-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wrapt/_wrappers.c ADDED
@@ -0,0 +1,5069 @@
1
+ /* ------------------------------------------------------------------------- */
2
+
3
+ #include "Python.h"
4
+
5
+ #include "structmember.h"
6
+
7
+ /* ------------------------------------------------------------------------- */
8
+
9
+ typedef struct
10
+ {
11
+ PyObject_HEAD
12
+
13
+ PyObject *dict;
14
+ PyObject *wrapped;
15
+ PyObject *weakreflist;
16
+ int init_called;
17
+ } WraptObjectProxyObject;
18
+
19
+ typedef struct
20
+ {
21
+ WraptObjectProxyObject object_proxy;
22
+
23
+ PyObject *args;
24
+ PyObject *kwargs;
25
+ } WraptPartialCallableObjectProxyObject;
26
+
27
+ typedef struct
28
+ {
29
+ WraptObjectProxyObject object_proxy;
30
+
31
+ PyObject *instance;
32
+ PyObject *wrapper;
33
+ PyObject *enabled;
34
+ PyObject *binding;
35
+ PyObject *parent;
36
+ PyObject *owner;
37
+ } WraptFunctionWrapperObject;
38
+
39
+ /* ------------------------------------------------------------------------- */
40
+
41
+ /* Forward declaration of moduledef so module-state helpers can reference it. */
42
+
43
+ static struct PyModuleDef moduledef;
44
+
45
+ /* Per-interpreter module state. Holds the heap type objects so that methods
46
+ * can reach them via PyType_GetModuleByDef without using static globals.
47
+ *
48
+ * Cached interned strings will be added here in a later stage of the
49
+ * sub-interpreter / free-threading migration. */
50
+
51
+ typedef struct
52
+ {
53
+ PyTypeObject *ObjectProxy_Type;
54
+ PyTypeObject *CallableObjectProxy_Type;
55
+ PyTypeObject *PartialCallableObjectProxy_Type;
56
+ PyTypeObject *FunctionWrapperBase_Type;
57
+ PyTypeObject *BoundFunctionWrapper_Type;
58
+ PyTypeObject *FunctionWrapper_Type;
59
+
60
+ /* Cached interned attribute / argument names. Initialized eagerly in
61
+ * wrapt_exec, released in wrapt_clear. Per-interpreter so they remain
62
+ * sub-interpreter safe; eager init makes them free-threading safe by
63
+ * eliminating the lazy-init data race that the previous static-local
64
+ * string pattern had. */
65
+
66
+ PyObject *str_wrapped; /* "__wrapped__" */
67
+ PyObject *str_wrapped_factory; /* "__wrapped_factory__" */
68
+ PyObject *str_wrapped_get; /* "__wrapped_get__" */
69
+ PyObject *str_module; /* "__module__" */
70
+ PyObject *str_doc; /* "__doc__" */
71
+ PyObject *str_setattr_fixups; /* "__wrapped_setattr_fixups__" */
72
+ PyObject *str_getattr; /* "__getattr__" */
73
+ PyObject *str_self_; /* "_self_" */
74
+ PyObject *str_callable; /* "callable" */
75
+ PyObject *str_bound_function_wrapper; /* "__bound_function_wrapper__" */
76
+ PyObject *str_function; /* "function" */
77
+ PyObject *str_classmethod; /* "classmethod" */
78
+ PyObject *str_staticmethod; /* "staticmethod" */
79
+ PyObject *str_builtin; /* "builtin" */
80
+ PyObject *str_class; /* "class" */
81
+ PyObject *str_instancemethod; /* "instancemethod" */
82
+ PyObject *str_object_proxy; /* "__object_proxy__" */
83
+ PyObject *str_enter; /* "__enter__" */
84
+ PyObject *str_exit; /* "__exit__" */
85
+ PyObject *str_aenter; /* "__aenter__" */
86
+ PyObject *str_aexit; /* "__aexit__" */
87
+ PyObject *str_mro_entries; /* "__mro_entries__" */
88
+ PyObject *str_name; /* "__name__" */
89
+ PyObject *str_qualname; /* "__qualname__" */
90
+ PyObject *str_annotations; /* "__annotations__" */
91
+ PyObject *str_dunder_class; /* "__class__" */
92
+ PyObject *str_self; /* "__self__" */
93
+ PyObject *str_set_name; /* "__set_name__" */
94
+ PyObject *str_self_binding; /* "_self_binding" */
95
+ PyObject *str_dict; /* "__dict__" */
96
+
97
+ /* Cached exception type from wrapt.wrappers. Initialized eagerly in
98
+ * wrapt_exec after type creation. The wrapt.wrappers module is guaranteed
99
+ * to be imported before _wrappers because __wrapt__.py imports it first. */
100
+
101
+ PyObject *WrapperNotInitializedError;
102
+ } wrapt_module_state;
103
+
104
+ static inline wrapt_module_state *wrapt_get_state(PyObject *module)
105
+ {
106
+ return (wrapt_module_state *)PyModule_GetState(module);
107
+ }
108
+
109
+ /* Polyfill PyType_GetModuleByDef for Python < 3.11. The borrowed-reference
110
+ * contract matches the real API so callers are version-agnostic. */
111
+
112
+ #if PY_VERSION_HEX < 0x030B0000
113
+ static PyObject *PyType_GetModuleByDef(PyTypeObject *type,
114
+ struct PyModuleDef *def)
115
+ {
116
+ PyObject *mro = type->tp_mro;
117
+ Py_ssize_t i, n;
118
+
119
+ if (mro == NULL)
120
+ return NULL;
121
+
122
+ n = PyTuple_GET_SIZE(mro);
123
+
124
+ for (i = 0; i < n; i++)
125
+ {
126
+ PyTypeObject *t = (PyTypeObject *)PyTuple_GET_ITEM(mro, i);
127
+ PyObject *m;
128
+
129
+ if (!PyType_HasFeature(t, Py_TPFLAGS_HEAPTYPE))
130
+ continue;
131
+
132
+ m = PyType_GetModule(t);
133
+
134
+ if (m != NULL && PyModule_GetDef(m) == def)
135
+ return m;
136
+
137
+ if (m == NULL)
138
+ {
139
+ /* PyType_GetModule only raises TypeError here (no module
140
+ * association on heap type); no other failure mode is expected.
141
+ * Guard with WriteUnraisable for completeness. */
142
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
143
+ PyErr_WriteUnraisable((PyObject *)t);
144
+ PyErr_Clear();
145
+ }
146
+ }
147
+
148
+ PyErr_Format(PyExc_TypeError,
149
+ "PyType_GetModuleByDef: No superclass of '%s' has the given "
150
+ "module",
151
+ type->tp_name);
152
+ return NULL;
153
+ }
154
+ #endif
155
+
156
+ /* Polyfill the critical section API for Python < 3.13. Free-threaded
157
+ * builds only exist on 3.13+, where the real macros are provided (and are
158
+ * no-ops on GIL builds), so expanding to plain blocks here is safe. */
159
+
160
+ #if PY_VERSION_HEX < 0x030D0000
161
+ #define Py_BEGIN_CRITICAL_SECTION(op) {
162
+ #define Py_END_CRITICAL_SECTION() }
163
+ #endif
164
+
165
+ /* Polyfill PyObject_GetOptionalAttrString for Python < 3.13. Matches the
166
+ * 3.13+ semantics: returns 1 if found, 0 if not found (AttributeError
167
+ * only), -1 on other errors with exception set. */
168
+
169
+ #if PY_VERSION_HEX < 0x030d0000
170
+ static inline int
171
+ PyObject_GetOptionalAttrString(PyObject *obj, const char *name, PyObject **result)
172
+ {
173
+ *result = PyObject_GetAttrString(obj, name);
174
+ if (*result)
175
+ return 1;
176
+ if (PyErr_ExceptionMatches(PyExc_AttributeError))
177
+ {
178
+ PyErr_Clear();
179
+ return 0;
180
+ }
181
+ return -1;
182
+ }
183
+ #endif
184
+
185
+ /* Get module state for the wrapt module given any type whose MRO includes
186
+ * one of our heap types. Returns NULL and sets an exception on miss. */
187
+
188
+ static wrapt_module_state *wrapt_state_from_type(PyTypeObject *type)
189
+ {
190
+ PyObject *m = PyType_GetModuleByDef(type, &moduledef);
191
+ return m ? wrapt_get_state(m) : NULL;
192
+ }
193
+
194
+ /* Walk type's MRO checking each tp_dict directly for the named attribute.
195
+ * Does not invoke type's tp_getattro, so it does NOT trigger Python's lazy
196
+ * creation of class-level attributes such as __annotations__. Used by
197
+ * setattro to decide "is this a local attribute on the proxy type" without
198
+ * shadowing our getset descriptors via lazy class-dict mutation. */
199
+
200
+ static int wrapt_type_has_attr(PyTypeObject *type, PyObject *name)
201
+ {
202
+ PyObject *mro = type->tp_mro;
203
+ Py_ssize_t i, n;
204
+
205
+ if (mro == NULL)
206
+ return 0;
207
+
208
+ n = PyTuple_GET_SIZE(mro);
209
+
210
+ for (i = 0; i < n; i++)
211
+ {
212
+ PyTypeObject *t = (PyTypeObject *)PyTuple_GET_ITEM(mro, i);
213
+ PyObject *dict = t->tp_dict;
214
+ int contains;
215
+
216
+ if (dict == NULL)
217
+ continue;
218
+
219
+ contains = PyDict_Contains(dict, name);
220
+ if (contains < 0)
221
+ {
222
+ /* PyDict_Contains cannot fail with string keys against a
223
+ * string-keyed tp_dict; no failure is expected here. Guard
224
+ * with WriteUnraisable for completeness. */
225
+ PyErr_WriteUnraisable((PyObject *)t);
226
+ PyErr_Clear();
227
+ continue;
228
+ }
229
+ if (contains > 0)
230
+ return 1;
231
+ }
232
+
233
+ return 0;
234
+ }
235
+
236
+ /* Non-error-setting predicate: is this object an instance of any wrapt
237
+ * proxy type? Walks the MRO directly. PyType_GetModule sets an exception
238
+ * for heap types that lack a module association (e.g. pure-Python user
239
+ * subclasses), so we save/restore exception state to keep this predicate
240
+ * side-effect-free for the caller. */
241
+
242
+ static int wrapt_is_proxy(PyObject *o)
243
+ {
244
+ PyTypeObject *type = Py_TYPE(o);
245
+ PyObject *mro = type->tp_mro;
246
+ Py_ssize_t i, n;
247
+ PyObject *saved_type = NULL, *saved_value = NULL, *saved_tb = NULL;
248
+ int had_error = PyErr_Occurred() != NULL;
249
+ int result = 0;
250
+
251
+ if (had_error)
252
+ PyErr_Fetch(&saved_type, &saved_value, &saved_tb);
253
+
254
+ if (mro == NULL)
255
+ goto done;
256
+
257
+ n = PyTuple_GET_SIZE(mro);
258
+
259
+ for (i = 0; i < n; i++)
260
+ {
261
+ PyTypeObject *t = (PyTypeObject *)PyTuple_GET_ITEM(mro, i);
262
+ PyObject *m;
263
+
264
+ if (!PyType_HasFeature(t, Py_TPFLAGS_HEAPTYPE))
265
+ continue;
266
+
267
+ m = PyType_GetModule(t);
268
+
269
+ if (m == NULL)
270
+ {
271
+ /* PyType_GetModule only raises TypeError here (no module
272
+ * association on heap type); no other failure mode is expected.
273
+ * Guard with WriteUnraisable for completeness. */
274
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
275
+ PyErr_WriteUnraisable((PyObject *)t);
276
+ PyErr_Clear();
277
+ continue;
278
+ }
279
+
280
+ if (PyModule_GetDef(m) == &moduledef)
281
+ {
282
+ result = 1;
283
+ break;
284
+ }
285
+ }
286
+
287
+ done:
288
+ if (had_error)
289
+ PyErr_Restore(saved_type, saved_value, saved_tb);
290
+ else
291
+ PyErr_Clear();
292
+ return result;
293
+ }
294
+
295
+ /* ------------------------------------------------------------------------- */
296
+
297
+ static int raise_uninitialized_wrapper_error(WraptObjectProxyObject *object)
298
+ {
299
+ // Before raising an exception we need to first check whether this is a lazy
300
+ // proxy object and attempt to intialize the wrapped object using the supplied
301
+ // callback if so. If the callback is not present then we can proceed to raise
302
+ // the exception, but if the callback is present and returns a value, we set
303
+ // that as the wrapped object and continue and return without raising an error.
304
+
305
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(object));
306
+ if (!state)
307
+ return -1;
308
+
309
+ PyObject *wrapped_str = state->str_wrapped;
310
+ PyObject *wrapped_factory_str = state->str_wrapped_factory;
311
+ PyObject *wrapped_get_str = state->str_wrapped_get;
312
+
313
+ PyObject *callback = NULL;
314
+ PyObject *value = NULL;
315
+
316
+ // Note that we use existance of __wrapped_factory__ to gate whether we can
317
+ // attempt to initialize the wrapped object lazily, but it is __wrapped_get__
318
+ // that we actually call to do the initialization. This is so that we can
319
+ // handle multithreading correctly by having __wrapped_get__ use a lock to
320
+ // protect against multiple threads trying to initialize the wrapped object
321
+ // at the same time.
322
+
323
+ callback = PyObject_GenericGetAttr((PyObject *)object, wrapped_factory_str);
324
+
325
+ if (callback)
326
+ {
327
+ if (callback != Py_None)
328
+ {
329
+ Py_DECREF(callback);
330
+
331
+ callback = PyObject_GenericGetAttr((PyObject *)object, wrapped_get_str);
332
+
333
+ if (!callback)
334
+ return -1;
335
+
336
+ value = PyObject_CallObject(callback, NULL);
337
+
338
+ Py_DECREF(callback);
339
+
340
+ if (value)
341
+ {
342
+ // We use setattr so that special dunder methods will be properly set.
343
+
344
+ if (PyObject_SetAttr((PyObject *)object, wrapped_str, value) == -1)
345
+ {
346
+ Py_DECREF(value);
347
+ return -1;
348
+ }
349
+
350
+ Py_DECREF(value);
351
+
352
+ return 0;
353
+ }
354
+ else
355
+ {
356
+ return -1;
357
+ }
358
+ }
359
+ else
360
+ {
361
+ Py_DECREF(callback);
362
+ }
363
+ }
364
+ else
365
+ {
366
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
367
+ return -1;
368
+ PyErr_Clear();
369
+ }
370
+
371
+ if (object->init_called)
372
+ {
373
+ PyErr_SetString(state->WrapperNotInitializedError,
374
+ "wrapper is in an inconsistent state: __wrapped__ is not set");
375
+ }
376
+ else
377
+ {
378
+ PyErr_Format(PyExc_AttributeError,
379
+ "'%.100s' object has no attribute '__wrapped__'",
380
+ Py_TYPE(object)->tp_name);
381
+ }
382
+
383
+ return -1;
384
+ }
385
+
386
+ /* ------------------------------------------------------------------------- */
387
+
388
+ static PyObject *WraptObjectProxy_new(PyTypeObject *type, PyObject *args,
389
+ PyObject *kwds)
390
+ {
391
+ WraptObjectProxyObject *self;
392
+
393
+ self = (WraptObjectProxyObject *)type->tp_alloc(type, 0);
394
+
395
+ if (!self)
396
+ return NULL;
397
+
398
+ self->dict = PyDict_New();
399
+
400
+ if (!self->dict)
401
+ {
402
+ Py_DECREF(self);
403
+ return NULL;
404
+ }
405
+
406
+ self->wrapped = NULL;
407
+ self->weakreflist = NULL;
408
+ self->init_called = 0;
409
+
410
+ return (PyObject *)self;
411
+ }
412
+
413
+ /* ------------------------------------------------------------------------- */
414
+
415
+ static int WraptObjectProxy_raw_init(WraptObjectProxyObject *self,
416
+ PyObject *wrapped)
417
+ {
418
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
419
+ if (!state)
420
+ return -1;
421
+
422
+ PyObject *module_str = state->str_module;
423
+ PyObject *doc_str = state->str_doc;
424
+ PyObject *wrapped_factory_str = state->str_wrapped_factory;
425
+
426
+ PyObject *object = NULL;
427
+
428
+ // If wrapped is Py_None and we have a __wrapped_factory__ attribute
429
+ // then we defer initialization of the wrapped object until it is first needed.
430
+
431
+ if (wrapped == Py_None)
432
+ {
433
+ PyObject *callback = NULL;
434
+
435
+ callback = PyObject_GenericGetAttr((PyObject *)self, wrapped_factory_str);
436
+
437
+ if (callback)
438
+ {
439
+ if (callback != Py_None)
440
+ {
441
+ Py_DECREF(callback);
442
+ self->init_called = 1;
443
+ return 0;
444
+ }
445
+ else
446
+ {
447
+ Py_DECREF(callback);
448
+ }
449
+ }
450
+ else
451
+ {
452
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
453
+ return -1;
454
+ PyErr_Clear();
455
+ }
456
+ }
457
+
458
+ /* Serialise the swap for the same reason as in the __wrapped__ setter.
459
+ * Normally __init__ runs before the proxy is shared, but it can be
460
+ * re-invoked on an already published proxy, in which case concurrent
461
+ * callers on a free-threaded build could otherwise decref the same old
462
+ * wrapped object twice. */
463
+
464
+ Py_INCREF(wrapped);
465
+ Py_BEGIN_CRITICAL_SECTION(self);
466
+ Py_XSETREF(self->wrapped, wrapped);
467
+ Py_END_CRITICAL_SECTION();
468
+
469
+ self->init_called = 1;
470
+
471
+ object = PyObject_GetAttr(wrapped, module_str);
472
+
473
+ if (object)
474
+ {
475
+ if (PyDict_SetItem(self->dict, module_str, object) == -1)
476
+ {
477
+ Py_DECREF(object);
478
+ return -1;
479
+ }
480
+ Py_DECREF(object);
481
+ }
482
+ else
483
+ {
484
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
485
+ return -1;
486
+ PyErr_Clear();
487
+ }
488
+
489
+ object = PyObject_GetAttr(wrapped, doc_str);
490
+
491
+ if (object)
492
+ {
493
+ if (PyDict_SetItem(self->dict, doc_str, object) == -1)
494
+ {
495
+ Py_DECREF(object);
496
+ return -1;
497
+ }
498
+ Py_DECREF(object);
499
+ }
500
+ else
501
+ {
502
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
503
+ return -1;
504
+ PyErr_Clear();
505
+ }
506
+
507
+ return 0;
508
+ }
509
+
510
+ /* ------------------------------------------------------------------------- */
511
+
512
+ static int WraptObjectProxy_init(WraptObjectProxyObject *self, PyObject *args,
513
+ PyObject *kwds)
514
+ {
515
+ PyObject *wrapped = NULL;
516
+
517
+ char *const kwlist[] = {"wrapped", NULL};
518
+
519
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:ObjectProxy", kwlist,
520
+ &wrapped))
521
+ {
522
+ return -1;
523
+ }
524
+
525
+ return WraptObjectProxy_raw_init(self, wrapped);
526
+ }
527
+
528
+ /* ------------------------------------------------------------------------- */
529
+
530
+ static int WraptObjectProxy_traverse(WraptObjectProxyObject *self,
531
+ visitproc visit, void *arg)
532
+ {
533
+ Py_VISIT(Py_TYPE(self));
534
+ Py_VISIT(self->dict);
535
+ Py_VISIT(self->wrapped);
536
+
537
+ return 0;
538
+ }
539
+
540
+ /* ------------------------------------------------------------------------- */
541
+
542
+ static int WraptObjectProxy_clear(WraptObjectProxyObject *self)
543
+ {
544
+ Py_CLEAR(self->dict);
545
+ Py_CLEAR(self->wrapped);
546
+
547
+ return 0;
548
+ }
549
+
550
+ /* ------------------------------------------------------------------------- */
551
+
552
+ static void WraptObjectProxy_dealloc(WraptObjectProxyObject *self)
553
+ {
554
+ PyTypeObject *tp = Py_TYPE(self);
555
+
556
+ PyObject_GC_UnTrack(self);
557
+
558
+ if (self->weakreflist != NULL)
559
+ PyObject_ClearWeakRefs((PyObject *)self);
560
+
561
+ WraptObjectProxy_clear(self);
562
+
563
+ tp->tp_free(self);
564
+
565
+ #if PY_VERSION_HEX >= 0x030C0000
566
+ PyObject *exc = PyErr_GetRaisedException();
567
+ Py_DECREF(tp);
568
+ PyErr_SetRaisedException(exc);
569
+ #else
570
+ PyObject *exc_type, *exc_value, *exc_tb;
571
+ PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
572
+ Py_DECREF(tp);
573
+ PyErr_Restore(exc_type, exc_value, exc_tb);
574
+ #endif
575
+ }
576
+
577
+ /* ------------------------------------------------------------------------- */
578
+
579
+ static PyObject *WraptObjectProxy_repr(WraptObjectProxyObject *self)
580
+ {
581
+ if (!self->wrapped)
582
+ {
583
+ if (raise_uninitialized_wrapper_error(self) == -1)
584
+ return NULL;
585
+ }
586
+
587
+ char self_addr[24], wrapped_addr[24];
588
+ snprintf(self_addr, sizeof(self_addr), "0x%zx", (Py_uintptr_t)self);
589
+ snprintf(wrapped_addr, sizeof(wrapped_addr), "0x%zx",
590
+ (Py_uintptr_t)self->wrapped);
591
+ return PyUnicode_FromFormat("<%s at %s for %s at %s>",
592
+ Py_TYPE(self)->tp_name, self_addr,
593
+ Py_TYPE(self->wrapped)->tp_name, wrapped_addr);
594
+ }
595
+
596
+ /* ------------------------------------------------------------------------- */
597
+
598
+ static Py_hash_t WraptObjectProxy_hash(WraptObjectProxyObject *self)
599
+ {
600
+ if (!self->wrapped)
601
+ {
602
+ if (raise_uninitialized_wrapper_error(self) == -1)
603
+ return -1;
604
+ }
605
+
606
+ return PyObject_Hash(self->wrapped);
607
+ }
608
+
609
+ /* ------------------------------------------------------------------------- */
610
+
611
+ static PyObject *WraptObjectProxy_str(WraptObjectProxyObject *self)
612
+ {
613
+ if (!self->wrapped)
614
+ {
615
+ if (raise_uninitialized_wrapper_error(self) == -1)
616
+ return NULL;
617
+ }
618
+
619
+ return PyObject_Str(self->wrapped);
620
+ }
621
+
622
+ /* ------------------------------------------------------------------------- */
623
+
624
+ static PyObject *WraptObjectProxy_add(PyObject *o1, PyObject *o2)
625
+ {
626
+ if (wrapt_is_proxy(o1))
627
+ {
628
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
629
+ {
630
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
631
+ return NULL;
632
+ }
633
+
634
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
635
+ }
636
+
637
+ if (wrapt_is_proxy(o2))
638
+ {
639
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
640
+ {
641
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
642
+ return NULL;
643
+ }
644
+
645
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
646
+ }
647
+
648
+ return PyNumber_Add(o1, o2);
649
+ }
650
+
651
+ /* ------------------------------------------------------------------------- */
652
+
653
+ static PyObject *WraptObjectProxy_subtract(PyObject *o1, PyObject *o2)
654
+ {
655
+ if (wrapt_is_proxy(o1))
656
+ {
657
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
658
+ {
659
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
660
+ return NULL;
661
+ }
662
+
663
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
664
+ }
665
+
666
+ if (wrapt_is_proxy(o2))
667
+ {
668
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
669
+ {
670
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
671
+ return NULL;
672
+ }
673
+
674
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
675
+ }
676
+
677
+ return PyNumber_Subtract(o1, o2);
678
+ }
679
+
680
+ /* ------------------------------------------------------------------------- */
681
+
682
+ static PyObject *WraptObjectProxy_multiply(PyObject *o1, PyObject *o2)
683
+ {
684
+ if (wrapt_is_proxy(o1))
685
+ {
686
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
687
+ {
688
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
689
+ return NULL;
690
+ }
691
+
692
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
693
+ }
694
+
695
+ if (wrapt_is_proxy(o2))
696
+ {
697
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
698
+ {
699
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
700
+ return NULL;
701
+ }
702
+
703
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
704
+ }
705
+
706
+ return PyNumber_Multiply(o1, o2);
707
+ }
708
+
709
+ /* ------------------------------------------------------------------------- */
710
+
711
+ static PyObject *WraptObjectProxy_remainder(PyObject *o1, PyObject *o2)
712
+ {
713
+ if (wrapt_is_proxy(o1))
714
+ {
715
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
716
+ {
717
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
718
+ return NULL;
719
+ }
720
+
721
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
722
+ }
723
+
724
+ if (wrapt_is_proxy(o2))
725
+ {
726
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
727
+ {
728
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
729
+ return NULL;
730
+ }
731
+
732
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
733
+ }
734
+
735
+ return PyNumber_Remainder(o1, o2);
736
+ }
737
+
738
+ /* ------------------------------------------------------------------------- */
739
+
740
+ static PyObject *WraptObjectProxy_divmod(PyObject *o1, PyObject *o2)
741
+ {
742
+ if (wrapt_is_proxy(o1))
743
+ {
744
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
745
+ {
746
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
747
+ return NULL;
748
+ }
749
+
750
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
751
+ }
752
+
753
+ if (wrapt_is_proxy(o2))
754
+ {
755
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
756
+ {
757
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
758
+ return NULL;
759
+ }
760
+
761
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
762
+ }
763
+
764
+ return PyNumber_Divmod(o1, o2);
765
+ }
766
+
767
+ /* ------------------------------------------------------------------------- */
768
+
769
+ static PyObject *WraptObjectProxy_power(PyObject *o1, PyObject *o2,
770
+ PyObject *modulo)
771
+ {
772
+ /*
773
+ * If modulo is a proxy we cannot call PyNumber_Power with it, otherwise
774
+ * we would recurse back into this slot via CPython's ternary_op fallback
775
+ * and blow the C stack. Return NotImplemented so a TypeError is raised,
776
+ * matching the pure Python implementation which does not unwrap modulo.
777
+ */
778
+
779
+ if (wrapt_is_proxy(modulo))
780
+ {
781
+ Py_RETURN_NOTIMPLEMENTED;
782
+ }
783
+
784
+ if (wrapt_is_proxy(o1))
785
+ {
786
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
787
+ {
788
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
789
+ return NULL;
790
+ }
791
+
792
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
793
+ }
794
+
795
+ if (wrapt_is_proxy(o2))
796
+ {
797
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
798
+ {
799
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
800
+ return NULL;
801
+ }
802
+
803
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
804
+ }
805
+
806
+ return PyNumber_Power(o1, o2, modulo);
807
+ }
808
+
809
+ /* ------------------------------------------------------------------------- */
810
+
811
+ static PyObject *WraptObjectProxy_negative(WraptObjectProxyObject *self)
812
+ {
813
+ if (!self->wrapped)
814
+ {
815
+ if (raise_uninitialized_wrapper_error(self) == -1)
816
+ return NULL;
817
+ }
818
+
819
+ return PyNumber_Negative(self->wrapped);
820
+ }
821
+
822
+ /* ------------------------------------------------------------------------- */
823
+
824
+ static PyObject *WraptObjectProxy_positive(WraptObjectProxyObject *self)
825
+ {
826
+ if (!self->wrapped)
827
+ {
828
+ if (raise_uninitialized_wrapper_error(self) == -1)
829
+ return NULL;
830
+ }
831
+
832
+ return PyNumber_Positive(self->wrapped);
833
+ }
834
+
835
+ /* ------------------------------------------------------------------------- */
836
+
837
+ static PyObject *WraptObjectProxy_absolute(WraptObjectProxyObject *self)
838
+ {
839
+ if (!self->wrapped)
840
+ {
841
+ if (raise_uninitialized_wrapper_error(self) == -1)
842
+ return NULL;
843
+ }
844
+
845
+ return PyNumber_Absolute(self->wrapped);
846
+ }
847
+
848
+ /* ------------------------------------------------------------------------- */
849
+
850
+ static int WraptObjectProxy_bool(WraptObjectProxyObject *self)
851
+ {
852
+ if (!self->wrapped)
853
+ {
854
+ if (raise_uninitialized_wrapper_error(self) == -1)
855
+ return -1;
856
+ }
857
+
858
+ return PyObject_IsTrue(self->wrapped);
859
+ }
860
+
861
+ /* ------------------------------------------------------------------------- */
862
+
863
+ static PyObject *WraptObjectProxy_invert(WraptObjectProxyObject *self)
864
+ {
865
+ if (!self->wrapped)
866
+ {
867
+ if (raise_uninitialized_wrapper_error(self) == -1)
868
+ return NULL;
869
+ }
870
+
871
+ return PyNumber_Invert(self->wrapped);
872
+ }
873
+
874
+ /* ------------------------------------------------------------------------- */
875
+
876
+ static PyObject *WraptObjectProxy_lshift(PyObject *o1, PyObject *o2)
877
+ {
878
+ if (wrapt_is_proxy(o1))
879
+ {
880
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
881
+ {
882
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
883
+ return NULL;
884
+ }
885
+
886
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
887
+ }
888
+
889
+ if (wrapt_is_proxy(o2))
890
+ {
891
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
892
+ {
893
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
894
+ return NULL;
895
+ }
896
+
897
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
898
+ }
899
+
900
+ return PyNumber_Lshift(o1, o2);
901
+ }
902
+
903
+ /* ------------------------------------------------------------------------- */
904
+
905
+ static PyObject *WraptObjectProxy_rshift(PyObject *o1, PyObject *o2)
906
+ {
907
+ if (wrapt_is_proxy(o1))
908
+ {
909
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
910
+ {
911
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
912
+ return NULL;
913
+ }
914
+
915
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
916
+ }
917
+
918
+ if (wrapt_is_proxy(o2))
919
+ {
920
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
921
+ {
922
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
923
+ return NULL;
924
+ }
925
+
926
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
927
+ }
928
+
929
+ return PyNumber_Rshift(o1, o2);
930
+ }
931
+
932
+ /* ------------------------------------------------------------------------- */
933
+
934
+ static PyObject *WraptObjectProxy_and(PyObject *o1, PyObject *o2)
935
+ {
936
+ if (wrapt_is_proxy(o1))
937
+ {
938
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
939
+ {
940
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
941
+ return NULL;
942
+ }
943
+
944
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
945
+ }
946
+
947
+ if (wrapt_is_proxy(o2))
948
+ {
949
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
950
+ {
951
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
952
+ return NULL;
953
+ }
954
+
955
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
956
+ }
957
+
958
+ return PyNumber_And(o1, o2);
959
+ }
960
+
961
+ /* ------------------------------------------------------------------------- */
962
+
963
+ static PyObject *WraptObjectProxy_xor(PyObject *o1, PyObject *o2)
964
+ {
965
+ if (wrapt_is_proxy(o1))
966
+ {
967
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
968
+ {
969
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
970
+ return NULL;
971
+ }
972
+
973
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
974
+ }
975
+
976
+ if (wrapt_is_proxy(o2))
977
+ {
978
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
979
+ {
980
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
981
+ return NULL;
982
+ }
983
+
984
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
985
+ }
986
+
987
+ return PyNumber_Xor(o1, o2);
988
+ }
989
+
990
+ /* ------------------------------------------------------------------------- */
991
+
992
+ static PyObject *WraptObjectProxy_or(PyObject *o1, PyObject *o2)
993
+ {
994
+ if (wrapt_is_proxy(o1))
995
+ {
996
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
997
+ {
998
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
999
+ return NULL;
1000
+ }
1001
+
1002
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
1003
+ }
1004
+
1005
+ if (wrapt_is_proxy(o2))
1006
+ {
1007
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
1008
+ {
1009
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
1010
+ return NULL;
1011
+ }
1012
+
1013
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
1014
+ }
1015
+
1016
+ return PyNumber_Or(o1, o2);
1017
+ }
1018
+
1019
+ /* ------------------------------------------------------------------------- */
1020
+
1021
+ static PyObject *WraptObjectProxy_long(WraptObjectProxyObject *self)
1022
+ {
1023
+ if (!self->wrapped)
1024
+ {
1025
+ if (raise_uninitialized_wrapper_error(self) == -1)
1026
+ return NULL;
1027
+ }
1028
+
1029
+ return PyNumber_Long(self->wrapped);
1030
+ }
1031
+
1032
+ /* ------------------------------------------------------------------------- */
1033
+
1034
+ static PyObject *WraptObjectProxy_float(WraptObjectProxyObject *self)
1035
+ {
1036
+ if (!self->wrapped)
1037
+ {
1038
+ if (raise_uninitialized_wrapper_error(self) == -1)
1039
+ return NULL;
1040
+ }
1041
+
1042
+ return PyNumber_Float(self->wrapped);
1043
+ }
1044
+
1045
+ /* ------------------------------------------------------------------------- */
1046
+
1047
+ static PyObject *WraptObjectProxy_inplace_add(WraptObjectProxyObject *self,
1048
+ PyObject *other)
1049
+ {
1050
+ PyObject *object = NULL;
1051
+
1052
+ if (!self->wrapped)
1053
+ {
1054
+ if (raise_uninitialized_wrapper_error(self) == -1)
1055
+ return NULL;
1056
+ }
1057
+
1058
+ if (wrapt_is_proxy(other))
1059
+ {
1060
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1061
+ {
1062
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1063
+ return NULL;
1064
+ }
1065
+
1066
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1067
+ }
1068
+
1069
+ PyObject *attr;
1070
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__iadd__", &attr);
1071
+ Py_XDECREF(attr);
1072
+ if (rc < 0)
1073
+ return NULL;
1074
+ if (rc)
1075
+ {
1076
+ object = PyNumber_InPlaceAdd(self->wrapped, other);
1077
+
1078
+ if (!object)
1079
+ return NULL;
1080
+
1081
+ /* As in the __wrapped__ setter, serialise the swap so that concurrent
1082
+ * in-place operations on the same proxy cannot decref the same old
1083
+ * wrapped object twice on a free-threaded build. The wrapped object
1084
+ * was read before the operation ran, so a concurrent mutation can
1085
+ * still be lost, matching pure Python semantics. The same applies to
1086
+ * all of the in-place operators which follow. */
1087
+
1088
+ Py_BEGIN_CRITICAL_SECTION(self);
1089
+ Py_SETREF(self->wrapped, object);
1090
+ Py_END_CRITICAL_SECTION();
1091
+
1092
+ Py_INCREF(self);
1093
+ return (PyObject *)self;
1094
+ }
1095
+ else
1096
+ {
1097
+ PyObject *result = PyNumber_Add(self->wrapped, other);
1098
+
1099
+ if (!result)
1100
+ return NULL;
1101
+
1102
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1103
+ if (!state)
1104
+ {
1105
+ Py_DECREF(result);
1106
+ return NULL;
1107
+ }
1108
+
1109
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1110
+
1111
+ if (!proxy_type)
1112
+ {
1113
+ Py_DECREF(result);
1114
+ return NULL;
1115
+ }
1116
+
1117
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1118
+
1119
+ Py_DECREF(result);
1120
+
1121
+ if (!proxy_args)
1122
+ {
1123
+ Py_DECREF(proxy_type);
1124
+ return NULL;
1125
+ }
1126
+
1127
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1128
+
1129
+ Py_DECREF(proxy_type);
1130
+ Py_DECREF(proxy_args);
1131
+
1132
+ return proxy_instance;
1133
+ }
1134
+ }
1135
+
1136
+ /* ------------------------------------------------------------------------- */
1137
+
1138
+ static PyObject *WraptObjectProxy_inplace_subtract(WraptObjectProxyObject *self,
1139
+ PyObject *other)
1140
+ {
1141
+ PyObject *object = NULL;
1142
+
1143
+ if (!self->wrapped)
1144
+ {
1145
+ if (raise_uninitialized_wrapper_error(self) == -1)
1146
+ return NULL;
1147
+ }
1148
+
1149
+ if (wrapt_is_proxy(other))
1150
+ {
1151
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1152
+ {
1153
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1154
+ return NULL;
1155
+ }
1156
+
1157
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1158
+ }
1159
+
1160
+ PyObject *attr;
1161
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__isub__", &attr);
1162
+ Py_XDECREF(attr);
1163
+ if (rc < 0)
1164
+ return NULL;
1165
+ if (rc)
1166
+ {
1167
+ object = PyNumber_InPlaceSubtract(self->wrapped, other);
1168
+
1169
+ if (!object)
1170
+ return NULL;
1171
+
1172
+ Py_BEGIN_CRITICAL_SECTION(self);
1173
+ Py_SETREF(self->wrapped, object);
1174
+ Py_END_CRITICAL_SECTION();
1175
+
1176
+ Py_INCREF(self);
1177
+ return (PyObject *)self;
1178
+ }
1179
+ else
1180
+ {
1181
+ PyObject *result = PyNumber_Subtract(self->wrapped, other);
1182
+
1183
+ if (!result)
1184
+ return NULL;
1185
+
1186
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1187
+ if (!state)
1188
+ {
1189
+ Py_DECREF(result);
1190
+ return NULL;
1191
+ }
1192
+
1193
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1194
+
1195
+ if (!proxy_type)
1196
+ {
1197
+ Py_DECREF(result);
1198
+ return NULL;
1199
+ }
1200
+
1201
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1202
+
1203
+ Py_DECREF(result);
1204
+
1205
+ if (!proxy_args)
1206
+ {
1207
+ Py_DECREF(proxy_type);
1208
+ return NULL;
1209
+ }
1210
+
1211
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1212
+
1213
+ Py_DECREF(proxy_type);
1214
+ Py_DECREF(proxy_args);
1215
+
1216
+ return proxy_instance;
1217
+ }
1218
+ }
1219
+
1220
+ /* ------------------------------------------------------------------------- */
1221
+
1222
+ static PyObject *WraptObjectProxy_inplace_multiply(WraptObjectProxyObject *self,
1223
+ PyObject *other)
1224
+ {
1225
+ PyObject *object = NULL;
1226
+
1227
+ if (!self->wrapped)
1228
+ {
1229
+ if (raise_uninitialized_wrapper_error(self) == -1)
1230
+ return NULL;
1231
+ }
1232
+
1233
+ if (wrapt_is_proxy(other))
1234
+ {
1235
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1236
+ {
1237
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1238
+ return NULL;
1239
+ }
1240
+
1241
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1242
+ }
1243
+
1244
+ PyObject *attr;
1245
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__imul__", &attr);
1246
+ Py_XDECREF(attr);
1247
+ if (rc < 0)
1248
+ return NULL;
1249
+ if (rc)
1250
+ {
1251
+ object = PyNumber_InPlaceMultiply(self->wrapped, other);
1252
+
1253
+ if (!object)
1254
+ return NULL;
1255
+
1256
+ Py_BEGIN_CRITICAL_SECTION(self);
1257
+ Py_SETREF(self->wrapped, object);
1258
+ Py_END_CRITICAL_SECTION();
1259
+
1260
+ Py_INCREF(self);
1261
+ return (PyObject *)self;
1262
+ }
1263
+ else
1264
+ {
1265
+ PyObject *result = PyNumber_Multiply(self->wrapped, other);
1266
+
1267
+ if (!result)
1268
+ return NULL;
1269
+
1270
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1271
+ if (!state)
1272
+ {
1273
+ Py_DECREF(result);
1274
+ return NULL;
1275
+ }
1276
+
1277
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1278
+
1279
+ if (!proxy_type)
1280
+ {
1281
+ Py_DECREF(result);
1282
+ return NULL;
1283
+ }
1284
+
1285
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1286
+
1287
+ Py_DECREF(result);
1288
+
1289
+ if (!proxy_args)
1290
+ {
1291
+ Py_DECREF(proxy_type);
1292
+ return NULL;
1293
+ }
1294
+
1295
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1296
+
1297
+ Py_DECREF(proxy_type);
1298
+ Py_DECREF(proxy_args);
1299
+
1300
+ return proxy_instance;
1301
+ }
1302
+ }
1303
+
1304
+ /* ------------------------------------------------------------------------- */
1305
+
1306
+ static PyObject *
1307
+ WraptObjectProxy_inplace_remainder(WraptObjectProxyObject *self,
1308
+ PyObject *other)
1309
+ {
1310
+ PyObject *object = NULL;
1311
+
1312
+ if (!self->wrapped)
1313
+ {
1314
+ if (raise_uninitialized_wrapper_error(self) == -1)
1315
+ return NULL;
1316
+ }
1317
+
1318
+ if (wrapt_is_proxy(other))
1319
+ {
1320
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1321
+ {
1322
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1323
+ return NULL;
1324
+ }
1325
+
1326
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1327
+ }
1328
+
1329
+ PyObject *attr;
1330
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__imod__", &attr);
1331
+ Py_XDECREF(attr);
1332
+ if (rc < 0)
1333
+ return NULL;
1334
+ if (rc)
1335
+ {
1336
+ object = PyNumber_InPlaceRemainder(self->wrapped, other);
1337
+
1338
+ if (!object)
1339
+ return NULL;
1340
+
1341
+ Py_BEGIN_CRITICAL_SECTION(self);
1342
+ Py_SETREF(self->wrapped, object);
1343
+ Py_END_CRITICAL_SECTION();
1344
+
1345
+ Py_INCREF(self);
1346
+ return (PyObject *)self;
1347
+ }
1348
+ else
1349
+ {
1350
+ PyObject *result = PyNumber_Remainder(self->wrapped, other);
1351
+
1352
+ if (!result)
1353
+ return NULL;
1354
+
1355
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1356
+ if (!state)
1357
+ {
1358
+ Py_DECREF(result);
1359
+ return NULL;
1360
+ }
1361
+
1362
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1363
+
1364
+ if (!proxy_type)
1365
+ {
1366
+ Py_DECREF(result);
1367
+ return NULL;
1368
+ }
1369
+
1370
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1371
+
1372
+ Py_DECREF(result);
1373
+
1374
+ if (!proxy_args)
1375
+ {
1376
+ Py_DECREF(proxy_type);
1377
+ return NULL;
1378
+ }
1379
+
1380
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1381
+
1382
+ Py_DECREF(proxy_type);
1383
+ Py_DECREF(proxy_args);
1384
+
1385
+ return proxy_instance;
1386
+ }
1387
+ }
1388
+
1389
+ /* ------------------------------------------------------------------------- */
1390
+
1391
+ static PyObject *WraptObjectProxy_inplace_power(WraptObjectProxyObject *self,
1392
+ PyObject *other,
1393
+ PyObject *modulo)
1394
+ {
1395
+ PyObject *object = NULL;
1396
+
1397
+ if (!self->wrapped)
1398
+ {
1399
+ if (raise_uninitialized_wrapper_error(self) == -1)
1400
+ return NULL;
1401
+ }
1402
+
1403
+ if (wrapt_is_proxy(other))
1404
+ {
1405
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1406
+ {
1407
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1408
+ return NULL;
1409
+ }
1410
+
1411
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1412
+ }
1413
+
1414
+ PyObject *attr;
1415
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__ipow__", &attr);
1416
+ Py_XDECREF(attr);
1417
+ if (rc < 0)
1418
+ return NULL;
1419
+ if (rc)
1420
+ {
1421
+ object = PyNumber_InPlacePower(self->wrapped, other, modulo);
1422
+
1423
+ if (!object)
1424
+ return NULL;
1425
+
1426
+ Py_BEGIN_CRITICAL_SECTION(self);
1427
+ Py_SETREF(self->wrapped, object);
1428
+ Py_END_CRITICAL_SECTION();
1429
+
1430
+ Py_INCREF(self);
1431
+ return (PyObject *)self;
1432
+ }
1433
+ else
1434
+ {
1435
+ PyObject *result = PyNumber_Power(self->wrapped, other, modulo);
1436
+
1437
+ if (!result)
1438
+ return NULL;
1439
+
1440
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1441
+ if (!state)
1442
+ {
1443
+ Py_DECREF(result);
1444
+ return NULL;
1445
+ }
1446
+
1447
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1448
+
1449
+ if (!proxy_type)
1450
+ {
1451
+ Py_DECREF(result);
1452
+ return NULL;
1453
+ }
1454
+
1455
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1456
+
1457
+ Py_DECREF(result);
1458
+
1459
+ if (!proxy_args)
1460
+ {
1461
+ Py_DECREF(proxy_type);
1462
+ return NULL;
1463
+ }
1464
+
1465
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1466
+
1467
+ Py_DECREF(proxy_type);
1468
+ Py_DECREF(proxy_args);
1469
+
1470
+ return proxy_instance;
1471
+ }
1472
+ }
1473
+
1474
+ /* ------------------------------------------------------------------------- */
1475
+
1476
+ static PyObject *WraptObjectProxy_inplace_lshift(WraptObjectProxyObject *self,
1477
+ PyObject *other)
1478
+ {
1479
+ PyObject *object = NULL;
1480
+
1481
+ if (!self->wrapped)
1482
+ {
1483
+ if (raise_uninitialized_wrapper_error(self) == -1)
1484
+ return NULL;
1485
+ }
1486
+
1487
+ if (wrapt_is_proxy(other))
1488
+ {
1489
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1490
+ {
1491
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1492
+ return NULL;
1493
+ }
1494
+
1495
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1496
+ }
1497
+
1498
+ PyObject *attr;
1499
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__ilshift__", &attr);
1500
+ Py_XDECREF(attr);
1501
+ if (rc < 0)
1502
+ return NULL;
1503
+ if (rc)
1504
+ {
1505
+ object = PyNumber_InPlaceLshift(self->wrapped, other);
1506
+
1507
+ if (!object)
1508
+ return NULL;
1509
+
1510
+ Py_BEGIN_CRITICAL_SECTION(self);
1511
+ Py_SETREF(self->wrapped, object);
1512
+ Py_END_CRITICAL_SECTION();
1513
+
1514
+ Py_INCREF(self);
1515
+ return (PyObject *)self;
1516
+ }
1517
+ else
1518
+ {
1519
+ PyObject *result = PyNumber_Lshift(self->wrapped, other);
1520
+
1521
+ if (!result)
1522
+ return NULL;
1523
+
1524
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1525
+ if (!state)
1526
+ {
1527
+ Py_DECREF(result);
1528
+ return NULL;
1529
+ }
1530
+
1531
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1532
+
1533
+ if (!proxy_type)
1534
+ {
1535
+ Py_DECREF(result);
1536
+ return NULL;
1537
+ }
1538
+
1539
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1540
+
1541
+ Py_DECREF(result);
1542
+
1543
+ if (!proxy_args)
1544
+ {
1545
+ Py_DECREF(proxy_type);
1546
+ return NULL;
1547
+ }
1548
+
1549
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1550
+
1551
+ Py_DECREF(proxy_type);
1552
+ Py_DECREF(proxy_args);
1553
+
1554
+ return proxy_instance;
1555
+ }
1556
+ }
1557
+
1558
+ /* ------------------------------------------------------------------------- */
1559
+
1560
+ static PyObject *WraptObjectProxy_inplace_rshift(WraptObjectProxyObject *self,
1561
+ PyObject *other)
1562
+ {
1563
+ PyObject *object = NULL;
1564
+
1565
+ if (!self->wrapped)
1566
+ {
1567
+ if (raise_uninitialized_wrapper_error(self) == -1)
1568
+ return NULL;
1569
+ }
1570
+
1571
+ if (wrapt_is_proxy(other))
1572
+ {
1573
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1574
+ {
1575
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1576
+ return NULL;
1577
+ }
1578
+
1579
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1580
+ }
1581
+
1582
+ PyObject *attr;
1583
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__irshift__", &attr);
1584
+ Py_XDECREF(attr);
1585
+ if (rc < 0)
1586
+ return NULL;
1587
+ if (rc)
1588
+ {
1589
+ object = PyNumber_InPlaceRshift(self->wrapped, other);
1590
+
1591
+ if (!object)
1592
+ return NULL;
1593
+
1594
+ Py_BEGIN_CRITICAL_SECTION(self);
1595
+ Py_SETREF(self->wrapped, object);
1596
+ Py_END_CRITICAL_SECTION();
1597
+
1598
+ Py_INCREF(self);
1599
+ return (PyObject *)self;
1600
+ }
1601
+ else
1602
+ {
1603
+ PyObject *result = PyNumber_Rshift(self->wrapped, other);
1604
+
1605
+ if (!result)
1606
+ return NULL;
1607
+
1608
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1609
+ if (!state)
1610
+ {
1611
+ Py_DECREF(result);
1612
+ return NULL;
1613
+ }
1614
+
1615
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1616
+
1617
+ if (!proxy_type)
1618
+ {
1619
+ Py_DECREF(result);
1620
+ return NULL;
1621
+ }
1622
+
1623
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1624
+
1625
+ Py_DECREF(result);
1626
+
1627
+ if (!proxy_args)
1628
+ {
1629
+ Py_DECREF(proxy_type);
1630
+ return NULL;
1631
+ }
1632
+
1633
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1634
+
1635
+ Py_DECREF(proxy_type);
1636
+ Py_DECREF(proxy_args);
1637
+
1638
+ return proxy_instance;
1639
+ }
1640
+ }
1641
+
1642
+ /* ------------------------------------------------------------------------- */
1643
+
1644
+ static PyObject *WraptObjectProxy_inplace_and(WraptObjectProxyObject *self,
1645
+ PyObject *other)
1646
+ {
1647
+ PyObject *object = NULL;
1648
+
1649
+ if (!self->wrapped)
1650
+ {
1651
+ if (raise_uninitialized_wrapper_error(self) == -1)
1652
+ return NULL;
1653
+ }
1654
+
1655
+ if (wrapt_is_proxy(other))
1656
+ {
1657
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1658
+ {
1659
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1660
+ return NULL;
1661
+ }
1662
+
1663
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1664
+ }
1665
+
1666
+ PyObject *attr;
1667
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__iand__", &attr);
1668
+ Py_XDECREF(attr);
1669
+ if (rc < 0)
1670
+ return NULL;
1671
+ if (rc)
1672
+ {
1673
+ object = PyNumber_InPlaceAnd(self->wrapped, other);
1674
+
1675
+ if (!object)
1676
+ return NULL;
1677
+
1678
+ Py_BEGIN_CRITICAL_SECTION(self);
1679
+ Py_SETREF(self->wrapped, object);
1680
+ Py_END_CRITICAL_SECTION();
1681
+
1682
+ Py_INCREF(self);
1683
+ return (PyObject *)self;
1684
+ }
1685
+ else
1686
+ {
1687
+ PyObject *result = PyNumber_And(self->wrapped, other);
1688
+
1689
+ if (!result)
1690
+ return NULL;
1691
+
1692
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1693
+ if (!state)
1694
+ {
1695
+ Py_DECREF(result);
1696
+ return NULL;
1697
+ }
1698
+
1699
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1700
+
1701
+ if (!proxy_type)
1702
+ {
1703
+ Py_DECREF(result);
1704
+ return NULL;
1705
+ }
1706
+
1707
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1708
+
1709
+ Py_DECREF(result);
1710
+
1711
+ if (!proxy_args)
1712
+ {
1713
+ Py_DECREF(proxy_type);
1714
+ return NULL;
1715
+ }
1716
+
1717
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1718
+
1719
+ Py_DECREF(proxy_type);
1720
+ Py_DECREF(proxy_args);
1721
+
1722
+ return proxy_instance;
1723
+ }
1724
+ }
1725
+
1726
+ /* ------------------------------------------------------------------------- */
1727
+
1728
+ static PyObject *WraptObjectProxy_inplace_xor(WraptObjectProxyObject *self,
1729
+ PyObject *other)
1730
+ {
1731
+ PyObject *object = NULL;
1732
+
1733
+ if (!self->wrapped)
1734
+ {
1735
+ if (raise_uninitialized_wrapper_error(self) == -1)
1736
+ return NULL;
1737
+ }
1738
+
1739
+ if (wrapt_is_proxy(other))
1740
+ {
1741
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1742
+ {
1743
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1744
+ return NULL;
1745
+ }
1746
+
1747
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1748
+ }
1749
+
1750
+ PyObject *attr;
1751
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__ixor__", &attr);
1752
+ Py_XDECREF(attr);
1753
+ if (rc < 0)
1754
+ return NULL;
1755
+ if (rc)
1756
+ {
1757
+ object = PyNumber_InPlaceXor(self->wrapped, other);
1758
+
1759
+ if (!object)
1760
+ return NULL;
1761
+
1762
+ Py_BEGIN_CRITICAL_SECTION(self);
1763
+ Py_SETREF(self->wrapped, object);
1764
+ Py_END_CRITICAL_SECTION();
1765
+
1766
+ Py_INCREF(self);
1767
+ return (PyObject *)self;
1768
+ }
1769
+ else
1770
+ {
1771
+ PyObject *result = PyNumber_Xor(self->wrapped, other);
1772
+
1773
+ if (!result)
1774
+ return NULL;
1775
+
1776
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1777
+ if (!state)
1778
+ {
1779
+ Py_DECREF(result);
1780
+ return NULL;
1781
+ }
1782
+
1783
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1784
+
1785
+ if (!proxy_type)
1786
+ {
1787
+ Py_DECREF(result);
1788
+ return NULL;
1789
+ }
1790
+
1791
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1792
+
1793
+ Py_DECREF(result);
1794
+
1795
+ if (!proxy_args)
1796
+ {
1797
+ Py_DECREF(proxy_type);
1798
+ return NULL;
1799
+ }
1800
+
1801
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1802
+
1803
+ Py_DECREF(proxy_type);
1804
+ Py_DECREF(proxy_args);
1805
+
1806
+ return proxy_instance;
1807
+ }
1808
+ }
1809
+
1810
+ /* ------------------------------------------------------------------------- */
1811
+
1812
+ static PyObject *WraptObjectProxy_inplace_or(WraptObjectProxyObject *self,
1813
+ PyObject *other)
1814
+ {
1815
+ PyObject *object = NULL;
1816
+
1817
+ if (!self->wrapped)
1818
+ {
1819
+ if (raise_uninitialized_wrapper_error(self) == -1)
1820
+ return NULL;
1821
+ }
1822
+
1823
+ if (wrapt_is_proxy(other))
1824
+ {
1825
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1826
+ {
1827
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1828
+ return NULL;
1829
+ }
1830
+
1831
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1832
+ }
1833
+
1834
+ PyObject *attr;
1835
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__ior__", &attr);
1836
+ Py_XDECREF(attr);
1837
+ if (rc < 0)
1838
+ return NULL;
1839
+ if (rc)
1840
+ {
1841
+ object = PyNumber_InPlaceOr(self->wrapped, other);
1842
+
1843
+ if (!object)
1844
+ return NULL;
1845
+
1846
+ Py_BEGIN_CRITICAL_SECTION(self);
1847
+ Py_SETREF(self->wrapped, object);
1848
+ Py_END_CRITICAL_SECTION();
1849
+
1850
+ Py_INCREF(self);
1851
+ return (PyObject *)self;
1852
+ }
1853
+ else
1854
+ {
1855
+ PyObject *result = PyNumber_Or(self->wrapped, other);
1856
+
1857
+ if (!result)
1858
+ return NULL;
1859
+
1860
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
1861
+ if (!state)
1862
+ {
1863
+ Py_DECREF(result);
1864
+ return NULL;
1865
+ }
1866
+
1867
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
1868
+
1869
+ if (!proxy_type)
1870
+ {
1871
+ Py_DECREF(result);
1872
+ return NULL;
1873
+ }
1874
+
1875
+ PyObject *proxy_args = PyTuple_Pack(1, result);
1876
+
1877
+ Py_DECREF(result);
1878
+
1879
+ if (!proxy_args)
1880
+ {
1881
+ Py_DECREF(proxy_type);
1882
+ return NULL;
1883
+ }
1884
+
1885
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
1886
+
1887
+ Py_DECREF(proxy_type);
1888
+ Py_DECREF(proxy_args);
1889
+
1890
+ return proxy_instance;
1891
+ }
1892
+ }
1893
+
1894
+ /* ------------------------------------------------------------------------- */
1895
+
1896
+ static PyObject *WraptObjectProxy_floor_divide(PyObject *o1, PyObject *o2)
1897
+ {
1898
+ if (wrapt_is_proxy(o1))
1899
+ {
1900
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
1901
+ {
1902
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
1903
+ return NULL;
1904
+ }
1905
+
1906
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
1907
+ }
1908
+
1909
+ if (wrapt_is_proxy(o2))
1910
+ {
1911
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
1912
+ {
1913
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
1914
+ return NULL;
1915
+ }
1916
+
1917
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
1918
+ }
1919
+
1920
+ return PyNumber_FloorDivide(o1, o2);
1921
+ }
1922
+
1923
+ /* ------------------------------------------------------------------------- */
1924
+
1925
+ static PyObject *WraptObjectProxy_true_divide(PyObject *o1, PyObject *o2)
1926
+ {
1927
+ if (wrapt_is_proxy(o1))
1928
+ {
1929
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
1930
+ {
1931
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
1932
+ return NULL;
1933
+ }
1934
+
1935
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
1936
+ }
1937
+
1938
+ if (wrapt_is_proxy(o2))
1939
+ {
1940
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
1941
+ {
1942
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
1943
+ return NULL;
1944
+ }
1945
+
1946
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
1947
+ }
1948
+
1949
+ return PyNumber_TrueDivide(o1, o2);
1950
+ }
1951
+
1952
+ /* ------------------------------------------------------------------------- */
1953
+
1954
+ static PyObject *
1955
+ WraptObjectProxy_inplace_floor_divide(WraptObjectProxyObject *self,
1956
+ PyObject *other)
1957
+ {
1958
+ PyObject *object = NULL;
1959
+
1960
+ if (!self->wrapped)
1961
+ {
1962
+ if (raise_uninitialized_wrapper_error(self) == -1)
1963
+ return NULL;
1964
+ }
1965
+
1966
+ if (wrapt_is_proxy(other))
1967
+ {
1968
+ if (!((WraptObjectProxyObject *)other)->wrapped)
1969
+ {
1970
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
1971
+ return NULL;
1972
+ }
1973
+
1974
+ other = ((WraptObjectProxyObject *)other)->wrapped;
1975
+ }
1976
+
1977
+ PyObject *attr;
1978
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__ifloordiv__", &attr);
1979
+ Py_XDECREF(attr);
1980
+ if (rc < 0)
1981
+ return NULL;
1982
+ if (rc)
1983
+ {
1984
+ object = PyNumber_InPlaceFloorDivide(self->wrapped, other);
1985
+
1986
+ if (!object)
1987
+ return NULL;
1988
+
1989
+ Py_BEGIN_CRITICAL_SECTION(self);
1990
+ Py_SETREF(self->wrapped, object);
1991
+ Py_END_CRITICAL_SECTION();
1992
+
1993
+ Py_INCREF(self);
1994
+ return (PyObject *)self;
1995
+ }
1996
+ else
1997
+ {
1998
+ PyObject *result = PyNumber_FloorDivide(self->wrapped, other);
1999
+
2000
+ if (!result)
2001
+ return NULL;
2002
+
2003
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2004
+ if (!state)
2005
+ {
2006
+ Py_DECREF(result);
2007
+ return NULL;
2008
+ }
2009
+
2010
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
2011
+
2012
+ if (!proxy_type)
2013
+ {
2014
+ Py_DECREF(result);
2015
+ return NULL;
2016
+ }
2017
+
2018
+ PyObject *proxy_args = PyTuple_Pack(1, result);
2019
+
2020
+ Py_DECREF(result);
2021
+
2022
+ if (!proxy_args)
2023
+ {
2024
+ Py_DECREF(proxy_type);
2025
+ return NULL;
2026
+ }
2027
+
2028
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
2029
+
2030
+ Py_DECREF(proxy_type);
2031
+ Py_DECREF(proxy_args);
2032
+
2033
+ return proxy_instance;
2034
+ }
2035
+ }
2036
+
2037
+ /* ------------------------------------------------------------------------- */
2038
+
2039
+ static PyObject *
2040
+ WraptObjectProxy_inplace_true_divide(WraptObjectProxyObject *self,
2041
+ PyObject *other)
2042
+ {
2043
+ PyObject *object = NULL;
2044
+
2045
+ if (!self->wrapped)
2046
+ {
2047
+ if (raise_uninitialized_wrapper_error(self) == -1)
2048
+ return NULL;
2049
+ }
2050
+
2051
+ if (wrapt_is_proxy(other))
2052
+ {
2053
+ if (!((WraptObjectProxyObject *)other)->wrapped)
2054
+ {
2055
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
2056
+ return NULL;
2057
+ }
2058
+
2059
+ other = ((WraptObjectProxyObject *)other)->wrapped;
2060
+ }
2061
+
2062
+ PyObject *attr;
2063
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__itruediv__", &attr);
2064
+ Py_XDECREF(attr);
2065
+ if (rc < 0)
2066
+ return NULL;
2067
+ if (rc)
2068
+ {
2069
+ object = PyNumber_InPlaceTrueDivide(self->wrapped, other);
2070
+
2071
+ if (!object)
2072
+ return NULL;
2073
+
2074
+ Py_BEGIN_CRITICAL_SECTION(self);
2075
+ Py_SETREF(self->wrapped, object);
2076
+ Py_END_CRITICAL_SECTION();
2077
+
2078
+ Py_INCREF(self);
2079
+ return (PyObject *)self;
2080
+ }
2081
+ else
2082
+ {
2083
+ PyObject *result = PyNumber_TrueDivide(self->wrapped, other);
2084
+
2085
+ if (!result)
2086
+ return NULL;
2087
+
2088
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2089
+ if (!state)
2090
+ {
2091
+ Py_DECREF(result);
2092
+ return NULL;
2093
+ }
2094
+
2095
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
2096
+
2097
+ if (!proxy_type)
2098
+ {
2099
+ Py_DECREF(result);
2100
+ return NULL;
2101
+ }
2102
+
2103
+ PyObject *proxy_args = PyTuple_Pack(1, result);
2104
+
2105
+ Py_DECREF(result);
2106
+
2107
+ if (!proxy_args)
2108
+ {
2109
+ Py_DECREF(proxy_type);
2110
+ return NULL;
2111
+ }
2112
+
2113
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
2114
+
2115
+ Py_DECREF(proxy_type);
2116
+ Py_DECREF(proxy_args);
2117
+
2118
+ return proxy_instance;
2119
+ }
2120
+ }
2121
+
2122
+ /* ------------------------------------------------------------------------- */
2123
+
2124
+ static PyObject *WraptObjectProxy_index(WraptObjectProxyObject *self)
2125
+ {
2126
+ if (!self->wrapped)
2127
+ {
2128
+ if (raise_uninitialized_wrapper_error(self) == -1)
2129
+ return NULL;
2130
+ }
2131
+
2132
+ return PyNumber_Index(self->wrapped);
2133
+ }
2134
+
2135
+ /* ------------------------------------------------------------------------- */
2136
+
2137
+ static PyObject *WraptObjectProxy_matrix_multiply(PyObject *o1, PyObject *o2)
2138
+ {
2139
+ if (wrapt_is_proxy(o1))
2140
+ {
2141
+ if (!((WraptObjectProxyObject *)o1)->wrapped)
2142
+ {
2143
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o1) == -1)
2144
+ return NULL;
2145
+ }
2146
+
2147
+ o1 = ((WraptObjectProxyObject *)o1)->wrapped;
2148
+ }
2149
+
2150
+ if (wrapt_is_proxy(o2))
2151
+ {
2152
+ if (!((WraptObjectProxyObject *)o2)->wrapped)
2153
+ {
2154
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)o2) == -1)
2155
+ return NULL;
2156
+ }
2157
+
2158
+ o2 = ((WraptObjectProxyObject *)o2)->wrapped;
2159
+ }
2160
+
2161
+ return PyNumber_MatrixMultiply(o1, o2);
2162
+ }
2163
+
2164
+ /* ------------------------------------------------------------------------- */
2165
+
2166
+ static PyObject *WraptObjectProxy_inplace_matrix_multiply(
2167
+ WraptObjectProxyObject *self, PyObject *other)
2168
+ {
2169
+ PyObject *object = NULL;
2170
+
2171
+ if (!self->wrapped)
2172
+ {
2173
+ if (raise_uninitialized_wrapper_error(self) == -1)
2174
+ return NULL;
2175
+ }
2176
+
2177
+ if (wrapt_is_proxy(other))
2178
+ {
2179
+ if (!((WraptObjectProxyObject *)other)->wrapped)
2180
+ {
2181
+ if (raise_uninitialized_wrapper_error((WraptObjectProxyObject *)other) == -1)
2182
+ return NULL;
2183
+ }
2184
+
2185
+ other = ((WraptObjectProxyObject *)other)->wrapped;
2186
+ }
2187
+
2188
+ PyObject *attr;
2189
+ int rc = PyObject_GetOptionalAttrString(self->wrapped, "__imatmul__", &attr);
2190
+ Py_XDECREF(attr);
2191
+ if (rc < 0)
2192
+ return NULL;
2193
+ if (rc)
2194
+ {
2195
+ object = PyNumber_InPlaceMatrixMultiply(self->wrapped, other);
2196
+
2197
+ if (!object)
2198
+ return NULL;
2199
+
2200
+ Py_BEGIN_CRITICAL_SECTION(self);
2201
+ Py_SETREF(self->wrapped, object);
2202
+ Py_END_CRITICAL_SECTION();
2203
+
2204
+ Py_INCREF(self);
2205
+ return (PyObject *)self;
2206
+ }
2207
+ else
2208
+ {
2209
+ PyObject *result = PyNumber_MatrixMultiply(self->wrapped, other);
2210
+
2211
+ if (!result)
2212
+ return NULL;
2213
+
2214
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2215
+ if (!state)
2216
+ {
2217
+ Py_DECREF(result);
2218
+ return NULL;
2219
+ }
2220
+
2221
+ PyObject *proxy_type = PyObject_GetAttr((PyObject *)self, state->str_object_proxy);
2222
+
2223
+ if (!proxy_type)
2224
+ {
2225
+ Py_DECREF(result);
2226
+ return NULL;
2227
+ }
2228
+
2229
+ PyObject *proxy_args = PyTuple_Pack(1, result);
2230
+
2231
+ Py_DECREF(result);
2232
+
2233
+ if (!proxy_args)
2234
+ {
2235
+ Py_DECREF(proxy_type);
2236
+ return NULL;
2237
+ }
2238
+
2239
+ PyObject *proxy_instance = PyObject_Call(proxy_type, proxy_args, NULL);
2240
+
2241
+ Py_DECREF(proxy_type);
2242
+ Py_DECREF(proxy_args);
2243
+
2244
+ return proxy_instance;
2245
+ }
2246
+ }
2247
+
2248
+ /* ------------------------------------------------------------------------- */
2249
+
2250
+ static Py_ssize_t WraptObjectProxy_length(WraptObjectProxyObject *self)
2251
+ {
2252
+ if (!self->wrapped)
2253
+ {
2254
+ if (raise_uninitialized_wrapper_error(self) == -1)
2255
+ return -1;
2256
+ }
2257
+
2258
+ return PyObject_Length(self->wrapped);
2259
+ }
2260
+
2261
+ /* ------------------------------------------------------------------------- */
2262
+
2263
+ static int WraptObjectProxy_contains(WraptObjectProxyObject *self,
2264
+ PyObject *value)
2265
+ {
2266
+ if (!self->wrapped)
2267
+ {
2268
+ if (raise_uninitialized_wrapper_error(self) == -1)
2269
+ return -1;
2270
+ }
2271
+
2272
+ return PySequence_Contains(self->wrapped, value);
2273
+ }
2274
+
2275
+ /* ------------------------------------------------------------------------- */
2276
+
2277
+ static PyObject *WraptObjectProxy_getitem(WraptObjectProxyObject *self,
2278
+ PyObject *key)
2279
+ {
2280
+ if (!self->wrapped)
2281
+ {
2282
+ if (raise_uninitialized_wrapper_error(self) == -1)
2283
+ return NULL;
2284
+ }
2285
+
2286
+ return PyObject_GetItem(self->wrapped, key);
2287
+ }
2288
+
2289
+ /* ------------------------------------------------------------------------- */
2290
+
2291
+ static int WraptObjectProxy_setitem(WraptObjectProxyObject *self, PyObject *key,
2292
+ PyObject *value)
2293
+ {
2294
+ if (!self->wrapped)
2295
+ {
2296
+ if (raise_uninitialized_wrapper_error(self) == -1)
2297
+ return -1;
2298
+ }
2299
+
2300
+ if (value == NULL)
2301
+ return PyObject_DelItem(self->wrapped, key);
2302
+ else
2303
+ return PyObject_SetItem(self->wrapped, key, value);
2304
+ }
2305
+
2306
+ /* ------------------------------------------------------------------------- */
2307
+
2308
+ static PyObject *WraptObjectProxy_self_setattr(WraptObjectProxyObject *self,
2309
+ PyObject *args)
2310
+ {
2311
+ PyObject *name = NULL;
2312
+ PyObject *value = NULL;
2313
+
2314
+ if (!PyArg_ParseTuple(args, "UO:__self_setattr__", &name, &value))
2315
+ return NULL;
2316
+
2317
+ if (PyObject_GenericSetAttr((PyObject *)self, name, value) != 0)
2318
+ {
2319
+ return NULL;
2320
+ }
2321
+
2322
+ Py_RETURN_NONE;
2323
+ }
2324
+
2325
+ /* ------------------------------------------------------------------------- */
2326
+
2327
+ static PyObject *WraptObjectProxy_dir(WraptObjectProxyObject *self,
2328
+ PyObject *args)
2329
+ {
2330
+ if (!self->wrapped)
2331
+ {
2332
+ if (raise_uninitialized_wrapper_error(self) == -1)
2333
+ return NULL;
2334
+ }
2335
+
2336
+ return PyObject_Dir(self->wrapped);
2337
+ }
2338
+
2339
+ /* ------------------------------------------------------------------------- */
2340
+
2341
+ static PyObject *WraptObjectProxy_enter(WraptObjectProxyObject *self,
2342
+ PyObject *args, PyObject *kwds)
2343
+ {
2344
+ PyObject *method = NULL;
2345
+ PyObject *result = NULL;
2346
+
2347
+ if (!self->wrapped)
2348
+ {
2349
+ if (raise_uninitialized_wrapper_error(self) == -1)
2350
+ return NULL;
2351
+ }
2352
+
2353
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2354
+ if (!state)
2355
+ return NULL;
2356
+
2357
+ method = PyObject_GetAttr(self->wrapped, state->str_enter);
2358
+
2359
+ if (!method)
2360
+ return NULL;
2361
+
2362
+ result = PyObject_Call(method, args, kwds);
2363
+
2364
+ Py_DECREF(method);
2365
+
2366
+ return result;
2367
+ }
2368
+
2369
+ /* ------------------------------------------------------------------------- */
2370
+
2371
+ static PyObject *WraptObjectProxy_exit(WraptObjectProxyObject *self,
2372
+ PyObject *args, PyObject *kwds)
2373
+ {
2374
+ PyObject *method = NULL;
2375
+ PyObject *result = NULL;
2376
+
2377
+ if (!self->wrapped)
2378
+ {
2379
+ if (raise_uninitialized_wrapper_error(self) == -1)
2380
+ return NULL;
2381
+ }
2382
+
2383
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2384
+ if (!state)
2385
+ return NULL;
2386
+
2387
+ method = PyObject_GetAttr(self->wrapped, state->str_exit);
2388
+
2389
+ if (!method)
2390
+ return NULL;
2391
+
2392
+ result = PyObject_Call(method, args, kwds);
2393
+
2394
+ Py_DECREF(method);
2395
+
2396
+ return result;
2397
+ }
2398
+
2399
+ /* ------------------------------------------------------------------------- */
2400
+
2401
+ static PyObject *WraptObjectProxy_aenter(WraptObjectProxyObject *self,
2402
+ PyObject *args, PyObject *kwds)
2403
+ {
2404
+ PyObject *method = NULL;
2405
+ PyObject *result = NULL;
2406
+
2407
+ if (!self->wrapped)
2408
+ {
2409
+ if (raise_uninitialized_wrapper_error(self) == -1)
2410
+ return NULL;
2411
+ }
2412
+
2413
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2414
+ if (!state)
2415
+ return NULL;
2416
+
2417
+ method = PyObject_GetAttr(self->wrapped, state->str_aenter);
2418
+
2419
+ if (!method)
2420
+ return NULL;
2421
+
2422
+ result = PyObject_Call(method, args, kwds);
2423
+
2424
+ Py_DECREF(method);
2425
+
2426
+ return result;
2427
+ }
2428
+
2429
+ /* ------------------------------------------------------------------------- */
2430
+
2431
+ static PyObject *WraptObjectProxy_aexit(WraptObjectProxyObject *self,
2432
+ PyObject *args, PyObject *kwds)
2433
+ {
2434
+ PyObject *method = NULL;
2435
+ PyObject *result = NULL;
2436
+
2437
+ if (!self->wrapped)
2438
+ {
2439
+ if (raise_uninitialized_wrapper_error(self) == -1)
2440
+ return NULL;
2441
+ }
2442
+
2443
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2444
+ if (!state)
2445
+ return NULL;
2446
+
2447
+ method = PyObject_GetAttr(self->wrapped, state->str_aexit);
2448
+
2449
+ if (!method)
2450
+ return NULL;
2451
+
2452
+ result = PyObject_Call(method, args, kwds);
2453
+
2454
+ Py_DECREF(method);
2455
+
2456
+ return result;
2457
+ }
2458
+
2459
+ /* ------------------------------------------------------------------------- */
2460
+
2461
+ static PyObject *WraptObjectProxy_copy(WraptObjectProxyObject *self,
2462
+ PyObject *Py_UNUSED(ignored))
2463
+ {
2464
+ PyErr_SetString(PyExc_NotImplementedError,
2465
+ "object proxy must define __copy__()");
2466
+
2467
+ return NULL;
2468
+ }
2469
+
2470
+ /* ------------------------------------------------------------------------- */
2471
+
2472
+ static PyObject *WraptObjectProxy_deepcopy(WraptObjectProxyObject *self,
2473
+ PyObject *args, PyObject *kwds)
2474
+ {
2475
+ PyErr_SetString(PyExc_NotImplementedError,
2476
+ "object proxy must define __deepcopy__()");
2477
+
2478
+ return NULL;
2479
+ }
2480
+
2481
+ /* ------------------------------------------------------------------------- */
2482
+
2483
+ static PyObject *WraptObjectProxy_reduce(WraptObjectProxyObject *self,
2484
+ PyObject *Py_UNUSED(ignored))
2485
+ {
2486
+ PyErr_SetString(PyExc_NotImplementedError,
2487
+ "object proxy must define __reduce__()");
2488
+
2489
+ return NULL;
2490
+ }
2491
+
2492
+ /* ------------------------------------------------------------------------- */
2493
+
2494
+ static PyObject *WraptObjectProxy_bytes(WraptObjectProxyObject *self,
2495
+ PyObject *args)
2496
+ {
2497
+ if (!self->wrapped)
2498
+ {
2499
+ if (raise_uninitialized_wrapper_error(self) == -1)
2500
+ return NULL;
2501
+ }
2502
+
2503
+ return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type,
2504
+ self->wrapped, NULL);
2505
+ }
2506
+
2507
+ /* ------------------------------------------------------------------------- */
2508
+
2509
+ static PyObject *WraptObjectProxy_format(WraptObjectProxyObject *self,
2510
+ PyObject *args)
2511
+ {
2512
+ PyObject *format_spec = NULL;
2513
+
2514
+ if (!self->wrapped)
2515
+ {
2516
+ if (raise_uninitialized_wrapper_error(self) == -1)
2517
+ return NULL;
2518
+ }
2519
+
2520
+ if (!PyArg_ParseTuple(args, "O:format", &format_spec))
2521
+ return NULL;
2522
+
2523
+ return PyObject_Format(self->wrapped, format_spec);
2524
+ }
2525
+
2526
+ /* ------------------------------------------------------------------------- */
2527
+
2528
+ static PyObject *WraptObjectProxy_reversed(WraptObjectProxyObject *self,
2529
+ PyObject *args)
2530
+ {
2531
+ if (!self->wrapped)
2532
+ {
2533
+ if (raise_uninitialized_wrapper_error(self) == -1)
2534
+ return NULL;
2535
+ }
2536
+
2537
+ return PyObject_CallFunctionObjArgs((PyObject *)&PyReversed_Type,
2538
+ self->wrapped, NULL);
2539
+ }
2540
+
2541
+ /* ------------------------------------------------------------------------- */
2542
+
2543
+ static PyObject *WraptObjectProxy_round(WraptObjectProxyObject *self,
2544
+ PyObject *args, PyObject *kwds)
2545
+ {
2546
+ PyObject *ndigits = NULL;
2547
+
2548
+ PyObject *module = NULL;
2549
+ PyObject *round = NULL;
2550
+
2551
+ PyObject *result = NULL;
2552
+
2553
+ char *const kwlist[] = {"ndigits", NULL};
2554
+
2555
+ if (!self->wrapped)
2556
+ {
2557
+ if (raise_uninitialized_wrapper_error(self) == -1)
2558
+ return NULL;
2559
+ }
2560
+
2561
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:ObjectProxy", kwlist,
2562
+ &ndigits))
2563
+ {
2564
+ return NULL;
2565
+ }
2566
+
2567
+ module = PyImport_ImportModule("builtins");
2568
+
2569
+ if (!module)
2570
+ return NULL;
2571
+
2572
+ round = PyObject_GetAttrString(module, "round");
2573
+
2574
+ if (!round)
2575
+ {
2576
+ Py_DECREF(module);
2577
+ return NULL;
2578
+ }
2579
+
2580
+ Py_DECREF(module);
2581
+
2582
+ result = PyObject_CallFunctionObjArgs(round, self->wrapped, ndigits, NULL);
2583
+
2584
+ Py_DECREF(round);
2585
+
2586
+ return result;
2587
+ }
2588
+
2589
+ /* ------------------------------------------------------------------------- */
2590
+
2591
+ static PyObject *wrapt_call_math_unary(WraptObjectProxyObject *self,
2592
+ const char *name)
2593
+ {
2594
+ PyObject *module = NULL;
2595
+ PyObject *function = NULL;
2596
+ PyObject *result = NULL;
2597
+
2598
+ if (!self->wrapped)
2599
+ {
2600
+ if (raise_uninitialized_wrapper_error(self) == -1)
2601
+ return NULL;
2602
+ }
2603
+
2604
+ module = PyImport_ImportModule("math");
2605
+
2606
+ if (!module)
2607
+ return NULL;
2608
+
2609
+ function = PyObject_GetAttrString(module, name);
2610
+
2611
+ if (!function)
2612
+ {
2613
+ Py_DECREF(module);
2614
+ return NULL;
2615
+ }
2616
+
2617
+ Py_DECREF(module);
2618
+
2619
+ result = PyObject_CallFunctionObjArgs(function, self->wrapped, NULL);
2620
+
2621
+ Py_DECREF(function);
2622
+
2623
+ return result;
2624
+ }
2625
+
2626
+ /* ------------------------------------------------------------------------- */
2627
+
2628
+ static PyObject *WraptObjectProxy_trunc(WraptObjectProxyObject *self,
2629
+ PyObject *Py_UNUSED(ignored))
2630
+ {
2631
+ return wrapt_call_math_unary(self, "trunc");
2632
+ }
2633
+
2634
+ /* ------------------------------------------------------------------------- */
2635
+
2636
+ static PyObject *WraptObjectProxy_floor(WraptObjectProxyObject *self,
2637
+ PyObject *Py_UNUSED(ignored))
2638
+ {
2639
+ return wrapt_call_math_unary(self, "floor");
2640
+ }
2641
+
2642
+ /* ------------------------------------------------------------------------- */
2643
+
2644
+ static PyObject *WraptObjectProxy_ceil(WraptObjectProxyObject *self,
2645
+ PyObject *Py_UNUSED(ignored))
2646
+ {
2647
+ return wrapt_call_math_unary(self, "ceil");
2648
+ }
2649
+
2650
+ /* ------------------------------------------------------------------------- */
2651
+
2652
+ static PyObject *WraptObjectProxy_complex(WraptObjectProxyObject *self,
2653
+ PyObject *args)
2654
+ {
2655
+ if (!self->wrapped)
2656
+ {
2657
+ if (raise_uninitialized_wrapper_error(self) == -1)
2658
+ return NULL;
2659
+ }
2660
+
2661
+ return PyObject_CallFunctionObjArgs((PyObject *)&PyComplex_Type,
2662
+ self->wrapped, NULL);
2663
+ }
2664
+
2665
+ /* ------------------------------------------------------------------------- */
2666
+
2667
+ static PyObject *WraptObjectProxy_mro_entries(WraptObjectProxyObject *self,
2668
+ PyObject *args, PyObject *kwds)
2669
+ {
2670
+ PyObject *wrapped = NULL;
2671
+ PyObject *mro_entries_method = NULL;
2672
+ PyObject *result = NULL;
2673
+ int is_type = 0;
2674
+
2675
+ if (!self->wrapped)
2676
+ {
2677
+ if (raise_uninitialized_wrapper_error(self) == -1)
2678
+ return NULL;
2679
+ }
2680
+
2681
+ wrapped = self->wrapped;
2682
+
2683
+ // Check if wrapped is a type (class).
2684
+
2685
+ is_type = PyType_Check(wrapped);
2686
+
2687
+ // If wrapped is not a type and has __mro_entries__, forward to it.
2688
+
2689
+ if (!is_type)
2690
+ {
2691
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2692
+ if (!state)
2693
+ return NULL;
2694
+
2695
+ mro_entries_method = PyObject_GetAttr(wrapped, state->str_mro_entries);
2696
+
2697
+ if (mro_entries_method)
2698
+ {
2699
+ // Call wrapped.__mro_entries__(bases).
2700
+
2701
+ result = PyObject_Call(mro_entries_method, args, kwds);
2702
+
2703
+ Py_DECREF(mro_entries_method);
2704
+
2705
+ return result;
2706
+ }
2707
+ else
2708
+ {
2709
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2710
+ return NULL;
2711
+ PyErr_Clear();
2712
+ }
2713
+ }
2714
+
2715
+ return Py_BuildValue("(O)", wrapped);
2716
+ }
2717
+
2718
+ /* ------------------------------------------------------------------------- */
2719
+
2720
+ static PyObject *WraptObjectProxy_get_name(WraptObjectProxyObject *self)
2721
+ {
2722
+ if (!self->wrapped)
2723
+ {
2724
+ if (raise_uninitialized_wrapper_error(self) == -1)
2725
+ return NULL;
2726
+ }
2727
+
2728
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2729
+ if (!state)
2730
+ return NULL;
2731
+
2732
+ return PyObject_GetAttr(self->wrapped, state->str_name);
2733
+ }
2734
+
2735
+ /* ------------------------------------------------------------------------- */
2736
+
2737
+ static int WraptObjectProxy_set_name(WraptObjectProxyObject *self,
2738
+ PyObject *value)
2739
+ {
2740
+ if (!self->wrapped)
2741
+ {
2742
+ if (raise_uninitialized_wrapper_error(self) == -1)
2743
+ return -1;
2744
+ }
2745
+
2746
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2747
+ if (!state)
2748
+ return -1;
2749
+
2750
+ return PyObject_SetAttr(self->wrapped, state->str_name, value);
2751
+ }
2752
+
2753
+ /* ------------------------------------------------------------------------- */
2754
+
2755
+ static PyObject *WraptObjectProxy_get_qualname(WraptObjectProxyObject *self)
2756
+ {
2757
+ if (!self->wrapped)
2758
+ {
2759
+ if (raise_uninitialized_wrapper_error(self) == -1)
2760
+ return NULL;
2761
+ }
2762
+
2763
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2764
+ if (!state)
2765
+ return NULL;
2766
+
2767
+ return PyObject_GetAttr(self->wrapped, state->str_qualname);
2768
+ }
2769
+
2770
+ /* ------------------------------------------------------------------------- */
2771
+
2772
+ static int WraptObjectProxy_set_qualname(WraptObjectProxyObject *self,
2773
+ PyObject *value)
2774
+ {
2775
+ if (!self->wrapped)
2776
+ {
2777
+ if (raise_uninitialized_wrapper_error(self) == -1)
2778
+ return -1;
2779
+ }
2780
+
2781
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2782
+ if (!state)
2783
+ return -1;
2784
+
2785
+ return PyObject_SetAttr(self->wrapped, state->str_qualname, value);
2786
+ }
2787
+
2788
+ /* ------------------------------------------------------------------------- */
2789
+
2790
+ static PyObject *WraptObjectProxy_get_module(WraptObjectProxyObject *self)
2791
+ {
2792
+ if (!self->wrapped)
2793
+ {
2794
+ if (raise_uninitialized_wrapper_error(self) == -1)
2795
+ return NULL;
2796
+ }
2797
+
2798
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2799
+ if (!state)
2800
+ return NULL;
2801
+
2802
+ return PyObject_GetAttr(self->wrapped, state->str_module);
2803
+ }
2804
+
2805
+ /* ------------------------------------------------------------------------- */
2806
+
2807
+ static int WraptObjectProxy_set_module(WraptObjectProxyObject *self,
2808
+ PyObject *value)
2809
+ {
2810
+ if (!self->wrapped)
2811
+ {
2812
+ if (raise_uninitialized_wrapper_error(self) == -1)
2813
+ return -1;
2814
+ }
2815
+
2816
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2817
+ if (!state)
2818
+ return -1;
2819
+
2820
+ if (PyObject_SetAttr(self->wrapped, state->str_module, value) == -1)
2821
+ return -1;
2822
+
2823
+ return PyDict_SetItemString(self->dict, "__module__", value);
2824
+ }
2825
+
2826
+ /* ------------------------------------------------------------------------- */
2827
+
2828
+ static PyObject *WraptObjectProxy_get_doc(WraptObjectProxyObject *self)
2829
+ {
2830
+ if (!self->wrapped)
2831
+ {
2832
+ if (raise_uninitialized_wrapper_error(self) == -1)
2833
+ return NULL;
2834
+ }
2835
+
2836
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2837
+ if (!state)
2838
+ return NULL;
2839
+
2840
+ return PyObject_GetAttr(self->wrapped, state->str_doc);
2841
+ }
2842
+
2843
+ /* ------------------------------------------------------------------------- */
2844
+
2845
+ static int WraptObjectProxy_set_doc(WraptObjectProxyObject *self,
2846
+ PyObject *value)
2847
+ {
2848
+ if (!self->wrapped)
2849
+ {
2850
+ if (raise_uninitialized_wrapper_error(self) == -1)
2851
+ return -1;
2852
+ }
2853
+
2854
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2855
+ if (!state)
2856
+ return -1;
2857
+
2858
+ if (PyObject_SetAttr(self->wrapped, state->str_doc, value) == -1)
2859
+ return -1;
2860
+
2861
+ return PyDict_SetItemString(self->dict, "__doc__", value);
2862
+ }
2863
+
2864
+ /* ------------------------------------------------------------------------- */
2865
+
2866
+ static PyObject *WraptObjectProxy_get_class(WraptObjectProxyObject *self)
2867
+ {
2868
+ if (!self->wrapped)
2869
+ {
2870
+ if (raise_uninitialized_wrapper_error(self) == -1)
2871
+ return NULL;
2872
+ }
2873
+
2874
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2875
+ if (!state)
2876
+ return NULL;
2877
+
2878
+ return PyObject_GetAttr(self->wrapped, state->str_dunder_class);
2879
+ }
2880
+
2881
+ /* ------------------------------------------------------------------------- */
2882
+
2883
+ static int WraptObjectProxy_set_class(WraptObjectProxyObject *self,
2884
+ PyObject *value)
2885
+ {
2886
+ if (!self->wrapped)
2887
+ {
2888
+ if (raise_uninitialized_wrapper_error(self) == -1)
2889
+ return -1;
2890
+ }
2891
+
2892
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2893
+ if (!state)
2894
+ return -1;
2895
+
2896
+ return PyObject_SetAttr(self->wrapped, state->str_dunder_class, value);
2897
+ }
2898
+
2899
+ /* ------------------------------------------------------------------------- */
2900
+
2901
+ static PyObject *
2902
+ WraptObjectProxy_get_annotations(WraptObjectProxyObject *self)
2903
+ {
2904
+ if (!self->wrapped)
2905
+ {
2906
+ if (raise_uninitialized_wrapper_error(self) == -1)
2907
+ return NULL;
2908
+ }
2909
+
2910
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2911
+ if (!state)
2912
+ return NULL;
2913
+
2914
+ return PyObject_GetAttr(self->wrapped, state->str_annotations);
2915
+ }
2916
+
2917
+ /* ------------------------------------------------------------------------- */
2918
+
2919
+ static int WraptObjectProxy_set_annotations(WraptObjectProxyObject *self,
2920
+ PyObject *value)
2921
+ {
2922
+ if (!self->wrapped)
2923
+ {
2924
+ if (raise_uninitialized_wrapper_error(self) == -1)
2925
+ return -1;
2926
+ }
2927
+
2928
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2929
+ if (!state)
2930
+ return -1;
2931
+
2932
+ return PyObject_SetAttr(self->wrapped, state->str_annotations, value);
2933
+ }
2934
+
2935
+ /* ------------------------------------------------------------------------- */
2936
+
2937
+ static PyObject *WraptObjectProxy_get_dict(WraptObjectProxyObject *self)
2938
+ {
2939
+ if (!self->wrapped)
2940
+ {
2941
+ if (raise_uninitialized_wrapper_error(self) == -1)
2942
+ return NULL;
2943
+ }
2944
+
2945
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
2946
+ if (!state)
2947
+ return NULL;
2948
+
2949
+ return PyObject_GetAttr(self->wrapped, state->str_dict);
2950
+ }
2951
+
2952
+ /* ------------------------------------------------------------------------- */
2953
+
2954
+ static int WraptObjectProxy_set_dict(WraptObjectProxyObject *self,
2955
+ PyObject *value)
2956
+ {
2957
+ if (!value)
2958
+ {
2959
+ PyErr_SetString(PyExc_AttributeError,
2960
+ "property '__dict__' of 'ObjectProxy' object has no deleter");
2961
+ return -1;
2962
+ }
2963
+
2964
+ PyErr_SetString(PyExc_AttributeError,
2965
+ "property '__dict__' of 'ObjectProxy' object has no setter");
2966
+ return -1;
2967
+ }
2968
+
2969
+ /* ------------------------------------------------------------------------- */
2970
+
2971
+ static PyObject *WraptObjectProxy_get_self_dict(WraptObjectProxyObject *self)
2972
+ {
2973
+ PyObject *result = NULL;
2974
+
2975
+ /* self->dict is only NULL after tp_clear has run. Perform the lazy
2976
+ * recreation and the reference acquisition inside a critical section
2977
+ * so that two threads racing here on a free-threaded build cannot both
2978
+ * create a dict, leaking one of them, or return a dict which the other
2979
+ * thread has already replaced. */
2980
+
2981
+ Py_BEGIN_CRITICAL_SECTION(self);
2982
+ if (!self->dict)
2983
+ self->dict = PyDict_New();
2984
+ result = self->dict;
2985
+ Py_XINCREF(result);
2986
+ Py_END_CRITICAL_SECTION();
2987
+
2988
+ return result;
2989
+ }
2990
+
2991
+ /* ------------------------------------------------------------------------- */
2992
+
2993
+ static PyObject *WraptObjectProxy_get_wrapped(WraptObjectProxyObject *self)
2994
+ {
2995
+ if (!self->wrapped)
2996
+ {
2997
+ if (raise_uninitialized_wrapper_error(self) == -1)
2998
+ return NULL;
2999
+ }
3000
+
3001
+ Py_INCREF(self->wrapped);
3002
+ return self->wrapped;
3003
+ }
3004
+
3005
+ /* ------------------------------------------------------------------------- */
3006
+
3007
+ static PyObject *WraptObjectProxy_get_object_proxy(WraptObjectProxyObject *self)
3008
+ {
3009
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3010
+ if (!state)
3011
+ return NULL;
3012
+ Py_INCREF(state->ObjectProxy_Type);
3013
+ return (PyObject *)state->ObjectProxy_Type;
3014
+ }
3015
+
3016
+ /* ------------------------------------------------------------------------- */
3017
+
3018
+ static int WraptObjectProxy_set_wrapped(WraptObjectProxyObject *self,
3019
+ PyObject *value)
3020
+ {
3021
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3022
+ if (!state)
3023
+ return -1;
3024
+
3025
+ PyObject *fixups = NULL;
3026
+
3027
+ if (!value)
3028
+ {
3029
+ PyErr_SetString(PyExc_TypeError, "can't delete __wrapped__ attribute");
3030
+ return -1;
3031
+ }
3032
+
3033
+ /* On free-threaded builds the swap must be serialised, otherwise two
3034
+ * threads assigning __wrapped__ on the same proxy can both read the same
3035
+ * old value and both decref it, releasing it twice. The critical section
3036
+ * covers only the swap; __wrapped_setattr_fixups__ below runs arbitrary
3037
+ * Python code and must stay outside it. */
3038
+
3039
+ Py_INCREF(value);
3040
+ Py_BEGIN_CRITICAL_SECTION(self);
3041
+ Py_XSETREF(self->wrapped, value);
3042
+ Py_END_CRITICAL_SECTION();
3043
+
3044
+ fixups = PyObject_GetAttr((PyObject *)self, state->str_setattr_fixups);
3045
+
3046
+ if (fixups)
3047
+ {
3048
+ PyObject *result = NULL;
3049
+
3050
+ result = PyObject_CallObject(fixups, NULL);
3051
+ Py_DECREF(fixups);
3052
+
3053
+ if (!result)
3054
+ return -1;
3055
+
3056
+ Py_DECREF(result);
3057
+ }
3058
+ else
3059
+ {
3060
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3061
+ return -1;
3062
+ PyErr_Clear();
3063
+ }
3064
+
3065
+ return 0;
3066
+ }
3067
+
3068
+ /* ------------------------------------------------------------------------- */
3069
+
3070
+ static PyObject *WraptObjectProxy_getattro(WraptObjectProxyObject *self,
3071
+ PyObject *name)
3072
+ {
3073
+ PyObject *object = NULL;
3074
+ PyObject *result = NULL;
3075
+
3076
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3077
+ if (!state)
3078
+ return NULL;
3079
+
3080
+ /* Intercept __module__ and __doc__ so they proxy to the wrapped object.
3081
+ * These cannot be getset descriptors on heap types because that would
3082
+ * place a descriptor object in tp_dict, shadowing the type-level
3083
+ * __module__/__doc__ strings that type.__module__ reads via raw dict
3084
+ * lookup. */
3085
+
3086
+ if (name == state->str_module)
3087
+ return WraptObjectProxy_get_module(self);
3088
+ if (name == state->str_doc)
3089
+ return WraptObjectProxy_get_doc(self);
3090
+
3091
+ object = PyObject_GenericGetAttr((PyObject *)self, name);
3092
+
3093
+ if (object)
3094
+ return object;
3095
+
3096
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3097
+ return NULL;
3098
+
3099
+ PyErr_Clear();
3100
+
3101
+ object = PyObject_GenericGetAttr((PyObject *)self, state->str_getattr);
3102
+
3103
+ if (!object)
3104
+ return NULL;
3105
+
3106
+ result = PyObject_CallFunctionObjArgs(object, name, NULL);
3107
+
3108
+ Py_DECREF(object);
3109
+
3110
+ return result;
3111
+ }
3112
+
3113
+ /* ------------------------------------------------------------------------- */
3114
+
3115
+ static PyObject *WraptObjectProxy_getattr(WraptObjectProxyObject *self,
3116
+ PyObject *args)
3117
+ {
3118
+ PyObject *name = NULL;
3119
+
3120
+ if (!PyArg_ParseTuple(args, "U:__getattr__", &name))
3121
+ return NULL;
3122
+
3123
+ if (!self->wrapped)
3124
+ {
3125
+ if (raise_uninitialized_wrapper_error(self) == -1)
3126
+ return NULL;
3127
+ }
3128
+
3129
+ return PyObject_GetAttr(self->wrapped, name);
3130
+ }
3131
+
3132
+ /* ------------------------------------------------------------------------- */
3133
+
3134
+ static int WraptObjectProxy_setattro(WraptObjectProxyObject *self,
3135
+ PyObject *name, PyObject *value)
3136
+ {
3137
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3138
+ if (!state)
3139
+ return -1;
3140
+
3141
+ int match = PyUnicode_Tailmatch(name, state->str_self_, 0, PY_SSIZE_T_MAX, -1);
3142
+
3143
+ if (match < 0)
3144
+ return -1;
3145
+
3146
+ if (match)
3147
+ return PyObject_GenericSetAttr((PyObject *)self, name, value);
3148
+
3149
+ /* Intercept __module__ and __doc__ so they forward to the wrapped
3150
+ * object rather than being stored locally. Without this, the string
3151
+ * values in the type dict (from PyType_FromModuleAndSpec) would cause
3152
+ * wrapt_type_has_attr to match and GenericSetAttr to store locally. */
3153
+
3154
+ if (name == state->str_module)
3155
+ return WraptObjectProxy_set_module(self, value);
3156
+ if (name == state->str_doc)
3157
+ return WraptObjectProxy_set_doc(self, value);
3158
+
3159
+ if (wrapt_type_has_attr(Py_TYPE(self), name))
3160
+ return PyObject_GenericSetAttr((PyObject *)self, name, value);
3161
+
3162
+ if (!self->wrapped)
3163
+ {
3164
+ if (raise_uninitialized_wrapper_error(self) == -1)
3165
+ return -1;
3166
+ }
3167
+
3168
+ return PyObject_SetAttr(self->wrapped, name, value);
3169
+ }
3170
+
3171
+ /* ------------------------------------------------------------------------- */
3172
+
3173
+ static PyObject *WraptObjectProxy_richcompare(WraptObjectProxyObject *self,
3174
+ PyObject *other, int opcode)
3175
+ {
3176
+ if (!self->wrapped)
3177
+ {
3178
+ if (raise_uninitialized_wrapper_error(self) == -1)
3179
+ return NULL;
3180
+ }
3181
+
3182
+ return PyObject_RichCompare(self->wrapped, other, opcode);
3183
+ }
3184
+
3185
+ /* ------------------------------------------------------------------------- */
3186
+
3187
+ static PyObject *
3188
+ WraptObjectProxy_instancecheck(WraptObjectProxyObject *self,
3189
+ PyObject *instance)
3190
+ {
3191
+ int check = 0;
3192
+
3193
+ if (!self->wrapped)
3194
+ {
3195
+ if (raise_uninitialized_wrapper_error(self) == -1)
3196
+ return NULL;
3197
+ }
3198
+
3199
+ check = PyObject_IsInstance(instance, self->wrapped);
3200
+
3201
+ if (check < 0)
3202
+ {
3203
+ return NULL;
3204
+ }
3205
+
3206
+ return PyBool_FromLong(check);
3207
+ }
3208
+
3209
+ /* ------------------------------------------------------------------------- */
3210
+
3211
+ static PyObject *
3212
+ WraptObjectProxy_subclasscheck(WraptObjectProxyObject *self,
3213
+ PyObject *args)
3214
+ {
3215
+ PyObject *subclass = NULL;
3216
+ PyObject *object = NULL;
3217
+
3218
+ int check = 0;
3219
+
3220
+ if (!self->wrapped)
3221
+ {
3222
+ if (raise_uninitialized_wrapper_error(self) == -1)
3223
+ return NULL;
3224
+ }
3225
+
3226
+ if (!PyArg_ParseTuple(args, "O", &subclass))
3227
+ return NULL;
3228
+
3229
+ wrapt_module_state *state =
3230
+ wrapt_state_from_type(Py_TYPE(self));
3231
+ if (!state)
3232
+ return NULL;
3233
+
3234
+ object = PyObject_GetAttr(subclass, state->str_wrapped);
3235
+
3236
+ if (!object)
3237
+ {
3238
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3239
+ return NULL;
3240
+ PyErr_Clear();
3241
+ }
3242
+
3243
+ check = PyObject_IsSubclass(object ? object : subclass,
3244
+ self->wrapped);
3245
+
3246
+ Py_XDECREF(object);
3247
+
3248
+ if (check == -1)
3249
+ return NULL;
3250
+
3251
+ return PyBool_FromLong(check);
3252
+ }
3253
+
3254
+ /* ------------------------------------------------------------------------- */
3255
+
3256
+ static PyMethodDef WraptObjectProxy_methods[] = {
3257
+ {"__self_setattr__", (PyCFunction)WraptObjectProxy_self_setattr,
3258
+ METH_VARARGS, 0},
3259
+ {"__dir__", (PyCFunction)WraptObjectProxy_dir, METH_NOARGS, 0},
3260
+ {"__enter__", (PyCFunction)WraptObjectProxy_enter,
3261
+ METH_VARARGS | METH_KEYWORDS, 0},
3262
+ {"__exit__", (PyCFunction)WraptObjectProxy_exit,
3263
+ METH_VARARGS | METH_KEYWORDS, 0},
3264
+ {"__aenter__", (PyCFunction)WraptObjectProxy_aenter,
3265
+ METH_VARARGS | METH_KEYWORDS, 0},
3266
+ {"__aexit__", (PyCFunction)WraptObjectProxy_aexit,
3267
+ METH_VARARGS | METH_KEYWORDS, 0},
3268
+ {"__copy__", (PyCFunction)WraptObjectProxy_copy, METH_NOARGS, 0},
3269
+ {"__deepcopy__", (PyCFunction)WraptObjectProxy_deepcopy,
3270
+ METH_VARARGS | METH_KEYWORDS, 0},
3271
+ {"__reduce__", (PyCFunction)WraptObjectProxy_reduce, METH_NOARGS, 0},
3272
+ {"__getattr__", (PyCFunction)WraptObjectProxy_getattr, METH_VARARGS, 0},
3273
+ {"__bytes__", (PyCFunction)WraptObjectProxy_bytes, METH_NOARGS, 0},
3274
+ {"__format__", (PyCFunction)WraptObjectProxy_format, METH_VARARGS, 0},
3275
+ {"__reversed__", (PyCFunction)WraptObjectProxy_reversed, METH_NOARGS, 0},
3276
+ {"__round__", (PyCFunction)WraptObjectProxy_round,
3277
+ METH_VARARGS | METH_KEYWORDS, 0},
3278
+ {"__trunc__", (PyCFunction)WraptObjectProxy_trunc, METH_NOARGS, 0},
3279
+ {"__floor__", (PyCFunction)WraptObjectProxy_floor, METH_NOARGS, 0},
3280
+ {"__ceil__", (PyCFunction)WraptObjectProxy_ceil, METH_NOARGS, 0},
3281
+ {"__complex__", (PyCFunction)WraptObjectProxy_complex, METH_NOARGS, 0},
3282
+ {"__mro_entries__", (PyCFunction)WraptObjectProxy_mro_entries,
3283
+ METH_VARARGS | METH_KEYWORDS, 0},
3284
+ {"__instancecheck__", (PyCFunction)WraptObjectProxy_instancecheck,
3285
+ METH_O, 0},
3286
+ {"__subclasscheck__", (PyCFunction)WraptObjectProxy_subclasscheck,
3287
+ METH_VARARGS, 0},
3288
+ {"__class_getitem__", Py_GenericAlias, METH_O | METH_CLASS, 0},
3289
+ {NULL, NULL},
3290
+ };
3291
+
3292
+ static PyGetSetDef WraptObjectProxy_getset[] = {
3293
+ {"__name__", (getter)WraptObjectProxy_get_name,
3294
+ (setter)WraptObjectProxy_set_name, 0},
3295
+ {"__qualname__", (getter)WraptObjectProxy_get_qualname,
3296
+ (setter)WraptObjectProxy_set_qualname, 0},
3297
+ {"__class__", (getter)WraptObjectProxy_get_class,
3298
+ (setter)WraptObjectProxy_set_class, 0},
3299
+ {"__annotations__", (getter)WraptObjectProxy_get_annotations,
3300
+ (setter)WraptObjectProxy_set_annotations, 0},
3301
+ {"__dict__", (getter)WraptObjectProxy_get_dict,
3302
+ (setter)WraptObjectProxy_set_dict, 0},
3303
+ {"__self_dict__", (getter)WraptObjectProxy_get_self_dict, 0, 0},
3304
+ {"__wrapped__", (getter)WraptObjectProxy_get_wrapped,
3305
+ (setter)WraptObjectProxy_set_wrapped, 0},
3306
+ {"__object_proxy__", (getter)WraptObjectProxy_get_object_proxy, 0, 0},
3307
+ {NULL},
3308
+ };
3309
+
3310
+ static PyMemberDef WraptObjectProxy_members[] = {
3311
+ {"__dictoffset__", T_PYSSIZET,
3312
+ offsetof(WraptObjectProxyObject, dict), READONLY, NULL},
3313
+ {"__weaklistoffset__", T_PYSSIZET,
3314
+ offsetof(WraptObjectProxyObject, weakreflist), READONLY, NULL},
3315
+ {NULL},
3316
+ };
3317
+
3318
+ static PyType_Slot WraptObjectProxy_slots[] = {
3319
+ {Py_tp_dealloc, WraptObjectProxy_dealloc},
3320
+ {Py_tp_repr, WraptObjectProxy_repr},
3321
+ {Py_tp_hash, WraptObjectProxy_hash},
3322
+ {Py_tp_str, WraptObjectProxy_str},
3323
+ {Py_tp_getattro, WraptObjectProxy_getattro},
3324
+ {Py_tp_setattro, WraptObjectProxy_setattro},
3325
+ {Py_tp_traverse, WraptObjectProxy_traverse},
3326
+ {Py_tp_clear, WraptObjectProxy_clear},
3327
+ {Py_tp_richcompare, WraptObjectProxy_richcompare},
3328
+ {Py_tp_methods, WraptObjectProxy_methods},
3329
+ {Py_tp_members, WraptObjectProxy_members},
3330
+ {Py_tp_getset, WraptObjectProxy_getset},
3331
+ {Py_tp_init, WraptObjectProxy_init},
3332
+ {Py_tp_alloc, PyType_GenericAlloc},
3333
+ {Py_tp_new, WraptObjectProxy_new},
3334
+ {Py_tp_free, PyObject_GC_Del},
3335
+ /* PyNumberMethods */
3336
+ {Py_nb_add, WraptObjectProxy_add},
3337
+ {Py_nb_subtract, WraptObjectProxy_subtract},
3338
+ {Py_nb_multiply, WraptObjectProxy_multiply},
3339
+ {Py_nb_remainder, WraptObjectProxy_remainder},
3340
+ {Py_nb_divmod, WraptObjectProxy_divmod},
3341
+ {Py_nb_power, WraptObjectProxy_power},
3342
+ {Py_nb_negative, WraptObjectProxy_negative},
3343
+ {Py_nb_positive, WraptObjectProxy_positive},
3344
+ {Py_nb_absolute, WraptObjectProxy_absolute},
3345
+ {Py_nb_bool, WraptObjectProxy_bool},
3346
+ {Py_nb_invert, WraptObjectProxy_invert},
3347
+ {Py_nb_lshift, WraptObjectProxy_lshift},
3348
+ {Py_nb_rshift, WraptObjectProxy_rshift},
3349
+ {Py_nb_and, WraptObjectProxy_and},
3350
+ {Py_nb_xor, WraptObjectProxy_xor},
3351
+ {Py_nb_or, WraptObjectProxy_or},
3352
+ {Py_nb_int, WraptObjectProxy_long},
3353
+ {Py_nb_float, WraptObjectProxy_float},
3354
+ {Py_nb_inplace_add, WraptObjectProxy_inplace_add},
3355
+ {Py_nb_inplace_subtract, WraptObjectProxy_inplace_subtract},
3356
+ {Py_nb_inplace_multiply, WraptObjectProxy_inplace_multiply},
3357
+ {Py_nb_inplace_remainder, WraptObjectProxy_inplace_remainder},
3358
+ {Py_nb_inplace_power, WraptObjectProxy_inplace_power},
3359
+ {Py_nb_inplace_lshift, WraptObjectProxy_inplace_lshift},
3360
+ {Py_nb_inplace_rshift, WraptObjectProxy_inplace_rshift},
3361
+ {Py_nb_inplace_and, WraptObjectProxy_inplace_and},
3362
+ {Py_nb_inplace_xor, WraptObjectProxy_inplace_xor},
3363
+ {Py_nb_inplace_or, WraptObjectProxy_inplace_or},
3364
+ {Py_nb_floor_divide, WraptObjectProxy_floor_divide},
3365
+ {Py_nb_true_divide, WraptObjectProxy_true_divide},
3366
+ {Py_nb_inplace_floor_divide, WraptObjectProxy_inplace_floor_divide},
3367
+ {Py_nb_inplace_true_divide, WraptObjectProxy_inplace_true_divide},
3368
+ {Py_nb_index, WraptObjectProxy_index},
3369
+ {Py_nb_matrix_multiply, WraptObjectProxy_matrix_multiply},
3370
+ {Py_nb_inplace_matrix_multiply, WraptObjectProxy_inplace_matrix_multiply},
3371
+ /* PySequenceMethods */
3372
+ {Py_sq_length, WraptObjectProxy_length},
3373
+ {Py_sq_contains, WraptObjectProxy_contains},
3374
+ /* PyMappingMethods */
3375
+ {Py_mp_length, WraptObjectProxy_length},
3376
+ {Py_mp_subscript, WraptObjectProxy_getitem},
3377
+ {Py_mp_ass_subscript, WraptObjectProxy_setitem},
3378
+ {0, NULL},
3379
+ };
3380
+
3381
+ static PyType_Spec WraptObjectProxy_spec = {
3382
+ .name = "_wrappers.ObjectProxy",
3383
+ .basicsize = sizeof(WraptObjectProxyObject),
3384
+ .itemsize = 0,
3385
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3386
+ .slots = WraptObjectProxy_slots,
3387
+ };
3388
+
3389
+ /* ------------------------------------------------------------------------- */
3390
+
3391
+ static PyObject *WraptCallableObjectProxy_call(WraptObjectProxyObject *self,
3392
+ PyObject *args, PyObject *kwds)
3393
+ {
3394
+ if (!self->wrapped)
3395
+ {
3396
+ if (raise_uninitialized_wrapper_error(self) == -1)
3397
+ return NULL;
3398
+ }
3399
+
3400
+ return PyObject_Call(self->wrapped, args, kwds);
3401
+ }
3402
+
3403
+ /* ------------------------------------------------------------------------- */;
3404
+
3405
+ static PyType_Slot WraptCallableObjectProxy_slots[] = {
3406
+ {Py_tp_dealloc, WraptObjectProxy_dealloc},
3407
+ {Py_tp_traverse, WraptObjectProxy_traverse},
3408
+ {Py_tp_clear, WraptObjectProxy_clear},
3409
+ {Py_tp_call, WraptCallableObjectProxy_call},
3410
+ {Py_tp_init, WraptObjectProxy_init},
3411
+ {0, NULL},
3412
+ };
3413
+
3414
+ static PyType_Spec WraptCallableObjectProxy_spec = {
3415
+ .name = "_wrappers.CallableObjectProxy",
3416
+ .basicsize = sizeof(WraptObjectProxyObject),
3417
+ .itemsize = 0,
3418
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3419
+ .slots = WraptCallableObjectProxy_slots,
3420
+ };
3421
+
3422
+ /* ------------------------------------------------------------------------- */
3423
+
3424
+ static PyObject *WraptPartialCallableObjectProxy_new(PyTypeObject *type,
3425
+ PyObject *args,
3426
+ PyObject *kwds)
3427
+ {
3428
+ WraptPartialCallableObjectProxyObject *self;
3429
+
3430
+ self = (WraptPartialCallableObjectProxyObject *)WraptObjectProxy_new(
3431
+ type, args, kwds);
3432
+
3433
+ if (!self)
3434
+ return NULL;
3435
+
3436
+ self->args = NULL;
3437
+ self->kwargs = NULL;
3438
+
3439
+ return (PyObject *)self;
3440
+ }
3441
+
3442
+ /* ------------------------------------------------------------------------- */
3443
+
3444
+ static int WraptPartialCallableObjectProxy_raw_init(
3445
+ WraptPartialCallableObjectProxyObject *self, PyObject *wrapped,
3446
+ PyObject *args, PyObject *kwargs)
3447
+ {
3448
+ int result = 0;
3449
+
3450
+ result = WraptObjectProxy_raw_init((WraptObjectProxyObject *)self, wrapped);
3451
+
3452
+ if (result == 0)
3453
+ {
3454
+ /* Serialise the swaps for the same reason as in the __wrapped__
3455
+ * setter. Initialisation normally happens before the proxy is shared,
3456
+ * but __init__ can be re-invoked on an already published proxy, in
3457
+ * which case concurrent callers on a free-threaded build could
3458
+ * otherwise decref the same old args or kwargs twice. */
3459
+
3460
+ Py_INCREF(args);
3461
+ Py_XINCREF(kwargs);
3462
+
3463
+ Py_BEGIN_CRITICAL_SECTION(self);
3464
+ Py_XSETREF(self->args, args);
3465
+ Py_XSETREF(self->kwargs, kwargs);
3466
+ Py_END_CRITICAL_SECTION();
3467
+ }
3468
+
3469
+ return result;
3470
+ }
3471
+
3472
+ /* ------------------------------------------------------------------------- */
3473
+
3474
+ static int WraptPartialCallableObjectProxy_init(
3475
+ WraptPartialCallableObjectProxyObject *self, PyObject *args,
3476
+ PyObject *kwds)
3477
+ {
3478
+ PyObject *wrapped = NULL;
3479
+ PyObject *fnargs = NULL;
3480
+
3481
+ int result = 0;
3482
+
3483
+ if (PyTuple_GET_SIZE(args) < 1)
3484
+ {
3485
+ PyErr_SetString(PyExc_TypeError, "__init__ of partial needs an argument");
3486
+ return -1;
3487
+ }
3488
+
3489
+ wrapped = PyTuple_GetItem(args, 0);
3490
+
3491
+ if (!PyCallable_Check(wrapped))
3492
+ {
3493
+ PyErr_SetString(PyExc_TypeError, "the first argument must be callable");
3494
+ return -1;
3495
+ }
3496
+
3497
+ fnargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3498
+
3499
+ if (!fnargs)
3500
+ return -1;
3501
+
3502
+ result =
3503
+ WraptPartialCallableObjectProxy_raw_init(self, wrapped, fnargs, kwds);
3504
+
3505
+ Py_DECREF(fnargs);
3506
+
3507
+ return result;
3508
+ }
3509
+
3510
+ /* ------------------------------------------------------------------------- */
3511
+
3512
+ static int WraptPartialCallableObjectProxy_traverse(
3513
+ WraptPartialCallableObjectProxyObject *self, visitproc visit, void *arg)
3514
+ {
3515
+ int err = WraptObjectProxy_traverse((WraptObjectProxyObject *)self, visit, arg);
3516
+ if (err)
3517
+ return err;
3518
+
3519
+ Py_VISIT(self->args);
3520
+ Py_VISIT(self->kwargs);
3521
+
3522
+ return 0;
3523
+ }
3524
+
3525
+ /* ------------------------------------------------------------------------- */
3526
+
3527
+ static int WraptPartialCallableObjectProxy_clear(
3528
+ WraptPartialCallableObjectProxyObject *self)
3529
+ {
3530
+ WraptObjectProxy_clear((WraptObjectProxyObject *)self);
3531
+
3532
+ Py_CLEAR(self->args);
3533
+ Py_CLEAR(self->kwargs);
3534
+
3535
+ return 0;
3536
+ }
3537
+
3538
+ /* ------------------------------------------------------------------------- */
3539
+
3540
+ static void WraptPartialCallableObjectProxy_dealloc(
3541
+ WraptPartialCallableObjectProxyObject *self)
3542
+ {
3543
+ PyTypeObject *tp = Py_TYPE(self);
3544
+
3545
+ PyObject_GC_UnTrack(self);
3546
+
3547
+ if (self->object_proxy.weakreflist != NULL)
3548
+ PyObject_ClearWeakRefs((PyObject *)self);
3549
+
3550
+ WraptPartialCallableObjectProxy_clear(self);
3551
+
3552
+ tp->tp_free(self);
3553
+
3554
+ #if PY_VERSION_HEX >= 0x030C0000
3555
+ PyObject *exc = PyErr_GetRaisedException();
3556
+ Py_DECREF(tp);
3557
+ PyErr_SetRaisedException(exc);
3558
+ #else
3559
+ PyObject *exc_type, *exc_value, *exc_tb;
3560
+ PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
3561
+ Py_DECREF(tp);
3562
+ PyErr_Restore(exc_type, exc_value, exc_tb);
3563
+ #endif
3564
+ }
3565
+
3566
+ /* ------------------------------------------------------------------------- */
3567
+
3568
+ static PyObject *WraptPartialCallableObjectProxy_call(
3569
+ WraptPartialCallableObjectProxyObject *self, PyObject *args,
3570
+ PyObject *kwds)
3571
+ {
3572
+ PyObject *fnargs = NULL;
3573
+ PyObject *fnkwargs = NULL;
3574
+
3575
+ PyObject *result = NULL;
3576
+
3577
+ long i;
3578
+ long offset;
3579
+
3580
+ if (!self->object_proxy.wrapped)
3581
+ {
3582
+ if (raise_uninitialized_wrapper_error(&self->object_proxy) == -1)
3583
+ return NULL;
3584
+ }
3585
+
3586
+ fnargs = PyTuple_New(PyTuple_GET_SIZE(self->args) + PyTuple_GET_SIZE(args));
3587
+
3588
+ if (!fnargs)
3589
+ return NULL;
3590
+
3591
+ for (i = 0; i < PyTuple_GET_SIZE(self->args); i++)
3592
+ {
3593
+ PyObject *item;
3594
+ item = PyTuple_GetItem(self->args, i);
3595
+ Py_INCREF(item);
3596
+ PyTuple_SetItem(fnargs, i, item);
3597
+ }
3598
+
3599
+ offset = PyTuple_GET_SIZE(self->args);
3600
+
3601
+ for (i = 0; i < PyTuple_GET_SIZE(args); i++)
3602
+ {
3603
+ PyObject *item;
3604
+ item = PyTuple_GetItem(args, i);
3605
+ Py_INCREF(item);
3606
+ PyTuple_SetItem(fnargs, offset + i, item);
3607
+ }
3608
+
3609
+ fnkwargs = PyDict_New();
3610
+
3611
+ if (!fnkwargs)
3612
+ {
3613
+ Py_DECREF(fnargs);
3614
+ return NULL;
3615
+ }
3616
+
3617
+ if (self->kwargs && PyDict_Update(fnkwargs, self->kwargs) == -1)
3618
+ {
3619
+ Py_DECREF(fnargs);
3620
+ Py_DECREF(fnkwargs);
3621
+ return NULL;
3622
+ }
3623
+
3624
+ if (kwds && PyDict_Update(fnkwargs, kwds) == -1)
3625
+ {
3626
+ Py_DECREF(fnargs);
3627
+ Py_DECREF(fnkwargs);
3628
+ return NULL;
3629
+ }
3630
+
3631
+ result = PyObject_Call(self->object_proxy.wrapped, fnargs, fnkwargs);
3632
+
3633
+ Py_DECREF(fnargs);
3634
+ Py_DECREF(fnkwargs);
3635
+
3636
+ return result;
3637
+ }
3638
+
3639
+ /* ------------------------------------------------------------------------- */;
3640
+
3641
+ static PyType_Slot WraptPartialCallableObjectProxy_slots[] = {
3642
+ {Py_tp_dealloc, WraptPartialCallableObjectProxy_dealloc},
3643
+ {Py_tp_call, WraptPartialCallableObjectProxy_call},
3644
+ {Py_tp_traverse, WraptPartialCallableObjectProxy_traverse},
3645
+ {Py_tp_clear, WraptPartialCallableObjectProxy_clear},
3646
+ {Py_tp_init, WraptPartialCallableObjectProxy_init},
3647
+ {Py_tp_new, WraptPartialCallableObjectProxy_new},
3648
+ {0, NULL},
3649
+ };
3650
+
3651
+ static PyType_Spec WraptPartialCallableObjectProxy_spec = {
3652
+ .name = "_wrappers.PartialCallableObjectProxy",
3653
+ .basicsize = sizeof(WraptPartialCallableObjectProxyObject),
3654
+ .itemsize = 0,
3655
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3656
+ .slots = WraptPartialCallableObjectProxy_slots,
3657
+ };
3658
+
3659
+ /* ------------------------------------------------------------------------- */
3660
+
3661
+ static PyObject *WraptFunctionWrapperBase_new(PyTypeObject *type,
3662
+ PyObject *args, PyObject *kwds)
3663
+ {
3664
+ WraptFunctionWrapperObject *self;
3665
+
3666
+ self = (WraptFunctionWrapperObject *)WraptObjectProxy_new(type, args, kwds);
3667
+
3668
+ if (!self)
3669
+ return NULL;
3670
+
3671
+ self->instance = NULL;
3672
+ self->wrapper = NULL;
3673
+ self->enabled = NULL;
3674
+ self->binding = NULL;
3675
+ self->parent = NULL;
3676
+ self->owner = NULL;
3677
+
3678
+ return (PyObject *)self;
3679
+ }
3680
+
3681
+ /* ------------------------------------------------------------------------- */
3682
+
3683
+ static int WraptFunctionWrapperBase_raw_init(
3684
+ WraptFunctionWrapperObject *self, PyObject *wrapped, PyObject *instance,
3685
+ PyObject *wrapper, PyObject *enabled, PyObject *binding, PyObject *parent,
3686
+ PyObject *owner)
3687
+ {
3688
+ int result = 0;
3689
+
3690
+ result = WraptObjectProxy_raw_init((WraptObjectProxyObject *)self, wrapped);
3691
+
3692
+ if (result == 0)
3693
+ {
3694
+ /* Serialise the swaps for the same reason as in the __wrapped__
3695
+ * setter. These fields have no setters, so re-invocation of __init__
3696
+ * on an already published wrapper is the only path by which
3697
+ * concurrent callers on a free-threaded build could otherwise decref
3698
+ * the same old field values twice. */
3699
+
3700
+ Py_INCREF(instance);
3701
+ Py_INCREF(wrapper);
3702
+ Py_INCREF(enabled);
3703
+ Py_INCREF(binding);
3704
+ Py_INCREF(parent);
3705
+ Py_INCREF(owner);
3706
+
3707
+ Py_BEGIN_CRITICAL_SECTION(self);
3708
+ Py_XSETREF(self->instance, instance);
3709
+ Py_XSETREF(self->wrapper, wrapper);
3710
+ Py_XSETREF(self->enabled, enabled);
3711
+ Py_XSETREF(self->binding, binding);
3712
+ Py_XSETREF(self->parent, parent);
3713
+ Py_XSETREF(self->owner, owner);
3714
+ Py_END_CRITICAL_SECTION();
3715
+ }
3716
+
3717
+ return result;
3718
+ }
3719
+
3720
+ /* ------------------------------------------------------------------------- */
3721
+
3722
+ static int WraptFunctionWrapperBase_init(WraptFunctionWrapperObject *self,
3723
+ PyObject *args, PyObject *kwds)
3724
+ {
3725
+ PyObject *wrapped = NULL;
3726
+ PyObject *instance = NULL;
3727
+ PyObject *wrapper = NULL;
3728
+ PyObject *enabled = Py_None;
3729
+ PyObject *binding = NULL;
3730
+ PyObject *parent = Py_None;
3731
+ PyObject *owner = Py_None;
3732
+
3733
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3734
+ if (!state)
3735
+ return -1;
3736
+
3737
+ char *const kwlist[] = {"wrapped", "instance", "wrapper", "enabled",
3738
+ "binding", "parent", "owner", NULL};
3739
+
3740
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO|OOOO:FunctionWrapperBase",
3741
+ kwlist, &wrapped, &instance, &wrapper,
3742
+ &enabled, &binding, &parent, &owner))
3743
+ {
3744
+ return -1;
3745
+ }
3746
+
3747
+ if (!binding)
3748
+ binding = state->str_callable;
3749
+
3750
+ return WraptFunctionWrapperBase_raw_init(self, wrapped, instance, wrapper,
3751
+ enabled, binding, parent, owner);
3752
+ }
3753
+
3754
+ /* ------------------------------------------------------------------------- */
3755
+
3756
+ static int WraptFunctionWrapperBase_traverse(WraptFunctionWrapperObject *self,
3757
+ visitproc visit, void *arg)
3758
+ {
3759
+ int err = WraptObjectProxy_traverse((WraptObjectProxyObject *)self, visit, arg);
3760
+ if (err)
3761
+ return err;
3762
+
3763
+ Py_VISIT(self->instance);
3764
+ Py_VISIT(self->wrapper);
3765
+ Py_VISIT(self->enabled);
3766
+ Py_VISIT(self->binding);
3767
+ Py_VISIT(self->parent);
3768
+ Py_VISIT(self->owner);
3769
+
3770
+ return 0;
3771
+ }
3772
+
3773
+ /* ------------------------------------------------------------------------- */
3774
+
3775
+ static int WraptFunctionWrapperBase_clear(WraptFunctionWrapperObject *self)
3776
+ {
3777
+ WraptObjectProxy_clear((WraptObjectProxyObject *)self);
3778
+
3779
+ Py_CLEAR(self->instance);
3780
+ Py_CLEAR(self->wrapper);
3781
+ Py_CLEAR(self->enabled);
3782
+ Py_CLEAR(self->binding);
3783
+ Py_CLEAR(self->parent);
3784
+ Py_CLEAR(self->owner);
3785
+
3786
+ return 0;
3787
+ }
3788
+
3789
+ /* ------------------------------------------------------------------------- */
3790
+
3791
+ static void WraptFunctionWrapperBase_dealloc(WraptFunctionWrapperObject *self)
3792
+ {
3793
+ PyTypeObject *tp = Py_TYPE(self);
3794
+
3795
+ PyObject_GC_UnTrack(self);
3796
+
3797
+ if (self->object_proxy.weakreflist != NULL)
3798
+ PyObject_ClearWeakRefs((PyObject *)self);
3799
+
3800
+ WraptFunctionWrapperBase_clear(self);
3801
+
3802
+ tp->tp_free(self);
3803
+
3804
+ #if PY_VERSION_HEX >= 0x030C0000
3805
+ PyObject *exc = PyErr_GetRaisedException();
3806
+ Py_DECREF(tp);
3807
+ PyErr_SetRaisedException(exc);
3808
+ #else
3809
+ PyObject *exc_type, *exc_value, *exc_tb;
3810
+ PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
3811
+ Py_DECREF(tp);
3812
+ PyErr_Restore(exc_type, exc_value, exc_tb);
3813
+ #endif
3814
+ }
3815
+
3816
+ /* ------------------------------------------------------------------------- */
3817
+
3818
+ static PyObject *WraptFunctionWrapperBase_call(WraptFunctionWrapperObject *self,
3819
+ PyObject *args, PyObject *kwds)
3820
+ {
3821
+ PyObject *param_kwds = NULL;
3822
+
3823
+ PyObject *result = NULL;
3824
+
3825
+ if (!self->object_proxy.wrapped)
3826
+ {
3827
+ if (raise_uninitialized_wrapper_error(&self->object_proxy) == -1)
3828
+ return NULL;
3829
+ }
3830
+
3831
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3832
+ if (!state)
3833
+ return NULL;
3834
+
3835
+ if (self->enabled != Py_None)
3836
+ {
3837
+ if (PyCallable_Check(self->enabled))
3838
+ {
3839
+ PyObject *object = NULL;
3840
+ int is_false;
3841
+
3842
+ object = PyObject_CallFunctionObjArgs(self->enabled, NULL);
3843
+
3844
+ if (!object)
3845
+ return NULL;
3846
+
3847
+ is_false = PyObject_Not(object);
3848
+ Py_DECREF(object);
3849
+
3850
+ if (is_false < 0)
3851
+ return NULL;
3852
+
3853
+ if (is_false)
3854
+ return PyObject_Call(self->object_proxy.wrapped, args, kwds);
3855
+ }
3856
+ else
3857
+ {
3858
+ int is_false = PyObject_Not(self->enabled);
3859
+
3860
+ if (is_false < 0)
3861
+ return NULL;
3862
+
3863
+ if (is_false)
3864
+ return PyObject_Call(self->object_proxy.wrapped, args, kwds);
3865
+ }
3866
+ }
3867
+
3868
+ if (!kwds)
3869
+ {
3870
+ param_kwds = PyDict_New();
3871
+ if (!param_kwds)
3872
+ return NULL;
3873
+ kwds = param_kwds;
3874
+ }
3875
+
3876
+ if (self->instance == Py_None)
3877
+ {
3878
+ int matched =
3879
+ PyUnicode_CompareWithASCIIString(self->binding, "function") == 0 ||
3880
+ PyUnicode_CompareWithASCIIString(self->binding, "instancemethod") == 0 ||
3881
+ PyUnicode_CompareWithASCIIString(self->binding, "callable") == 0 ||
3882
+ PyUnicode_CompareWithASCIIString(self->binding, "classmethod") == 0;
3883
+
3884
+ if (matched)
3885
+ {
3886
+ PyObject *instance = NULL;
3887
+
3888
+ instance =
3889
+ PyObject_GetAttr(self->object_proxy.wrapped, state->str_self);
3890
+
3891
+ if (instance)
3892
+ {
3893
+ result = PyObject_CallFunctionObjArgs(self->wrapper,
3894
+ self->object_proxy.wrapped,
3895
+ instance, args, kwds, NULL);
3896
+
3897
+ Py_XDECREF(param_kwds);
3898
+
3899
+ Py_DECREF(instance);
3900
+
3901
+ return result;
3902
+ }
3903
+ else
3904
+ {
3905
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3906
+ {
3907
+ Py_XDECREF(param_kwds);
3908
+ return NULL;
3909
+ }
3910
+ PyErr_Clear();
3911
+ }
3912
+ }
3913
+ }
3914
+
3915
+ result =
3916
+ PyObject_CallFunctionObjArgs(self->wrapper, self->object_proxy.wrapped,
3917
+ self->instance, args, kwds, NULL);
3918
+
3919
+ Py_XDECREF(param_kwds);
3920
+
3921
+ return result;
3922
+ }
3923
+
3924
+ /* ------------------------------------------------------------------------- */
3925
+
3926
+ static PyObject *
3927
+ WraptFunctionWrapperBase_descr_get(WraptFunctionWrapperObject *self,
3928
+ PyObject *obj, PyObject *type)
3929
+ {
3930
+ PyObject *bound_type = NULL;
3931
+ PyObject *descriptor = NULL;
3932
+ PyObject *result = NULL;
3933
+
3934
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
3935
+ if (!state)
3936
+ return NULL;
3937
+
3938
+ PyObject *bound_type_str = state->str_bound_function_wrapper;
3939
+
3940
+ if (!self->object_proxy.wrapped)
3941
+ {
3942
+ if (raise_uninitialized_wrapper_error(&self->object_proxy) == -1)
3943
+ return NULL;
3944
+ }
3945
+
3946
+ if (self->parent == Py_None)
3947
+ {
3948
+ if (PyUnicode_CompareWithASCIIString(self->binding, "builtin") == 0)
3949
+ {
3950
+ Py_INCREF(self);
3951
+ return (PyObject *)self;
3952
+ }
3953
+
3954
+ if (PyUnicode_CompareWithASCIIString(self->binding, "class") == 0)
3955
+ {
3956
+ Py_INCREF(self);
3957
+ return (PyObject *)self;
3958
+ }
3959
+
3960
+ if (Py_TYPE(self->object_proxy.wrapped)->tp_descr_get == NULL)
3961
+ {
3962
+ Py_INCREF(self);
3963
+ return (PyObject *)self;
3964
+ }
3965
+
3966
+ descriptor = (Py_TYPE(self->object_proxy.wrapped)->tp_descr_get)(
3967
+ self->object_proxy.wrapped, obj, type);
3968
+
3969
+ if (!descriptor)
3970
+ return NULL;
3971
+
3972
+ if (Py_TYPE(self) != state->FunctionWrapper_Type)
3973
+ {
3974
+ bound_type = PyObject_GenericGetAttr((PyObject *)self, bound_type_str);
3975
+
3976
+ if (!bound_type)
3977
+ {
3978
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3979
+ {
3980
+ Py_DECREF(descriptor);
3981
+ return NULL;
3982
+ }
3983
+ PyErr_Clear();
3984
+ }
3985
+ }
3986
+
3987
+ if (obj == NULL)
3988
+ obj = Py_None;
3989
+
3990
+ result = PyObject_CallFunctionObjArgs(
3991
+ bound_type ? bound_type : (PyObject *)state->BoundFunctionWrapper_Type,
3992
+ descriptor, obj, self->wrapper, self->enabled, self->binding, self,
3993
+ type, NULL);
3994
+
3995
+ Py_XDECREF(bound_type);
3996
+ Py_DECREF(descriptor);
3997
+
3998
+ return result;
3999
+ }
4000
+
4001
+ if (self->instance == Py_None)
4002
+ {
4003
+ int matched =
4004
+ PyUnicode_CompareWithASCIIString(self->binding, "function") == 0 ||
4005
+ PyUnicode_CompareWithASCIIString(self->binding, "instancemethod") == 0 ||
4006
+ PyUnicode_CompareWithASCIIString(self->binding, "callable") == 0;
4007
+
4008
+ if (matched)
4009
+ {
4010
+ PyObject *wrapped = NULL;
4011
+
4012
+ if (PyObject_TypeCheck(self->parent, state->ObjectProxy_Type))
4013
+ {
4014
+ WraptObjectProxyObject *parent_proxy =
4015
+ (WraptObjectProxyObject *)self->parent;
4016
+
4017
+ if (!parent_proxy->wrapped)
4018
+ {
4019
+ if (raise_uninitialized_wrapper_error(parent_proxy) == -1)
4020
+ return NULL;
4021
+ }
4022
+
4023
+ wrapped = parent_proxy->wrapped;
4024
+ Py_INCREF(wrapped);
4025
+ }
4026
+ else
4027
+ {
4028
+ /* Fallback for the unusual case where parent is not a wrapt proxy. */
4029
+ wrapped = PyObject_GetAttr((PyObject *)self->parent, state->str_wrapped);
4030
+
4031
+ if (!wrapped)
4032
+ return NULL;
4033
+ }
4034
+
4035
+ if (Py_TYPE(wrapped)->tp_descr_get == NULL)
4036
+ {
4037
+ PyErr_Format(PyExc_AttributeError,
4038
+ "'%s' object has no attribute '__get__'",
4039
+ Py_TYPE(wrapped)->tp_name);
4040
+ Py_DECREF(wrapped);
4041
+ return NULL;
4042
+ }
4043
+
4044
+ descriptor = (Py_TYPE(wrapped)->tp_descr_get)(wrapped, obj, type);
4045
+
4046
+ Py_DECREF(wrapped);
4047
+
4048
+ if (!descriptor)
4049
+ return NULL;
4050
+
4051
+ if (Py_TYPE(self->parent) != state->FunctionWrapper_Type)
4052
+ {
4053
+ bound_type =
4054
+ PyObject_GenericGetAttr((PyObject *)self->parent, bound_type_str);
4055
+
4056
+ if (!bound_type)
4057
+ {
4058
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4059
+ {
4060
+ Py_DECREF(descriptor);
4061
+ return NULL;
4062
+ }
4063
+ PyErr_Clear();
4064
+ }
4065
+ }
4066
+
4067
+ if (obj == NULL)
4068
+ obj = Py_None;
4069
+
4070
+ result = PyObject_CallFunctionObjArgs(
4071
+ bound_type ? bound_type : (PyObject *)state->BoundFunctionWrapper_Type,
4072
+ descriptor, obj, self->wrapper, self->enabled, self->binding,
4073
+ self->parent, type, NULL);
4074
+
4075
+ Py_XDECREF(bound_type);
4076
+ Py_DECREF(descriptor);
4077
+
4078
+ return result;
4079
+ }
4080
+ }
4081
+
4082
+ Py_INCREF(self);
4083
+ return (PyObject *)self;
4084
+ }
4085
+
4086
+ /* ------------------------------------------------------------------------- */
4087
+
4088
+ static PyObject *
4089
+ WraptFunctionWrapperBase_set_name(WraptFunctionWrapperObject *self,
4090
+ PyObject *args, PyObject *kwds)
4091
+ {
4092
+ PyObject *method = NULL;
4093
+ PyObject *result = NULL;
4094
+
4095
+ if (!self->object_proxy.wrapped)
4096
+ {
4097
+ if (raise_uninitialized_wrapper_error(&self->object_proxy) == -1)
4098
+ return NULL;
4099
+ }
4100
+
4101
+ wrapt_module_state *state =
4102
+ wrapt_state_from_type(Py_TYPE(self));
4103
+ if (!state)
4104
+ return NULL;
4105
+
4106
+ method = PyObject_GetAttr(self->object_proxy.wrapped, state->str_set_name);
4107
+
4108
+ if (!method)
4109
+ {
4110
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4111
+ return NULL;
4112
+ PyErr_Clear();
4113
+ Py_RETURN_NONE;
4114
+ }
4115
+
4116
+ result = PyObject_Call(method, args, kwds);
4117
+
4118
+ Py_DECREF(method);
4119
+
4120
+ return result;
4121
+ }
4122
+
4123
+ /* ------------------------------------------------------------------------- */
4124
+
4125
+ static PyObject *
4126
+ WraptFunctionWrapperBase_get_self_instance(WraptFunctionWrapperObject *self,
4127
+ void *closure)
4128
+ {
4129
+ if (!self->instance)
4130
+ {
4131
+ Py_RETURN_NONE;
4132
+ }
4133
+
4134
+ Py_INCREF(self->instance);
4135
+ return self->instance;
4136
+ }
4137
+
4138
+ /* ------------------------------------------------------------------------- */
4139
+
4140
+ static PyObject *
4141
+ WraptFunctionWrapperBase_get_self_wrapper(WraptFunctionWrapperObject *self,
4142
+ void *closure)
4143
+ {
4144
+ if (!self->wrapper)
4145
+ {
4146
+ Py_RETURN_NONE;
4147
+ }
4148
+
4149
+ Py_INCREF(self->wrapper);
4150
+ return self->wrapper;
4151
+ }
4152
+
4153
+ /* ------------------------------------------------------------------------- */
4154
+
4155
+ static PyObject *
4156
+ WraptFunctionWrapperBase_get_self_enabled(WraptFunctionWrapperObject *self,
4157
+ void *closure)
4158
+ {
4159
+ if (!self->enabled)
4160
+ {
4161
+ Py_RETURN_NONE;
4162
+ }
4163
+
4164
+ Py_INCREF(self->enabled);
4165
+ return self->enabled;
4166
+ }
4167
+
4168
+ /* ------------------------------------------------------------------------- */
4169
+
4170
+ static PyObject *
4171
+ WraptFunctionWrapperBase_get_self_binding(WraptFunctionWrapperObject *self,
4172
+ void *closure)
4173
+ {
4174
+ if (!self->binding)
4175
+ {
4176
+ Py_RETURN_NONE;
4177
+ }
4178
+
4179
+ Py_INCREF(self->binding);
4180
+ return self->binding;
4181
+ }
4182
+
4183
+ /* ------------------------------------------------------------------------- */
4184
+
4185
+ static PyObject *
4186
+ WraptFunctionWrapperBase_get_self_parent(WraptFunctionWrapperObject *self,
4187
+ void *closure)
4188
+ {
4189
+ if (!self->parent)
4190
+ {
4191
+ Py_RETURN_NONE;
4192
+ }
4193
+
4194
+ Py_INCREF(self->parent);
4195
+ return self->parent;
4196
+ }
4197
+
4198
+ /* ------------------------------------------------------------------------- */
4199
+
4200
+ static PyObject *
4201
+ WraptFunctionWrapperBase_get_self_owner(WraptFunctionWrapperObject *self,
4202
+ void *closure)
4203
+ {
4204
+ if (!self->owner)
4205
+ {
4206
+ Py_RETURN_NONE;
4207
+ }
4208
+
4209
+ Py_INCREF(self->owner);
4210
+ return self->owner;
4211
+ }
4212
+
4213
+ /* ------------------------------------------------------------------------- */;
4214
+
4215
+ static PyMethodDef WraptFunctionWrapperBase_methods[] = {
4216
+ {"__set_name__", (PyCFunction)WraptFunctionWrapperBase_set_name,
4217
+ METH_VARARGS | METH_KEYWORDS, 0},
4218
+ {NULL, NULL},
4219
+ };
4220
+
4221
+ /* ------------------------------------------------------------------------- */;
4222
+
4223
+ static PyGetSetDef WraptFunctionWrapperBase_getset[] = {
4224
+ {"_self_instance", (getter)WraptFunctionWrapperBase_get_self_instance, NULL,
4225
+ 0},
4226
+ {"_self_wrapper", (getter)WraptFunctionWrapperBase_get_self_wrapper, NULL,
4227
+ 0},
4228
+ {"_self_enabled", (getter)WraptFunctionWrapperBase_get_self_enabled, NULL,
4229
+ 0},
4230
+ {"_self_binding", (getter)WraptFunctionWrapperBase_get_self_binding, NULL,
4231
+ 0},
4232
+ {"_self_parent", (getter)WraptFunctionWrapperBase_get_self_parent, NULL, 0},
4233
+ {"_self_owner", (getter)WraptFunctionWrapperBase_get_self_owner, NULL, 0},
4234
+ {NULL},
4235
+ };
4236
+
4237
+ static PyType_Slot WraptFunctionWrapperBase_slots[] = {
4238
+ {Py_tp_dealloc, WraptFunctionWrapperBase_dealloc},
4239
+ {Py_tp_call, WraptFunctionWrapperBase_call},
4240
+ {Py_tp_traverse, WraptFunctionWrapperBase_traverse},
4241
+ {Py_tp_clear, WraptFunctionWrapperBase_clear},
4242
+ {Py_tp_methods, WraptFunctionWrapperBase_methods},
4243
+ {Py_tp_getset, WraptFunctionWrapperBase_getset},
4244
+ {Py_tp_descr_get, WraptFunctionWrapperBase_descr_get},
4245
+ {Py_tp_init, WraptFunctionWrapperBase_init},
4246
+ {Py_tp_new, WraptFunctionWrapperBase_new},
4247
+ {0, NULL},
4248
+ };
4249
+
4250
+ static PyType_Spec WraptFunctionWrapperBase_spec = {
4251
+ .name = "_wrappers._FunctionWrapperBase",
4252
+ .basicsize = sizeof(WraptFunctionWrapperObject),
4253
+ .itemsize = 0,
4254
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4255
+ .slots = WraptFunctionWrapperBase_slots,
4256
+ };
4257
+
4258
+ /* ------------------------------------------------------------------------- */
4259
+
4260
+ static PyObject *
4261
+ WraptBoundFunctionWrapper_call(WraptFunctionWrapperObject *self, PyObject *args,
4262
+ PyObject *kwds)
4263
+ {
4264
+ PyObject *param_args = NULL;
4265
+ PyObject *param_kwds = NULL;
4266
+
4267
+ PyObject *wrapped = NULL;
4268
+ PyObject *instance = NULL;
4269
+
4270
+ PyObject *result = NULL;
4271
+
4272
+ int matched;
4273
+
4274
+ if (!self->object_proxy.wrapped)
4275
+ {
4276
+ if (raise_uninitialized_wrapper_error(&self->object_proxy) == -1)
4277
+ return NULL;
4278
+ }
4279
+
4280
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
4281
+ if (!state)
4282
+ return NULL;
4283
+
4284
+ if (self->enabled != Py_None)
4285
+ {
4286
+ if (PyCallable_Check(self->enabled))
4287
+ {
4288
+ PyObject *object = NULL;
4289
+ int is_false;
4290
+
4291
+ object = PyObject_CallFunctionObjArgs(self->enabled, NULL);
4292
+
4293
+ if (!object)
4294
+ return NULL;
4295
+
4296
+ is_false = PyObject_Not(object);
4297
+ Py_DECREF(object);
4298
+
4299
+ if (is_false < 0)
4300
+ return NULL;
4301
+
4302
+ if (is_false)
4303
+ return PyObject_Call(self->object_proxy.wrapped, args, kwds);
4304
+ }
4305
+ else
4306
+ {
4307
+ int is_false = PyObject_Not(self->enabled);
4308
+
4309
+ if (is_false < 0)
4310
+ return NULL;
4311
+
4312
+ if (is_false)
4313
+ return PyObject_Call(self->object_proxy.wrapped, args, kwds);
4314
+ }
4315
+ }
4316
+
4317
+ /*
4318
+ * We need to do things different depending on whether we are likely
4319
+ * wrapping an instance method vs a static method or class method.
4320
+ */
4321
+
4322
+ matched =
4323
+ PyUnicode_CompareWithASCIIString(self->binding, "function") == 0 ||
4324
+ PyUnicode_CompareWithASCIIString(self->binding, "callable") == 0;
4325
+
4326
+ if (matched)
4327
+ {
4328
+
4329
+ if (self->instance == Py_None && PyTuple_GET_SIZE(args) != 0)
4330
+ {
4331
+ /*
4332
+ * This situation can occur where someone is calling the
4333
+ * instancemethod via the class type and passing the
4334
+ * instance as the first argument. We need to shift the args
4335
+ * before making the call to the wrapper and effectively
4336
+ * bind the instance to the wrapped function using a partial
4337
+ * so the wrapper doesn't see anything as being different.
4338
+ */
4339
+
4340
+ instance = PyTuple_GetItem(args, 0);
4341
+
4342
+ if (!instance)
4343
+ return NULL;
4344
+
4345
+ int check = PyObject_IsInstance(instance, self->owner);
4346
+
4347
+ if (check < 0)
4348
+ return NULL;
4349
+
4350
+ if (check)
4351
+ {
4352
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
4353
+ if (!state)
4354
+ return NULL;
4355
+ wrapped = PyObject_CallFunctionObjArgs(
4356
+ (PyObject *)state->PartialCallableObjectProxy_Type,
4357
+ self->object_proxy.wrapped, instance, NULL);
4358
+
4359
+ if (!wrapped)
4360
+ return NULL;
4361
+
4362
+ param_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4363
+
4364
+ if (!param_args)
4365
+ {
4366
+ Py_DECREF(wrapped);
4367
+ return NULL;
4368
+ }
4369
+
4370
+ args = param_args;
4371
+ }
4372
+ else
4373
+ {
4374
+ instance = self->instance;
4375
+ }
4376
+ }
4377
+ else
4378
+ {
4379
+ instance = self->instance;
4380
+ }
4381
+
4382
+ if (!wrapped)
4383
+ {
4384
+ Py_INCREF(self->object_proxy.wrapped);
4385
+ wrapped = self->object_proxy.wrapped;
4386
+ }
4387
+
4388
+ if (!kwds)
4389
+ {
4390
+ param_kwds = PyDict_New();
4391
+ if (!param_kwds)
4392
+ {
4393
+ Py_XDECREF(param_args);
4394
+ Py_DECREF(wrapped);
4395
+ return NULL;
4396
+ }
4397
+ kwds = param_kwds;
4398
+ }
4399
+
4400
+ result = PyObject_CallFunctionObjArgs(self->wrapper, wrapped, instance,
4401
+ args, kwds, NULL);
4402
+
4403
+ Py_XDECREF(param_args);
4404
+ Py_XDECREF(param_kwds);
4405
+ Py_DECREF(wrapped);
4406
+
4407
+ return result;
4408
+ }
4409
+ else
4410
+ {
4411
+ /*
4412
+ * As in this case we would be dealing with a classmethod or
4413
+ * staticmethod, then _self_instance will only tell us whether
4414
+ * when calling the classmethod or staticmethod they did it via
4415
+ * an instance of the class it is bound to and not the case
4416
+ * where done by the class type itself. We thus ignore
4417
+ * _self_instance and use the __self__ attribute of the bound
4418
+ * function instead. For a classmethod, this means instance will
4419
+ * be the class type and for a staticmethod it will be None.
4420
+ * This is probably the more useful thing we can pass through
4421
+ * even though we loose knowledge of whether they were called on
4422
+ * the instance vs the class type, as it reflects what they have
4423
+ * available in the decoratored function.
4424
+ */
4425
+
4426
+ instance = PyObject_GetAttr(self->object_proxy.wrapped, state->str_self);
4427
+
4428
+ if (!instance)
4429
+ {
4430
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4431
+ return NULL;
4432
+ PyErr_Clear();
4433
+ Py_INCREF(Py_None);
4434
+ instance = Py_None;
4435
+ }
4436
+
4437
+ if (!kwds)
4438
+ {
4439
+ param_kwds = PyDict_New();
4440
+ if (!param_kwds)
4441
+ {
4442
+ Py_DECREF(instance);
4443
+ return NULL;
4444
+ }
4445
+ kwds = param_kwds;
4446
+ }
4447
+
4448
+ result = PyObject_CallFunctionObjArgs(
4449
+ self->wrapper, self->object_proxy.wrapped, instance, args, kwds, NULL);
4450
+
4451
+ Py_XDECREF(param_kwds);
4452
+
4453
+ Py_DECREF(instance);
4454
+
4455
+ return result;
4456
+ }
4457
+ }
4458
+
4459
+ /* ------------------------------------------------------------------------- */
4460
+
4461
+ static PyObject *WraptBoundFunctionWrapper_getattr(
4462
+ WraptFunctionWrapperObject *self, PyObject *args)
4463
+ {
4464
+ PyObject *name = NULL;
4465
+
4466
+ if (!PyArg_ParseTuple(args, "U:__getattr__", &name))
4467
+ return NULL;
4468
+
4469
+ if (self->parent && self->parent != Py_None)
4470
+ {
4471
+ PyObject *result = PyObject_GetAttr(self->parent, name);
4472
+
4473
+ if (result)
4474
+ return result;
4475
+
4476
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4477
+ return NULL;
4478
+
4479
+ PyErr_Clear();
4480
+ }
4481
+
4482
+ return WraptObjectProxy_getattr((WraptObjectProxyObject *)self, args);
4483
+ }
4484
+
4485
+ /* ------------------------------------------------------------------------- */
4486
+
4487
+ static int WraptBoundFunctionWrapper_setattro(
4488
+ WraptFunctionWrapperObject *self, PyObject *name, PyObject *value)
4489
+ {
4490
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
4491
+ if (!state)
4492
+ return -1;
4493
+
4494
+ int match = PyUnicode_Tailmatch(name, state->str_self_, 0, PY_SSIZE_T_MAX, -1);
4495
+
4496
+ if (match < 0)
4497
+ return -1;
4498
+
4499
+ if (match)
4500
+ {
4501
+ /* Check if this is an internal slot (has a getset descriptor on the type) */
4502
+ if (wrapt_type_has_attr(Py_TYPE(self), name))
4503
+ return PyObject_GenericSetAttr((PyObject *)self, name, value);
4504
+
4505
+ /* User-defined _self_ attribute — delegate to parent */
4506
+ if (self->parent && self->parent != Py_None)
4507
+ return PyObject_GenericSetAttr(self->parent, name, value);
4508
+ }
4509
+
4510
+ /* Fall through to base ObjectProxy setattro for everything else */
4511
+ return WraptObjectProxy_setattro((WraptObjectProxyObject *)self, name, value);
4512
+ }
4513
+
4514
+ /* ------------------------------------------------------------------------- */
4515
+
4516
+ static PyMethodDef WraptBoundFunctionWrapper_methods[] = {
4517
+ {"__getattr__", (PyCFunction)WraptBoundFunctionWrapper_getattr, METH_VARARGS,
4518
+ 0},
4519
+ {NULL, NULL},
4520
+ };
4521
+
4522
+ /* ------------------------------------------------------------------------- */
4523
+
4524
+ static PyType_Slot WraptBoundFunctionWrapper_slots[] = {
4525
+ {Py_tp_dealloc, WraptFunctionWrapperBase_dealloc},
4526
+ {Py_tp_traverse, WraptFunctionWrapperBase_traverse},
4527
+ {Py_tp_clear, WraptFunctionWrapperBase_clear},
4528
+ {Py_tp_call, WraptBoundFunctionWrapper_call},
4529
+ {Py_tp_setattro, WraptBoundFunctionWrapper_setattro},
4530
+ {Py_tp_methods, WraptBoundFunctionWrapper_methods},
4531
+ {0, NULL},
4532
+ };
4533
+
4534
+ static PyType_Spec WraptBoundFunctionWrapper_spec = {
4535
+ .name = "_wrappers.BoundFunctionWrapper",
4536
+ .basicsize = sizeof(WraptFunctionWrapperObject),
4537
+ .itemsize = 0,
4538
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4539
+ .slots = WraptBoundFunctionWrapper_slots,
4540
+ };
4541
+
4542
+ /* ------------------------------------------------------------------------- */
4543
+
4544
+ static int WraptFunctionWrapper_init(WraptFunctionWrapperObject *self,
4545
+ PyObject *args, PyObject *kwds)
4546
+ {
4547
+ PyObject *wrapped = NULL;
4548
+ PyObject *wrapper = NULL;
4549
+ PyObject *enabled = Py_None;
4550
+ PyObject *binding = NULL;
4551
+ PyObject *binding_owned = NULL;
4552
+ PyObject *instance = NULL;
4553
+
4554
+ wrapt_module_state *state = wrapt_state_from_type(Py_TYPE(self));
4555
+ if (!state)
4556
+ return -1;
4557
+
4558
+ PyObject *function_str = state->str_function;
4559
+ PyObject *classmethod_str = state->str_classmethod;
4560
+ PyObject *staticmethod_str = state->str_staticmethod;
4561
+ PyObject *callable_str = state->str_callable;
4562
+ PyObject *builtin_str = state->str_builtin;
4563
+ PyObject *class_str = state->str_class;
4564
+ PyObject *instancemethod_str = state->str_instancemethod;
4565
+
4566
+ int result = 0;
4567
+
4568
+ char *const kwlist[] = {"wrapped", "wrapper", "enabled", NULL};
4569
+
4570
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O:FunctionWrapper", kwlist,
4571
+ &wrapped, &wrapper, &enabled))
4572
+ {
4573
+ return -1;
4574
+ }
4575
+
4576
+ if (PyObject_TypeCheck(wrapped, state->FunctionWrapperBase_Type))
4577
+ {
4578
+ binding_owned = PyObject_GetAttr(wrapped, state->str_self_binding);
4579
+ if (!binding_owned)
4580
+ {
4581
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4582
+ return -1;
4583
+ PyErr_Clear();
4584
+ }
4585
+ else
4586
+ {
4587
+ binding = binding_owned;
4588
+ }
4589
+ }
4590
+
4591
+ if (!binding)
4592
+ {
4593
+ int check;
4594
+
4595
+ if (PyCFunction_Check(wrapped))
4596
+ {
4597
+ binding = builtin_str;
4598
+ }
4599
+ else
4600
+ {
4601
+ check = PyObject_IsInstance(wrapped, (PyObject *)&PyFunction_Type);
4602
+ if (check < 0)
4603
+ goto error;
4604
+ if (check)
4605
+ binding = function_str;
4606
+ }
4607
+
4608
+ if (!binding)
4609
+ {
4610
+ check = PyObject_IsInstance(wrapped, (PyObject *)&PyType_Type);
4611
+ if (check < 0)
4612
+ goto error;
4613
+ if (check)
4614
+ binding = class_str;
4615
+ }
4616
+
4617
+ if (!binding)
4618
+ {
4619
+ check = PyObject_IsInstance(wrapped, (PyObject *)&PyClassMethod_Type);
4620
+ if (check < 0)
4621
+ goto error;
4622
+ if (check)
4623
+ binding = classmethod_str;
4624
+ }
4625
+
4626
+ if (!binding)
4627
+ {
4628
+ check = PyObject_IsInstance(wrapped, (PyObject *)&PyStaticMethod_Type);
4629
+ if (check < 0)
4630
+ goto error;
4631
+ if (check)
4632
+ binding = staticmethod_str;
4633
+ }
4634
+
4635
+ if (!binding)
4636
+ {
4637
+ instance = PyObject_GetAttr(wrapped, state->str_self);
4638
+ if (instance != NULL)
4639
+ {
4640
+ check = PyObject_IsInstance(instance, (PyObject *)&PyType_Type);
4641
+ if (check < 0)
4642
+ {
4643
+ Py_DECREF(instance);
4644
+ goto error;
4645
+ }
4646
+ if (check)
4647
+ {
4648
+ binding = classmethod_str;
4649
+ }
4650
+ else
4651
+ {
4652
+ check = PyObject_IsInstance(wrapped, (PyObject *)&PyMethod_Type);
4653
+ if (check < 0)
4654
+ {
4655
+ Py_DECREF(instance);
4656
+ goto error;
4657
+ }
4658
+ if (check)
4659
+ binding = instancemethod_str;
4660
+ else
4661
+ binding = callable_str;
4662
+ }
4663
+
4664
+ Py_DECREF(instance);
4665
+ }
4666
+ else
4667
+ {
4668
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4669
+ goto error;
4670
+ PyErr_Clear();
4671
+
4672
+ binding = callable_str;
4673
+ }
4674
+ }
4675
+ }
4676
+
4677
+ result = WraptFunctionWrapperBase_raw_init(
4678
+ self, wrapped, Py_None, wrapper, enabled, binding, Py_None, Py_None);
4679
+
4680
+ Py_XDECREF(binding_owned);
4681
+
4682
+ return result;
4683
+
4684
+ error:
4685
+ Py_XDECREF(binding_owned);
4686
+ return -1;
4687
+ }
4688
+
4689
+ /* ------------------------------------------------------------------------- */
4690
+
4691
+ static PyType_Slot WraptFunctionWrapper_slots[] = {
4692
+ {Py_tp_dealloc, WraptFunctionWrapperBase_dealloc},
4693
+ {Py_tp_traverse, WraptFunctionWrapperBase_traverse},
4694
+ {Py_tp_clear, WraptFunctionWrapperBase_clear},
4695
+ {Py_tp_init, WraptFunctionWrapper_init},
4696
+ {0, NULL},
4697
+ };
4698
+
4699
+ static PyType_Spec WraptFunctionWrapper_spec = {
4700
+ .name = "_wrappers.FunctionWrapper",
4701
+ .basicsize = sizeof(WraptFunctionWrapperObject),
4702
+ .itemsize = 0,
4703
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4704
+ .slots = WraptFunctionWrapper_slots,
4705
+ };
4706
+
4707
+ /* ------------------------------------------------------------------------- */
4708
+
4709
+ /* PyModule_AddObjectRef polyfill for Python 3.9. */
4710
+ #if PY_VERSION_HEX >= 0x030A0000
4711
+ #define WRAPT_ADD_TYPE(mod, name, type) \
4712
+ do \
4713
+ { \
4714
+ if (PyModule_AddObjectRef((mod), (name), (PyObject *)(type)) < 0) \
4715
+ return -1; \
4716
+ } while (0)
4717
+ #else
4718
+ #define WRAPT_ADD_TYPE(mod, name, type) \
4719
+ do \
4720
+ { \
4721
+ Py_INCREF((PyObject *)(type)); \
4722
+ if (PyModule_AddObject((mod), (name), (PyObject *)(type)) < 0) \
4723
+ { \
4724
+ Py_DECREF((PyObject *)(type)); \
4725
+ return -1; \
4726
+ } \
4727
+ } while (0)
4728
+ #endif
4729
+
4730
+ /* Helper to create a heap type via PyType_FromModuleAndSpecWithBases and
4731
+ * store it on the module. Returns 0 on success, -1 on failure. */
4732
+
4733
+ static int wrapt_create_type(PyObject *module, PyTypeObject **out,
4734
+ PyType_Spec *spec, PyObject *bases,
4735
+ const char *name)
4736
+ {
4737
+ PyObject *type;
4738
+
4739
+ type = PyType_FromModuleAndSpec(module, spec, bases);
4740
+ if (type == NULL)
4741
+ return -1;
4742
+
4743
+ /* PyType_FromModuleAndSpec returns a new reference. Add the type to
4744
+ * the module dict first (WRAPT_ADD_TYPE adds another ref via
4745
+ * PyModule_AddObjectRef on 3.10+, or via manual incref +
4746
+ * PyModule_AddObject on 3.9). Only then store the owned reference
4747
+ * in module state so that *out is never populated on failure. */
4748
+
4749
+ WRAPT_ADD_TYPE(module, name, type);
4750
+
4751
+ *out = (PyTypeObject *)type;
4752
+
4753
+ return 0;
4754
+ }
4755
+
4756
+ /* Helper: intern a C string into the given module-state slot. Returns 0
4757
+ * on success, -1 on failure (with PyErr set). */
4758
+
4759
+ static int wrapt_intern_string(PyObject **slot, const char *s)
4760
+ {
4761
+ PyObject *o = PyUnicode_InternFromString(s);
4762
+ if (o == NULL)
4763
+ return -1;
4764
+ *slot = o;
4765
+ return 0;
4766
+ }
4767
+
4768
+ static int wrapt_init_strings(wrapt_module_state *state)
4769
+ {
4770
+ if (wrapt_intern_string(&state->str_wrapped, "__wrapped__") < 0)
4771
+ return -1;
4772
+ if (wrapt_intern_string(&state->str_wrapped_factory,
4773
+ "__wrapped_factory__") < 0)
4774
+ return -1;
4775
+ if (wrapt_intern_string(&state->str_wrapped_get, "__wrapped_get__") < 0)
4776
+ return -1;
4777
+ if (wrapt_intern_string(&state->str_module, "__module__") < 0)
4778
+ return -1;
4779
+ if (wrapt_intern_string(&state->str_doc, "__doc__") < 0)
4780
+ return -1;
4781
+ if (wrapt_intern_string(&state->str_setattr_fixups,
4782
+ "__wrapped_setattr_fixups__") < 0)
4783
+ return -1;
4784
+ if (wrapt_intern_string(&state->str_getattr, "__getattr__") < 0)
4785
+ return -1;
4786
+ if (wrapt_intern_string(&state->str_self_, "_self_") < 0)
4787
+ return -1;
4788
+ if (wrapt_intern_string(&state->str_callable, "callable") < 0)
4789
+ return -1;
4790
+ if (wrapt_intern_string(&state->str_bound_function_wrapper,
4791
+ "__bound_function_wrapper__") < 0)
4792
+ return -1;
4793
+ if (wrapt_intern_string(&state->str_function, "function") < 0)
4794
+ return -1;
4795
+ if (wrapt_intern_string(&state->str_classmethod, "classmethod") < 0)
4796
+ return -1;
4797
+ if (wrapt_intern_string(&state->str_staticmethod, "staticmethod") < 0)
4798
+ return -1;
4799
+ if (wrapt_intern_string(&state->str_builtin, "builtin") < 0)
4800
+ return -1;
4801
+ if (wrapt_intern_string(&state->str_class, "class") < 0)
4802
+ return -1;
4803
+ if (wrapt_intern_string(&state->str_instancemethod, "instancemethod") < 0)
4804
+ return -1;
4805
+ if (wrapt_intern_string(&state->str_object_proxy, "__object_proxy__") < 0)
4806
+ return -1;
4807
+ if (wrapt_intern_string(&state->str_enter, "__enter__") < 0)
4808
+ return -1;
4809
+ if (wrapt_intern_string(&state->str_exit, "__exit__") < 0)
4810
+ return -1;
4811
+ if (wrapt_intern_string(&state->str_aenter, "__aenter__") < 0)
4812
+ return -1;
4813
+ if (wrapt_intern_string(&state->str_aexit, "__aexit__") < 0)
4814
+ return -1;
4815
+ if (wrapt_intern_string(&state->str_mro_entries, "__mro_entries__") < 0)
4816
+ return -1;
4817
+ if (wrapt_intern_string(&state->str_name, "__name__") < 0)
4818
+ return -1;
4819
+ if (wrapt_intern_string(&state->str_qualname, "__qualname__") < 0)
4820
+ return -1;
4821
+ if (wrapt_intern_string(&state->str_annotations, "__annotations__") < 0)
4822
+ return -1;
4823
+ if (wrapt_intern_string(&state->str_dunder_class, "__class__") < 0)
4824
+ return -1;
4825
+ if (wrapt_intern_string(&state->str_self, "__self__") < 0)
4826
+ return -1;
4827
+ if (wrapt_intern_string(&state->str_set_name, "__set_name__") < 0)
4828
+ return -1;
4829
+ if (wrapt_intern_string(&state->str_self_binding, "_self_binding") < 0)
4830
+ return -1;
4831
+ if (wrapt_intern_string(&state->str_dict, "__dict__") < 0)
4832
+ return -1;
4833
+ return 0;
4834
+ }
4835
+
4836
+ static int wrapt_exec(PyObject *module)
4837
+ {
4838
+ wrapt_module_state *state = wrapt_get_state(module);
4839
+ PyObject *bases = NULL;
4840
+
4841
+ /* Eagerly intern all cached strings. m_clear releases anything that
4842
+ * was set before a failure, so we can return -1 on the first error. */
4843
+ if (wrapt_init_strings(state) < 0)
4844
+ return -1;
4845
+
4846
+ /* ObjectProxy: base = object (default). */
4847
+ if (wrapt_create_type(module, &state->ObjectProxy_Type,
4848
+ &WraptObjectProxy_spec, NULL, "ObjectProxy") < 0)
4849
+ return -1;
4850
+
4851
+ /* CallableObjectProxy: base = ObjectProxy. */
4852
+ bases = PyTuple_Pack(1, (PyObject *)state->ObjectProxy_Type);
4853
+ if (!bases)
4854
+ return -1;
4855
+ if (wrapt_create_type(module, &state->CallableObjectProxy_Type,
4856
+ &WraptCallableObjectProxy_spec, bases,
4857
+ "CallableObjectProxy") < 0)
4858
+ {
4859
+ Py_DECREF(bases);
4860
+ return -1;
4861
+ }
4862
+ Py_DECREF(bases);
4863
+
4864
+ /* PartialCallableObjectProxy: base = ObjectProxy. */
4865
+ bases = PyTuple_Pack(1, (PyObject *)state->ObjectProxy_Type);
4866
+ if (!bases)
4867
+ return -1;
4868
+ if (wrapt_create_type(module, &state->PartialCallableObjectProxy_Type,
4869
+ &WraptPartialCallableObjectProxy_spec, bases,
4870
+ "PartialCallableObjectProxy") < 0)
4871
+ {
4872
+ Py_DECREF(bases);
4873
+ return -1;
4874
+ }
4875
+ Py_DECREF(bases);
4876
+
4877
+ /* _FunctionWrapperBase: base = ObjectProxy. */
4878
+ bases = PyTuple_Pack(1, (PyObject *)state->ObjectProxy_Type);
4879
+ if (!bases)
4880
+ return -1;
4881
+ if (wrapt_create_type(module, &state->FunctionWrapperBase_Type,
4882
+ &WraptFunctionWrapperBase_spec, bases,
4883
+ "_FunctionWrapperBase") < 0)
4884
+ {
4885
+ Py_DECREF(bases);
4886
+ return -1;
4887
+ }
4888
+ Py_DECREF(bases);
4889
+
4890
+ /* BoundFunctionWrapper: base = _FunctionWrapperBase. */
4891
+ bases = PyTuple_Pack(1, (PyObject *)state->FunctionWrapperBase_Type);
4892
+ if (!bases)
4893
+ return -1;
4894
+ if (wrapt_create_type(module, &state->BoundFunctionWrapper_Type,
4895
+ &WraptBoundFunctionWrapper_spec, bases,
4896
+ "BoundFunctionWrapper") < 0)
4897
+ {
4898
+ Py_DECREF(bases);
4899
+ return -1;
4900
+ }
4901
+ Py_DECREF(bases);
4902
+
4903
+ /* FunctionWrapper: base = _FunctionWrapperBase. */
4904
+ bases = PyTuple_Pack(1, (PyObject *)state->FunctionWrapperBase_Type);
4905
+ if (!bases)
4906
+ return -1;
4907
+ if (wrapt_create_type(module, &state->FunctionWrapper_Type,
4908
+ &WraptFunctionWrapper_spec, bases,
4909
+ "FunctionWrapper") < 0)
4910
+ {
4911
+ Py_DECREF(bases);
4912
+ return -1;
4913
+ }
4914
+ Py_DECREF(bases);
4915
+
4916
+ /* Cache WrapperNotInitializedError from wrapt.wrappers. The module is
4917
+ * already in sys.modules because __wrapt__.py imports it before us. */
4918
+
4919
+ {
4920
+ PyObject *wrapt_wrappers_module = PyImport_ImportModule("wrapt.wrappers");
4921
+ if (!wrapt_wrappers_module)
4922
+ return -1;
4923
+
4924
+ state->WrapperNotInitializedError = PyObject_GetAttrString(
4925
+ wrapt_wrappers_module, "WrapperNotInitializedError");
4926
+
4927
+ Py_DECREF(wrapt_wrappers_module);
4928
+
4929
+ if (!state->WrapperNotInitializedError)
4930
+ return -1;
4931
+ }
4932
+
4933
+ return 0;
4934
+ }
4935
+
4936
+ static int wrapt_traverse(PyObject *module, visitproc visit, void *arg)
4937
+ {
4938
+ wrapt_module_state *state = wrapt_get_state(module);
4939
+ if (state == NULL)
4940
+ return 0;
4941
+ Py_VISIT(state->ObjectProxy_Type);
4942
+ Py_VISIT(state->CallableObjectProxy_Type);
4943
+ Py_VISIT(state->PartialCallableObjectProxy_Type);
4944
+ Py_VISIT(state->FunctionWrapperBase_Type);
4945
+ Py_VISIT(state->BoundFunctionWrapper_Type);
4946
+ Py_VISIT(state->FunctionWrapper_Type);
4947
+ Py_VISIT(state->str_wrapped);
4948
+ Py_VISIT(state->str_wrapped_factory);
4949
+ Py_VISIT(state->str_wrapped_get);
4950
+ Py_VISIT(state->str_module);
4951
+ Py_VISIT(state->str_doc);
4952
+ Py_VISIT(state->str_setattr_fixups);
4953
+ Py_VISIT(state->str_getattr);
4954
+ Py_VISIT(state->str_self_);
4955
+ Py_VISIT(state->str_callable);
4956
+ Py_VISIT(state->str_bound_function_wrapper);
4957
+ Py_VISIT(state->str_function);
4958
+ Py_VISIT(state->str_classmethod);
4959
+ Py_VISIT(state->str_staticmethod);
4960
+ Py_VISIT(state->str_builtin);
4961
+ Py_VISIT(state->str_class);
4962
+ Py_VISIT(state->str_instancemethod);
4963
+ Py_VISIT(state->str_object_proxy);
4964
+ Py_VISIT(state->str_enter);
4965
+ Py_VISIT(state->str_exit);
4966
+ Py_VISIT(state->str_aenter);
4967
+ Py_VISIT(state->str_aexit);
4968
+ Py_VISIT(state->str_mro_entries);
4969
+ Py_VISIT(state->str_name);
4970
+ Py_VISIT(state->str_qualname);
4971
+ Py_VISIT(state->str_annotations);
4972
+ Py_VISIT(state->str_dunder_class);
4973
+ Py_VISIT(state->str_self);
4974
+ Py_VISIT(state->str_set_name);
4975
+ Py_VISIT(state->str_self_binding);
4976
+ Py_VISIT(state->str_dict);
4977
+ Py_VISIT(state->WrapperNotInitializedError);
4978
+ return 0;
4979
+ }
4980
+
4981
+ static int wrapt_clear(PyObject *module)
4982
+ {
4983
+ wrapt_module_state *state = wrapt_get_state(module);
4984
+ if (state == NULL)
4985
+ return 0;
4986
+ Py_CLEAR(state->ObjectProxy_Type);
4987
+ Py_CLEAR(state->CallableObjectProxy_Type);
4988
+ Py_CLEAR(state->PartialCallableObjectProxy_Type);
4989
+ Py_CLEAR(state->FunctionWrapperBase_Type);
4990
+ Py_CLEAR(state->BoundFunctionWrapper_Type);
4991
+ Py_CLEAR(state->FunctionWrapper_Type);
4992
+ Py_CLEAR(state->str_wrapped);
4993
+ Py_CLEAR(state->str_wrapped_factory);
4994
+ Py_CLEAR(state->str_wrapped_get);
4995
+ Py_CLEAR(state->str_module);
4996
+ Py_CLEAR(state->str_doc);
4997
+ Py_CLEAR(state->str_setattr_fixups);
4998
+ Py_CLEAR(state->str_getattr);
4999
+ Py_CLEAR(state->str_self_);
5000
+ Py_CLEAR(state->str_callable);
5001
+ Py_CLEAR(state->str_bound_function_wrapper);
5002
+ Py_CLEAR(state->str_function);
5003
+ Py_CLEAR(state->str_classmethod);
5004
+ Py_CLEAR(state->str_staticmethod);
5005
+ Py_CLEAR(state->str_builtin);
5006
+ Py_CLEAR(state->str_class);
5007
+ Py_CLEAR(state->str_instancemethod);
5008
+ Py_CLEAR(state->str_object_proxy);
5009
+ Py_CLEAR(state->str_enter);
5010
+ Py_CLEAR(state->str_exit);
5011
+ Py_CLEAR(state->str_aenter);
5012
+ Py_CLEAR(state->str_aexit);
5013
+ Py_CLEAR(state->str_mro_entries);
5014
+ Py_CLEAR(state->str_name);
5015
+ Py_CLEAR(state->str_qualname);
5016
+ Py_CLEAR(state->str_annotations);
5017
+ Py_CLEAR(state->str_dunder_class);
5018
+ Py_CLEAR(state->str_self);
5019
+ Py_CLEAR(state->str_set_name);
5020
+ Py_CLEAR(state->str_self_binding);
5021
+ Py_CLEAR(state->str_dict);
5022
+ Py_CLEAR(state->WrapperNotInitializedError);
5023
+ return 0;
5024
+ }
5025
+
5026
+ static void wrapt_free(void *module)
5027
+ {
5028
+ (void)wrapt_clear((PyObject *)module);
5029
+ }
5030
+
5031
+ static PyModuleDef_Slot wrapt_slots[] = {
5032
+ {Py_mod_exec, wrapt_exec},
5033
+ #if PY_VERSION_HEX >= 0x030C0000
5034
+ /* Per-interpreter GIL support (PEP 684, 3.12+). All process-wide
5035
+ * mutable state has been removed: types are heap types created per
5036
+ * interpreter via PyType_FromModuleAndSpec, cached strings live in
5037
+ * per-interpreter module state, and there are no static PyObject *
5038
+ * globals. */
5039
+ {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
5040
+ #endif
5041
+ #if PY_VERSION_HEX >= 0x030D0000
5042
+ /* Free-threading support (3.13+). The previous Py_MOD_GIL_NOT_USED
5043
+ * declaration via PyUnstable_Module_SetGIL was unsound because the
5044
+ * 19 cached interned strings were lazily initialized via racy
5045
+ * static locals. With eager init in wrapt_exec the declaration is
5046
+ * now actually correct. */
5047
+ {Py_mod_gil, Py_MOD_GIL_NOT_USED},
5048
+ #endif
5049
+ {0, NULL},
5050
+ };
5051
+
5052
+ static struct PyModuleDef moduledef = {
5053
+ PyModuleDef_HEAD_INIT,
5054
+ .m_name = "_wrappers",
5055
+ .m_doc = NULL,
5056
+ .m_size = sizeof(wrapt_module_state),
5057
+ .m_methods = NULL,
5058
+ .m_slots = wrapt_slots,
5059
+ .m_traverse = wrapt_traverse,
5060
+ .m_clear = wrapt_clear,
5061
+ .m_free = wrapt_free,
5062
+ };
5063
+
5064
+ PyMODINIT_FUNC PyInit__wrappers(void)
5065
+ {
5066
+ return PyModuleDef_Init(&moduledef);
5067
+ }
5068
+
5069
+ /* ------------------------------------------------------------------------- */