Mercurial > cpython-lru_cache-weakref
annotate Python/modsupport.c @ 998:09b48604855f legacy-trunk
suppress unnecessary error message if too many arguments are passed
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 27 Aug 1992 07:45:12 +0000 |
| parents | 5c6cdb58a801 |
| children | 533e36de1e4d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
795
0cd197947d81
Copyright for 1992 added
Guido van Rossum <guido@python.org>
parents:
733
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 145 | 3 Netherlands. |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* Module support implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 #include "modsupport.h" |
| 29 #include "import.h" | |
| 30 | |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
31 #ifdef HAVE_PROTOTYPES |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
32 #define USE_STDARG |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
33 #endif |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
34 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
35 #ifdef USE_STDARG |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
36 #include <stdarg.h> |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
37 #else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
38 #include <varargs.h> |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
39 #endif |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
40 |
| 6 | 41 |
| 42 object * | |
| 43 initmodule(name, methods) | |
| 44 char *name; | |
| 45 struct methodlist *methods; | |
| 46 { | |
| 47 object *m, *d, *v; | |
| 48 struct methodlist *ml; | |
| 86 | 49 char namebuf[256]; |
| 50 if ((m = add_module(name)) == NULL) { | |
| 6 | 51 fprintf(stderr, "initializing module: %s\n", name); |
| 52 fatal("can't create a module"); | |
| 53 } | |
| 54 d = getmoduledict(m); | |
| 55 for (ml = methods; ml->ml_name != NULL; ml++) { | |
| 86 | 56 sprintf(namebuf, "%s.%s", name, ml->ml_name); |
| 57 v = newmethodobject(strdup(namebuf), ml->ml_meth, | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
372
diff
changeset
|
58 (object *)NULL, ml->ml_varargs); |
| 86 | 59 /* XXX The strdup'ed memory is never freed */ |
| 6 | 60 if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) { |
| 61 fprintf(stderr, "initializing module: %s\n", name); | |
| 62 fatal("can't initialize module"); | |
| 63 } | |
| 64 DECREF(v); | |
| 65 } | |
| 86 | 66 return m; |
| 6 | 67 } |
| 68 | |
| 69 | |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
70 /* Helper for getargs() and mkvalue() to scan the length of a format */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
71 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
72 static int countformat PROTO((char *format, int endchar)); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
73 static int countformat(format, endchar) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
74 char *format; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
75 int endchar; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
76 { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
77 int count = 0; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
78 int level = 0; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
79 while (level > 0 || *format != endchar) { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
80 if (*format == '\0') { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
81 /* Premature end */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
82 err_setstr(SystemError, "unmatched paren in format"); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
83 return -1; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
84 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
85 else if (*format == '(') { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
86 if (level == 0) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
87 count++; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
88 level++; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
89 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
90 else if (*format == ')') |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
91 level--; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
92 else if (level == 0 && *format != '#') |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
93 count++; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
94 format++; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
95 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
96 return count; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
97 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
98 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
99 |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
100 /* Generic argument list parser */ |
| 6 | 101 |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
102 static int do_arg PROTO((object *arg, char** p_format, va_list *p_va)); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
103 static int |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
104 do_arg(arg, p_format, p_va) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
105 object *arg; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
106 char** p_format; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
107 va_list *p_va; |
| 6 | 108 { |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
109 char *format = *p_format; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
110 va_list va = *p_va; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
111 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
112 if (arg == NULL) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
113 return 0; /* Incomplete tuple or list */ |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
114 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
115 switch (*format++) { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
116 |
|
998
09b48604855f
suppress unnecessary error message if too many arguments are passed
Guido van Rossum <guido@python.org>
parents:
957
diff
changeset
|
117 case '(': /* tuple, distributed over C parameters */ { |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
118 int i, n; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
119 if (!is_tupleobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
120 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
121 n = gettuplesize(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
122 for (i = 0; i < n; i++) { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
123 if (!do_arg(gettupleitem(arg, i), &format, &va)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
124 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
125 } |
|
998
09b48604855f
suppress unnecessary error message if too many arguments are passed
Guido van Rossum <guido@python.org>
parents:
957
diff
changeset
|
126 if (*format++ != ')') |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
127 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
128 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
129 } |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
130 |
|
998
09b48604855f
suppress unnecessary error message if too many arguments are passed
Guido van Rossum <guido@python.org>
parents:
957
diff
changeset
|
131 case ')': /* End of format -- too many arguments */ |
|
09b48604855f
suppress unnecessary error message if too many arguments are passed
Guido van Rossum <guido@python.org>
parents:
957
diff
changeset
|
132 return 0; |
|
09b48604855f
suppress unnecessary error message if too many arguments are passed
Guido van Rossum <guido@python.org>
parents:
957
diff
changeset
|
133 |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
134 case 'b': /* byte -- very short int */ { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
135 char *p = va_arg(va, char *); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
136 if (is_intobject(arg)) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
137 *p = getintvalue(arg); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
138 else |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
139 return 0; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
140 break; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
141 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
142 |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
143 case 'h': /* short int */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
144 short *p = va_arg(va, short *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
145 if (is_intobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
146 *p = getintvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
147 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
148 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
149 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
150 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
151 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
152 case 'i': /* int */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
153 int *p = va_arg(va, int *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
154 if (is_intobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
155 *p = getintvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
156 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
157 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
158 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
159 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
160 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
161 case 'l': /* long int */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
162 long *p = va_arg(va, long *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
163 if (is_intobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
164 *p = getintvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
165 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
166 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
167 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
168 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
169 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
170 case 'f': /* float */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
171 float *p = va_arg(va, float *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
172 if (is_floatobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
173 *p = getfloatvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
174 else if (is_intobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
175 *p = (float)getintvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
176 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
177 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
178 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
179 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
180 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
181 case 'd': /* double */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
182 double *p = va_arg(va, double *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
183 if (is_floatobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
184 *p = getfloatvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
185 else if (is_intobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
186 *p = (double)getintvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
187 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
188 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
189 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
190 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
191 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
192 case 'c': /* char */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
193 char *p = va_arg(va, char *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
194 if (is_stringobject(arg) && getstringsize(arg) == 1) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
195 *p = getstringvalue(arg)[0]; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
196 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
197 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
198 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
199 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
200 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
201 case 's': /* string */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
202 char **p = va_arg(va, char **); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
203 if (is_stringobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
204 *p = getstringvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
205 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
206 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
207 if (*format == '#') { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
208 int *q = va_arg(va, int *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
209 *q = getstringsize(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
210 format++; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
211 } |
|
957
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
212 else if (strlen(*p) != getstringsize(arg)) { |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
213 err_setstr(ValueError, "embedded '\\0' in string arg"); |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
214 return 0; |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
215 } |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
216 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
217 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
218 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
219 case 'z': /* string, may be NULL (None) */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
220 char **p = va_arg(va, char **); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
221 if (arg == None) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
222 *p = 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
223 else if (is_stringobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
224 *p = getstringvalue(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
225 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
226 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
227 if (*format == '#') { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
228 int *q = va_arg(va, int *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
229 if (arg == None) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
230 *q = 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
231 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
232 *q = getstringsize(arg); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
233 format++; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
234 } |
|
957
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
235 else if (*p != NULL && strlen(*p) != getstringsize(arg)) { |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
236 err_setstr(ValueError, "embedded '\\0' in string arg"); |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
237 return 0; |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
238 } |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
239 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
240 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
241 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
242 case 'S': /* string object */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
243 object **p = va_arg(va, object **); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
244 if (is_stringobject(arg)) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
245 *p = arg; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
246 else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
247 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
248 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
249 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
250 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
251 case 'O': /* object */ { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
252 object **p = va_arg(va, object **); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
253 *p = arg; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
254 break; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
255 } |
| 6 | 256 |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
257 default: |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
258 fprintf(stderr, "bad do_arg format: x%x '%c'\n", |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
259 format[-1], format[-1]); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
260 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
261 |
| 6 | 262 } |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
263 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
264 *p_va = va; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
265 *p_format = format; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
266 |
| 6 | 267 return 1; |
| 268 } | |
| 269 | |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
270 #ifdef USE_STDARG |
| 733 | 271 /* VARARGS2 */ |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
272 int getargs(object *arg, char *format, ...) |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
273 #else |
| 733 | 274 /* VARARGS */ |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
275 int getargs(va_alist) va_dcl |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
276 #endif |
| 6 | 277 { |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
278 char *f; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
279 int ok; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
280 va_list va; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
281 #ifdef USE_STDARG |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
282 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
283 va_start(va, format); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
284 #else |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
285 object *arg; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
286 char *format; |
| 6 | 287 |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
288 va_start(va); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
289 arg = va_arg(va, object *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
290 format = va_arg(va, char *); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
291 #endif |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
292 if (*format == '\0') { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
293 va_end(va); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
294 if (arg != NULL) { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
295 err_setstr(TypeError, "no arguments needed"); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
296 return 0; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
297 } |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
298 return 1; |
| 6 | 299 } |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
300 |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
301 f = format; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
302 ok = do_arg(arg, &f, &va) && *f == '\0'; |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
303 va_end(va); |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
304 if (!ok) { |
|
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
305 char buf[256]; |
|
957
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
306 if (!err_occurred()) { |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
307 sprintf(buf, "bad argument list (format '%s')", |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
308 format); |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
309 err_setstr(TypeError, buf); |
|
5c6cdb58a801
check for embedded \0 in strings for z and s formats
Guido van Rossum <guido@python.org>
parents:
860
diff
changeset
|
310 } |
| 6 | 311 } |
|
637
3cb686c48cdd
New getargs() function: a single varargs function,
Guido van Rossum <guido@python.org>
parents:
488
diff
changeset
|
312 return ok; |
| 6 | 313 } |
| 314 | |
| 315 int | |
| 316 getlongtuplearg(args, a, n) | |
| 317 object *args; | |
| 318 long *a; /* [n] */ | |
| 319 int n; | |
| 320 { | |
| 321 int i; | |
| 322 if (!is_tupleobject(args) || gettuplesize(args) != n) { | |
| 323 return err_badarg(); | |
| 324 } | |
| 325 for (i = 0; i < n; i++) { | |
| 326 object *v = gettupleitem(args, i); | |
| 327 if (!is_intobject(v)) { | |
| 328 return err_badarg(); | |
| 329 } | |
| 330 a[i] = getintvalue(v); | |
| 331 } | |
| 332 return 1; | |
| 333 } | |
| 334 | |
| 335 int | |
| 336 getshorttuplearg(args, a, n) | |
| 337 object *args; | |
| 338 short *a; /* [n] */ | |
| 339 int n; | |
| 340 { | |
| 341 int i; | |
| 342 if (!is_tupleobject(args) || gettuplesize(args) != n) { | |
| 343 return err_badarg(); | |
| 344 } | |
| 345 for (i = 0; i < n; i++) { | |
| 346 object *v = gettupleitem(args, i); | |
| 347 if (!is_intobject(v)) { | |
| 348 return err_badarg(); | |
| 349 } | |
| 350 a[i] = getintvalue(v); | |
| 351 } | |
| 352 return 1; | |
| 353 } | |
| 354 | |
| 355 int | |
| 356 getlonglistarg(args, a, n) | |
| 357 object *args; | |
| 358 long *a; /* [n] */ | |
| 359 int n; | |
| 360 { | |
| 361 int i; | |
| 362 if (!is_listobject(args) || getlistsize(args) != n) { | |
| 363 return err_badarg(); | |
| 364 } | |
| 365 for (i = 0; i < n; i++) { | |
| 366 object *v = getlistitem(args, i); | |
| 367 if (!is_intobject(v)) { | |
| 368 return err_badarg(); | |
| 369 } | |
| 370 a[i] = getintvalue(v); | |
| 371 } | |
| 372 return 1; | |
| 373 } | |
| 374 | |
| 375 int | |
| 376 getshortlistarg(args, a, n) | |
| 377 object *args; | |
| 378 short *a; /* [n] */ | |
| 379 int n; | |
| 380 { | |
| 381 int i; | |
| 382 if (!is_listobject(args) || getlistsize(args) != n) { | |
| 383 return err_badarg(); | |
| 384 } | |
| 385 for (i = 0; i < n; i++) { | |
| 386 object *v = getlistitem(args, i); | |
| 387 if (!is_intobject(v)) { | |
| 388 return err_badarg(); | |
| 389 } | |
| 390 a[i] = getintvalue(v); | |
| 391 } | |
| 392 return 1; | |
| 393 } | |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
394 |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
395 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
396 /* Generic function to create a value -- the inverse of getargs() */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
397 /* After an original idea and first implementation by Steven Miale */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
398 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
399 static object *do_mktuple PROTO((char**, va_list *, int, int)); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
400 static object *do_mkvalue PROTO((char**, va_list *)); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
401 |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
402 static object * |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
403 do_mktuple(p_format, p_va, endchar, n) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
404 char **p_format; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
405 va_list *p_va; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
406 int endchar; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
407 int n; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
408 { |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
409 object *v; |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
410 int i; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
411 if (n < 0) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
412 return NULL; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
413 if ((v = newtupleobject(n)) == NULL) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
414 return NULL; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
415 for (i = 0; i < n; i++) { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
416 object *w = do_mkvalue(p_format, p_va); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
417 if (w == NULL) { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
418 DECREF(v); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
419 return NULL; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
420 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
421 settupleitem(v, i, w); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
422 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
423 if (v != NULL && **p_format != endchar) { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
424 DECREF(v); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
425 v = NULL; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
426 err_setstr(SystemError, "Unmatched paren in format"); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
427 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
428 else if (endchar) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
429 ++*p_format; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
430 return v; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
431 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
432 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
433 static object * |
|
860
39134c99c923
Added parameter decls to do_mkvalue for non-template machines
Guido van Rossum <guido@python.org>
parents:
817
diff
changeset
|
434 do_mkvalue(p_format, p_va) |
|
39134c99c923
Added parameter decls to do_mkvalue for non-template machines
Guido van Rossum <guido@python.org>
parents:
817
diff
changeset
|
435 char **p_format; |
|
39134c99c923
Added parameter decls to do_mkvalue for non-template machines
Guido van Rossum <guido@python.org>
parents:
817
diff
changeset
|
436 va_list *p_va; |
|
39134c99c923
Added parameter decls to do_mkvalue for non-template machines
Guido van Rossum <guido@python.org>
parents:
817
diff
changeset
|
437 { |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
438 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
439 switch (*(*p_format)++) { |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
440 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
441 case '(': |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
442 return do_mktuple(p_format, p_va, ')', |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
443 countformat(*p_format, ')')); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
444 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
445 case 'b': |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
446 case 'h': |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
447 case 'i': |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
448 return newintobject((long)va_arg(*p_va, int)); |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
449 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
450 case 'l': |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
451 return newintobject((long)va_arg(*p_va, long)); |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
452 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
453 case 'f': |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
454 case 'd': |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
455 return newfloatobject((double)va_arg(*p_va, double)); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
456 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
457 case 'c': |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
458 { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
459 char p[1]; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
460 p[0] = va_arg(*p_va, int); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
461 return newsizedstringobject(p, 1); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
462 } |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
463 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
464 case 's': |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
465 case 'z': |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
466 { |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
467 object *v; |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
468 char *str = va_arg(*p_va, char *); |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
469 int n; |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
470 if (**p_format == '#') { |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
471 ++*p_format; |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
472 n = va_arg(*p_va, int); |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
473 } |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
474 else |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
475 n = -1; |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
476 if (str == NULL) { |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
477 v = None; |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
478 INCREF(v); |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
479 } |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
480 else { |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
481 if (n < 0) |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
482 n = strlen(str); |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
483 v = newsizedstringobject(str, n); |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
484 } |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
485 return v; |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
486 } |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
487 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
488 case 'S': |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
489 case 'O': |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
490 { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
491 object *v; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
492 v = va_arg(*p_va, object *); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
493 if (v != NULL) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
494 INCREF(v); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
495 else if (!err_occurred()) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
496 /* If a NULL was passed because a call |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
497 that should have constructed a value |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
498 failed, that's OK, and we pass the error |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
499 on; but if no error occurred it's not |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
500 clear that the caller knew what she |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
501 was doing. */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
502 err_setstr(SystemError, |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
503 "NULL object passed to mkvalue"); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
504 return v; |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
505 } |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
506 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
507 default: |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
508 err_setstr(SystemError, "bad format char passed to mkvalue"); |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
509 return NULL; |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
510 |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
511 } |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
512 } |
|
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
513 |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
514 #ifdef USE_STDARG |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
515 /* VARARGS 2 */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
516 object *mkvalue(char *format, ...) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
517 #else |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
518 /* VARARGS */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
519 object *mkvalue(va_alist) va_dcl |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
520 #endif |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
521 { |
|
817
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
522 int n; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
523 char *f; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
524 va_list va; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
525 object* retval; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
526 #ifdef USE_STDARG |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
527 va_start(va, format); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
528 #else |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
529 char *format; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
530 |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
531 va_start(va); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
532 format = va_arg(va, char *); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
533 #endif |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
534 f = format; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
535 n = countformat(f, '\0'); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
536 if (n < 0) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
537 retval = NULL; /* Error in the format */ |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
538 else if (n == 0) { |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
539 retval = None; |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
540 INCREF(retval); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
541 } |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
542 else if (n == 1) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
543 retval = do_mkvalue(&f, &va); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
544 else |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
545 retval = do_mktuple(&f, &va, '\0', n); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
546 va_end(va); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
547 if (retval == NULL) |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
548 fprintf(stderr, "format \"%s\", f \"%s\"\n", format, f); |
|
aa633f0defa6
Merged with Steve Miale's version
Guido van Rossum <guido@python.org>
parents:
816
diff
changeset
|
549 return retval; |
|
816
e4a27d84ea36
Added mkvalue() function
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
550 } |
