From c60041175547df61e67ff167bd5ad18f7967795d Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 5 Nov 1993 10:22:19 +0000 Subject: * mpzmodule.c: removed redundant mpz_print function. * object.[ch], bltinmodule.c, fileobject.c: changed str() to call strobject() which calls an object's __str__ method if it has one. strobject() is also called by writeobject() when PRINT_RAW is passed. * ceval.c: rationalize code for PRINT_ITEM (no change in function!) * funcobject.c, codeobject.c: added compare and hash functionality. Functions with identical code objects and the same global dictionary are equal. Code objects are equal when their code, constants list and names list are identical (i.e. the filename and code name don't count). (hash doesn't work yet since the constants are in a list and lists can't be hashed -- suppose this should really be done with a tuple now we have resizetuple!) --- Include/object.h | 1 + Modules/mpzmodule.c | 20 +------------------- Objects/fileobject.c | 15 ++++++--------- Objects/object.c | 36 +++++++++++++++++++++++++++++++++++- Python/bltinmodule.c | 7 +------ Python/ceval.c | 13 +++++++------ 6 files changed, 51 insertions(+), 41 deletions(-) diff --git a/Include/object.h b/Include/object.h index ab270f8..24989bf 100644 --- a/Include/object.h +++ b/Include/object.h @@ -214,6 +214,7 @@ extern typeobject Typetype; /* The type of type objects */ /* Generic operations on objects */ extern int printobject PROTO((object *, FILE *, int)); extern object * reprobject PROTO((object *)); +extern object * strobject PROTO((object *)); extern int cmpobject PROTO((object *, object *)); extern object *getattr PROTO((object *, char *)); extern int hasattr PROTO((object *, char *)); diff --git a/Modules/mpzmodule.c b/Modules/mpzmodule.c index a3e903e..5ff0801 100644 --- a/Modules/mpzmodule.c +++ b/Modules/mpzmodule.c @@ -265,24 +265,6 @@ mpz_dealloc(mpzp) DEL(mpzp); } /* mpz_dealloc() */ -/* ARGSUSED */ -static int -mpz_print(v, fp, flags) - object *v; - FILE *fp; - int flags; /* Not used but required by interface */ -{ - stringobject *str - = (stringobject *)mpz_format(v, 10, (unsigned char)1); - - if (str == NULL) - return -1; - - fputs(GETSTRINGVALUE(str), fp); - DECREF(str); - return 0; -} /* mpz_print() */ - /* pointers to frequently used values 0, 1 and -1 */ static mpzobject *mpz_value_zero, *mpz_value_one, *mpz_value_mone; @@ -1658,7 +1640,7 @@ static typeobject MPZtype = { 0, /*tp_itemsize*/ /* methods */ mpz_dealloc, /*tp_dealloc*/ - mpz_print, /*tp_print*/ + 0, /*tp_print*/ mpz_getattr, /*tp_getattr*/ 0, /*tp_setattr*/ mpz_compare, /*tp_compare*/ diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 7ed4fcd..518fe04 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -680,16 +680,13 @@ writeobject(v, f, flags) writer = getattr(f, "write"); if (writer == NULL) return -1; - if ((flags & PRINT_RAW) && is_stringobject(v)) { - value = v; - INCREF(value); - } - else { + if (flags & PRINT_RAW) + value = strobject(v); + else value = reprobject(v); - if (value == NULL) { - DECREF(writer); - return -1; - } + if (value == NULL) { + DECREF(writer); + return -1; } result = call_object(writer, value); DECREF(writer); diff --git a/Objects/object.c b/Objects/object.c index 73fba50..7a7383e 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -124,7 +124,11 @@ printobject(op, fp, flags) op->ob_type->tp_name, (long)op); } else { - object *s = reprobject(op); + object *s; + if (flags & PRINT_RAW) + s = strobject(op); + else + s = reprobject(op); if (s == NULL) ret = -1; else if (!is_stringobject(s)) { @@ -171,6 +175,36 @@ reprobject(v) return (*v->ob_type->tp_repr)(v); } +object * +strobject(v) + object *v; +{ + if (v == NULL) + return newstringobject(""); + if (is_stringobject(v)) { + INCREF(v); + return v; + } + else { + object *func = getattr(v, "__str__"); + object *args; + object *res; + if (func == NULL) { + err_clear(); + return reprobject(v); + } + args = newtupleobject(0); + if (args == NULL) + res = NULL; + else { + res = call_object(func, args); + DECREF(args); + } + DECREF(func); + return res; + } +} + int cmpobject(v, w) object *v, *w; diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 9210bd1..97ed2f4 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1096,12 +1096,7 @@ builtin_str(self, v) err_badarg(); return NULL; } - if (is_stringobject(v)) { - INCREF(v); - return v; - } - else - return reprobject(v); + return strobject(v); } static object * diff --git a/Python/ceval.c b/Python/ceval.c index 324ecdf..425e2a0 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -38,6 +38,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "graminit.h" #include "pythonrun.h" +#include + /* Turn this on if your compiler chokes on the big switch: */ /* #define CASE_TOO_BIG 1 /**/ @@ -660,16 +662,15 @@ eval_code(co, globals, locals, owner, arg) w = sysget("stdout"); if (softspace(w, 1)) writestring(" ", w); - if (is_stringobject(v)) { + err = writeobject(v, w, PRINT_RAW); + if (err == 0 && is_stringobject(v)) { + /* XXX move into writeobject() ? */ char *s = getstringvalue(v); int len = getstringsize(v); - err = writeobject(v, w, PRINT_RAW); - if (err == 0 && len > 0 && s[len-1] == '\n') + if (len > 0 && isspace(s[len-1]) && + s[len-1] != ' ') softspace(w, 0); } - else { - err = writeobject(v, w, 0); - } DECREF(v); break; -- cgit v0.12 /a> 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
/* -*- Mode: C; c-file-style: "python" -*- */

#include <Python.h>
#include <locale.h>

/* ascii character tests (as opposed to locale tests) */
#define ISSPACE(c)  ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
                     (c) == '\r' || (c) == '\t' || (c) == '\v')
#define ISDIGIT(c)  ((c) >= '0' && (c) <= '9')
#define ISXDIGIT(c) (ISDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))


/**
 * PyOS_ascii_strtod:
 * @nptr:    the string to convert to a numeric value.
 * @endptr:  if non-%NULL, it returns the character after
 *           the last character used in the conversion.
 * 
 * Converts a string to a #gdouble value.
 * This function behaves like the standard strtod() function
 * does in the C locale. It does this without actually
 * changing the current locale, since that would not be
 * thread-safe.
 *
 * This function is typically used when reading configuration
 * files or other non-user input that should be locale independent.
 * To handle input from the user you should normally use the
 * locale-sensitive system strtod() function.
 *
 * If the correct value would cause overflow, plus or minus %HUGE_VAL
 * is returned (according to the sign of the value), and %ERANGE is
 * stored in %errno. If the correct value would cause underflow,
 * zero is returned and %ERANGE is stored in %errno.
 * If memory allocation fails, %ENOMEM is stored in %errno.
 * 
 * This function resets %errno before calling strtod() so that
 * you can reliably detect overflow and underflow.
 *
 * Return value: the #gdouble value.
 **/
double
PyOS_ascii_strtod(const char *nptr, char **endptr)
{
	char *fail_pos;
	double val = -1.0;
	struct lconv *locale_data;
	const char *decimal_point;
	size_t decimal_point_len;
	const char *p, *decimal_point_pos;
	const char *end = NULL; /* Silence gcc */

	assert(nptr != NULL);

	fail_pos = NULL;

	locale_data = localeconv();
	decimal_point = locale_data->decimal_point;
	decimal_point_len = strlen(decimal_point);

	assert(decimal_point_len != 0);

	decimal_point_pos = NULL;
	if (decimal_point[0] != '.' || 
	    decimal_point[1] != 0)
	{
		p = nptr;
		  /* Skip leading space */
		while (ISSPACE(*p))
			p++;

		  /* Skip leading optional sign */
		if (*p == '+' || *p == '-')
			p++;

		while (ISDIGIT(*p))
			p++;

		if (*p == '.')
		{
			decimal_point_pos = p++;

			while (ISDIGIT(*p))
				p++;

			if (*p == 'e' || *p == 'E')
				p++;
			if (*p == '+' || *p == '-')
				p++;
			while (ISDIGIT(*p))
				p++;
			end = p;
		}
		else if (strncmp(p, decimal_point, decimal_point_len) == 0)
		{
			/* Python bug #1417699 */
			*endptr = (char*)nptr;
			errno = EINVAL;
			return val;
		}
		/* For the other cases, we need not convert the decimal point */
	}

	/* Set errno to zero, so that we can distinguish zero results
	   and underflows */
	errno = 0;

	if (decimal_point_pos)
	{
		char *copy, *c;

		/* We need to convert the '.' to the locale specific decimal point */
		copy = (char *)PyMem_MALLOC(end - nptr + 1 + decimal_point_len);
		if (copy == NULL) {
			if (endptr)
				*endptr = (char *)nptr;
			errno = ENOMEM;
			return val;
		}

		c = copy;
		memcpy(c, nptr, decimal_point_pos - nptr);
		c += decimal_point_pos - nptr;
		memcpy(c, decimal_point, decimal_point_len);
		c += decimal_point_len;
		memcpy(c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
		c += end - (decimal_point_pos + 1);
		*c = 0;

		val = strtod(copy, &fail_pos);

		if (fail_pos)
		{
			if (fail_pos > decimal_point_pos)
				fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
			else
				fail_pos = (char *)nptr + (fail_pos - copy);
		}

		PyMem_FREE(copy);

	}
	else {
		unsigned i = 0;
		if (nptr[i] == '-')
			i++;
		if (nptr[i] == '0' && (nptr[i+1] == 'x' || nptr[i+1] == 'X'))
			fail_pos = (char*)nptr;
		else
			val = strtod(nptr, &fail_pos);
	}

	if (endptr)
		*endptr = fail_pos;

	return val;
}


/**
 * PyOS_ascii_formatd:
 * @buffer: A buffer to place the resulting string in
 * @buf_len: The length of the buffer.
 * @format: The printf()-style format to use for the
 *          code to use for converting. 
 * @d: The #gdouble to convert
 *
 * Converts a #gdouble to a string, using the '.' as
 * decimal point. To format the number you pass in
 * a printf()-style format string. Allowed conversion
 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'. 
 * 
 * Return value: The pointer to the buffer with the converted string.
 **/
char *
PyOS_ascii_formatd(char       *buffer, 
		   size_t      buf_len, 
		   const char *format, 
		   double      d)
{
	struct lconv *locale_data;
	const char *decimal_point;
	size_t decimal_point_len, rest_len;
	char *p;
	char format_char;

/* 	g_return_val_if_fail (buffer != NULL, NULL); */
/* 	g_return_val_if_fail (format[0] == '%', NULL); */
/* 	g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL); */

	format_char = format[strlen(format) - 1];

/* 	g_return_val_if_fail (format_char == 'e' || format_char == 'E' || */
/* 			      format_char == 'f' || format_char == 'F' || */
/* 			      format_char == 'g' || format_char == 'G', */
/* 			      NULL); */

	if (format[0] != '%')
		return NULL;

	if (strpbrk(format + 1, "'l%"))
		return NULL;

	if (!(format_char == 'e' || format_char == 'E' || 
	      format_char == 'f' || format_char == 'F' || 
	      format_char == 'g' || format_char == 'G'))
		return NULL;


	PyOS_snprintf(buffer, buf_len, format, d);

	locale_data = localeconv();
	decimal_point = locale_data->decimal_point;
	decimal_point_len = strlen(decimal_point);

	assert(decimal_point_len != 0);

	if (decimal_point[0] != '.' || 
	    decimal_point[1] != 0)
	{
		p = buffer;

		if (*p == '+' || *p == '-')
			p++;

		while (isdigit((unsigned char)*p))
			p++;

		if (strncmp(p, decimal_point, decimal_point_len) == 0)
		{
			*p = '.';
			p++;
			if (decimal_point_len > 1) {
				rest_len = strlen(p + (decimal_point_len - 1));
				memmove(p, p + (decimal_point_len - 1), 
					rest_len);
				p[rest_len] = 0;
			}
		}
	}

	return buffer;
}

double
PyOS_ascii_atof(const char *nptr)
{
	return PyOS_ascii_strtod(nptr, NULL);
}