/
ctraits.c
5884 lines (5063 loc) · 182 KB
/
ctraits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
// All rights reserved.
//
// This software is provided without warranty under the terms of the BSD
// license included in LICENSE.txt and may be redistributed only under
// the conditions described in the aforementioned license. The license
// is also available online at http://www.enthought.com/licenses/BSD.txt
//
// Thanks for using Enthought open source!
/*-----------------------------------------------------------------------------
| Includes:
+----------------------------------------------------------------------------*/
#include "Python.h"
/*-----------------------------------------------------------------------------
| Constants:
+----------------------------------------------------------------------------*/
static PyObject *class_traits; /* == "__class_traits__" */
static PyObject *listener_traits; /* == "__listener_traits__" */
static PyObject *editor_property; /* == "editor" */
static PyObject *class_prefix; /* == "__prefix__" */
static PyObject *trait_added; /* == "trait_added" */
static PyObject *Undefined; /* Global 'Undefined' value */
static PyObject *Uninitialized; /* Global 'Uninitialized' value */
static PyObject *TraitError; /* TraitError exception */
static PyObject *DelegationError; /* DelegationError exception */
static PyObject *TraitListObject; /* TraitListObject class */
static PyObject *TraitSetObject; /* TraitSetObject class */
static PyObject *TraitDictObject; /* TraitDictObject class */
static PyObject *adapt; /* 'adapt' function */
static PyTypeObject *ctrait_type; /* Python-level CTrait type reference */
/*-----------------------------------------------------------------------------
| Macro definitions:
+----------------------------------------------------------------------------*/
#define PyTrait_CheckExact(op) ((Py_TYPE(op)) == ctrait_type)
#define PyHasTraits_Check(op) PyObject_TypeCheck(op, &has_traits_type)
/* Notification related: */
#define has_notifiers(tnotifiers, onotifiers) \
((((tnotifiers) != NULL) && (PyList_GET_SIZE((tnotifiers)) > 0)) \
|| (((onotifiers) != NULL) && (PyList_GET_SIZE((onotifiers)) > 0)))
/*-----------------------------------------------------------------------------
| Forward declarations:
+----------------------------------------------------------------------------*/
static PyTypeObject trait_type;
static PyTypeObject has_traits_type;
/*-----------------------------------------------------------------------------
| 'ctraits' module doc string:
+----------------------------------------------------------------------------*/
PyDoc_STRVAR(
ctraits__doc__,
"Fast base classes for HasTraits and CTrait.\n"
"\n"
"The ctraits module defines the CHasTraits and cTrait extension types\n"
"that define the core performance-oriented portions of the Traits\n"
"package. Users will rarely need to use this module directly. Instead,\n"
"they should use the API-complete HasTraits and CTrait subclasses of \n"
"CHasTraits and cTrait (respectively).\n"
);
/*-----------------------------------------------------------------------------
| HasTraits behavior modification flags:
+----------------------------------------------------------------------------*/
/* Object has been initialized: */
#define HASTRAITS_INITED 0x00000001U
/* Do not send notifications when a trait changes value: */
#define HASTRAITS_NO_NOTIFY 0x00000002U
/* Requests that no event notifications be sent when this object is assigned to
a trait: */
#define HASTRAITS_VETO_NOTIFY 0x00000004U
/*-----------------------------------------------------------------------------
| 'CHasTraits' instance definition:
|
| Note: traits are normally stored in the type's dictionary, but are added to
| the instance's traits dictionary 'trait_dict' when the traits are defined
| dynamically or 'on_trait_change' is called on an instance of the trait.
|
| All 'anytrait_changed' notification handlers are stored in the instance's
| 'notifiers' list.
+----------------------------------------------------------------------------*/
typedef struct {
PyObject_HEAD /* Standard Python object header */
PyDictObject *ctrait_dict; /* Class traits dictionary */
PyDictObject *itrait_dict; /* Instance traits dictionary */
PyListObject *notifiers; /* List of 'any trait changed' notification
handlers */
unsigned int flags; /* Behavior modification flags */
PyObject *obj_dict; /* Object attribute dictionary ('__dict__') */
/* NOTE: 'obj_dict' field MUST be last field */
} has_traits_object;
static int
call_notifiers(
PyListObject *, PyListObject *, has_traits_object *, PyObject *,
PyObject *, PyObject *new_value);
/*-----------------------------------------------------------------------------
| 'CTrait' flag values:
+----------------------------------------------------------------------------*/
/* The trait is a Property: */
#define TRAIT_PROPERTY 0x00000001U
/* Should the delegate be modified (or the original object)? */
#define TRAIT_MODIFY_DELEGATE 0x00000002U
/* Make 'setattr' store the original unvalidated value */
#define TRAIT_SETATTR_ORIGINAL_VALUE 0x00000008U
/* Send the 'post_setattr' method the original unvalidated value */
#define TRAIT_POST_SETATTR_ORIGINAL_VALUE 0x00000010U
/* Does this trait have an associated 'mapped' trait? */
#define TRAIT_IS_MAPPED 0x00000080U
/* Mask for the comparison mode bits, which determine when
notifications are emitted on trait assignment. */
#define TRAIT_COMPARISON_MODE_MASK 0x00000104U
/* Notify on every assignment. Corresponds to ComparisonMode.none. */
#define TRAIT_COMPARISON_MODE_NONE 0x00000100U
/* Notify if the new object "is not" the old one. Corresponds to
ComparisonMode.identity. */
#define TRAIT_COMPARISON_MODE_IDENTITY 0x00000004U
/* Notify if the new object is not the old one, and is not equal to the old
one. Corresponds to ComparisonMode.equality. */
#define TRAIT_COMPARISON_MODE_EQUALITY 0x00000000U
/*-----------------------------------------------------------------------------
| Default value type constants (see `default_value_for` method)
+----------------------------------------------------------------------------*/
/* The default_value of the trait is the default value */
#define CONSTANT_DEFAULT_VALUE 0
/* The default_value of the trait is Missing */
#define MISSING_DEFAULT_VALUE 1
/* The object containing the trait is the default value */
#define OBJECT_DEFAULT_VALUE 2
/* A new copy of the list specified by default_value is the default value */
#define LIST_COPY_DEFAULT_VALUE 3
/* A new copy of the dict specified by default_value is the default value */
#define DICT_COPY_DEFAULT_VALUE 4
/* A new instance of TraitListObject constructed using the default_value list
is the default value */
#define TRAIT_LIST_OBJECT_DEFAULT_VALUE 5
/* A new instance of TraitDictObject constructed using the default_value dict
is the default value */
#define TRAIT_DICT_OBJECT_DEFAULT_VALUE 6
/* The default_value is a tuple of the form: (*callable*, *args*, *kw*),
where *callable* is a callable, *args* is a tuple, and *kw* is either a
dictionary or None. The default value is the result obtained by invoking
``callable(\*args, \*\*kw)`` */
#define CALLABLE_AND_ARGS_DEFAULT_VALUE 7
/* The default_value is a callable. The default value is the result obtained
by invoking *default_value*(*object*), where *object* is the object
containing the trait. If the trait has a validate() method, the validate()
method is also called to validate the result */
#define CALLABLE_DEFAULT_VALUE 8
/* A new instance of TraitSetObject constructed using the default_value set
is the default value */
#define TRAIT_SET_OBJECT_DEFAULT_VALUE 9
/* This trait doesn't permit a default value */
#define DISALLOW_DEFAULT_VALUE 10
/* Maximum legal value for default_value_type, for use in testing and
validation. */
#define MAXIMUM_DEFAULT_VALUE_TYPE 10
/* The maximum value for comparison_mode. Valid values are between 0 and
the maximum value. */
#define MAXIMUM_COMPARISON_MODE_VALUE 2
/*-----------------------------------------------------------------------------
| 'CTrait' instance definition:
+----------------------------------------------------------------------------*/
typedef struct _trait_object a_trait_object;
typedef PyObject *(*trait_getattr)(
a_trait_object *, has_traits_object *, PyObject *);
typedef int (*trait_setattr)(
a_trait_object *, a_trait_object *, has_traits_object *, PyObject *,
PyObject *);
typedef int (*trait_post_setattr)(
a_trait_object *, has_traits_object *, PyObject *, PyObject *);
typedef PyObject *(*trait_validate)(
a_trait_object *, has_traits_object *, PyObject *, PyObject *);
typedef PyObject *(*delegate_attr_name_func)(
a_trait_object *, has_traits_object *, PyObject *);
typedef struct _trait_object {
PyObject_HEAD /* Standard Python object header */
unsigned int flags; /* Flag bits */
trait_getattr getattr; /* Get trait value handler */
trait_setattr setattr; /* Set trait value handler */
trait_post_setattr post_setattr; /* Optional post 'setattr' handler */
PyObject *py_post_setattr; /* Python-based post 'setattr' handler */
trait_validate validate; /* Validate trait value handler */
PyObject *py_validate; /* Python-based validate value handler */
int default_value_type; /* Type of default value: see the
'default_value_for' function */
PyObject *default_value; /* Default value for trait */
PyObject *delegate_name; /* Optional delegate name */
/* Also used for 'property get' */
PyObject *delegate_prefix; /* Optional delegate prefix */
/* Also used for 'property set' */
delegate_attr_name_func delegate_attr_name; /* Optional routine to return*/
/* the computed delegate attribute name */
PyListObject *notifiers; /* Optional list of notification handlers */
PyObject *handler; /* Associated trait handler object */
/* NOTE: The 'obj_dict' field MUST be last */
PyObject *obj_dict; /* Standard Python object dictionary */
} trait_object;
/* Forward declarations: */
static void
trait_clone(trait_object *, trait_object *);
static PyObject *
has_traits_getattro(has_traits_object *obj, PyObject *name);
static int
has_traits_setattro(has_traits_object *obj, PyObject *name, PyObject *value);
static PyObject *
get_trait(has_traits_object *obj, PyObject *name, int instance);
static int
trait_property_changed(
has_traits_object *obj, PyObject *name, PyObject *old_value,
PyObject *new_value);
static int
setattr_event(
trait_object *traito, trait_object *traitd, has_traits_object *obj,
PyObject *name, PyObject *value);
static int
setattr_disallow(
trait_object *traito, trait_object *traitd, has_traits_object *obj,
PyObject *name, PyObject *value);
/*-----------------------------------------------------------------------------
| Raise a TraitError:
+----------------------------------------------------------------------------*/
static PyObject *
raise_trait_error(
trait_object *trait, has_traits_object *obj, PyObject *name,
PyObject *value)
{
PyObject *result;
/* Clear any current exception. We are handling it by raising
* a TraitError. */
PyErr_Clear();
result = PyObject_CallMethod(
trait->handler, "error", "(OOO)", obj, name, value);
Py_XDECREF(result);
return NULL;
}
/*-----------------------------------------------------------------------------
| Raise a fatal trait error:
+----------------------------------------------------------------------------*/
static int
fatal_trait_error(void)
{
PyErr_SetString(TraitError, "Non-trait found in trait dictionary");
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an "attribute is not a string" error:
+----------------------------------------------------------------------------*/
static int
invalid_attribute_error(PyObject *name)
{
const char *fmt =
"attribute name must be an instance of <type 'str'>. "
"Got %R (%.200s).";
PyErr_Format(PyExc_TypeError, fmt, name, Py_TYPE(name)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an "cant set items error" error:
+----------------------------------------------------------------------------*/
static PyObject *
cant_set_items_error(void)
{
PyErr_SetString(TraitError, "Can not set a collection's '_items' trait.");
return NULL;
}
/*-----------------------------------------------------------------------------
| Raise an "invalid trait definition" error:
+----------------------------------------------------------------------------*/
static int
bad_trait_value_error(void)
{
PyErr_SetString(
TraitError,
"Result of 'as_ctrait' method was not a 'CTraits' instance.");
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an invalid delegate error:
+----------------------------------------------------------------------------*/
static int
bad_delegate_error(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
DelegationError,
"The '%.400U' attribute of a '%.50s' object"
" delegates to an attribute which is not a defined trait.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an invalid delegate error:
+----------------------------------------------------------------------------*/
static int
bad_delegate_error2(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
DelegationError,
"The '%.400U' attribute of a '%.50s' object"
" has a delegate which does not have traits.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise a delegation recursion error:
+----------------------------------------------------------------------------*/
static int
delegation_recursion_error(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
DelegationError,
"Delegation recursion limit exceeded while setting"
" the '%.400U' attribute of a '%.50s' object.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
static int
delegation_recursion_error2(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
DelegationError,
"Delegation recursion limit exceeded while getting"
" the definition of the '%.400U' attribute of a '%.50s' object.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an attempt to delete read-only attribute error:
+----------------------------------------------------------------------------*/
static int
delete_readonly_error(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
TraitError,
"Cannot delete the read only '%.400U' attribute of a '%.50s' object.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an attempt to set a read-only attribute error:
+----------------------------------------------------------------------------*/
static int
set_readonly_error(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
TraitError,
"Cannot modify the read only '%.400U' attribute of a '%.50s' object.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an attempt to set an undefined attribute error:
+----------------------------------------------------------------------------*/
static int
set_disallow_error(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
TraitError,
"Cannot set the undefined '%.400U' attribute of a '%.50s' object.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an attempt to delete a property error:
+----------------------------------------------------------------------------*/
static int
set_delete_property_error(has_traits_object *obj, PyObject *name)
{
if (!PyUnicode_Check(name)) {
return invalid_attribute_error(name);
}
PyErr_Format(
TraitError, "Cannot delete the '%.400U' property of a '%.50s' object.",
name, Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an undefined attribute error:
+----------------------------------------------------------------------------*/
static void
unknown_attribute_error(has_traits_object *obj, PyObject *name)
{
PyErr_Format(
PyExc_AttributeError, "'%.50s' object has no attribute '%.400U'",
Py_TYPE(obj)->tp_name, name);
}
/*-----------------------------------------------------------------------------
| Raise a '__dict__' must be set to a dictionary error:
+----------------------------------------------------------------------------*/
static int
dictionary_error(void)
{
PyErr_SetString(PyExc_TypeError, "__dict__ must be set to a dictionary.");
return -1;
}
/*-----------------------------------------------------------------------------
| Gets/Sets a possibly NULL (or callable) value:
+----------------------------------------------------------------------------*/
static PyObject *
get_value(PyObject *value)
{
if (value == NULL) {
value = Py_None;
}
Py_INCREF(value);
return value;
}
static int
set_value(PyObject **field, PyObject *value)
{
PyObject *old_value;
/* Caution: don't DECREF the old field contents until *after* the new
contents are assigned, else there's a possibility for external code
(triggered by the DECREF) to see an invalid field value. */
old_value = *field;
Py_XINCREF(value);
*field = value;
Py_XDECREF(old_value);
return 0;
}
/*-----------------------------------------------------------------------------
| Gets the value of a flag on a cTrait object specified by a mask.
+----------------------------------------------------------------------------*/
static PyObject *
get_trait_flag(trait_object *trait, int mask)
{
if (trait->flags & mask) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}
/*-----------------------------------------------------------------------------
| Sets the value of a flag on a cTrait object specified by a mask.
+----------------------------------------------------------------------------*/
static int
set_trait_flag(trait_object *trait, int mask, PyObject *value)
{
int flag = PyObject_IsTrue(value);
if (flag == -1) {
return -1;
}
if (flag) {
trait->flags |= mask;
}
else {
trait->flags &= ~mask;
}
return 0;
}
/*-----------------------------------------------------------------------------
| Returns the result of calling a specified 'class' object with 1 argument:
+----------------------------------------------------------------------------*/
static PyObject *
call_class(
PyObject *class, trait_object *trait, has_traits_object *obj,
PyObject *name, PyObject *value)
{
PyObject *result;
PyObject *args;
args = PyTuple_Pack(4, trait->handler, (PyObject *)obj, name, value);
if (args == NULL) {
return NULL;
}
result = PyObject_Call(class, args, NULL);
Py_DECREF(args);
return result;
}
/*-----------------------------------------------------------------------------
| Attempts to get the value of a key in a 'known to be a dictionary' object:
+----------------------------------------------------------------------------*/
static PyObject *
dict_getitem(PyDictObject *dict, PyObject *key)
{
assert(PyDict_Check(dict));
return PyDict_GetItem((PyObject *)dict, key);
}
/*-----------------------------------------------------------------------------
| Gets the definition of the matching prefix based trait for a specified name:
|
| - This should always return a trait definition unless a fatal Python error
| occurs.
| - The bulk of the work is delegated to a Python implemented method because
| the implementation is complicated in C and does not need to be executed
| very often relative to other operations.
|
| Note: returns a *borrowed* reference, to match dict_getitem.
+----------------------------------------------------------------------------*/
static trait_object *
get_prefix_trait(has_traits_object *obj, PyObject *name, int is_set)
{
PyObject *trait = PyObject_CallMethod(
(PyObject *)obj, "__prefix_trait__", "(Oi)", name, is_set);
if (trait != NULL) {
assert(obj->ctrait_dict != NULL);
PyDict_SetItem((PyObject *)obj->ctrait_dict, name, trait);
Py_DECREF(trait);
if (has_traits_setattro(obj, trait_added, name) < 0) {
return NULL;
}
trait = get_trait(obj, name, 0);
/* We return a borrowed reference, to match dict_getitem. */
Py_DECREF(trait);
}
return (trait_object *)trait;
}
/*-----------------------------------------------------------------------------
| Handles the 'setattr' operation on a 'CHasTraits' instance:
+----------------------------------------------------------------------------*/
static int
has_traits_setattro(has_traits_object *obj, PyObject *name, PyObject *value)
{
trait_object *trait;
if ((obj->itrait_dict == NULL)
|| ((trait = (trait_object *)dict_getitem(obj->itrait_dict, name))
== NULL)) {
trait = (trait_object *)dict_getitem(obj->ctrait_dict, name);
if ((trait == NULL)
&& ((trait = get_prefix_trait(obj, name, 1)) == NULL)) {
return -1;
}
}
return trait->setattr(trait, trait, obj, name, value);
}
/*-----------------------------------------------------------------------------
| Allocates a CTrait instance:
+----------------------------------------------------------------------------*/
PyObject *
has_traits_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
// Call PyBaseObject_Type.tp_new to do the actual construction.
// This allows things like ABCMeta machinery to work correctly
// which is implemented at the C level.
PyObject *new_args, *new_kwds;
has_traits_object *obj;
new_args = PyTuple_New(0);
if (new_args == NULL) {
return NULL;
}
new_kwds = PyDict_New();
if (new_kwds == NULL) {
Py_DECREF(new_args);
return NULL;
}
obj = (has_traits_object *)PyBaseObject_Type.tp_new(
type, new_args, new_kwds);
Py_DECREF(new_kwds);
Py_DECREF(new_args);
if (obj != NULL) {
if (type->tp_dict == NULL) {
PyErr_SetString(PyExc_RuntimeError, "No tp_dict");
return NULL;
}
obj->ctrait_dict =
(PyDictObject *)PyDict_GetItem(type->tp_dict, class_traits);
if (obj->ctrait_dict == NULL) {
PyErr_SetString(PyExc_RuntimeError, "No ctrait_dict");
return NULL;
}
if (!PyDict_Check((PyObject *)obj->ctrait_dict)) {
PyErr_SetString(PyExc_RuntimeError, "ctrait_dict not a dict");
return NULL;
}
Py_INCREF(obj->ctrait_dict);
}
return (PyObject *)obj;
}
int
has_traits_init(PyObject *obj, PyObject *args, PyObject *kwds)
{
PyObject *key;
PyObject *value;
int has_listeners;
Py_ssize_t i = 0;
/* Make sure no non-keyword arguments were specified: */
if (!PyArg_ParseTuple(args, "")) {
return -1;
}
/* Make sure all of the object's listeners have been set up: */
has_listeners =
(PyMapping_Size(PyDict_GetItem(Py_TYPE(obj)->tp_dict, listener_traits))
> 0);
if (has_listeners) {
value = PyObject_CallMethod(obj, "_init_trait_listeners", NULL);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
}
/* Make sure all of the object's observers have been set up: */
value = PyObject_CallMethod(obj, "_init_trait_observers", NULL);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
/* Set any traits specified in the constructor: */
if (kwds != NULL) {
while (PyDict_Next(kwds, &i, &key, &value)) {
if (has_traits_setattro((has_traits_object *)obj, key, value)
== -1) {
return -1;
}
}
}
/* Make sure all post constructor argument assignment listeners have been
set up: */
if (has_listeners) {
value = PyObject_CallMethod(obj, "_post_init_trait_listeners", NULL);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
}
/* Make sure all post constructor argument assignment observers have been
set up: */
value = PyObject_CallMethod(obj, "_post_init_trait_observers", NULL);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
/* Call the 'traits_init' method to finish up initialization: */
value = PyObject_CallMethod(obj, "traits_init", NULL);
if (value == NULL) {
return -1;
}
Py_DECREF(value);
/* Indicate that the object has finished being initialized: */
((has_traits_object *)obj)->flags |= HASTRAITS_INITED;
return 0;
}
/*-----------------------------------------------------------------------------
| Object clearing method:
+----------------------------------------------------------------------------*/
static int
has_traits_clear(has_traits_object *obj)
{
Py_CLEAR(obj->ctrait_dict);
Py_CLEAR(obj->itrait_dict);
Py_CLEAR(obj->notifiers);
Py_CLEAR(obj->obj_dict);
return 0;
}
/*-----------------------------------------------------------------------------
| Deallocates an unused 'CHasTraits' instance:
+----------------------------------------------------------------------------*/
static void
has_traits_dealloc(has_traits_object *obj)
{
PyObject_GC_UnTrack(obj);
Py_TRASHCAN_SAFE_BEGIN(obj);
has_traits_clear(obj);
Py_TYPE(obj)->tp_free((PyObject *)obj);
Py_TRASHCAN_SAFE_END(obj);
}
/*-----------------------------------------------------------------------------
| Garbage collector traversal method:
+----------------------------------------------------------------------------*/
static int
has_traits_traverse(has_traits_object *obj, visitproc visit, void *arg)
{
Py_VISIT(obj->ctrait_dict);
Py_VISIT(obj->itrait_dict);
Py_VISIT(obj->notifiers);
Py_VISIT(obj->obj_dict);
return 0;
}
/*-----------------------------------------------------------------------------
| Handles the 'getattr' operation on a 'CHasTraits' instance:
+----------------------------------------------------------------------------*/
static PyObject *
has_traits_getattro(has_traits_object *obj, PyObject *name)
{
trait_object *trait;
PyObject *value;
/* The following is a performance hack to short-circuit the normal
look-up when the value is in the object's dictionary.
*/
PyDictObject *dict = (PyDictObject *)obj->obj_dict;
if (dict != NULL) {
assert(PyDict_Check(dict));
if (!PyUnicode_Check(name)) {
invalid_attribute_error(name);
return NULL;
}
value = PyDict_GetItem((PyObject *)dict, name);
if (value != NULL) {
Py_INCREF(value);
return value;
}
}
/* End of performance hack */
if (((obj->itrait_dict != NULL)
&& ((trait = (trait_object *)dict_getitem(obj->itrait_dict, name))
!= NULL))
|| ((trait = (trait_object *)dict_getitem(obj->ctrait_dict, name))
!= NULL)) {
return trait->getattr(trait, obj, name);
}
/* Try normal Python attribute access, but if it fails with an
AttributeError then get a prefix trait. */
value = PyObject_GenericGetAttr((PyObject *)obj, name);
if (value != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
return value;
}
PyErr_Clear();
if ((trait = get_prefix_trait(obj, name, 0)) != NULL) {
return trait->getattr(trait, obj, name);
}
return NULL;
}
/*-----------------------------------------------------------------------------
| Returns (and optionally creates) a specified instance or class trait:
+----------------------------------------------------------------------------*/
static PyObject *
get_trait(has_traits_object *obj, PyObject *name, int instance)
{
int i, n;
PyDictObject *itrait_dict;
trait_object *trait;
trait_object *itrait;
PyListObject *notifiers;
PyListObject *inotifiers;
PyObject *item;
/* If there already is an instance specific version of the requested trait,
then return it: */
itrait_dict = obj->itrait_dict;
if (itrait_dict != NULL) {
trait = (trait_object *)dict_getitem(itrait_dict, name);
if (trait != NULL) {
assert(PyTrait_CheckExact(trait));
Py_INCREF(trait);
return (PyObject *)trait;
}
}
/* If only an instance trait can be returned (but not created), then
return None: */
if (instance == 1) {
Py_INCREF(Py_None);
return Py_None;
}
/* Otherwise, get the class specific version of the trait (creating a
trait class version if necessary): */
assert(obj->ctrait_dict != NULL);
trait = (trait_object *)dict_getitem(obj->ctrait_dict, name);
if (trait == NULL) {
if (instance == 0) {
Py_INCREF(Py_None);
return Py_None;
}
if ((trait = get_prefix_trait(obj, name, 0)) == NULL) {
return NULL;
}
}
assert(PyTrait_CheckExact(trait));
/* If an instance specific trait is not needed, return the class trait: */
if (instance <= 0) {
Py_INCREF(trait);
return (PyObject *)trait;
}
/* Otherwise, create an instance trait dictionary if it does not exist: */
if (itrait_dict == NULL) {
obj->itrait_dict = itrait_dict = (PyDictObject *)PyDict_New();
if (itrait_dict == NULL) {
return NULL;
}
}
/* Create a new instance trait and clone the class trait into it: */
itrait = (trait_object *)PyType_GenericAlloc(ctrait_type, 0);
trait_clone(itrait, trait);
itrait->obj_dict = trait->obj_dict;
Py_XINCREF(itrait->obj_dict);
/* Copy the class trait's notifier list into the instance trait: */
if ((notifiers = trait->notifiers) != NULL) {
n = PyList_GET_SIZE(notifiers);
itrait->notifiers = inotifiers = (PyListObject *)PyList_New(n);
if (inotifiers == NULL) {
return NULL;
}
for (i = 0; i < n; i++) {
item = PyList_GET_ITEM(notifiers, i);
PyList_SET_ITEM(inotifiers, i, item);
Py_INCREF(item);
}
}
/* Add the instance trait to the instance's trait dictionary and return
the instance trait if successful: */
if (PyDict_SetItem((PyObject *)itrait_dict, name, (PyObject *)itrait)
>= 0) {
return (PyObject *)itrait;
}
/* Otherwise, indicate that an error ocurred updating the dictionary: */
return NULL;
}
/*-----------------------------------------------------------------------------
| Returns (and optionally creates) a specified instance or class trait:
|
| The legal values for 'instance' are:
| 2: Return instance trait (force creation if it does not exist)
| 1: Return existing instance trait (do not create)
| 0: Return existing instance or class trait (do not create)
| -1: Return instance trait or force create class trait (i.e. prefix trait)
| -2: Return the base trait (after all delegation has been resolved)
+----------------------------------------------------------------------------*/
static PyObject *
_has_traits_trait(has_traits_object *obj, PyObject *args)
{
has_traits_object *delegate;
has_traits_object *temp_delegate;
trait_object *trait;
PyObject *name;
PyObject *daname;