@@ -614,9 +614,8 @@ def visitProduct(self, prod, name):
614
614
for f in prod .fields :
615
615
self .emit ('"%s",' % f .name , 1 )
616
616
self .emit ("};" , 0 )
617
- self .emit ("static const char * const %s_field_defaults[]={" % name , 0 )
618
- for f in prod .fields :
619
- self .emit ('"%s",' % f .extra , 1 )
617
+ self .emit ("static const char %s_field_defaults[]={" % name , 0 )
618
+ self .emit_field_defaults (prod .fields )
620
619
self .emit ("};" , 0 )
621
620
622
621
def visitSum (self , sum , name ):
@@ -645,11 +644,17 @@ def visitConstructor(self, cons, name):
645
644
for t in cons .fields :
646
645
self .emit ('"%s",' % t .name , 1 )
647
646
self .emit ("};" ,0 )
648
- self .emit ("static const char * const %s_field_defaults[]={" % cons .name , 0 )
649
- for t in cons .fields :
650
- self .emit ('"%s",' % t .extra , 1 )
647
+ self .emit ("static const char %s_field_defaults[]={" % cons .name , 0 )
648
+ self .emit_field_defaults (cons .fields )
651
649
self .emit ("};" ,0 )
652
650
651
+ def emit_field_defaults (self , fields ):
652
+ for field in fields :
653
+ if field .extra :
654
+ self .emit ("%r," % field .extra , 1 )
655
+ else :
656
+ self .emit ("' ',", 1 )
657
+
653
658
654
659
class PyTypesVisitor (PickleVisitor ):
655
660
@@ -688,23 +693,6 @@ def visitModule(self, mod):
688
693
return 0;
689
694
}
690
695
691
- static inline PyObject *
692
- find_field_default(PyObject *field_default)
693
- {
694
- if (PyUnicode_GET_LENGTH(field_default) < 1) {
695
- return NULL;
696
- }
697
- switch (PyUnicode_READ_CHAR(field_default, 0)){
698
- case '?':
699
- Py_INCREF(Py_None);
700
- return Py_None;
701
- case '*':
702
- return PyList_New(0);
703
- default:
704
- return NULL;
705
- }
706
- }
707
-
708
696
static int
709
697
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
710
698
{
@@ -752,25 +740,21 @@ def visitModule(self, mod):
752
740
goto cleanup;
753
741
}
754
742
}
755
- PyObject *field, *raw_field_default, * field_default;
743
+ PyObject *field, *field_default;
756
744
for (i = 0; i < numfields; i++) {
757
745
field = PySequence_GetItem(fields, i);
758
746
if (!field) {
759
747
res = -1;
760
748
goto cleanup;
761
749
}
762
750
int attr_present = PyObject_HasAttr(self, field);
751
+ field_default = PyDict_GetItem(field_defaults, field);
763
752
Py_DECREF(field);
764
- if (!attr_present) {
765
- raw_field_default = PySequence_GetItem(field_defaults, i);
766
- if (!raw_field_default) {
767
- res = -1;
768
- goto cleanup;
769
- }
770
- field_default = find_field_default(raw_field_default);
771
- Py_DECREF(raw_field_default);
772
- if (!field_default) {
773
- continue;
753
+ if (!attr_present && field_default) {
754
+ if (PyList_Check(field_default)) {
755
+ field_default = PyList_New(0);
756
+ } else {
757
+ Py_INCREF(field_default);
774
758
}
775
759
res = PyObject_SetAttr(self, field, field_default);
776
760
Py_DECREF(field_default);
@@ -843,7 +827,7 @@ def visitModule(self, mod):
843
827
const char *type,
844
828
PyObject* base,
845
829
const char* const* fields,
846
- const char* const* field_defaults,
830
+ const char* field_defaults,
847
831
Py_ssize_t num_fields,
848
832
const char *doc
849
833
)
@@ -856,21 +840,29 @@ def visitModule(self, mod):
856
840
if (!fnames) {
857
841
goto exit;
858
842
}
859
- fdefaults = PyTuple_New(num_fields );
843
+ fdefaults = PyDict_New( );
860
844
if (!fdefaults) {
861
845
goto exit;
862
846
}
847
+ PyObject *field, *field_default;
863
848
for (i = 0; i < num_fields; i++) {
864
- PyObject * field = PyUnicode_InternFromString(fields[i]);
849
+ field = PyUnicode_InternFromString(fields[i]);
865
850
if (!field) {
866
851
goto exit;
867
852
}
868
853
PyTuple_SET_ITEM(fnames, i, field);
869
- PyObject *field_default = PyUnicode_InternFromString(field_defaults[i]);
870
- if (!field_default) {
871
- goto exit;
854
+ if (field_defaults[i] == '?') {
855
+ field_default = Py_None;
856
+ Py_INCREF(field_default);
857
+ } else if (field_defaults[i] == '*') {
858
+ field_default = PyList_New(0);
859
+ if (!field_default) {
860
+ goto exit;
861
+ }
862
+ } else {
863
+ continue;
872
864
}
873
- PyTuple_SET_ITEM (fdefaults, i , field_default);
865
+ PyDict_SetItem (fdefaults, field , field_default);
874
866
}
875
867
result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOOOs}",
876
868
type, base,
0 commit comments