/
ctraits.c
5256 lines (4323 loc) · 176 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
/******************************************************************************
*
* Description: C based implementation of the Traits package
*
* Copyright (c) 2005, Enthought, Inc.
* All rights reserved.
*
* This software is provided without warranty under the terms of the BSD
* license included in enthought/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!
*
* Author: David C. Morrill
* Date: 06/15/2004
*
******************************************************************************/
/*-----------------------------------------------------------------------------
| Includes:
+----------------------------------------------------------------------------*/
#include "Python.h"
#include "structmember.h"
#include "py2to3.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 * empty_tuple; /* == () */
static PyObject * empty_dict; /* == {} */
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 * TraitValue; /* TraitValue class */
static PyObject * adapt; /* PyProtocols 'adapt' function */
static PyObject * validate_implements; /* 'validate implementation' function */
static PyObject * is_callable; /* Marker for 'callable' value */
static PyObject * _HasTraits_monitors; /* Object creation monitors. */
static PyObject * _trait_notification_handler; /* User supplied trait */
/* notification handler (intended for use by debugging tools) */
static PyTypeObject * ctrait_type; /* Python-level CTrait type reference */
/*-----------------------------------------------------------------------------
| Macro definitions:
+----------------------------------------------------------------------------*/
/* The following macro is automatically defined in Python 2.4 and later: */
#ifndef Py_VISIT
#define Py_VISIT(op) \
do { \
if (op) { \
int vret = visit((PyObject *)(op), arg); \
if (vret) return vret; \
} \
} while (0)
#endif
/* The following macro is automatically defined in Python 2.4 and later: */
#ifndef Py_CLEAR
#define Py_CLEAR(op) \
do { \
if (op) { \
PyObject *tmp = (PyObject *)(op); \
(op) = NULL; \
Py_DECREF(tmp); \
} \
} while (0)
#endif
#define DEFERRED_ADDRESS(ADDR) NULL
#define PyTrait_CheckExact(op) ((op)->ob_type == ctrait_type)
#define PyHasTraits_Check(op) PyObject_TypeCheck(op, &has_traits_type)
#define PyHasTraits_CheckExact(op) ((op)->ob_type == &has_traits_type)
/* Trait method related: */
#if PY_MAJOR_VERSION < 3
#define TP_DESCR_GET(t) \
(PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL)
#else
#define TP_DESCR_GET(t) \
((t)->tp_descr_get)
#endif
/* Notification related: */
#define has_notifiers(tnotifiers,onotifiers) \
((((tnotifiers) != NULL) && (PyList_GET_SIZE((tnotifiers))>0)) || \
(((onotifiers) != NULL) && (PyList_GET_SIZE((onotifiers))>0)))
/* Python version dependent macros: */
#if ( (PY_MAJOR_VERSION == 2) && (PY_MINOR_VERSION < 3) )
#define PyMODINIT_FUNC void
#define PyDoc_VAR(name) static char name[]
#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
#ifdef WITH_DOC_STRINGS
#define PyDoc_STR(str) str
#else
#define PyDoc_STR(str) ""
#endif
#endif
#if (PY_VERSION_HEX < 0x02050000)
typedef int Py_ssize_t;
#endif
/*-----------------------------------------------------------------------------
| Forward declarations:
+----------------------------------------------------------------------------*/
static PyTypeObject trait_type;
static PyTypeObject has_traits_type;
/*-----------------------------------------------------------------------------
| 'ctraits' module doc string:
+----------------------------------------------------------------------------*/
PyDoc_STRVAR( ctraits__doc__,
"The ctraits module defines the CHasTraits and CTrait C extension types that\n"
"define the core performance oriented portions of the Traits package." );
/*-----------------------------------------------------------------------------
| HasTraits behavior modification flags:
+----------------------------------------------------------------------------*/
/* Object has been initialized: */
#define HASTRAITS_INITED 0x00000001
/* Do not send notifications when a trait changes value: */
#define HASTRAITS_NO_NOTIFY 0x00000002
/* Requests that no event notifications be sent when this object is assigned to
a trait: */
#define HASTRAITS_VETO_NOTIFY 0x00000004
/*-----------------------------------------------------------------------------
| '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 */
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 0x00000001
/* Should the delegate be modified (or the original object)? */
#define TRAIT_MODIFY_DELEGATE 0x00000002
/* Should a simple object identity test be performed (or a rich compare)? */
#define TRAIT_OBJECT_IDENTITY 0x00000004
/* Make 'setattr' store the original unvalidated value */
#define TRAIT_SETATTR_ORIGINAL_VALUE 0x00000008
/* Send the 'post_setattr' method the original unvalidated value */
#define TRAIT_POST_SETATTR_ORIGINAL_VALUE 0x00000010
/* Can a 'TraitValue' be assigned to override the trait definition? */
#define TRAIT_VALUE_ALLOWED 0x00000020
/* Is this trait a special 'TraitValue' trait that uses a property? */
#define TRAIT_VALUE_PROPERTY 0x00000040
/* Does this trait have an associated 'mapped' trait? */
#define TRAIT_IS_MAPPED 0x00000080
/* Should any old/new value test be performed before generating
notifications? */
#define TRAIT_NO_VALUE_TEST 0x00000100
/*-----------------------------------------------------------------------------
| '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 */
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' hndlr */
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 ( void ) {
PyErr_SetString( PyExc_TypeError, "attribute name must be string" );
return -1;
}
/*-----------------------------------------------------------------------------
| Raise an "invalid trait definition" error:
+----------------------------------------------------------------------------*/
static int
bad_trait_error ( void ) {
PyErr_SetString( TraitError, "Invalid argument to trait constructor." );
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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
DelegationError,
"The '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object"
" delegates to an attribute which is not a defined trait.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
DelegationError,
"The '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object"
" has a delegate which does not have traits.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
DelegationError,
"Delegation recursion limit exceeded while setting"
" the '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( name ),
Py_TYPE(obj)->tp_name
);
return -1;
}
static int
delegation_recursion_error2 ( has_traits_object * obj, PyObject * name ) {
if ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
DelegationError,
"Delegation recursion limit exceeded while getting"
" the definition of"
" the '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
TraitError,
"Cannot delete the read only '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
TraitError,
"Cannot modify the read only '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
TraitError,
"Cannot set the undefined '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" attribute of a '%.50s' object.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 ( !Py2to3_SimpleString_Check( name ) ) {
return invalid_attribute_error();
}
PyErr_Format(
TraitError,
"Cannot delete the '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'"
" property of a '%.50s' object.",
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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 '%.400" Py2to3_PYERR_SIMPLE_STRING_FMTCHR "'",
Py_TYPE(obj)->tp_name,
Py2to3_PYERR_PREPARE_SIMPLE_STRING( 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_callable_value ( PyObject * value ) {
PyObject * tuple, * temp;
if ( value == NULL ) {
value = Py_None;
Py_INCREF( value );
} else if ( PyCallable_Check( value ) ) {
value = is_callable;
Py_INCREF( value );
} else if ( PyTuple_Check( value ) &&
( PyTuple_GET_SIZE( value ) >= 3 ) &&
( Py2to3_PyNum_AsLong( PyTuple_GET_ITEM( value, 0 ) ) == 10) ) {
tuple = PyTuple_New( 3 );
if ( tuple != NULL ) {
PyTuple_SET_ITEM( tuple, 0, temp = PyTuple_GET_ITEM( value, 0 ) );
Py_INCREF( temp );
PyTuple_SET_ITEM( tuple, 1, temp = PyTuple_GET_ITEM( value, 1 ) );
Py_INCREF( temp );
PyTuple_SET_ITEM( tuple, 2, is_callable );
Py_INCREF( is_callable );
value = tuple;
} else {
value = NULL;
}
} else {
Py_INCREF( value );
}
return 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 ) {
Py_INCREF( value );
Py_XDECREF( *field );
*field = value;
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 = PyTuple_New( 4 );
if ( args == NULL )
return NULL;
PyTuple_SET_ITEM( args, 0, trait->handler );
PyTuple_SET_ITEM( args, 1, (PyObject *) obj );
PyTuple_SET_ITEM( args, 2, name );
PyTuple_SET_ITEM( args, 3, value );
Py_INCREF( trait->handler );
Py_INCREF( obj );
Py_INCREF( name );
Py_INCREF( value );
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 ) {
#if !defined(Py_LIMITED_API) && (PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 3)
Py_hash_t hash;
#endif
assert( PyDict_Check( dict ) );
#if !defined(Py_LIMITED_API) && (PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 3)
hash = Py2to3_GetHash_wCache( key );
if ( hash == -1 ) {
PyErr_Clear();
return NULL;
}
return (dict->ma_lookup)( dict, key, hash )->me_value;
#else
return PyDict_GetItem((PyObject *)dict,key);
#endif
}
/*-----------------------------------------------------------------------------
| 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.
|
+----------------------------------------------------------------------------*/
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 );
Py_DECREF( trait );
}
return (trait_object *) trait;
}
/*-----------------------------------------------------------------------------
| Assigns a special TraitValue to a specified trait attribute:
+----------------------------------------------------------------------------*/
static int
setattr_value ( trait_object * trait,
has_traits_object * obj,
PyObject * name,
PyObject * value ) {
PyDictObject * dict;
PyObject * trait_new, * result, * obj_dict;
PyObject * trait_old = NULL;
PyObject * value_old = NULL;
trait_new = PyObject_CallMethod( value, "as_ctrait", "(O)", trait );
if ( trait_new == NULL )
goto error2;
if ( (trait_new != Py_None) && (!PyTrait_CheckExact( trait_new )) ) {
Py_DECREF( trait_new );
return bad_trait_value_error();
}
dict = obj->itrait_dict;
if ( (dict != NULL) &&
((trait_old = dict_getitem( dict, name )) != NULL) &&
((((trait_object *) trait_old)->flags & TRAIT_VALUE_PROPERTY) != 0) ) {
result = PyObject_CallMethod( trait_old, "_unregister",
"(OO)", obj, name );
if ( result == NULL )
goto error1;
Py_DECREF( result );
}
if ( trait_new == Py_None ) {
if ( trait_old != NULL ) {
PyDict_DelItem( (PyObject *) dict, name );
}
goto success;
}
if ( dict == NULL ) {
obj->itrait_dict = dict = (PyDictObject *) PyDict_New();
if ( dict == NULL )
goto error1;
}
if ( (((trait_object *) trait_new)->flags & TRAIT_VALUE_PROPERTY) != 0 ) {
if ( (value_old = has_traits_getattro( obj, name )) == NULL )
goto error1;
obj_dict = obj->obj_dict;
if ( obj_dict != NULL )
PyDict_DelItem( obj_dict, name );
}
if ( PyDict_SetItem( (PyObject *) dict, name, trait_new ) < 0 )
goto error0;
if ( (((trait_object *) trait_new)->flags & TRAIT_VALUE_PROPERTY) != 0 ) {
result = PyObject_CallMethod( trait_new, "_register",
"(OO)", obj, name );
if ( result == NULL )
goto error0;
Py_DECREF( result );
if ( trait_property_changed( obj, name, value_old, NULL ) )
goto error0;
Py_DECREF( value_old );
}
success:
Py_DECREF( trait_new );
return 0;
error0:
Py_XDECREF( value_old );
error1:
Py_DECREF( trait_new );
error2:
return -1;
}
/*-----------------------------------------------------------------------------
| 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;
}
if ( ((trait->flags & TRAIT_VALUE_ALLOWED) != 0) &&
(PyObject_IsInstance( value, TraitValue ) > 0) ) {
return setattr_value( trait, obj, name, value );
}
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.
has_traits_object * obj = (has_traits_object *) PyBaseObject_Type.tp_new(type, empty_tuple, empty_dict);
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;
PyObject * klass;
PyObject * handler;
PyObject * handler_args;
int n;
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( obj->ob_type->tp_dict,
listener_traits ) ) > 0);
if ( has_listeners ) {
value = PyObject_CallMethod( obj, "_init_trait_listeners", "()" );
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", "()" );
if ( value == NULL )
return -1;
Py_DECREF( value );
}
/* Notify any interested monitors that a new object has been created: */
for ( i = 0, n = PyList_GET_SIZE( _HasTraits_monitors ); i < n; i++ ) {
value = PyList_GET_ITEM( _HasTraits_monitors, i );
assert( PyTuple_Check( value ) );
assert( PyTuple_GET_SIZE( value ) == 2 );
klass = PyTuple_GET_ITEM( value, 0 );
handler = PyTuple_GET_ITEM( value, 1 );
if ( PyObject_IsInstance( obj, klass ) > 0 ) {
handler_args = PyTuple_New( 1 );
PyTuple_SetItem( handler_args, 0, obj );
Py_INCREF( obj );
PyObject_Call( handler, handler_args, NULL );
Py_DECREF( handler_args );
}
}
/* Call the 'traits_init' method to finish up initialization: */
value = PyObject_CallMethod( obj, "traits_init", "()" );
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;
PyObject *bad_attr_marker;
/* 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 ) );
bad_attr_marker = name;
value = Py2to3_GetAttrDictValue(dict, name, bad_attr_marker);
// there is a slight performance-hit here:
// Py2to3_GetAttrDictValue cannot signal invalid attributes
// unambiguously, so we have to reckeck in case the marker value is
// returned. Make sure to pick an unlikely marker value.
if((value==bad_attr_marker) && !Py2to3_AttrNameCheck(name)) {
invalid_attribute_error();
return NULL;
}
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 );
}
if ( (value = PyObject_GenericGetAttr( (PyObject *) obj, name )) != NULL )
return value;