annotate Objects/stringobject.c @ 81933:4d120cea3507 2.7

Issue #17034: Use Py_CLEAR() in stringobject.c.
author Serhiy Storchaka <storchaka@gmail.com>
date Sat, 02 Feb 2013 18:43:58 +0200
parents 5970c90dd8d1
children 391e3a7db1a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1 /* String (str/bytes) object implementation */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37678
diff changeset
3 #define PY_SSIZE_T_CLEAN
38376
a16948e9915e needforspeed: use Py_LOCAL on a few more locals in stringobject.c
Fredrik Lundh <fredrik@pythonware.com>
parents: 38372
diff changeset
4
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
5 #include "Python.h"
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
6 #include <ctype.h>
50036
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
7 #include <stddef.h>
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
8
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
9 #ifdef COUNT_ALLOCS
50627
f0e0c3536e22 Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 50036
diff changeset
10 Py_ssize_t null_strings, one_strings;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
11 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
12
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
13 static PyStringObject *characters[UCHAR_MAX + 1];
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
14 static PyStringObject *nullstring;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
15
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
16 /* This dictionary holds all interned strings. Note that references to
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
17 strings in this dictionary are *not* counted in the string's ob_refcnt.
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
18 When the interned string reaches a refcnt of 0 the string deallocation
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
19 function will delete the reference from this dictionary.
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
20
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
21 Another way to look at this is that to say that the actual reference
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
22 count of a string is: s->ob_refcnt + (s->ob_sstate?2:0)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
23 */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
24 static PyObject *interned;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
25
50036
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
26 /* PyStringObject_SIZE gives the basic size of a string; any memory allocation
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
27 for a string of length n should request PyStringObject_SIZE + n bytes.
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
28
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
29 Using PyStringObject_SIZE instead of sizeof(PyStringObject) saves
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
30 3 bytes per string allocation on a typical system.
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
31 */
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
32 #define PyStringObject_SIZE (offsetof(PyStringObject, ob_sval) + 1)
1f2a3b13e86d Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents: 49431
diff changeset
33
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
34 /*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
35 For PyString_FromString(), the parameter `str' points to a null-terminated
21737
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
36 string containing exactly `size' bytes.
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
37
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
38 For PyString_FromStringAndSize(), the parameter the parameter `str' is
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
39 either NULL or else points to a string containing at least `size' bytes.
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
40 For PyString_FromStringAndSize(), the string in the `str' parameter does
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
41 not have to be null-terminated. (Therefore it is safe to construct a
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
42 substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
43 If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1'
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
44 bytes (setting the last byte to the null terminating character) and you can
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
45 fill in the data yourself. If `str' is non-NULL then the resulting
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
46 PyString object must be treated as immutable and you must not fill in nor
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
47 alter the data yourself, since the strings may be shared.
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
48
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
49 The PyObject member `op->ob_size', which denotes the number of "extra
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
50 items" in a variable-size object, will contain the number of bytes
68907
44749e501982 Issue #11634: Remove misleading paragraph from a comment
Eli Bendersky <eliben@gmail.com>
parents: 68503
diff changeset
51 allocated for string data, not counting the null terminating character.
44749e501982 Issue #11634: Remove misleading paragraph from a comment
Eli Bendersky <eliben@gmail.com>
parents: 68503
diff changeset
52 It is therefore equal to the `size' parameter (for
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
53 PyString_FromStringAndSize()) or the length of the string in the `str'
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
54 parameter (for PyString_FromString()).
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
55 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
56 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
57 PyString_FromStringAndSize(const char *str, Py_ssize_t size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
59 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
60 if (size < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
61 PyErr_SetString(PyExc_SystemError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
62 "Negative size passed to PyString_FromStringAndSize");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
63 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
64 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
65 if (size == 0 && (op = nullstring) != NULL) {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
66 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
67 null_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
68 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
69 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
70 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
71 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
72 if (size == 1 && str != NULL &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
73 (op = characters[*str & UCHAR_MAX]) != NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
74 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
75 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
76 one_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
77 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
78 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
79 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
80 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
81
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
82 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
83 PyErr_SetString(PyExc_OverflowError, "string is too large");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
84 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
85 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
86
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
87 /* Inline PyObject_NewVar */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
88 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
89 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
90 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
91 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
92 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
93 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
94 if (str != NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
95 Py_MEMCPY(op->ob_sval, str, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
96 op->ob_sval[size] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
97 /* share short strings */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
98 if (size == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
99 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
100 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
101 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
102 nullstring = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
103 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
104 } else if (size == 1 && str != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
105 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
106 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
107 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
108 characters[*str & UCHAR_MAX] = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
109 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
110 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
111 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
114 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
115 PyString_FromString(const char *str)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
117 register size_t size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
118 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
119
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
120 assert(str != NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
121 size = strlen(str);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
122 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
123 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
124 "string is too long for a Python string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
125 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
126 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
127 if (size == 0 && (op = nullstring) != NULL) {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
128 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
129 null_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
130 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
131 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
132 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
133 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
134 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
135 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
136 one_strings++;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
137 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
138 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
139 return (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
140 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
141
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
142 /* Inline PyObject_NewVar */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
143 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
144 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
145 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
146 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
147 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
148 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
149 Py_MEMCPY(op->ob_sval, str, size+1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
150 /* share short strings */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
151 if (size == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
152 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
153 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
154 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
155 nullstring = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
156 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
157 } else if (size == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
158 PyObject *t = (PyObject *)op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
159 PyString_InternInPlace(&t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
160 op = (PyStringObject *)t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
161 characters[*str & UCHAR_MAX] = op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
162 Py_INCREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
163 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
164 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
167 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
168 PyString_FromFormatV(const char *format, va_list vargs)
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
169 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
170 va_list count;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
171 Py_ssize_t n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
172 const char* f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
173 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
174 PyObject* string;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
175
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
176 #ifdef VA_LIST_IS_ARRAY
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
177 Py_MEMCPY(count, vargs, sizeof(va_list));
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
178 #else
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
179 #ifdef __va_copy
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
180 __va_copy(count, vargs);
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
181 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
182 count = vargs;
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
183 #endif
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
184 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
185 /* step 1: figure out how large a buffer we need */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
186 for (f = format; *f; f++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
187 if (*f == '%') {
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
188 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
189 int longlongflag = 0;
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
190 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
191 const char* p = f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
192 while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
193 ;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
194
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
195 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
196 * they don't affect the amount of space we reserve.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
197 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
198 if (*f == 'l') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
199 if (f[1] == 'd' || f[1] == 'u') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
200 ++f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
201 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
202 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
203 else if (f[1] == 'l' &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
204 (f[2] == 'd' || f[2] == 'u')) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
205 longlongflag = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
206 f += 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
207 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
208 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
210 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
211 ++f;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
212 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
213
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
214 switch (*f) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
215 case 'c':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
216 (void)va_arg(count, int);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
217 /* fall through... */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
218 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
219 n++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
220 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
221 case 'd': case 'u': case 'i': case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
222 (void) va_arg(count, int);
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
223 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
224 /* Need at most
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
225 ceil(log10(256)*SIZEOF_LONG_LONG) digits,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
226 plus 1 for the sign. 53/22 is an upper
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
227 bound for log10(256). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
228 if (longlongflag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
229 n += 2 + (SIZEOF_LONG_LONG*53-1) / 22;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
230 else
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
231 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
232 /* 20 bytes is enough to hold a 64-bit
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
233 integer. Decimal takes the most
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
234 space. This isn't enough for
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
235 octal. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
236 n += 20;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
237
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
238 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
239 case 's':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
240 s = va_arg(count, char*);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
241 n += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
242 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
243 case 'p':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
244 (void) va_arg(count, int);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
245 /* maximum 64-bit pointer representation:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
246 * 0xffffffffffffffff
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
247 * so 19 characters is enough.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
248 * XXX I count 18 -- what's the extra for?
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
249 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
250 n += 19;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
251 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
252 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
253 /* if we stumble upon an unknown
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
254 formatting code, copy the rest of
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
255 the format string to the output
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
256 string. (we cannot just skip the
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
257 code, since there's no way to know
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
258 what's in the argument list) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
259 n += strlen(p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
260 goto expand;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
261 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
262 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
263 n++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
264 }
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
265 expand:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
266 /* step 2: fill the buffer */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
267 /* Since we've analyzed how much space we need for the worst case,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
268 use sprintf directly instead of the slower PyOS_snprintf. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
269 string = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
270 if (!string)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
271 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
272
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
273 s = PyString_AsString(string);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
274
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
275 for (f = format; *f; f++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
276 if (*f == '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
277 const char* p = f++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
278 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
279 int longflag = 0;
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
280 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
281 int longlongflag = 0;
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
282 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
283 int size_tflag = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
284 /* parse the width.precision part (we're only
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
285 interested in the precision value, if any) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
286 n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
287 while (isdigit(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
288 n = (n*10) + *f++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
289 if (*f == '.') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
290 f++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
291 n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
292 while (isdigit(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
293 n = (n*10) + *f++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
294 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
295 while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
296 f++;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
297 /* Handle %ld, %lu, %lld and %llu. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
298 if (*f == 'l') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
299 if (f[1] == 'd' || f[1] == 'u') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
300 longflag = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
301 ++f;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
302 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
303 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
304 else if (f[1] == 'l' &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
305 (f[2] == 'd' || f[2] == 'u')) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
306 longlongflag = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
307 f += 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
308 }
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
309 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
310 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
311 /* handle the size_t flag. */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
312 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
313 size_tflag = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
314 ++f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
315 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
316
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
317 switch (*f) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
318 case 'c':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
319 *s++ = va_arg(vargs, int);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
320 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
321 case 'd':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
322 if (longflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
323 sprintf(s, "%ld", va_arg(vargs, long));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
324 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
325 else if (longlongflag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
326 sprintf(s, "%" PY_FORMAT_LONG_LONG "d",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
327 va_arg(vargs, PY_LONG_LONG));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
328 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
329 else if (size_tflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
330 sprintf(s, "%" PY_FORMAT_SIZE_T "d",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
331 va_arg(vargs, Py_ssize_t));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
332 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
333 sprintf(s, "%d", va_arg(vargs, int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
334 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
335 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
336 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
337 if (longflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
338 sprintf(s, "%lu",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
339 va_arg(vargs, unsigned long));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
340 #ifdef HAVE_LONG_LONG
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
341 else if (longlongflag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
342 sprintf(s, "%" PY_FORMAT_LONG_LONG "u",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
343 va_arg(vargs, PY_LONG_LONG));
57277
51028709abc3 Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents: 56758
diff changeset
344 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
345 else if (size_tflag)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
346 sprintf(s, "%" PY_FORMAT_SIZE_T "u",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
347 va_arg(vargs, size_t));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
348 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
349 sprintf(s, "%u",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
350 va_arg(vargs, unsigned int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
351 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
352 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
353 case 'i':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
354 sprintf(s, "%i", va_arg(vargs, int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
355 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
356 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
357 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
358 sprintf(s, "%x", va_arg(vargs, int));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
359 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
360 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
361 case 's':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
362 p = va_arg(vargs, char*);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
363 i = strlen(p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
364 if (n > 0 && i > n)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
365 i = n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
366 Py_MEMCPY(s, p, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
367 s += i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
368 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
369 case 'p':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
370 sprintf(s, "%p", va_arg(vargs, void*));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
371 /* %p is ill-defined: ensure leading 0x. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
372 if (s[1] == 'X')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
373 s[1] = 'x';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
374 else if (s[1] != 'x') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
375 memmove(s+2, s, strlen(s)+1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
376 s[0] = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
377 s[1] = 'x';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
378 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
379 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
380 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
381 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
382 *s++ = '%';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
383 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
384 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
385 strcpy(s, p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
386 s += strlen(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
387 goto end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
388 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
389 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
390 *s++ = *f;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
391 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
392
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
393 end:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
394 if (_PyString_Resize(&string, s - PyString_AS_STRING(string)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
395 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
396 return string;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
397 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
398
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
399 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
400 PyString_FromFormat(const char *format, ...)
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
401 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
402 PyObject* ret;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
403 va_list vargs;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
404
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
405 #ifdef HAVE_STDARG_PROTOTYPES
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
406 va_start(vargs, format);
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
407 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
408 va_start(vargs);
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
409 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
410 ret = PyString_FromFormatV(format, vargs);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
411 va_end(vargs);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
412 return ret;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
413 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
414
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
415
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
416 PyObject *PyString_Decode(const char *s,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
417 Py_ssize_t size,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
418 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
419 const char *errors)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
420 {
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
421 PyObject *v, *str;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
422
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
423 str = PyString_FromStringAndSize(s, size);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
424 if (str == NULL)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
425 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
426 v = PyString_AsDecodedString(str, encoding, errors);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
427 Py_DECREF(str);
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
428 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
429 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
430
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
431 PyObject *PyString_AsDecodedObject(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
432 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
433 const char *errors)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
434 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
435 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
436
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
437 if (!PyString_Check(str)) {
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
438 PyErr_BadArgument();
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
439 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
440 }
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
441
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
442 if (encoding == NULL) {
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
443 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
444 encoding = PyUnicode_GetDefaultEncoding();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
445 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
446 PyErr_SetString(PyExc_ValueError, "no encoding specified");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
447 goto onError;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
448 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
449 }
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
450
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
451 /* Decode via the codec registry */
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
452 v = PyCodec_Decode(str, encoding, errors);
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
453 if (v == NULL)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
454 goto onError;
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
455
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
456 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
457
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
458 onError:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
459 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
460 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
461
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
462 PyObject *PyString_AsDecodedString(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
463 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
464 const char *errors)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
465 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
466 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
467
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
468 v = PyString_AsDecodedObject(str, encoding, errors);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
469 if (v == NULL)
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
470 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
471
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
472 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
473 /* Convert Unicode to a string using the default encoding */
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
474 if (PyUnicode_Check(v)) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
475 PyObject *temp = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
476 v = PyUnicode_AsEncodedString(v, NULL, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
477 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
478 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
479 goto onError;
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
480 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
481 #endif
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
482 if (!PyString_Check(v)) {
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
483 PyErr_Format(PyExc_TypeError,
13561
ea732719b3fd Fix typo in error message
Andrew M. Kuchling <amk@amk.ca>
parents: 13544
diff changeset
484 "decoder did not return a string object (type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
485 Py_TYPE(v)->tp_name);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
486 Py_DECREF(v);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
487 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
488 }
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
489
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
490 return v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
491
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
492 onError:
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
493 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
494 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
495
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
496 PyObject *PyString_Encode(const char *s,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
497 Py_ssize_t size,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
498 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
499 const char *errors)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
500 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
501 PyObject *v, *str;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
502
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
503 str = PyString_FromStringAndSize(s, size);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
504 if (str == NULL)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
505 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
506 v = PyString_AsEncodedString(str, encoding, errors);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
507 Py_DECREF(str);
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
508 return v;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
509 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
510
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
511 PyObject *PyString_AsEncodedObject(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
512 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
513 const char *errors)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
514 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
515 PyObject *v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
516
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
517 if (!PyString_Check(str)) {
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
518 PyErr_BadArgument();
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
519 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
520 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
521
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
522 if (encoding == NULL) {
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
523 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
524 encoding = PyUnicode_GetDefaultEncoding();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
525 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
526 PyErr_SetString(PyExc_ValueError, "no encoding specified");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
527 goto onError;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
528 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
529 }
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
530
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
531 /* Encode via the codec registry */
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
532 v = PyCodec_Encode(str, encoding, errors);
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
533 if (v == NULL)
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
534 goto onError;
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
535
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
536 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
537
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
538 onError:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
539 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
540 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
541
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
542 PyObject *PyString_AsEncodedString(PyObject *str,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
543 const char *encoding,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
544 const char *errors)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
545 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
546 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
547
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
548 v = PyString_AsEncodedObject(str, encoding, errors);
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
549 if (v == NULL)
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
550 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
551
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
552 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
553 /* Convert Unicode to a string using the default encoding */
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
554 if (PyUnicode_Check(v)) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
555 PyObject *temp = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
556 v = PyUnicode_AsEncodedString(v, NULL, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
557 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
558 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
559 goto onError;
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
560 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
561 #endif
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
562 if (!PyString_Check(v)) {
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
563 PyErr_Format(PyExc_TypeError,
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
564 "encoder did not return a string object (type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
565 Py_TYPE(v)->tp_name);
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
566 Py_DECREF(v);
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
567 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
568 }
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
569
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
570 return v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
571
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
572 onError:
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
573 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
574 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
575
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
576 static void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
577 string_dealloc(PyObject *op)
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
578 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
579 switch (PyString_CHECK_INTERNED(op)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
580 case SSTATE_NOT_INTERNED:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
581 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
582
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
583 case SSTATE_INTERNED_MORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
584 /* revive dead object temporarily for DelItem */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
585 Py_REFCNT(op) = 3;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
586 if (PyDict_DelItem(interned, op) != 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
587 Py_FatalError(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
588 "deletion of interned string failed");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
589 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
590
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
591 case SSTATE_INTERNED_IMMORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
592 Py_FatalError("Immortal interned string died.");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
593
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
594 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
595 Py_FatalError("Inconsistent interned string state.");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
596 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
597 Py_TYPE(op)->tp_free(op);
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
598 }
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
599
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
600 /* Unescape a backslash-escaped string. If unicode is non-zero,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
601 the string is a u-literal. If recode_encoding is non-zero,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
602 the string is UTF-8 encoded and should be re-encoded in the
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
603 specified encoding. */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
604
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
605 PyObject *PyString_DecodeEscape(const char *s,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
606 Py_ssize_t len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
607 const char *errors,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
608 Py_ssize_t unicode,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
609 const char *recode_encoding)
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
610 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
611 int c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
612 char *p, *buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
613 const char *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
614 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
615 Py_ssize_t newlen = recode_encoding ? 4*len:len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
616 v = PyString_FromStringAndSize((char *)NULL, newlen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
617 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
618 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
619 p = buf = PyString_AsString(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
620 end = s + len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
621 while (s < end) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
622 if (*s != '\\') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
623 non_esc:
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
624 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
625 if (recode_encoding && (*s & 0x80)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
626 PyObject *u, *w;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
627 char *r;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
628 const char* t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
629 Py_ssize_t rn;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
630 t = s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
631 /* Decode non-ASCII bytes as UTF-8. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
632 while (t < end && (*t & 0x80)) t++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
633 u = PyUnicode_DecodeUTF8(s, t - s, errors);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
634 if(!u) goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
635
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
636 /* Recode them in target encoding. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
637 w = PyUnicode_AsEncodedString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
638 u, recode_encoding, errors);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
639 Py_DECREF(u);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
640 if (!w) goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
641
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
642 /* Append bytes to output buffer. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
643 assert(PyString_Check(w));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
644 r = PyString_AS_STRING(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
645 rn = PyString_GET_SIZE(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
646 Py_MEMCPY(p, r, rn);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
647 p += rn;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
648 Py_DECREF(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
649 s = t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
650 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
651 *p++ = *s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
652 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
653 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
654 *p++ = *s++;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
655 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
656 continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
657 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
658 s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
659 if (s==end) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
660 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
661 "Trailing \\ in string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
662 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
663 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
664 switch (*s++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
665 /* XXX This assumes ASCII! */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
666 case '\n': break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
667 case '\\': *p++ = '\\'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
668 case '\'': *p++ = '\''; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
669 case '\"': *p++ = '\"'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
670 case 'b': *p++ = '\b'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
671 case 'f': *p++ = '\014'; break; /* FF */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
672 case 't': *p++ = '\t'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
673 case 'n': *p++ = '\n'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
674 case 'r': *p++ = '\r'; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
675 case 'v': *p++ = '\013'; break; /* VT */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
676 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
677 case '0': case '1': case '2': case '3':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
678 case '4': case '5': case '6': case '7':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
679 c = s[-1] - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
680 if (s < end && '0' <= *s && *s <= '7') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
681 c = (c<<3) + *s++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
682 if (s < end && '0' <= *s && *s <= '7')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
683 c = (c<<3) + *s++ - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
684 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
685 *p++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
686 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
687 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
688 if (s+1 < end &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
689 isxdigit(Py_CHARMASK(s[0])) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
690 isxdigit(Py_CHARMASK(s[1])))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
691 {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
692 unsigned int x = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
693 c = Py_CHARMASK(*s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
694 s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
695 if (isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
696 x = c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
697 else if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
698 x = 10 + c - 'a';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
699 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
700 x = 10 + c - 'A';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
701 x = x << 4;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
702 c = Py_CHARMASK(*s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
703 s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
704 if (isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
705 x += c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
706 else if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
707 x += 10 + c - 'a';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
708 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
709 x += 10 + c - 'A';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
710 *p++ = x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
711 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
712 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
713 if (!errors || strcmp(errors, "strict") == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
714 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
715 "invalid \\x escape");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
716 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
717 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
718 if (strcmp(errors, "replace") == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
719 *p++ = '?';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
720 } else if (strcmp(errors, "ignore") == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
721 /* do nothing */;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
722 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
723 PyErr_Format(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
724 "decoding error; "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
725 "unknown error handling code: %.400s",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
726 errors);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
727 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
728 }
81737
5970c90dd8d1 Issue #16975: Fix error handling bug in the escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81605
diff changeset
729 /* skip \x */
5970c90dd8d1 Issue #16975: Fix error handling bug in the escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81605
diff changeset
730 if (s < end && isxdigit(Py_CHARMASK(s[0])))
5970c90dd8d1 Issue #16975: Fix error handling bug in the escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81605
diff changeset
731 s++; /* and a hexdigit */
5970c90dd8d1 Issue #16975: Fix error handling bug in the escape-decode decoder.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81605
diff changeset
732 break;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
733 #ifndef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
734 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
735 case 'U':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
736 case 'N':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
737 if (unicode) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
738 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
739 "Unicode escapes not legal "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
740 "when Unicode disabled");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
741 goto failed;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
742 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
743 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
744 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
745 *p++ = '\\';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
746 s--;
68503
ee5350f1d0c2 #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
747 goto non_esc; /* an arbitrary number of unescaped
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
748 UTF-8 bytes may follow. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
749 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
750 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
751 if (p-buf < newlen && _PyString_Resize(&v, p - buf))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
752 goto failed;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
753 return v;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
754 failed:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
755 Py_DECREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
756 return NULL;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
757 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
758
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
759 /* -------------------------------------------------------------------- */
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
760 /* object api */
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
761
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
762 static Py_ssize_t
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
763 string_getsize(register PyObject *op)
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
764 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
765 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
766 Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
767 if (PyString_AsStringAndSize(op, &s, &len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
768 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
769 return len;
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
770 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
771
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
772 static /*const*/ char *
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
773 string_getbuffer(register PyObject *op)
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
774 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
775 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
776 Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
777 if (PyString_AsStringAndSize(op, &s, &len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
778 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
779 return s;
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
780 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
781
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
782 Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
783 PyString_Size(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
785 if (!PyString_Check(op))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
786 return string_getsize(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
787 return Py_SIZE(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
789
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 /*const*/ char *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
791 PyString_AsString(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
793 if (!PyString_Check(op))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
794 return string_getbuffer(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
795 return ((PyStringObject *)op) -> ob_sval;
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
796 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
797
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
798 int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
799 PyString_AsStringAndSize(register PyObject *obj,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
800 register char **s,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
801 register Py_ssize_t *len)
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
802 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
803 if (s == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
804 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
805 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
806 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
807
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
808 if (!PyString_Check(obj)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
809 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
810 if (PyUnicode_Check(obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
811 obj = _PyUnicode_AsDefaultEncodedString(obj, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
812 if (obj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
813 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
814 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
815 else
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
816 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
817 {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
818 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
819 "expected string or Unicode object, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
820 "%.200s found", Py_TYPE(obj)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
821 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
822 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
823 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
824
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
825 *s = PyString_AS_STRING(obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
826 if (len != NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
827 *len = PyString_GET_SIZE(obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
828 else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
829 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
830 "expected string without null bytes");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
831 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
832 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
833 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
835
38303
17eaa8b57e05 needforspeed: use "fastsearch" for count. this results in a 3x speedup
Fredrik Lundh <fredrik@pythonware.com>
parents: 38302
diff changeset
836 /* -------------------------------------------------------------------- */
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
837 /* Methods */
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38376
diff changeset
838
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
839 #include "stringlib/stringdefs.h"
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38376
diff changeset
840 #include "stringlib/fastsearch.h"
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
841
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
842 #include "stringlib/count.h"
38404
52f2c9f74f5a needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
843 #include "stringlib/find.h"
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
844 #include "stringlib/partition.h"
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
845 #include "stringlib/split.h"
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38376
diff changeset
846
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
847 #define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46871
diff changeset
848 #include "stringlib/localeutil.h"
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46871
diff changeset
849
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46871
diff changeset
850
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
851
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
852 static int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
853 string_print(PyStringObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
855 Py_ssize_t i, str_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
856 char c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
857 int quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
858
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
859 /* XXX Ought to check for interrupts when writing long strings */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
860 if (! PyString_CheckExact(op)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
861 int ret;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
862 /* A str subclass may have its own __str__ method. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
863 op = (PyStringObject *) PyObject_Str((PyObject *)op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
864 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
865 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
866 ret = string_print(op, fp, flags);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
867 Py_DECREF(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
868 return ret;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
869 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
870 if (flags & Py_PRINT_RAW) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
871 char *data = op->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
872 Py_ssize_t size = Py_SIZE(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
873 Py_BEGIN_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
874 while (size > INT_MAX) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
875 /* Very long strings cannot be written atomically.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
876 * But don't write exactly INT_MAX bytes at a time
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
877 * to avoid memory aligment issues.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
878 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
879 const int chunk_size = INT_MAX & ~0x3FFF;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
880 fwrite(data, 1, chunk_size, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
881 data += chunk_size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
882 size -= chunk_size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
883 }
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
884 #ifdef __VMS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
885 if (size) fwrite(data, (int)size, 1, fp);
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
886 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
887 fwrite(data, 1, (int)size, fp);
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
888 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
889 Py_END_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
890 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
891 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
892
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
893 /* figure out which quote to use; single is preferred */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
894 quote = '\'';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
895 if (memchr(op->ob_sval, '\'', Py_SIZE(op)) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
896 !memchr(op->ob_sval, '"', Py_SIZE(op)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
897 quote = '"';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
898
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
899 str_len = Py_SIZE(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
900 Py_BEGIN_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
901 fputc(quote, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
902 for (i = 0; i < str_len; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
903 /* Since strings are immutable and the caller should have a
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
904 reference, accessing the interal buffer should not be an issue
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
905 with the GIL released. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
906 c = op->ob_sval[i];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
907 if (c == quote || c == '\\')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
908 fprintf(fp, "\\%c", c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
909 else if (c == '\t')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
910 fprintf(fp, "\\t");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
911 else if (c == '\n')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
912 fprintf(fp, "\\n");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
913 else if (c == '\r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
914 fprintf(fp, "\\r");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
915 else if (c < ' ' || c >= 0x7f)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
916 fprintf(fp, "\\x%02x", c & 0xff);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
917 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
918 fputc(c, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
919 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
920 fputc(quote, fp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
921 Py_END_ALLOW_THREADS
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
922 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
923 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
924
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
925 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
926 PyString_Repr(PyObject *obj, int smartquotes)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
928 register PyStringObject* op = (PyStringObject*) obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
929 size_t newsize = 2 + 4 * Py_SIZE(op);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
930 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
931 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_SIZE(op)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
932 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
933 "string is too large to make repr");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
934 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
935 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
936 v = PyString_FromStringAndSize((char *)NULL, newsize);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
937 if (v == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
938 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
939 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
940 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
941 register Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
942 register char c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
943 register char *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
944 int quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
945
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
946 /* figure out which quote to use; single is preferred */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
947 quote = '\'';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
948 if (smartquotes &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
949 memchr(op->ob_sval, '\'', Py_SIZE(op)) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
950 !memchr(op->ob_sval, '"', Py_SIZE(op)))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
951 quote = '"';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
952
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
953 p = PyString_AS_STRING(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
954 *p++ = quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
955 for (i = 0; i < Py_SIZE(op); i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
956 /* There's at least enough room for a hex escape
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
957 and a closing quote. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
958 assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
959 c = op->ob_sval[i];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
960 if (c == quote || c == '\\')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
961 *p++ = '\\', *p++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
962 else if (c == '\t')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
963 *p++ = '\\', *p++ = 't';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
964 else if (c == '\n')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
965 *p++ = '\\', *p++ = 'n';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
966 else if (c == '\r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
967 *p++ = '\\', *p++ = 'r';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
968 else if (c < ' ' || c >= 0x7f) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
969 /* For performance, we don't want to call
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
970 PyOS_snprintf here (extra layers of
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
971 function call). */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
972 sprintf(p, "\\x%02x", c & 0xff);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
973 p += 4;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
974 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
975 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
976 *p++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
977 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
978 assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
979 *p++ = quote;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
980 *p = '\0';
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
981 if (_PyString_Resize(&v, (p - PyString_AS_STRING(v))))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
982 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
983 return v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
984 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
986
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
987 static PyObject *
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
988 string_repr(PyObject *op)
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
989 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
990 return PyString_Repr(op, 1);
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
991 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
992
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
993 static PyObject *
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
994 string_str(PyObject *s)
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
995 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
996 assert(PyString_Check(s));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
997 if (PyString_CheckExact(s)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
998 Py_INCREF(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
999 return s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1000 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1001 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1002 /* Subtype -- return genuine string with the same value. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1003 PyStringObject *t = (PyStringObject *) s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1004 return PyString_FromStringAndSize(t->ob_sval, Py_SIZE(t));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1005 }
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
1006 }
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
1007
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1008 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1009 string_length(PyStringObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1010 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1011 return Py_SIZE(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1014 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1015 string_concat(register PyStringObject *a, register PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1017 register Py_ssize_t size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1018 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1019 if (!PyString_Check(bb)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1020 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1021 if (PyUnicode_Check(bb))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1022 return PyUnicode_Concat((PyObject *)a, bb);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1023 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1024 if (PyByteArray_Check(bb))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1025 return PyByteArray_Concat((PyObject *)a, bb);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1026 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1027 "cannot concatenate 'str' and '%.200s' objects",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1028 Py_TYPE(bb)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1029 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1030 }
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1031 #define b ((PyStringObject *)bb)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1032 /* Optimize cases with empty left or right operand */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1033 if ((Py_SIZE(a) == 0 || Py_SIZE(b) == 0) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1034 PyString_CheckExact(a) && PyString_CheckExact(b)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1035 if (Py_SIZE(a) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1036 Py_INCREF(bb);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1037 return bb;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1038 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1039 Py_INCREF(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1040 return (PyObject *)a;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1041 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1042 size = Py_SIZE(a) + Py_SIZE(b);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1043 /* Check that string sizes are not negative, to prevent an
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1044 overflow in cases where we are passed incorrectly-created
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1045 strings with negative lengths (due to a bug in other code).
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1046 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1047 if (Py_SIZE(a) < 0 || Py_SIZE(b) < 0 ||
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1048 Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1049 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1050 "strings are too large to concat");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1051 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1052 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1053
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1054 /* Inline PyObject_NewVar */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1055 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1056 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1057 "strings are too large to concat");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1058 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1059 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1060 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1061 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1062 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1063 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1064 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1065 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1066 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1067 Py_MEMCPY(op->ob_sval + Py_SIZE(a), b->ob_sval, Py_SIZE(b));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1068 op->ob_sval[size] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1069 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1072
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1073 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1074 string_repeat(register PyStringObject *a, register Py_ssize_t n)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1076 register Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1077 register Py_ssize_t j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1078 register Py_ssize_t size;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1079 register PyStringObject *op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1080 size_t nbytes;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1081 if (n < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1082 n = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1083 /* watch out for overflows: the size can overflow int,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1084 * and the # of bytes needed can overflow size_t
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1085 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1086 size = Py_SIZE(a) * n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1087 if (n && size / n != Py_SIZE(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1088 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1089 "repeated string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1090 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1091 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1092 if (size == Py_SIZE(a) && PyString_CheckExact(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1093 Py_INCREF(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1094 return (PyObject *)a;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1095 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1096 nbytes = (size_t)size;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1097 if (nbytes + PyStringObject_SIZE <= nbytes) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1098 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1099 "repeated string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1100 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1101 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1102 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + nbytes);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1103 if (op == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1104 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1105 PyObject_INIT_VAR(op, &PyString_Type, size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1106 op->ob_shash = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1107 op->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1108 op->ob_sval[size] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1109 if (Py_SIZE(a) == 1 && n > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1110 memset(op->ob_sval, a->ob_sval[0] , n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1111 return (PyObject *) op;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1112 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1113 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1114 if (i < size) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1115 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1116 i = Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1117 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1118 while (i < size) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1119 j = (i <= size-i) ? i : size-i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1120 Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1121 i += j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1122 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1123 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1124 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1126 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1128 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1129 string_slice(register PyStringObject *a, register Py_ssize_t i,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1130 register Py_ssize_t j)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1131 /* j -- may be negative! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1133 if (i < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1134 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1135 if (j < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1136 j = 0; /* Avoid signed/unsigned bug in next line */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1137 if (j > Py_SIZE(a))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1138 j = Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1139 if (i == 0 && j == Py_SIZE(a) && PyString_CheckExact(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1140 /* It's the same as a */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1141 Py_INCREF(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1142 return (PyObject *)a;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1143 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1144 if (j < i)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1145 j = i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1146 return PyString_FromStringAndSize(a->ob_sval + i, j-i);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1149 static int
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
1150 string_contains(PyObject *str_obj, PyObject *sub_obj)
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1151 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1152 if (!PyString_CheckExact(sub_obj)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1153 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1154 if (PyUnicode_Check(sub_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1155 return PyUnicode_Contains(str_obj, sub_obj);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1156 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1157 if (!PyString_Check(sub_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1158 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1159 "'in <string>' requires string as left operand, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1160 "not %.200s", Py_TYPE(sub_obj)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1161 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1162 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1163 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1164
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1165 return stringlib_contains_obj(str_obj, sub_obj);
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1166 }
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1167
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1168 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1169 string_item(PyStringObject *a, register Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1171 char pchar;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1172 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1173 if (i < 0 || i >= Py_SIZE(a)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1174 PyErr_SetString(PyExc_IndexError, "string index out of range");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1175 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1176 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1177 pchar = a->ob_sval[i];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1178 v = (PyObject *)characters[pchar & UCHAR_MAX];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1179 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1180 v = PyString_FromStringAndSize(&pchar, 1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1181 else {
17712
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1182 #ifdef COUNT_ALLOCS
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1183 one_strings++;
17712
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1184 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1185 Py_INCREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1186 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1187 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1190 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1191 string_richcompare(PyStringObject *a, PyStringObject *b, int op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1193 int c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1194 Py_ssize_t len_a, len_b;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1195 Py_ssize_t min_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1196 PyObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1197
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1198 /* Make sure both arguments are strings. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1199 if (!(PyString_Check(a) && PyString_Check(b))) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1200 result = Py_NotImplemented;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1201 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1202 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1203 if (a == b) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1204 switch (op) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1205 case Py_EQ:case Py_LE:case Py_GE:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1206 result = Py_True;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1207 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1208 case Py_NE:case Py_LT:case Py_GT:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1209 result = Py_False;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1210 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1211 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1212 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1213 if (op == Py_EQ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1214 /* Supporting Py_NE here as well does not save
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1215 much time, since Py_NE is rarely used. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1216 if (Py_SIZE(a) == Py_SIZE(b)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1217 && (a->ob_sval[0] == b->ob_sval[0]
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1218 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1219 result = Py_True;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1220 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1221 result = Py_False;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1222 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1223 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1224 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1225 len_a = Py_SIZE(a); len_b = Py_SIZE(b);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1226 min_len = (len_a < len_b) ? len_a : len_b;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1227 if (min_len > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1228 c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1229 if (c==0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1230 c = memcmp(a->ob_sval, b->ob_sval, min_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1231 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1232 c = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1233 if (c == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1234 c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1235 switch (op) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1236 case Py_LT: c = c < 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1237 case Py_LE: c = c <= 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1238 case Py_EQ: assert(0); break; /* unreachable */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1239 case Py_NE: c = c != 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1240 case Py_GT: c = c > 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1241 case Py_GE: c = c >= 0; break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1242 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1243 result = Py_NotImplemented;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1244 goto out;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1245 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1246 result = c ? Py_True : Py_False;
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1247 out:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1248 Py_INCREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1249 return result;
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1250 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1251
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1252 int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1253 _PyString_Eq(PyObject *o1, PyObject *o2)
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1254 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1255 PyStringObject *a = (PyStringObject*) o1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1256 PyStringObject *b = (PyStringObject*) o2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1257 return Py_SIZE(a) == Py_SIZE(b)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1258 && *a->ob_sval == *b->ob_sval
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1259 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1262 static long
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1263 string_hash(PyStringObject *a)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1264 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1265 register Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1266 register unsigned char *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1267 register long x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1268
76189
a11a2bbd8241 fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents: 75135
diff changeset
1269 #ifdef Py_DEBUG
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1270 assert(_Py_HashSecret_Initialized);
76189
a11a2bbd8241 fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents: 75135
diff changeset
1271 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1272 if (a->ob_shash != -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1273 return a->ob_shash;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1274 len = Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1275 /*
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1276 We make the hash of the empty string be 0, rather than using
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1277 (prefix ^ suffix), since this slightly obfuscates the hash secret
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1278 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1279 if (len == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1280 a->ob_shash = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1281 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1282 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1283 p = (unsigned char *) a->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1284 x = _Py_HashSecret.prefix;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1285 x ^= *p << 7;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1286 while (--len >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1287 x = (1000003*x) ^ *p++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1288 x ^= Py_SIZE(a);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1289 x ^= _Py_HashSecret.suffix;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1290 if (x == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1291 x = -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1292 a->ob_shash = x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1293 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1294 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1295
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1296 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1297 string_subscript(PyStringObject* self, PyObject* item)
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1298 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1299 if (PyIndex_Check(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1300 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1301 if (i == -1 && PyErr_Occurred())
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1302 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1303 if (i < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1304 i += PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1305 return string_item(self, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1306 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1307 else if (PySlice_Check(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1308 Py_ssize_t start, stop, step, slicelength, cur, i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1309 char* source_buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1310 char* result_buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1311 PyObject* result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1312
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1313 if (PySlice_GetIndicesEx((PySliceObject*)item,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1314 PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1315 &start, &stop, &step, &slicelength) < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1316 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1317 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1318
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1319 if (slicelength <= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1320 return PyString_FromStringAndSize("", 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1321 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1322 else if (start == 0 && step == 1 &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1323 slicelength == PyString_GET_SIZE(self) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1324 PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1325 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1326 return (PyObject *)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1327 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1328 else if (step == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1329 return PyString_FromStringAndSize(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1330 PyString_AS_STRING(self) + start,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1331 slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1332 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1333 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1334 source_buf = PyString_AsString((PyObject*)self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1335 result_buf = (char *)PyMem_Malloc(slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1336 if (result_buf == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1337 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1338
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1339 for (cur = start, i = 0; i < slicelength;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1340 cur += step, i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1341 result_buf[i] = source_buf[cur];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1342 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1343
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1344 result = PyString_FromStringAndSize(result_buf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1345 slicelength);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1346 PyMem_Free(result_buf);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1347 return result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1348 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1349 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1350 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1351 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1352 "string indices must be integers, not %.200s",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1353 Py_TYPE(item)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1354 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1355 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1356 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1357
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1358 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1359 string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1360 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1361 if ( index != 0 ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1362 PyErr_SetString(PyExc_SystemError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1363 "accessing non-existent string segment");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1364 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1365 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1366 *ptr = (void *)self->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1367 return Py_SIZE(self);
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1368 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1369
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1370 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1371 string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1372 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1373 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1374 "Cannot use string as modifiable buffer");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1375 return -1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1376 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1377
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1378 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1379 string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1380 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1381 if ( lenp )
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1382 *lenp = Py_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1383 return 1;
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1384 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1385
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1386 static Py_ssize_t
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1387 string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr)
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1388 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1389 if ( index != 0 ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1390 PyErr_SetString(PyExc_SystemError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1391 "accessing non-existent string segment");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1392 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1393 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1394 *ptr = self->ob_sval;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1395 return Py_SIZE(self);
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1396 }
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1397
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1398 static int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1399 string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags)
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1400 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1401 return PyBuffer_FillInfo(view, (PyObject*)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1402 (void *)self->ob_sval, Py_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1403 1, flags);
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1404 }
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45731
diff changeset
1405
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1406 static PySequenceMethods string_as_sequence = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1407 (lenfunc)string_length, /*sq_length*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1408 (binaryfunc)string_concat, /*sq_concat*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1409 (ssizeargfunc)string_repeat, /*sq_repeat*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1410 (ssizeargfunc)string_item, /*sq_item*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1411 (ssizessizeargfunc)string_slice, /*sq_slice*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1412 0, /*sq_ass_item*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1413 0, /*sq_ass_slice*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1414 (objobjproc)string_contains /*sq_contains*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1415 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1416
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1417 static PyMappingMethods string_as_mapping = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1418 (lenfunc)string_length,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1419 (binaryfunc)string_subscript,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1420 0,
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1421 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1422
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1423 static PyBufferProcs string_as_buffer = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1424 (readbufferproc)string_buffer_getreadbuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1425 (writebufferproc)string_buffer_getwritebuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1426 (segcountproc)string_buffer_getsegcount,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1427 (charbufferproc)string_buffer_getcharbuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1428 (getbufferproc)string_buffer_getbuffer,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1429 0, /* XXX */
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1430 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1431
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1432
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1433
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1434 #define LEFTSTRIP 0
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1435 #define RIGHTSTRIP 1
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1436 #define BOTHSTRIP 2
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1437
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1438 /* Arrays indexed by above */
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1439 static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1440
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1441 #define STRIPNAME(i) (stripformat[i]+3)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1442
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1443 PyDoc_STRVAR(split__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1444 "S.split([sep [,maxsplit]]) -> list of strings\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1445 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1446 Return a list of the words in the string S, using sep as the\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1447 delimiter string. If maxsplit is given, at most maxsplit\n\
24561
b25c31e008c8 SF 582071 clarified the .split() method's docstring to note that sep=None
Raymond Hettinger <python@rcn.com>
parents: 24415
diff changeset
1448 splits are done. If sep is not specified or is None, any\n\
46908
ed2c1888e143 #2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents: 46898
diff changeset
1449 whitespace string is a separator and empty strings are removed\n\
ed2c1888e143 #2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents: 46898
diff changeset
1450 from the result.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1451
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1452 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1453 string_split(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1454 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1455 Py_ssize_t len = PyString_GET_SIZE(self), n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1456 Py_ssize_t maxsplit = -1;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1457 const char *s = PyString_AS_STRING(self), *sub;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1458 PyObject *subobj = Py_None;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1459
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1460 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1461 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1462 if (maxsplit < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1463 maxsplit = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1464 if (subobj == Py_None)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1465 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1466 if (PyString_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1467 sub = PyString_AS_STRING(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1468 n = PyString_GET_SIZE(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1469 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1470 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1471 else if (PyUnicode_Check(subobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1472 return PyUnicode_Split((PyObject *)self, subobj, maxsplit);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1473 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1474 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1475 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1476
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1477 return stringlib_split((PyObject*) self, s, len, sub, n, maxsplit);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1478 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1479
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1480 PyDoc_STRVAR(partition__doc__,
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1481 "S.partition(sep) -> (head, sep, tail)\n\
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1482 \n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1483 Search for the separator sep in S, and return the part before it,\n\
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1484 the separator itself, and the part after it. If the separator is not\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1485 found, return S and two empty strings.");
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1486
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1487 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1488 string_partition(PyStringObject *self, PyObject *sep_obj)
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1489 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1490 const char *sep;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1491 Py_ssize_t sep_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1492
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1493 if (PyString_Check(sep_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1494 sep = PyString_AS_STRING(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1495 sep_len = PyString_GET_SIZE(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1496 }
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38332
diff changeset
1497 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1498 else if (PyUnicode_Check(sep_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1499 return PyUnicode_Partition((PyObject *) self, sep_obj);
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1500 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1501 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1502 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1503
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1504 return stringlib_partition(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1505 (PyObject*) self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1506 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1507 sep_obj, sep, sep_len
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1508 );
38332
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1509 }
eb237a37ea02 needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38321
diff changeset
1510
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1511 PyDoc_STRVAR(rpartition__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1512 "S.rpartition(sep) -> (head, sep, tail)\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1513 \n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1514 Search for the separator sep in S, starting at the end of S, and return\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1515 the part before it, the separator itself, and the part after it. If the\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1516 separator is not found, return two empty strings and S.");
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1517
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1518 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1519 string_rpartition(PyStringObject *self, PyObject *sep_obj)
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1520 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1521 const char *sep;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1522 Py_ssize_t sep_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1523
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1524 if (PyString_Check(sep_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1525 sep = PyString_AS_STRING(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1526 sep_len = PyString_GET_SIZE(sep_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1527 }
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1528 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1529 else if (PyUnicode_Check(sep_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1530 return PyUnicode_RPartition((PyObject *) self, sep_obj);
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1531 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1532 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1533 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1534
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1535 return stringlib_rpartition(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1536 (PyObject*) self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1537 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1538 sep_obj, sep, sep_len
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1539 );
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1540 }
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38385
diff changeset
1541
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1542 PyDoc_STRVAR(rsplit__doc__,
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1543 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1544 \n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1545 Return a list of the words in the string S, using sep as the\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1546 delimiter string, starting at the end of the string and working\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1547 to the front. If maxsplit is given, at most maxsplit splits are\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1548 done. If sep is not specified or is None, any whitespace string\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1549 is a separator.");
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1550
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1551 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1552 string_rsplit(PyStringObject *self, PyObject *args)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1553 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1554 Py_ssize_t len = PyString_GET_SIZE(self), n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1555 Py_ssize_t maxsplit = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1556 const char *s = PyString_AS_STRING(self), *sub;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1557 PyObject *subobj = Py_None;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1558
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1559 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1560 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1561 if (maxsplit < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1562 maxsplit = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1563 if (subobj == Py_None)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1564 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1565 if (PyString_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1566 sub = PyString_AS_STRING(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1567 n = PyString_GET_SIZE(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1568 }
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1569 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1570 else if (PyUnicode_Check(subobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1571 return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1572 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1573 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1574 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1575
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1576 return stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1577 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1578
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1579
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1580 PyDoc_STRVAR(join__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1581 "S.join(iterable) -> string\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1582 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1583 Return a string which is the concatenation of the strings in the\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1584 iterable. The separator between elements is S.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1585
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1586 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1587 string_join(PyStringObject *self, PyObject *orig)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1588 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1589 char *sep = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1590 const Py_ssize_t seplen = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1591 PyObject *res = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1592 char *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1593 Py_ssize_t seqlen = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1594 size_t sz = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1595 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1596 PyObject *seq, *item;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1597
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1598 seq = PySequence_Fast(orig, "");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1599 if (seq == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1600 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1601 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1602
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1603 seqlen = PySequence_Size(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1604 if (seqlen == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1605 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1606 return PyString_FromString("");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1607 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1608 if (seqlen == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1609 item = PySequence_Fast_GET_ITEM(seq, 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1610 if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1611 Py_INCREF(item);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1612 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1613 return item;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1614 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1615 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1616
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1617 /* There are at least two things to join, or else we have a subclass
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1618 * of the builtin types in the sequence.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1619 * Do a pre-pass to figure out the total amount of space we'll
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1620 * need (sz), see whether any argument is absurd, and defer to
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1621 * the Unicode join if appropriate.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1622 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1623 for (i = 0; i < seqlen; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1624 const size_t old_sz = sz;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1625 item = PySequence_Fast_GET_ITEM(seq, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1626 if (!PyString_Check(item)){
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1627 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1628 if (PyUnicode_Check(item)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1629 /* Defer to Unicode join.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1630 * CAUTION: There's no gurantee that the
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1631 * original sequence can be iterated over
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1632 * again, so we must pass seq here.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1633 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1634 PyObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1635 result = PyUnicode_Join((PyObject *)self, seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1636 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1637 return result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1638 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1639 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1640 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1641 "sequence item %zd: expected string,"
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1642 " %.80s found",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1643 i, Py_TYPE(item)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1644 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1645 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1646 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1647 sz += PyString_GET_SIZE(item);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1648 if (i != 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1649 sz += seplen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1650 if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1651 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1652 "join() result is too long for a Python string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1653 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1654 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1655 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1656 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1657
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1658 /* Allocate result space. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1659 res = PyString_FromStringAndSize((char*)NULL, sz);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1660 if (res == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1661 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1662 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1663 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1664
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1665 /* Catenate everything. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1666 p = PyString_AS_STRING(res);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1667 for (i = 0; i < seqlen; ++i) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1668 size_t n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1669 item = PySequence_Fast_GET_ITEM(seq, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1670 n = PyString_GET_SIZE(item);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1671 Py_MEMCPY(p, PyString_AS_STRING(item), n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1672 p += n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1673 if (i < seqlen - 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1674 Py_MEMCPY(p, sep, seplen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1675 p += seplen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1676 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1677 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1678
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1679 Py_DECREF(seq);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1680 return res;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1681 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1682
18130
8eea2efb34cd Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1683 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1684 _PyString_Join(PyObject *sep, PyObject *x)
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1685 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1686 assert(sep != NULL && PyString_Check(sep));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1687 assert(x != NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1688 return string_join((PyStringObject *)sep, x);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1689 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1690
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
1691 /* helper macro to fixup start/end slice values */
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
1692 #define ADJUST_INDICES(start, end, len) \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1693 if (end > len) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1694 end = len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1695 else if (end < 0) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1696 end += len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1697 if (end < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1698 end = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1699 } \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1700 if (start < 0) { \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1701 start += len; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1702 if (start < 0) \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1703 start = 0; \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
1704 }
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1705
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1706 Py_LOCAL_INLINE(Py_ssize_t)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1707 string_find_internal(PyStringObject *self, PyObject *args, int dir)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1708 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1709 PyObject *subobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1710 const char *sub;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1711 Py_ssize_t sub_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1712 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
1713
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
1714 if (!stringlib_parse_args_finds("find/rfind/index/rindex",
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
1715 args, &subobj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1716 return -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1717
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1718 if (PyString_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1719 sub = PyString_AS_STRING(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1720 sub_len = PyString_GET_SIZE(subobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1721 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1722 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1723 else if (PyUnicode_Check(subobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1724 return PyUnicode_Find(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1725 (PyObject *)self, subobj, start, end, dir);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1726 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1727 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1728 /* XXX - the "expected a character buffer object" is pretty
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1729 confusing for a non-expert. remap to something else ? */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1730 return -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1731
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1732 if (dir > 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1733 return stringlib_find_slice(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1734 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1735 sub, sub_len, start, end);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1736 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1737 return stringlib_rfind_slice(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1738 PyString_AS_STRING(self), PyString_GET_SIZE(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1739 sub, sub_len, start, end);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1740 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1741
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1742
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1743 PyDoc_STRVAR(find__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1744 "S.find(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1745 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1746 Return the lowest index in S where substring sub is found,\n\
71529
e266415cf42c merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents: 70947
diff changeset
1747 such that sub is contained within S[start:end]. Optional\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1748 arguments start and end are interpreted as in slice notation.\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1749 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1750 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1751
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1752 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1753 string_find(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1754 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1755 Py_ssize_t result = string_find_internal(self, args, +1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1756 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1757 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1758 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1759 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1760
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1761
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1762 PyDoc_STRVAR(index__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1763 "S.index(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1764 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1765 Like S.find() but raise ValueError when the substring is not found.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1766
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1767 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1768 string_index(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1769 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1770 Py_ssize_t result = string_find_internal(self, args, +1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1771 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1772 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1773 if (result == -1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1774 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1775 "substring not found");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1776 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1777 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1778 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1779 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1780
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1781
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1782 PyDoc_STRVAR(rfind__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1783 "S.rfind(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1784 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1785 Return the highest index in S where substring sub is found,\n\
71529
e266415cf42c merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents: 70947
diff changeset
1786 such that sub is contained within S[start:end]. Optional\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1787 arguments start and end are interpreted as in slice notation.\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1788 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1789 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1790
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1791 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1792 string_rfind(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1793 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1794 Py_ssize_t result = string_find_internal(self, args, -1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1795 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1796 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1797 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1798 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1799
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1800
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1801 PyDoc_STRVAR(rindex__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1802 "S.rindex(sub [,start [,end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1803 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1804 Like S.rfind() but raise ValueError when the substring is not found.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1805
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1806 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1807 string_rindex(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1808 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1809 Py_ssize_t result = string_find_internal(self, args, -1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1810 if (result == -2)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1811 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1812 if (result == -1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1813 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1814 "substring not found");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1815 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1816 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1817 return PyInt_FromSsize_t(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1818 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1819
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1820
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1821 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1822 do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1823 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1824 char *s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1825 Py_ssize_t len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1826 char *sep = PyString_AS_STRING(sepobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1827 Py_ssize_t seplen = PyString_GET_SIZE(sepobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1828 Py_ssize_t i, j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1829
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1830 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1831 if (striptype != RIGHTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1832 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1833 i++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1834 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1835 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1836
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1837 j = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1838 if (striptype != LEFTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1839 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1840 j--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1841 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1842 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1843 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1844
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1845 if (i == 0 && j == len && PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1846 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1847 return (PyObject*)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1848 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1849 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1850 return PyString_FromStringAndSize(s+i, j-i);
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1851 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1852
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1853
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1854 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1855 do_strip(PyStringObject *self, int striptype)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1856 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1857 char *s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1858 Py_ssize_t len = PyString_GET_SIZE(self), i, j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1859
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1860 i = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1861 if (striptype != RIGHTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1862 while (i < len && isspace(Py_CHARMASK(s[i]))) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1863 i++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1864 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1865 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1866
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1867 j = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1868 if (striptype != LEFTSTRIP) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1869 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1870 j--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1871 } while (j >= i && isspace(Py_CHARMASK(s[j])));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1872 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1873 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1874
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1875 if (i == 0 && j == len && PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1876 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1877 return (PyObject*)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1878 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1879 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1880 return PyString_FromStringAndSize(s+i, j-i);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1881 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1882
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1883
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
1884 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1885 do_argstrip(PyStringObject *self, int striptype, PyObject *args)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1886 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1887 PyObject *sep = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1888
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1889 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1890 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1891
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1892 if (sep != NULL && sep != Py_None) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1893 if (PyString_Check(sep))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1894 return do_xstrip(self, striptype, sep);
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1895 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1896 else if (PyUnicode_Check(sep)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1897 PyObject *uniself = PyUnicode_FromObject((PyObject *)self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1898 PyObject *res;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1899 if (uniself==NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1900 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1901 res = _PyUnicode_XStrip((PyUnicodeObject *)uniself,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1902 striptype, sep);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1903 Py_DECREF(uniself);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1904 return res;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1905 }
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1906 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1907 PyErr_Format(PyExc_TypeError,
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1908 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1909 "%s arg must be None, str or unicode",
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1910 #else
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1911 "%s arg must be None or str",
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1912 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1913 STRIPNAME(striptype));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1914 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1915 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1916
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1917 return do_strip(self, striptype);
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1918 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1919
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1920
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1921 PyDoc_STRVAR(strip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1922 "S.strip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1923 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1924 Return a copy of the string S with leading and trailing\n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1925 whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1926 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1927 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1928
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1929 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1930 string_strip(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1931 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1932 if (PyTuple_GET_SIZE(args) == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1933 return do_strip(self, BOTHSTRIP); /* Common case */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1934 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1935 return do_argstrip(self, BOTHSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1936 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1937
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1938
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1939 PyDoc_STRVAR(lstrip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1940 "S.lstrip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1941 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1942 Return a copy of the string S with leading whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1943 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1944 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1945
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1946 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1947 string_lstrip(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1948 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1949 if (PyTuple_GET_SIZE(args) == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1950 return do_strip(self, LEFTSTRIP); /* Common case */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1951 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1952 return do_argstrip(self, LEFTSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1953 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1954
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1955
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1956 PyDoc_STRVAR(rstrip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1957 "S.rstrip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1958 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1959 Return a copy of the string S with trailing whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1960 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1961 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1962
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1963 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1964 string_rstrip(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1965 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1966 if (PyTuple_GET_SIZE(args) == 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1967 return do_strip(self, RIGHTSTRIP); /* Common case */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1968 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1969 return do_argstrip(self, RIGHTSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1970 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1971
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1972
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1973 PyDoc_STRVAR(lower__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1974 "S.lower() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1975 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1976 Return a copy of the string S converted to lowercase.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1977
38294
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1978 /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1979 #ifndef _tolower
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1980 #define _tolower tolower
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1981 #endif
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
1982
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1983 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1984 string_lower(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1985 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1986 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1987 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1988 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1989
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1990 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1991 if (!newobj)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1992 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1993
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1994 s = PyString_AS_STRING(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1995
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1996 Py_MEMCPY(s, PyString_AS_STRING(self), n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1997
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1998 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
1999 int c = Py_CHARMASK(s[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2000 if (isupper(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2001 s[i] = _tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2002 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2003
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2004 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2005 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2006
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2007 PyDoc_STRVAR(upper__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2008 "S.upper() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2009 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2010 Return a copy of the string S converted to uppercase.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2011
38294
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2012 #ifndef _toupper
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2013 #define _toupper toupper
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2014 #endif
41768de17ead needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents: 38292
diff changeset
2015
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2016 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2017 string_upper(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2018 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2019 char *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2020 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2021 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2022
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2023 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2024 if (!newobj)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2025 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2026
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2027 s = PyString_AS_STRING(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2028
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2029 Py_MEMCPY(s, PyString_AS_STRING(self), n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2030
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2031 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2032 int c = Py_CHARMASK(s[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2033 if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2034 s[i] = _toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2035 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2036
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2037 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2038 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2039
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2040 PyDoc_STRVAR(title__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2041 "S.title() -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2042 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2043 Return a titlecased version of S, i.e. words start with uppercase\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2044 characters, all remaining cased characters have lowercase.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2045
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2046 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2047 string_title(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2048 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2049 char *s = PyString_AS_STRING(self), *s_new;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2050 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2051 int previous_is_cased = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2052 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2053
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2054 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2055 if (newobj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2056 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2057 s_new = PyString_AsString(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2058 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2059 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2060 if (islower(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2061 if (!previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2062 c = toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2063 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2064 } else if (isupper(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2065 if (previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2066 c = tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2067 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2068 } else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2069 previous_is_cased = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2070 *s_new++ = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2071 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2072 return newobj;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2073 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2074
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2075 PyDoc_STRVAR(capitalize__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2076 "S.capitalize() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2077 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2078 Return a copy of the string S with only its first character\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2079 capitalized.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2080
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2081 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2082 string_capitalize(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2083 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2084 char *s = PyString_AS_STRING(self), *s_new;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2085 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2086 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2087
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2088 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2089 if (newobj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2090 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2091 s_new = PyString_AsString(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2092 if (0 < n) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2093 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2094 if (islower(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2095 *s_new = toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2096 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2097 *s_new = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2098 s_new++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2099 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2100 for (i = 1; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2101 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2102 if (isupper(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2103 *s_new = tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2104 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2105 *s_new = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2106 s_new++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2107 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2108 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2109 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2110
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2111
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2112 PyDoc_STRVAR(count__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2113 "S.count(sub[, start[, end]]) -> int\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2114 \n\
38236
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2115 Return the number of non-overlapping occurrences of substring sub in\n\
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2116 string S[start:end]. Optional arguments start and end are interpreted\n\
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38173
diff changeset
2117 as in slice notation.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2118
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2119 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2120 string_count(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2121 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2122 PyObject *sub_obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2123 const char *str = PyString_AS_STRING(self), *sub;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2124 Py_ssize_t sub_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2125 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2126
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
2127 if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2128 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2129
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2130 if (PyString_Check(sub_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2131 sub = PyString_AS_STRING(sub_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2132 sub_len = PyString_GET_SIZE(sub_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2133 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2134 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2135 else if (PyUnicode_Check(sub_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2136 Py_ssize_t count;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2137 count = PyUnicode_Count((PyObject *)self, sub_obj, start, end);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2138 if (count == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2139 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2140 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2141 return PyInt_FromSsize_t(count);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2142 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2143 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2144 else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2145 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2146
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2147 ADJUST_INDICES(start, end, PyString_GET_SIZE(self));
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2148
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2149 return PyInt_FromSsize_t(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2150 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2151 );
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2152 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2153
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2154 PyDoc_STRVAR(swapcase__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2155 "S.swapcase() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2156 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2157 Return a copy of the string S with uppercase characters\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2158 converted to lowercase and vice versa.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2159
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2160 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2161 string_swapcase(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2162 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2163 char *s = PyString_AS_STRING(self), *s_new;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2164 Py_ssize_t i, n = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2165 PyObject *newobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2166
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2167 newobj = PyString_FromStringAndSize(NULL, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2168 if (newobj == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2169 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2170 s_new = PyString_AsString(newobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2171 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2172 int c = Py_CHARMASK(*s++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2173 if (islower(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2174 *s_new = toupper(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2175 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2176 else if (isupper(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2177 *s_new = tolower(c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2178 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2179 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2180 *s_new = c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2181 s_new++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2182 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2183 return newobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2184 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2185
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2186
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2187 PyDoc_STRVAR(translate__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2188 "S.translate(table [,deletechars]) -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2189 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2190 Return a copy of the string S, where all characters occurring\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2191 in the optional argument deletechars are removed, and the\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2192 remaining characters have been mapped through the given\n\
70947
60b3a5615c4e Issue #12164: Document (in docstring) that str.translate accepts None for the first argument.
Mark Dickinson <mdickinson@enthought.com>
parents: 69562
diff changeset
2193 translation table, which must be a string of length 256 or None.\n\
60b3a5615c4e Issue #12164: Document (in docstring) that str.translate accepts None for the first argument.
Mark Dickinson <mdickinson@enthought.com>
parents: 69562
diff changeset
2194 If the table argument is None, no translation is applied and\n\
60b3a5615c4e Issue #12164: Document (in docstring) that str.translate accepts None for the first argument.
Mark Dickinson <mdickinson@enthought.com>
parents: 69562
diff changeset
2195 the operation simply removes the characters in deletechars.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2196
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2197 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2198 string_translate(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2199 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2200 register char *input, *output;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2201 const char *table;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2202 register Py_ssize_t i, c, changed = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2203 PyObject *input_obj = (PyObject*)self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2204 const char *output_start, *del_table=NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2205 Py_ssize_t inlen, tablen, dellen = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2206 PyObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2207 int trans_table[256];
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2208 PyObject *tableobj, *delobj = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2209
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2210 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2211 &tableobj, &delobj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2212 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2213
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2214 if (PyString_Check(tableobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2215 table = PyString_AS_STRING(tableobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2216 tablen = PyString_GET_SIZE(tableobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2217 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2218 else if (tableobj == Py_None) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2219 table = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2220 tablen = 256;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2221 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2222 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2223 else if (PyUnicode_Check(tableobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2224 /* Unicode .translate() does not support the deletechars
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2225 parameter; instead a mapping to None will cause characters
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2226 to be deleted. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2227 if (delobj != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2228 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2229 "deletions are implemented differently for unicode");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2230 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2231 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2232 return PyUnicode_Translate((PyObject *)self, tableobj, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2233 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2234 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2235 else if (PyObject_AsCharBuffer(tableobj, &table, &tablen))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2236 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2237
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2238 if (tablen != 256) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2239 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2240 "translation table must be 256 characters long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2241 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2242 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2243
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2244 if (delobj != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2245 if (PyString_Check(delobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2246 del_table = PyString_AS_STRING(delobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2247 dellen = PyString_GET_SIZE(delobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2248 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2249 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2250 else if (PyUnicode_Check(delobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2251 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2252 "deletions are implemented differently for unicode");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2253 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2254 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2255 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2256 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2257 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2258 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2259 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2260 del_table = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2261 dellen = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2262 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2263
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2264 inlen = PyString_GET_SIZE(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2265 result = PyString_FromStringAndSize((char *)NULL, inlen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2266 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2267 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2268 output_start = output = PyString_AsString(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2269 input = PyString_AS_STRING(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2270
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2271 if (dellen == 0 && table != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2272 /* If no deletions are required, use faster code */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2273 for (i = inlen; --i >= 0; ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2274 c = Py_CHARMASK(*input++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2275 if (Py_CHARMASK((*output++ = table[c])) != c)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2276 changed = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2277 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2278 if (changed || !PyString_CheckExact(input_obj))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2279 return result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2280 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2281 Py_INCREF(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2282 return input_obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2283 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2284
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2285 if (table == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2286 for (i = 0; i < 256; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2287 trans_table[i] = Py_CHARMASK(i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2288 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2289 for (i = 0; i < 256; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2290 trans_table[i] = Py_CHARMASK(table[i]);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2291 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2292
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2293 for (i = 0; i < dellen; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2294 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2295
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2296 for (i = inlen; --i >= 0; ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2297 c = Py_CHARMASK(*input++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2298 if (trans_table[c] != -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2299 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2300 continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2301 changed = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2302 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2303 if (!changed && PyString_CheckExact(input_obj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2304 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2305 Py_INCREF(input_obj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2306 return input_obj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2307 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2308 /* Fix the size of the resulting string */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2309 if (inlen > 0 && _PyString_Resize(&result, output - output_start))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2310 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2311 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2312 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2313
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2314
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2315 /* find and count characters and substrings */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2316
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2317 #define findchar(target, target_len, c) \
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2318 ((char *)memchr((const void *)(target), c, target_len))
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2319
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2320 /* String ops must return a string. */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2321 /* If the object is subclass of string, create a copy */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2322 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2323 return_self(PyStringObject *self)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2324 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2325 if (PyString_CheckExact(self)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2326 Py_INCREF(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2327 return self;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2328 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2329 return (PyStringObject *)PyString_FromStringAndSize(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2330 PyString_AS_STRING(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2331 PyString_GET_SIZE(self));
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2332 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2333
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
2334 Py_LOCAL_INLINE(Py_ssize_t)
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38738
diff changeset
2335 countchar(const char *target, int target_len, char c, Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2336 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2337 Py_ssize_t count=0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2338 const char *start=target;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2339 const char *end=target+target_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2340
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2341 while ( (start=findchar(start, end-start, c)) != NULL ) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2342 count++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2343 if (count >= maxcount)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2344 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2345 start += 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2346 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2347 return count;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2348 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2349
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2350
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38417
diff changeset
2351 /* Algorithms for different cases of string replacement */
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2352
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2353 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2354 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2355 replace_interleave(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2356 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2357 Py_ssize_t maxcount)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2358 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2359 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2360 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2361 Py_ssize_t count, i, product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2362 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2363
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2364 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2365
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2366 /* 1 at the end plus 1 after every character */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2367 count = self_len+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2368 if (maxcount < count)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2369 count = maxcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2370
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2371 /* Check for overflow */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2372 /* result_len = count * to_len + self_len; */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2373 product = count * to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2374 if (product / to_len != count) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2375 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2376 "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2377 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2378 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2379 result_len = product + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2380 if (result_len < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2381 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2382 "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2383 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2384 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2385
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2386 if (! (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2387 PyString_FromStringAndSize(NULL, result_len)) )
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2388 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2389
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2390 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2391 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2392
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2393 /* TODO: special case single character, which doesn't need memcpy */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2394
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2395 /* Lay the first one down (guaranteed this will occur) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2396 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2397 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2398 count -= 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2399
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2400 for (i=0; i<count; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2401 *result_s++ = *self_s++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2402 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2403 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2404 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2405
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2406 /* Copy the rest of the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2407 Py_MEMCPY(result_s, self_s, self_len-i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2408
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2409 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2410 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2411
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2412 /* Special case for deleting a single character */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2413 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2414 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2415 replace_delete_single_character(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2416 char from_c, Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2417 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2418 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2419 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2420 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2421 Py_ssize_t count;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2422 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2423
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2424 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2425 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2426
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2427 count = countchar(self_s, self_len, from_c, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2428 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2429 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2430 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2431
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2432 result_len = self_len - count; /* from_len == 1 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2433 assert(result_len>=0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2434
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2435 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2436 PyString_FromStringAndSize(NULL, result_len)) == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2437 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2438 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2439
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2440 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2441 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2442 while (count-- > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2443 next = findchar(start, end-start, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2444 if (next == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2445 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2446 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2447 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2448 start = next+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2449 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2450 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2451
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2452 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2453 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2454
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2455 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2456
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2457 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2458 replace_delete_substring(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2459 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2460 Py_ssize_t maxcount) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2461 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2462 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2463 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2464 Py_ssize_t count, offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2465 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2466
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2467 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2468 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2469
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2470 count = stringlib_count(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2471 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2472 maxcount);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2473
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2474 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2475 /* no matches */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2476 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2477 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2478
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2479 result_len = self_len - (count * from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2480 assert (result_len>=0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2481
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2482 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2483 PyString_FromStringAndSize(NULL, result_len)) == NULL )
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2484 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2485
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2486 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2487
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2488 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2489 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2490 while (count-- > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2491 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2492 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2493 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2494 if (offset == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2495 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2496 next = start + offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2497
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2498 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2499
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2500 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2501 start = next+from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2502 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2503 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2504 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2505 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2506
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2507 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2508 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2509 replace_single_character_in_place(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2510 char from_c, char to_c,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2511 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2512 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2513 char *self_s, *result_s, *start, *end, *next;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2514 Py_ssize_t self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2515 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2516
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2517 /* The result string will be the same size */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2518 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2519 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2520
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2521 next = findchar(self_s, self_len, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2522
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2523 if (next == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2524 /* No matches; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2525 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2526 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2527
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2528 /* Need to make a new string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2529 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2530 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2531 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2532 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2533 Py_MEMCPY(result_s, self_s, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2534
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2535 /* change everything in-place, starting with this one */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2536 start = result_s + (next-self_s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2537 *start = to_c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2538 start++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2539 end = result_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2540
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2541 while (--maxcount > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2542 next = findchar(start, end-start, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2543 if (next == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2544 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2545 *next = to_c;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2546 start = next+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2547 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2548
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2549 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2550 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2551
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2552 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2553 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2554 replace_substring_in_place(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2555 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2556 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2557 Py_ssize_t maxcount)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2558 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2559 char *result_s, *start, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2560 char *self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2561 Py_ssize_t self_len, offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2562 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2563
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2564 /* The result string will be the same size */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2565
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2566 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2567 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2568
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2569 offset = stringlib_find(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2570 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2571 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2572 if (offset == -1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2573 /* No matches; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2574 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2575 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2576
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2577 /* Need to make a new string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2578 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2579 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2580 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2581 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2582 Py_MEMCPY(result_s, self_s, self_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2583
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2584 /* change everything in-place, starting with this one */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2585 start = result_s + offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2586 Py_MEMCPY(start, to_s, from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2587 start += from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2588 end = result_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2589
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2590 while ( --maxcount > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2591 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2592 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2593 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2594 if (offset==-1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2595 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2596 Py_MEMCPY(start+offset, to_s, from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2597 start += offset+from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2598 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2599
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2600 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2601 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2602
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2603 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2604 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2605 replace_single_character(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2606 char from_c,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2607 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2608 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2609 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2610 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2611 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2612 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2613 Py_ssize_t count, product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2614 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2615
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2616 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2617 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2618
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2619 count = countchar(self_s, self_len, from_c, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2620 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2621 /* no matches, return unchanged */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2622 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2623 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2624
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2625 /* use the difference between current and new, hence the "-1" */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2626 /* result_len = self_len + count * (to_len-1) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2627 product = count * (to_len-1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2628 if (product / (to_len-1) != count) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2629 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2630 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2631 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2632 result_len = self_len + product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2633 if (result_len < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2634 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2635 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2636 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2637
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2638 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2639 PyString_FromStringAndSize(NULL, result_len)) == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2640 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2641 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2642
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2643 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2644 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2645 while (count-- > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2646 next = findchar(start, end-start, from_c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2647 if (next == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2648 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2649
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2650 if (next == start) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2651 /* replace with the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2652 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2653 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2654 start += 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2655 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2656 /* copy the unchanged old then the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2657 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2658 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2659 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2660 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2661 start = next+1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2662 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2663 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2664 /* Copy the remainder of the remaining string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2665 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2666
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2667 return result;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2668 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2669
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2670 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2671 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2672 replace_substring(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2673 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2674 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2675 Py_ssize_t maxcount) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2676 char *self_s, *result_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2677 char *start, *next, *end;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2678 Py_ssize_t self_len, result_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2679 Py_ssize_t count, offset, product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2680 PyStringObject *result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2681
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2682 self_s = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2683 self_len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2684
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2685 count = stringlib_count(self_s, self_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2686 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2687 maxcount);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2688
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2689 if (count == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2690 /* no matches, return unchanged */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2691 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2692 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2693
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2694 /* Check for overflow */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2695 /* result_len = self_len + count * (to_len-from_len) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2696 product = count * (to_len-from_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2697 if (product / (to_len-from_len) != count) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2698 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2699 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2700 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2701 result_len = self_len + product;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2702 if (result_len < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2703 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2704 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2705 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2706
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2707 if ( (result = (PyStringObject *)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2708 PyString_FromStringAndSize(NULL, result_len)) == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2709 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2710 result_s = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2711
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2712 start = self_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2713 end = self_s + self_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2714 while (count-- > 0) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2715 offset = stringlib_find(start, end-start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2716 from_s, from_len,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2717 0);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2718 if (offset == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2719 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2720 next = start+offset;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2721 if (next == start) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2722 /* replace with the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2723 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2724 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2725 start += from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2726 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2727 /* copy the unchanged old then the 'to' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2728 Py_MEMCPY(result_s, start, next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2729 result_s += (next-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2730 Py_MEMCPY(result_s, to_s, to_len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2731 result_s += to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2732 start = next+from_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2733 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2734 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2735 /* Copy the remainder of the remaining string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2736 Py_MEMCPY(result_s, start, end-start);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2737
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2738 return result;
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2739 }
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2740
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2741
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2742 Py_LOCAL(PyStringObject *)
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2743 replace(PyStringObject *self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2744 const char *from_s, Py_ssize_t from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2745 const char *to_s, Py_ssize_t to_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2746 Py_ssize_t maxcount)
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2747 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2748 if (maxcount < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2749 maxcount = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2750 } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2751 /* nothing to do; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2752 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2753 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2754
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2755 if (maxcount == 0 ||
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2756 (from_len == 0 && to_len == 0)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2757 /* nothing to do; return the original string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2758 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2759 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2760
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2761 /* Handle zero-length special cases */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2762
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2763 if (from_len == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2764 /* insert the 'to' string everywhere. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2765 /* >>> "Python".replace("", ".") */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2766 /* '.P.y.t.h.o.n.' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2767 return replace_interleave(self, to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2768 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2769
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2770 /* Except for "".replace("", "A") == "A" there is no way beyond this */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2771 /* point for an empty self string to generate a non-empty string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2772 /* Special case so the remaining code always gets a non-empty string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2773 if (PyString_GET_SIZE(self) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2774 return return_self(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2775 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2776
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2777 if (to_len == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2778 /* delete all occurances of 'from' string */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2779 if (from_len == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2780 return replace_delete_single_character(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2781 self, from_s[0], maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2782 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2783 return replace_delete_substring(self, from_s, from_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2784 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2785 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2786
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2787 /* Handle special case where both strings have the same length */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2788
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2789 if (from_len == to_len) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2790 if (from_len == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2791 return replace_single_character_in_place(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2792 self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2793 from_s[0],
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2794 to_s[0],
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2795 maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2796 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2797 return replace_substring_in_place(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2798 self, from_s, from_len, to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2799 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2800 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2801
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2802 /* Otherwise use the more generic algorithms */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2803 if (from_len == 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2804 return replace_single_character(self, from_s[0],
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2805 to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2806 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2807 /* len('from')>=2, len('to')>=1 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2808 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2809 }
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2810 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2811
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2812 PyDoc_STRVAR(replace__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2813 "S.replace(old, new[, count]) -> string\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2814 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2815 Return a copy of string S with all occurrences of substring\n\
30751
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2816 old replaced by new. If the optional argument count is\n\
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2817 given, only the first count occurrences are replaced.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2818
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2819 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2820 string_replace(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2821 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2822 Py_ssize_t count = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2823 PyObject *from, *to;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2824 const char *from_s, *to_s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2825 Py_ssize_t from_len, to_len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2826
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2827 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2828 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2829
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2830 if (PyString_Check(from)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2831 from_s = PyString_AS_STRING(from);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2832 from_len = PyString_GET_SIZE(from);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2833 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2834 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2835 if (PyUnicode_Check(from))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2836 return PyUnicode_Replace((PyObject *)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2837 from, to, count);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2838 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2839 else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2840 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2841
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2842 if (PyString_Check(to)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2843 to_s = PyString_AS_STRING(to);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2844 to_len = PyString_GET_SIZE(to);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2845 }
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2846 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2847 else if (PyUnicode_Check(to))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2848 return PyUnicode_Replace((PyObject *)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2849 from, to, count);
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2850 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2851 else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2852 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2853
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2854 return (PyObject *)replace((PyStringObject *) self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2855 from_s, from_len,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2856 to_s, to_len, count);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2857 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2858
38300
41809406a35e needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents: 38299
diff changeset
2859 /** End DALKE **/
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2860
38738
0a8a392e1fae Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
2861 /* Matches the end (direction >= 0) or start (direction < 0) of self
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2862 * against substr, using the start and end arguments. Returns
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2863 * -1 on error, 0 if not found and 1 if found.
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2864 */
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2865 Py_LOCAL(int)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2866 _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2867 Py_ssize_t end, int direction)
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2868 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2869 Py_ssize_t len = PyString_GET_SIZE(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2870 Py_ssize_t slen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2871 const char* sub;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2872 const char* str;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2873
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2874 if (PyString_Check(substr)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2875 sub = PyString_AS_STRING(substr);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2876 slen = PyString_GET_SIZE(substr);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2877 }
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2878 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2879 else if (PyUnicode_Check(substr))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2880 return PyUnicode_Tailmatch((PyObject *)self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2881 substr, start, end, direction);
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2882 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2883 else if (PyObject_AsCharBuffer(substr, &sub, &slen))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2884 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2885 str = PyString_AS_STRING(self);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2886
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
2887 ADJUST_INDICES(start, end, len);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2888
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2889 if (direction < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2890 /* startswith */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2891 if (start+slen > len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2892 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2893 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2894 /* endswith */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2895 if (end-start < slen || start > len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2896 return 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2897
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2898 if (end-slen > start)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2899 start = end - slen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2900 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2901 if (end-start >= slen)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2902 return ! memcmp(str+start, sub, slen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2903 return 0;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2904 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2905
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2906
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2907 PyDoc_STRVAR(startswith__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2908 "S.startswith(prefix[, start[, end]]) -> bool\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2909 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2910 Return True if S starts with the specified prefix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2911 With optional start, test S beginning at that position.\n\
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2912 With optional end, stop comparing S at that position.\n\
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2913 prefix can also be a tuple of strings to try.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2914
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2915 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2916 string_startswith(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2917 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2918 Py_ssize_t start = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2919 Py_ssize_t end = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2920 PyObject *subobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2921 int result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2922
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
2923 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2924 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2925 if (PyTuple_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2926 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2927 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2928 result = _string_tailmatch(self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2929 PyTuple_GET_ITEM(subobj, i),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2930 start, end, -1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2931 if (result == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2932 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2933 else if (result) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2934 Py_RETURN_TRUE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2935 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2936 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2937 Py_RETURN_FALSE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2938 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2939 result = _string_tailmatch(self, subobj, start, end, -1);
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2940 if (result == -1) {
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2941 if (PyErr_ExceptionMatches(PyExc_TypeError))
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2942 PyErr_Format(PyExc_TypeError, "startswith first arg must be str, "
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2943 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2944 return NULL;
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2945 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2946 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2947 return PyBool_FromLong(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2948 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2949
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2950
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2951 PyDoc_STRVAR(endswith__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2952 "S.endswith(suffix[, start[, end]]) -> bool\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2953 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2954 Return True if S ends with the specified suffix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2955 With optional start, test S beginning at that position.\n\
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2956 With optional end, stop comparing S at that position.\n\
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38531
diff changeset
2957 suffix can also be a tuple of strings to try.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2958
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2959 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2960 string_endswith(PyStringObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2961 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2962 Py_ssize_t start = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2963 Py_ssize_t end = PY_SSIZE_T_MAX;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2964 PyObject *subobj;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2965 int result;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2966
69471
f4da64529e8c startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents: 68907
diff changeset
2967 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2968 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2969 if (PyTuple_Check(subobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2970 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2971 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2972 result = _string_tailmatch(self,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2973 PyTuple_GET_ITEM(subobj, i),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2974 start, end, +1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2975 if (result == -1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2976 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2977 else if (result) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2978 Py_RETURN_TRUE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2979 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2980 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2981 Py_RETURN_FALSE;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2982 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2983 result = _string_tailmatch(self, subobj, start, end, +1);
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2984 if (result == -1) {
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2985 if (PyErr_ExceptionMatches(PyExc_TypeError))
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2986 PyErr_Format(PyExc_TypeError, "endswith first arg must be str, "
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2987 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2988 return NULL;
69562
3ceeccbc2c3b #6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents: 69471
diff changeset
2989 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2990 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
2991 return PyBool_FromLong(result);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2992 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2993
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2994
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2995 PyDoc_STRVAR(encode__doc__,
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2996 "S.encode([encoding[,errors]]) -> object\n\
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2997 \n\
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2998 Encodes S using the codec registered for encoding. encoding defaults\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2999 to the default encoding. errors may be given to set a different error\n\
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3000 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3001 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3002 'xmlcharrefreplace' as well as any other name registered with\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3003 codecs.register_error that is able to handle UnicodeEncodeErrors.");
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3004
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3005 static PyObject *
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3006 string_encode(PyStringObject *self, PyObject *args, PyObject *kwargs)
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3007 {
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3008 static char *kwlist[] = {"encoding", "errors", 0};
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3009 char *encoding = NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3010 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3011 PyObject *v;
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
3012
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3013 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3014 kwlist, &encoding, &errors))
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3015 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3016 v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3017 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3018 goto onError;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3019 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3020 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3021 "encoder did not return a string/unicode object "
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3022 "(type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
3023 Py_TYPE(v)->tp_name);
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3024 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3025 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3026 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3027 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3028
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3029 onError:
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3030 return NULL;
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3031 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3032
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3033
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3034 PyDoc_STRVAR(decode__doc__,
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3035 "S.decode([encoding[,errors]]) -> object\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3036 \n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3037 Decodes S using the codec registered for encoding. encoding defaults\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3038 to the default encoding. errors may be given to set a different error\n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3039 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3040 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3041 as well as any other name registered with codecs.register_error that is\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
3042 able to handle UnicodeDecodeErrors.");
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3043
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3044 static PyObject *
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3045 string_decode(PyStringObject *self, PyObject *args, PyObject *kwargs)
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3046 {
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3047 static char *kwlist[] = {"encoding", "errors", 0};
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3048 char *encoding = NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3049 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3050 PyObject *v;
37037
bfb74a420ac8 Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents: 36882
diff changeset
3051
56093
f64c0eb8cc62 add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents: 56050
diff changeset
3052 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3053 kwlist, &encoding, &errors))
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
3054 return NULL;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3055 v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3056 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3057 goto onError;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3058 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3059 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3060 "decoder did not return a string/unicode object "
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3061 "(type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44225
diff changeset
3062 Py_TYPE(v)->tp_name);
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3063 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3064 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3065 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32423
diff changeset
3066 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3067
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3068 onError:
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
3069 return NULL;
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3070 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3071
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
3072
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3073 PyDoc_STRVAR(expandtabs__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3074 "S.expandtabs([tabsize]) -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3075 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3076 Return a copy of S where all tab characters are expanded using spaces.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3077 If tabsize is not given, a tab size of 8 characters is assumed.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3078
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3079 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3080 string_expandtabs(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3081 {
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3082 const char *e, *p, *qe;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3083 char *q;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3084 Py_ssize_t i, j, incr;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3085 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3086 int tabsize = 8;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3087
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3088 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3089 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3090
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
3091 /* First pass: determine size of output string */
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3092 i = 0; /* chars up to and including most recent \n or \r */
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3093 j = 0; /* chars since most recent \n or \r (use in tab calculations) */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3094 e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); /* end of input */
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3095 for (p = PyString_AS_STRING(self); p < e; p++)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3096 if (*p == '\t') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3097 if (tabsize > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3098 incr = tabsize - (j % tabsize);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3099 if (j > PY_SSIZE_T_MAX - incr)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3100 goto overflow1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3101 j += incr;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3102 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3103 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3104 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3105 if (j > PY_SSIZE_T_MAX - 1)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3106 goto overflow1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3107 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3108 if (*p == '\n' || *p == '\r') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3109 if (i > PY_SSIZE_T_MAX - j)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3110 goto overflow1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3111 i += j;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3112 j = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3113 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3114 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3115
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3116 if (i > PY_SSIZE_T_MAX - j)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3117 goto overflow1;
42004
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 41440
diff changeset
3118
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3119 /* Second pass: create output string and fill it */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3120 u = PyString_FromStringAndSize(NULL, i + j);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3121 if (!u)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3122 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3123
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3124 j = 0; /* same as in first pass */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3125 q = PyString_AS_STRING(u); /* next output char */
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3126 qe = PyString_AS_STRING(u) + PyString_GET_SIZE(u); /* end of output */
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3127
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3128 for (p = PyString_AS_STRING(self); p < e; p++)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3129 if (*p == '\t') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3130 if (tabsize > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3131 i = tabsize - (j % tabsize);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3132 j += i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3133 while (i--) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3134 if (q >= qe)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3135 goto overflow2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3136 *q++ = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3137 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3138 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3139 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3140 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3141 if (q >= qe)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3142 goto overflow2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3143 *q++ = *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3144 j++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3145 if (*p == '\n' || *p == '\r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3146 j = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3147 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3148
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3149 return u;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3150
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3151 overflow2:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3152 Py_DECREF(u);
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3153 overflow1:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3154 PyErr_SetString(PyExc_OverflowError, "new string is too long");
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
3155 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3156 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3157
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
3158 Py_LOCAL_INLINE(PyObject *)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3159 pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3160 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3161 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3162
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3163 if (left < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3164 left = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3165 if (right < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3166 right = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3167
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3168 if (left == 0 && right == 0 && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3169 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3170 return (PyObject *)self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3171 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3172
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3173 u = PyString_FromStringAndSize(NULL,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3174 left + PyString_GET_SIZE(self) + right);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3175 if (u) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3176 if (left)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3177 memset(PyString_AS_STRING(u), fill, left);
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3178 Py_MEMCPY(PyString_AS_STRING(u) + left,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3179 PyString_AS_STRING(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3180 PyString_GET_SIZE(self));
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3181 if (right)
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3182 memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self),
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3183 fill, right);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3184 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3185
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3186 return u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3187 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3188
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3189 PyDoc_STRVAR(ljust__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3190 "S.ljust(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
3191 "\n"
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3192 "Return S left-justified in a string of length width. Padding is\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3193 "done using the specified fill character (default is a space).");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3194
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3195 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3196 string_ljust(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3197 {
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3198 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3199 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3200
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3201 if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3202 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3203
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3204 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3205 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3206 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3207 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3208
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3209 return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3210 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3211
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3212
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3213 PyDoc_STRVAR(rjust__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3214 "S.rjust(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
3215 "\n"
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3216 "Return S right-justified in a string of length width. Padding is\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3217 "done using the specified fill character (default is a space)");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3218
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3219 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3220 string_rjust(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3221 {
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3222 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3223 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3224
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3225 if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3226 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3227
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3228 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3229 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3230 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3231 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3232
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3233 return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3234 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3235
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3236
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3237 PyDoc_STRVAR(center__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3238 "S.center(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
3239 "\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3240 "Return S centered in a string of length width. Padding is\n"
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3241 "done using the specified fill character (default is a space)");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3242
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3243 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3244 string_center(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3245 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
3246 Py_ssize_t marg, left;
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3247 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3248 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3249
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3250 if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3251 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3252
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3253 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3254 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3255 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3256 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3257
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3258 marg = width - PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3259 left = marg / 2 + (marg & width & 1);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3260
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
3261 return pad(self, left, marg - left, fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3262 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3263
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3264 PyDoc_STRVAR(zfill__doc__,
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3265 "S.zfill(width) -> string\n"
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3266 "\n"
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3267 "Pad a numeric string S with zeros on the left, to fill a field\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3268 "of the specified width. The string S is never truncated.");
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3269
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3270 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3271 string_zfill(PyStringObject *self, PyObject *args)
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3272 {
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
3273 Py_ssize_t fill;
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3274 PyObject *s;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3275 char *p;
37855
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3276 Py_ssize_t width;
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3277
dc8efd70ab56 Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents: 37815
diff changeset
3278 if (!PyArg_ParseTuple(args, "n:zfill", &width))
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3279 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3280
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3281 if (PyString_GET_SIZE(self) >= width) {
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3282 if (PyString_CheckExact(self)) {
23051
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3283 Py_INCREF(self);
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3284 return (PyObject*) self;
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3285 }
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3286 else
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3287 return PyString_FromStringAndSize(
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3288 PyString_AS_STRING(self),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3289 PyString_GET_SIZE(self)
23051
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
3290 );
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3291 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3292
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3293 fill = width - PyString_GET_SIZE(self);
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3294
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3295 s = pad(self, fill, 0, '0');
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3296
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3297 if (s == NULL)
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3298 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3299
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3300 p = PyString_AS_STRING(s);
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3301 if (p[fill] == '+' || p[fill] == '-') {
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3302 /* move sign to beginning of string */
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3303 p[0] = p[fill];
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3304 p[fill] = '0';
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3305 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3306
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3307 return (PyObject*) s;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3308 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
3309
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3310 PyDoc_STRVAR(isspace__doc__,
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3311 "S.isspace() -> bool\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3312 \n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3313 Return True if all characters in S are whitespace\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3314 and there is at least one character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3315
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3316 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3317 string_isspace(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3318 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3319 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3320 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3321 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3322
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3323 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3324 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3325 isspace(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3326 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3327
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3328 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3329 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3330 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3331
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3332 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3333 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3334 if (!isspace(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3335 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3336 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3337 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3338 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3339
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3340
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3341 PyDoc_STRVAR(isalpha__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3342 "S.isalpha() -> bool\n\
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3343 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3344 Return True if all characters in S are alphabetic\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3345 and there is at least one character in S, False otherwise.");
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3346
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3347 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3348 string_isalpha(PyStringObject *self)
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3349 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3350 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3351 = (unsigned char *) PyString_AS_STRING(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3352 register const unsigned char *e;
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3353
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3354 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3355 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3356 isalpha(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3357 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3358
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3359 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3360 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3361 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3362
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3363 e = p + PyString_GET_SIZE(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3364 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3365 if (!isalpha(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3366 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3367 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3368 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3369 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3370
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3371
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3372 PyDoc_STRVAR(isalnum__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3373 "S.isalnum() -> bool\n\
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3374 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3375 Return True if all characters in S are alphanumeric\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3376 and there is at least one character in S, False otherwise.");
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3377
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3378 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3379 string_isalnum(PyStringObject *self)
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3380 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3381 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3382 = (unsigned char *) PyString_AS_STRING(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3383 register const unsigned char *e;
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3384
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3385 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3386 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3387 isalnum(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3388 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3389
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3390 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3391 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3392 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3393
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3394 e = p + PyString_GET_SIZE(self);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3395 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3396 if (!isalnum(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3397 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3398 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3399 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3400 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3401
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3402
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3403 PyDoc_STRVAR(isdigit__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3404 "S.isdigit() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3405 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3406 Return True if all characters in S are digits\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3407 and there is at least one character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3408
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3409 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3410 string_isdigit(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3411 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3412 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3413 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3414 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3415
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3416 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3417 if (PyString_GET_SIZE(self) == 1 &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3418 isdigit(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3419 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3420
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3421 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3422 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3423 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3424
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3425 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3426 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3427 if (!isdigit(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3428 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3429 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3430 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3431 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3432
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3433
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3434 PyDoc_STRVAR(islower__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3435 "S.islower() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3436 \n\
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3437 Return True if all cased characters in S are lowercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3438 at least one cased character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3439
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3440 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3441 string_islower(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3442 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3443 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3444 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3445 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3446 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3447
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3448 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3449 if (PyString_GET_SIZE(self) == 1)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3450 return PyBool_FromLong(islower(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3451
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3452 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3453 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3454 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3455
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3456 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3457 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3458 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3459 if (isupper(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3460 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3461 else if (!cased && islower(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3462 cased = 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3463 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3464 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3465 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3466
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3467
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3468 PyDoc_STRVAR(isupper__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3469 "S.isupper() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3470 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3471 Return True if all cased characters in S are uppercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3472 at least one cased character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3473
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3474 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3475 string_isupper(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3476 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3477 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3478 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3479 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3480 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3481
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3482 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3483 if (PyString_GET_SIZE(self) == 1)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3484 return PyBool_FromLong(isupper(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3485
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3486 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3487 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3488 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3489
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3490 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3491 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3492 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3493 if (islower(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3494 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3495 else if (!cased && isupper(*p))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3496 cased = 1;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3497 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3498 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3499 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3500
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3501
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3502 PyDoc_STRVAR(istitle__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3503 "S.istitle() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3504 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3505 Return True if S is a titlecased string and there is at least one\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3506 character in S, i.e. uppercase characters may only follow uncased\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3507 characters and lowercase characters only cased ones. Return False\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3508 otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3509
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3510 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3511 string_istitle(PyStringObject *self, PyObject *uncased)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3512 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3513 register const unsigned char *p
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3514 = (unsigned char *) PyString_AS_STRING(self);
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3515 register const unsigned char *e;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3516 int cased, previous_is_cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3517
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3518 /* Shortcut for single character strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3519 if (PyString_GET_SIZE(self) == 1)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3520 return PyBool_FromLong(isupper(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3521
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3522 /* Special case for empty strings */
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3523 if (PyString_GET_SIZE(self) == 0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3524 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3525
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3526 e = p + PyString_GET_SIZE(self);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3527 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3528 previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3529 for (; p < e; p++) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3530 register const unsigned char ch = *p;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3531
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3532 if (isupper(ch)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3533 if (previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3534 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3535 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3536 cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3537 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3538 else if (islower(ch)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3539 if (!previous_is_cased)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3540 return PyBool_FromLong(0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3541 previous_is_cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3542 cased = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3543 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3544 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3545 previous_is_cased = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3546 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3547 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3548 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3549
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3550
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3551 PyDoc_STRVAR(splitlines__doc__,
77292
2fbee0b741f7 Improve tooltips for splitlines() by showing that the default for keepends is False.
Raymond Hettinger <python@rcn.com>
parents: 76189
diff changeset
3552 "S.splitlines(keepends=False) -> list of strings\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3553 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3554 Return a list of the lines in S, breaking at line boundaries.\n\
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3555 Line breaks are not included in the resulting list unless keepends\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3556 is given and true.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3557
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3558 static PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3559 string_splitlines(PyStringObject *self, PyObject *args)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3560 {
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3561 int keepends = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3562
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3563 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3564 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3565
58288
66f1d4a4c799 Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
3566 return stringlib_splitlines(
62208
ba2f2d08504b Issue #8930: fix some C code indentation
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
3567 (PyObject*) self, PyString_AS_STRING(self), PyString_GET_SIZE(self),
ba2f2d08504b Issue #8930: fix some C code indentation
Antoine Pitrou <solipsis@pitrou.net>
parents: 61463
diff changeset
3568 keepends
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3569 );
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3570 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3571
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3572 PyDoc_STRVAR(sizeof__doc__,
47421
a65f095b27f3 Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents: 47419
diff changeset
3573 "S.__sizeof__() -> size of S in memory, in bytes");
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3574
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3575 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3576 string_sizeof(PyStringObject *v)
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3577 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3578 Py_ssize_t res;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3579 res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3580 return PyInt_FromSsize_t(res);
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3581 }
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3582
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3583 static PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3584 string_getnewargs(PyStringObject *v)
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3585 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3586 return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v));
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3587 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3588
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3589
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3590 #include "stringlib/string_format.h"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3591
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3592 PyDoc_STRVAR(format__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3593 "S.format(*args, **kwargs) -> string\n\
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3594 \n\
66044
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3595 Return a formatted version of S, using substitutions from args and kwargs.\n\
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3596 The substitutions are identified by braces ('{' and '}').");
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3597
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3598 static PyObject *
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3599 string__format__(PyObject* self, PyObject* args)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3600 {
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3601 PyObject *format_spec;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3602 PyObject *result = NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3603 PyObject *tmp = NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3604
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3605 /* If 2.x, convert format_spec to the same type as value */
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3606 /* This is to allow things like u''.format('') */
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3607 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3608 goto done;
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3609 if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) {
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3610 PyErr_Format(PyExc_TypeError, "__format__ arg must be str "
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3611 "or unicode, not %s", Py_TYPE(format_spec)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3612 goto done;
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3613 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3614 tmp = PyObject_Str(format_spec);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3615 if (tmp == NULL)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3616 goto done;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3617 format_spec = tmp;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3618
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3619 result = _PyBytes_FormatAdvanced(self,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3620 PyString_AS_STRING(format_spec),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3621 PyString_GET_SIZE(format_spec));
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3622 done:
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3623 Py_XDECREF(tmp);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3624 return result;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3625 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47296
diff changeset
3626
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3627 PyDoc_STRVAR(p_format__doc__,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3628 "S.__format__(format_spec) -> string\n\
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3629 \n\
66044
1499db99c4cd Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents: 63374
diff changeset
3630 Return a formatted version of S as described by format_spec.");
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 44350
diff changeset
3631
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3632
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3633 static PyMethodDef
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3634 string_methods[] = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3635 /* Counterparts of the obsolete stropmodule functions; except
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3636 string.maketrans(). */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3637 {"join", (PyCFunction)string_join, METH_O, join__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3638 {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3639 {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3640 {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3641 {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3642 {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3643 {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3644 {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3645 {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3646 {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3647 {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3648 {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3649 {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3650 capitalize__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3651 {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3652 {"endswith", (PyCFunction)string_endswith, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3653 endswith__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3654 {"partition", (PyCFunction)string_partition, METH_O, partition__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3655 {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3656 {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3657 {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3658 {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3659 {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3660 {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3661 {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3662 {"rpartition", (PyCFunction)string_rpartition, METH_O,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3663 rpartition__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3664 {"startswith", (PyCFunction)string_startswith, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3665 startswith__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3666 {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3667 {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3668 swapcase__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3669 {"translate", (PyCFunction)string_translate, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3670 translate__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3671 {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3672 {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3673 {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3674 {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3675 {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3676 {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3677 {"__format__", (PyCFunction) string__format__, METH_VARARGS, p_format__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3678 {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3679 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3680 {"encode", (PyCFunction)string_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3681 {"decode", (PyCFunction)string_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__},
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3682 {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3683 expandtabs__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3684 {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3685 splitlines__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3686 {"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3687 sizeof__doc__},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3688 {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS},
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3689 {NULL, NULL} /* sentinel */
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3690 };
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3691
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24184
diff changeset
3692 static PyObject *
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3693 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3694
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3695 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3696 string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3697 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3698 PyObject *x = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3699 static char *kwlist[] = {"object", 0};
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3700
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3701 if (type != &PyString_Type)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3702 return str_subtype_new(type, args, kwds);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3703 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3704 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3705 if (x == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3706 return PyString_FromString("");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3707 return PyObject_Str(x);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3708 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3709
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3710 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3711 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3712 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3713 PyObject *tmp, *pnew;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3714 Py_ssize_t n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3715
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3716 assert(PyType_IsSubtype(type, &PyString_Type));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3717 tmp = string_new(&PyString_Type, args, kwds);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3718 if (tmp == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3719 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3720 assert(PyString_CheckExact(tmp));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3721 n = PyString_GET_SIZE(tmp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3722 pnew = type->tp_alloc(type, n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3723 if (pnew != NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3724 Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3725 ((PyStringObject *)pnew)->ob_shash =
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3726 ((PyStringObject *)tmp)->ob_shash;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3727 ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3728 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3729 Py_DECREF(tmp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3730 return pnew;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3731 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3732
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3733 static PyObject *
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3734 basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3735 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3736 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3737 "The basestring type cannot be instantiated");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3738 return NULL;
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3739 }
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3740
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3741 static PyObject *
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3742 string_mod(PyObject *v, PyObject *w)
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3743 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3744 if (!PyString_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3745 Py_INCREF(Py_NotImplemented);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3746 return Py_NotImplemented;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3747 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3748 return PyString_Format(v, w);
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3749 }
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3750
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3751 PyDoc_STRVAR(basestring_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3752 "Type basestring cannot be instantiated; it is the base for str and unicode.");
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3753
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3754 static PyNumberMethods string_as_number = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3755 0, /*nb_add*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3756 0, /*nb_subtract*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3757 0, /*nb_multiply*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3758 0, /*nb_divide*/
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3759 string_mod, /*nb_remainder*/
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3760 };
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3761
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3762
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3763 PyTypeObject PyBaseString_Type = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3764 PyVarObject_HEAD_INIT(&PyType_Type, 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3765 "basestring",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3766 0,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3767 0,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3768 0, /* tp_dealloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3769 0, /* tp_print */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3770 0, /* tp_getattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3771 0, /* tp_setattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3772 0, /* tp_compare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3773 0, /* tp_repr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3774 0, /* tp_as_number */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3775 0, /* tp_as_sequence */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3776 0, /* tp_as_mapping */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3777 0, /* tp_hash */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3778 0, /* tp_call */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3779 0, /* tp_str */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3780 0, /* tp_getattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3781 0, /* tp_setattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3782 0, /* tp_as_buffer */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3783 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3784 basestring_doc, /* tp_doc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3785 0, /* tp_traverse */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3786 0, /* tp_clear */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3787 0, /* tp_richcompare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3788 0, /* tp_weaklistoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3789 0, /* tp_iter */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3790 0, /* tp_iternext */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3791 0, /* tp_methods */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3792 0, /* tp_members */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3793 0, /* tp_getset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3794 &PyBaseObject_Type, /* tp_base */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3795 0, /* tp_dict */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3796 0, /* tp_descr_get */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3797 0, /* tp_descr_set */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3798 0, /* tp_dictoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3799 0, /* tp_init */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3800 0, /* tp_alloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3801 basestring_new, /* tp_new */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3802 0, /* tp_free */
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3803 };
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3804
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3805 PyDoc_STRVAR(string_doc,
79590
3b484f53f91b Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 78792
diff changeset
3806 "str(object='') -> string\n\
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3807 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3808 Return a nice string representation of the object.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3809 If the argument is a string, the return value is the same object.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3810
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3811 PyTypeObject PyString_Type = {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3812 PyVarObject_HEAD_INIT(&PyType_Type, 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3813 "str",
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3814 PyStringObject_SIZE,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3815 sizeof(char),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3816 string_dealloc, /* tp_dealloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3817 (printfunc)string_print, /* tp_print */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3818 0, /* tp_getattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3819 0, /* tp_setattr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3820 0, /* tp_compare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3821 string_repr, /* tp_repr */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3822 &string_as_number, /* tp_as_number */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3823 &string_as_sequence, /* tp_as_sequence */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3824 &string_as_mapping, /* tp_as_mapping */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3825 (hashfunc)string_hash, /* tp_hash */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3826 0, /* tp_call */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3827 string_str, /* tp_str */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3828 PyObject_GenericGetAttr, /* tp_getattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3829 0, /* tp_setattro */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3830 &string_as_buffer, /* tp_as_buffer */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3831 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3832 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_STRING_SUBCLASS |
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3833 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3834 string_doc, /* tp_doc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3835 0, /* tp_traverse */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3836 0, /* tp_clear */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3837 (richcmpfunc)string_richcompare, /* tp_richcompare */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3838 0, /* tp_weaklistoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3839 0, /* tp_iter */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3840 0, /* tp_iternext */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3841 string_methods, /* tp_methods */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3842 0, /* tp_members */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3843 0, /* tp_getset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3844 &PyBaseString_Type, /* tp_base */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3845 0, /* tp_dict */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3846 0, /* tp_descr_get */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3847 0, /* tp_descr_set */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3848 0, /* tp_dictoffset */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3849 0, /* tp_init */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3850 0, /* tp_alloc */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3851 string_new, /* tp_new */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3852 PyObject_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3853 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3854
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3855 void
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3856 PyString_Concat(register PyObject **pv, register PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3857 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3858 register PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3859 if (*pv == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3860 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3861 if (w == NULL || !PyString_Check(*pv)) {
81933
4d120cea3507 Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81737
diff changeset
3862 Py_CLEAR(*pv);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3863 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3864 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3865 v = string_concat((PyStringObject *) *pv, w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3866 Py_DECREF(*pv);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3867 *pv = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3868 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3869
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3870 void
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3871 PyString_ConcatAndDel(register PyObject **pv, register PyObject *w)
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3872 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3873 PyString_Concat(pv, w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3874 Py_XDECREF(w);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3875 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3876
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3877
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3878 /* The following function breaks the notion that strings are immutable:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3879 it changes the size of a string. We get away with this only if there
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3880 is only one module referencing the object. You can also think of it
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3881 as creating a new string object and destroying the old one, only
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3882 more efficiently. In any case, don't use this if the string may
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3883 already be known to some other part of the code...
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3884 Note that if there's not enough memory to resize the string, the original
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3885 string object at *pv is deallocated, *pv is set to NULL, an "out of
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3886 memory" exception is set, and -1 is returned. Else (on success) 0 is
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3887 returned, and the value in *pv may or may not be the same as on input.
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3888 As always, an extra byte is allocated for a trailing \0 byte (newsize
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3889 does *not* include that), and a trailing \0 byte is stored.
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3890 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3891
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3892 int
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3893 _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3894 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3895 register PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3896 register PyStringObject *sv;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3897 v = *pv;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3898 if (!PyString_Check(v) || Py_REFCNT(v) != 1 || newsize < 0 ||
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3899 PyString_CHECK_INTERNED(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3900 *pv = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3901 Py_DECREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3902 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3903 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3904 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3905 /* XXX UNREF/NEWREF interface should be more symmetrical */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3906 _Py_DEC_REFTOTAL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3907 _Py_ForgetReference(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3908 *pv = (PyObject *)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3909 PyObject_REALLOC((char *)v, PyStringObject_SIZE + newsize);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3910 if (*pv == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3911 PyObject_Del(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3912 PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3913 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3914 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3915 _Py_NewReference(*pv);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3916 sv = (PyStringObject *) *pv;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3917 Py_SIZE(sv) = newsize;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3918 sv->ob_sval[newsize] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3919 sv->ob_shash = -1; /* invalidate cached hash value */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3920 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3921 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3922
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3923 /* Helpers for formatstring */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3924
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
3925 Py_LOCAL_INLINE(PyObject *)
36528
76ce880dce49 Use Py_ssize_t in helper function between Py_ssize_t-using functions.
Thomas Wouters <thomas@python.org>
parents: 36519
diff changeset
3926 getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3927 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3928 Py_ssize_t argidx = *p_argidx;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3929 if (argidx < arglen) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3930 (*p_argidx)++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3931 if (arglen < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3932 return args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3933 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3934 return PyTuple_GetItem(args, argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3935 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3936 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3937 "not enough arguments for format string");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3938 return NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3939 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3940
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3941 /* Format codes
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3942 * F_LJUST '-'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3943 * F_SIGN '+'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3944 * F_BLANK ' '
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3945 * F_ALT '#'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3946 * F_ZERO '0'
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3947 */
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3948 #define F_LJUST (1<<0)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3949 #define F_SIGN (1<<1)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3950 #define F_BLANK (1<<2)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3951 #define F_ALT (1<<3)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3952 #define F_ZERO (1<<4)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3953
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3954 /* Returns a new reference to a PyString object, or NULL on failure. */
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3955
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3956 static PyObject *
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
3957 formatfloat(PyObject *v, int flags, int prec, int type)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3958 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3959 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3960 PyObject *result;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3961 double x;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3962
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3963 x = PyFloat_AsDouble(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3964 if (x == -1.0 && PyErr_Occurred()) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3965 PyErr_Format(PyExc_TypeError, "float argument required, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3966 "not %.200s", Py_TYPE(v)->tp_name);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3967 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3968 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3969
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3970 if (prec < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3971 prec = 6;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3972
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3973 p = PyOS_double_to_string(x, type, prec,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3974 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3975
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3976 if (p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3977 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3978 result = PyString_FromStringAndSize(p, strlen(p));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3979 PyMem_Free(p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
3980 return result;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3981 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3982
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
3983 /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3984 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3985 * Python's regular ints.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3986 * Return value: a new PyString*, or NULL if error.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3987 * . *pbuf is set to point into it,
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3988 * *plen set to the # of chars following that.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3989 * Caller must decref it when done using pbuf.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3990 * The string starting at *pbuf is of the form
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3991 * "-"? ("0x" | "0X")? digit+
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3992 * "0x"/"0X" are present only for x and X conversions, with F_ALT
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3993 * set in flags. The case of hex digits will be correct,
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3994 * There will be at least prec digits, zero-filled on the left if
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3995 * necessary to get that many.
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3996 * val object to be converted
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3997 * flags bitmask of format flags; only F_ALT is looked at
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3998 * prec minimum number of digits; 0-fill on left if needed
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
3999 * type a character in [duoxX]; u acts the same as d
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4000 *
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4001 * CAUTION: o, x and X conversions on regular ints can never
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4002 * produce a '-' sign, but can for Python's unbounded ints.
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4003 */
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4004 PyObject*
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
4005 _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4006 char **pbuf, int *plen)
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4007 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4008 PyObject *result = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4009 char *buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4010 Py_ssize_t i;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4011 int sign; /* 1 if '-', else 0 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4012 int len; /* number of characters */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4013 Py_ssize_t llen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4014 int numdigits; /* len == numnondigits + numdigits */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4015 int numnondigits = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4016
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4017 switch (type) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4018 case 'd':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4019 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4020 result = Py_TYPE(val)->tp_str(val);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4021 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4022 case 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4023 result = Py_TYPE(val)->tp_as_number->nb_oct(val);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4024 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4025 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4026 case 'X':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4027 numnondigits = 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4028 result = Py_TYPE(val)->tp_as_number->nb_hex(val);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4029 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4030 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4031 assert(!"'type' not in [duoxX]");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4032 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4033 if (!result)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4034 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4035
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4036 buf = PyString_AsString(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4037 if (!buf) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4038 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4039 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4040 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4041
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4042 /* To modify the string in-place, there can only be one reference. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4043 if (Py_REFCNT(result) != 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4044 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4045 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4046 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4047 llen = PyString_Size(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4048 if (llen > INT_MAX) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4049 PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4050 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4051 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4052 len = (int)llen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4053 if (buf[len-1] == 'L') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4054 --len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4055 buf[len] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4056 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4057 sign = buf[0] == '-';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4058 numnondigits += sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4059 numdigits = len - numnondigits;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4060 assert(numdigits > 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4061
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4062 /* Get rid of base marker unless F_ALT */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4063 if ((flags & F_ALT) == 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4064 /* Need to skip 0x, 0X or 0. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4065 int skipped = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4066 switch (type) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4067 case 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4068 assert(buf[sign] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4069 /* If 0 is only digit, leave it alone. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4070 if (numdigits > 1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4071 skipped = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4072 --numdigits;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4073 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4074 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4075 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4076 case 'X':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4077 assert(buf[sign] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4078 assert(buf[sign + 1] == 'x');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4079 skipped = 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4080 numnondigits -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4081 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4082 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4083 if (skipped) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4084 buf += skipped;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4085 len -= skipped;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4086 if (sign)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4087 buf[0] = '-';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4088 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4089 assert(len == numnondigits + numdigits);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4090 assert(numdigits > 0);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4091 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4092
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4093 /* Fill with leading zeroes to meet minimum width. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4094 if (prec > numdigits) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4095 PyObject *r1 = PyString_FromStringAndSize(NULL,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4096 numnondigits + prec);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4097 char *b1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4098 if (!r1) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4099 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4100 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4101 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4102 b1 = PyString_AS_STRING(r1);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4103 for (i = 0; i < numnondigits; ++i)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4104 *b1++ = *buf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4105 for (i = 0; i < prec - numdigits; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4106 *b1++ = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4107 for (i = 0; i < numdigits; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4108 *b1++ = *buf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4109 *b1 = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4110 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4111 result = r1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4112 buf = PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4113 len = numnondigits + prec;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4114 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4115
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4116 /* Fix up case for hex conversions. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4117 if (type == 'X') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4118 /* Need to convert all lower case letters to upper case.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4119 and need to convert 0x to 0X (and -0x to -0X). */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4120 for (i = 0; i < len; i++)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4121 if (buf[i] >= 'a' && buf[i] <= 'x')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4122 buf[i] -= 'a'-'A';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4123 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4124 *pbuf = buf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4125 *plen = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4126 return result;
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4127 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4128
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
4129 Py_LOCAL_INLINE(int)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4130 formatint(char *buf, size_t buflen, int flags,
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4131 int prec, int type, PyObject *v)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4132 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4133 /* fmt = '%#.' + `prec` + 'l' + `type`
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4134 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4135 + 1 + 1 = 24 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4136 char fmt[64]; /* plenty big enough! */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4137 char *sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4138 long x;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4139
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4140 x = PyInt_AsLong(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4141 if (x == -1 && PyErr_Occurred()) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4142 PyErr_Format(PyExc_TypeError, "int argument required, not %.200s",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4143 Py_TYPE(v)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4144 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4145 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4146 if (x < 0 && type == 'u') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4147 type = 'd';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4148 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4149 if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4150 sign = "-";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4151 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4152 sign = "";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4153 if (prec < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4154 prec = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4155
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4156 if ((flags & F_ALT) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4157 (type == 'x' || type == 'X')) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4158 /* When converting under %#x or %#X, there are a number
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4159 * of issues that cause pain:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4160 * - when 0 is being converted, the C standard leaves off
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4161 * the '0x' or '0X', which is inconsistent with other
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4162 * %#x/%#X conversions and inconsistent with Python's
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4163 * hex() function
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4164 * - there are platforms that violate the standard and
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4165 * convert 0 with the '0x' or '0X'
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4166 * (Metrowerks, Compaq Tru64)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4167 * - there are platforms that give '0x' when converting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4168 * under %#X, but convert 0 in accordance with the
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4169 * standard (OS/2 EMX)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4170 *
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4171 * We can achieve the desired consistency by inserting our
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4172 * own '0x' or '0X' prefix, and substituting %x/%X in place
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4173 * of %#x/%#X.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4174 *
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4175 * Note that this is the same approach as used in
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4176 * formatint() in unicodeobject.c
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4177 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4178 PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4179 sign, type, prec, type);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4180 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4181 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4182 PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4183 sign, (flags&F_ALT) ? "#" : "",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4184 prec, type);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4185 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4186
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4187 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4188 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4189 */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4190 if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4191 PyErr_SetString(PyExc_OverflowError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4192 "formatted integer is too long (precision too large?)");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4193 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4194 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4195 if (sign[0])
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4196 PyOS_snprintf(buf, buflen, fmt, -x);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4197 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4198 PyOS_snprintf(buf, buflen, fmt, x);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4199 return (int)strlen(buf);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4200 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4201
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38438
diff changeset
4202 Py_LOCAL_INLINE(int)
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4203 formatchar(char *buf, size_t buflen, PyObject *v)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4204 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4205 /* presume that the buffer is at least 2 characters long */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4206 if (PyString_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4207 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4208 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4209 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4210 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4211 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4212 return -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4213 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4214 buf[1] = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4215 return 1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4216 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4217
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4218 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4219
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 57277
diff changeset
4220 FORMATBUFLEN is the length of the buffer in which the ints &
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4221 chars are formatted. XXX This is a magic number. Each formatting
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4222 routine does bounds checking to ensure no overflow, but a better
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4223 solution may be to malloc a buffer of appropriate size for each
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4224 format. For now, the current solution is sufficient.
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4225 */
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4226 #define FORMATBUFLEN (size_t)120
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4227
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4228 PyObject *
47560
acda43252710 More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
4229 PyString_Format(PyObject *format, PyObject *args)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4230 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4231 char *fmt, *res;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4232 Py_ssize_t arglen, argidx;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4233 Py_ssize_t reslen, rescnt, fmtcnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4234 int args_owned = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4235 PyObject *result, *orig_args;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4236 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4237 PyObject *v, *w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4238 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4239 PyObject *dict = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4240 if (format == NULL || !PyString_Check(format) || args == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4241 PyErr_BadInternalCall();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4242 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4243 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4244 orig_args = args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4245 fmt = PyString_AS_STRING(format);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4246 fmtcnt = PyString_GET_SIZE(format);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4247 reslen = rescnt = fmtcnt + 100;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4248 result = PyString_FromStringAndSize((char *)NULL, reslen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4249 if (result == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4250 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4251 res = PyString_AsString(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4252 if (PyTuple_Check(args)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4253 arglen = PyTuple_GET_SIZE(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4254 argidx = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4255 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4256 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4257 arglen = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4258 argidx = -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4259 }
78792
2801bf875a24 use the stricter PyMapping_Check (closes #15801)
Benjamin Peterson <benjamin@python.org>
parents: 77292
diff changeset
4260 if (PyMapping_Check(args) && !PyTuple_Check(args) &&
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4261 !PyObject_TypeCheck(args, &PyBaseString_Type))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4262 dict = args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4263 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4264 if (*fmt != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4265 if (--rescnt < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4266 rescnt = fmtcnt + 100;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4267 reslen += rescnt;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4268 if (_PyString_Resize(&result, reslen))
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4269 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4270 res = PyString_AS_STRING(result)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4271 + reslen - rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4272 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4273 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4274 *res++ = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4275 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4276 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4277 /* Got a format specifier */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4278 int flags = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4279 Py_ssize_t width = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4280 int prec = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4281 int c = '\0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4282 int fill;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4283 int isnumok;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4284 PyObject *v = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4285 PyObject *temp = NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4286 char *pbuf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4287 int sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4288 Py_ssize_t len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4289 char formatbuf[FORMATBUFLEN];
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4290 /* For format{int,char}() */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4291 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4292 char *fmt_start = fmt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4293 Py_ssize_t argidx_start = argidx;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4294 #endif
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4295
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4296 fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4297 if (*fmt == '(') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4298 char *keystart;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4299 Py_ssize_t keylen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4300 PyObject *key;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4301 int pcount = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4302
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4303 if (dict == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4304 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4305 "format requires a mapping");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4306 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4307 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4308 ++fmt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4309 --fmtcnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4310 keystart = fmt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4311 /* Skip over balanced parentheses */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4312 while (pcount > 0 && --fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4313 if (*fmt == ')')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4314 --pcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4315 else if (*fmt == '(')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4316 ++pcount;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4317 fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4318 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4319 keylen = fmt - keystart - 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4320 if (fmtcnt < 0 || pcount > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4321 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4322 "incomplete format key");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4323 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4324 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4325 key = PyString_FromStringAndSize(keystart,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4326 keylen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4327 if (key == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4328 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4329 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4330 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4331 args_owned = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4332 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4333 args = PyObject_GetItem(dict, key);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4334 Py_DECREF(key);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4335 if (args == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4336 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4337 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4338 args_owned = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4339 arglen = -1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4340 argidx = -2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4341 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4342 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4343 switch (c = *fmt++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4344 case '-': flags |= F_LJUST; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4345 case '+': flags |= F_SIGN; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4346 case ' ': flags |= F_BLANK; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4347 case '#': flags |= F_ALT; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4348 case '0': flags |= F_ZERO; continue;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4349 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4350 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4351 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4352 if (c == '*') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4353 v = getnextarg(args, arglen, &argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4354 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4355 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4356 if (!PyInt_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4357 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4358 "* wants int");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4359 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4360 }
81605
ee93a89b4e0f Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81245
diff changeset
4361 width = PyInt_AsSsize_t(v);
ee93a89b4e0f Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81245
diff changeset
4362 if (width == -1 && PyErr_Occurred())
ee93a89b4e0f Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81245
diff changeset
4363 goto error;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4364 if (width < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4365 flags |= F_LJUST;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4366 width = -width;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4367 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4368 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4369 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4370 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4371 else if (c >= 0 && isdigit(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4372 width = c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4373 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4374 c = Py_CHARMASK(*fmt++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4375 if (!isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4376 break;
80014
21fb1767e185 Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 79590
diff changeset
4377 if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4378 PyErr_SetString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4379 PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4380 "width too big");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4381 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4382 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4383 width = width*10 + (c - '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4384 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4385 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4386 if (c == '.') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4387 prec = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4388 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4389 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4390 if (c == '*') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4391 v = getnextarg(args, arglen, &argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4392 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4393 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4394 if (!PyInt_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4395 PyErr_SetString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4396 PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4397 "* wants int");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4398 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4399 }
81605
ee93a89b4e0f Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81245
diff changeset
4400 prec = _PyInt_AsInt(v);
ee93a89b4e0f Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81245
diff changeset
4401 if (prec == -1 && PyErr_Occurred())
ee93a89b4e0f Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81245
diff changeset
4402 goto error;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4403 if (prec < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4404 prec = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4405 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4406 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4407 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4408 else if (c >= 0 && isdigit(c)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4409 prec = c - '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4410 while (--fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4411 c = Py_CHARMASK(*fmt++);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4412 if (!isdigit(c))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4413 break;
80014
21fb1767e185 Issue #14700: Fix buggy overflow checks for large precision and width in new-style and old-style formatting.
Mark Dickinson <mdickinson@enthought.com>
parents: 79590
diff changeset
4414 if (prec > (INT_MAX - ((int)c - '0')) / 10) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4415 PyErr_SetString(
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4416 PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4417 "prec too big");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4418 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4419 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4420 prec = prec*10 + (c - '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4421 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4422 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4423 } /* prec */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4424 if (fmtcnt >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4425 if (c == 'h' || c == 'l' || c == 'L') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4426 if (--fmtcnt >= 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4427 c = *fmt++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4428 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4429 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4430 if (fmtcnt < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4431 PyErr_SetString(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4432 "incomplete format");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4433 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4434 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4435 if (c != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4436 v = getnextarg(args, arglen, &argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4437 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4438 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4439 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4440 sign = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4441 fill = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4442 switch (c) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4443 case '%':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4444 pbuf = "%";
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4445 len = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4446 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4447 case 's':
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4448 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4449 if (PyUnicode_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4450 fmt = fmt_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4451 argidx = argidx_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4452 goto unicode;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4453 }
35665
dc5d706283d6 Fix PyString_Format so that the "%s" format works again when Unicode is not
Georg Brandl <georg@python.org>
parents: 35503
diff changeset
4454 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4455 temp = _PyObject_Str(v);
35665
dc5d706283d6 Fix PyString_Format so that the "%s" format works again when Unicode is not
Georg Brandl <georg@python.org>
parents: 35503
diff changeset
4456 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4457 if (temp != NULL && PyUnicode_Check(temp)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4458 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4459 fmt = fmt_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4460 argidx = argidx_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4461 goto unicode;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4462 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4463 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4464 /* Fall through */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4465 case 'r':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4466 if (c == 'r')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4467 temp = PyObject_Repr(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4468 if (temp == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4469 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4470 if (!PyString_Check(temp)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4471 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4472 "%s argument has non-string str()");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4473 Py_DECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4474 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4475 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4476 pbuf = PyString_AS_STRING(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4477 len = PyString_GET_SIZE(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4478 if (prec >= 0 && len > prec)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4479 len = prec;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4480 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4481 case 'i':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4482 case 'd':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4483 case 'u':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4484 case 'o':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4485 case 'x':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4486 case 'X':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4487 if (c == 'i')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4488 c = 'd';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4489 isnumok = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4490 if (PyNumber_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4491 PyObject *iobj=NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4492
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4493 if (PyInt_Check(v) || (PyLong_Check(v))) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4494 iobj = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4495 Py_INCREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4496 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4497 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4498 iobj = PyNumber_Int(v);
81240
80cfd4caa726 call PyErr_Clear() when ignoring error from PyNumber_Int (closes #15516)
Benjamin Peterson <benjamin@python.org>
parents: 80014
diff changeset
4499 if (iobj==NULL) {
81245
ded5386604fd untabify
Benjamin Peterson <benjamin@python.org>
parents: 81240
diff changeset
4500 PyErr_Clear();
ded5386604fd untabify
Benjamin Peterson <benjamin@python.org>
parents: 81240
diff changeset
4501 iobj = PyNumber_Long(v);
ded5386604fd untabify
Benjamin Peterson <benjamin@python.org>
parents: 81240
diff changeset
4502 }
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4503 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4504 if (iobj!=NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4505 if (PyInt_Check(iobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4506 isnumok = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4507 pbuf = formatbuf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4508 len = formatint(pbuf,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4509 sizeof(formatbuf),
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4510 flags, prec, c, iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4511 Py_DECREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4512 if (len < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4513 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4514 sign = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4515 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4516 else if (PyLong_Check(iobj)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4517 int ilen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4518
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4519 isnumok = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4520 temp = _PyString_FormatLong(iobj, flags,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4521 prec, c, &pbuf, &ilen);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4522 Py_DECREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4523 len = ilen;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4524 if (!temp)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4525 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4526 sign = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4527 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4528 else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4529 Py_DECREF(iobj);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4530 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4531 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4532 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4533 if (!isnumok) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4534 PyErr_Format(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4535 "%%%c format: a number is required, "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4536 "not %.200s", c, Py_TYPE(v)->tp_name);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4537 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4538 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4539 if (flags & F_ZERO)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4540 fill = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4541 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4542 case 'e':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4543 case 'E':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4544 case 'f':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4545 case 'F':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4546 case 'g':
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4547 case 'G':
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4548 temp = formatfloat(v, flags, prec, c);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4549 if (temp == NULL)
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4550 goto error;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4551 pbuf = PyString_AS_STRING(temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4552 len = PyString_GET_SIZE(temp);
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4553 sign = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4554 if (flags & F_ZERO)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4555 fill = '0';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4556 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4557 case 'c':
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4558 #ifdef Py_USING_UNICODE
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4559 if (PyUnicode_Check(v)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4560 fmt = fmt_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4561 argidx = argidx_start;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4562 goto unicode;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4563 }
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4564 #endif
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4565 pbuf = formatbuf;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4566 len = formatchar(pbuf, sizeof(formatbuf), v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4567 if (len < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4568 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4569 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4570 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4571 PyErr_Format(PyExc_ValueError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4572 "unsupported format character '%c' (0x%x) "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4573 "at index %zd",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4574 c, c,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4575 (Py_ssize_t)(fmt - 1 -
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4576 PyString_AsString(format)));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4577 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4578 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4579 if (sign) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4580 if (*pbuf == '-' || *pbuf == '+') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4581 sign = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4582 len--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4583 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4584 else if (flags & F_SIGN)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4585 sign = '+';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4586 else if (flags & F_BLANK)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4587 sign = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4588 else
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4589 sign = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4590 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4591 if (width < len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4592 width = len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4593 if (rescnt - (sign != 0) < width) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4594 reslen -= rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4595 rescnt = width + fmtcnt + 100;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4596 reslen += rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4597 if (reslen < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4598 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4599 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4600 return PyErr_NoMemory();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4601 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4602 if (_PyString_Resize(&result, reslen)) {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4603 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4604 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4605 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4606 res = PyString_AS_STRING(result)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4607 + reslen - rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4608 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4609 if (sign) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4610 if (fill != ' ')
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4611 *res++ = sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4612 rescnt--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4613 if (width > len)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4614 width--;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4615 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4616 if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4617 assert(pbuf[0] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4618 assert(pbuf[1] == c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4619 if (fill != ' ') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4620 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4621 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4622 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4623 rescnt -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4624 width -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4625 if (width < 0)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4626 width = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4627 len -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4628 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4629 if (width > len && !(flags & F_LJUST)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4630 do {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4631 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4632 *res++ = fill;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4633 } while (--width > len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4634 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4635 if (fill == ' ') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4636 if (sign)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4637 *res++ = sign;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4638 if ((flags & F_ALT) &&
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4639 (c == 'x' || c == 'X')) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4640 assert(pbuf[0] == '0');
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4641 assert(pbuf[1] == c);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4642 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4643 *res++ = *pbuf++;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4644 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4645 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4646 Py_MEMCPY(res, pbuf, len);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4647 res += len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4648 rescnt -= len;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4649 while (--width >= len) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4650 --rescnt;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4651 *res++ = ' ';
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4652 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4653 if (dict && (argidx < arglen) && c != '%') {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4654 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4655 "not all arguments converted during string formatting");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4656 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4657 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4658 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4659 Py_XDECREF(temp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4660 } /* '%' */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4661 } /* until end */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4662 if (argidx < arglen && !dict) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4663 PyErr_SetString(PyExc_TypeError,
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4664 "not all arguments converted during string formatting");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4665 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4666 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4667 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4668 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4669 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4670 if (_PyString_Resize(&result, reslen - rescnt))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 60200
diff changeset
4671 return NULL;
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4672 return result;
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4673
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4674 #ifdef Py_USING_UNICODE
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4675 unicode:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4676 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4677 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4678 args_owned = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4679 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4680 /* Fiddle args right (remove the first argidx arguments) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4681 if (PyTuple_Check(orig_args) && argidx > 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4682 PyObject *v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4683 Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4684 v = PyTuple_New(n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4685 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4686 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4687 while (--n >= 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4688 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4689 Py_INCREF(w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4690 PyTuple_SET_ITEM(v, n, w);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4691 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4692 args = v;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4693 } else {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4694 Py_INCREF(orig_args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4695 args = orig_args;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4696 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4697 args_owned = 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4698 /* Take what we have of the result and let the Unicode formatting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4699 function format the rest of the input. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4700 rescnt = res - PyString_AS_STRING(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4701 if (_PyString_Resize(&result, rescnt))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4702 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4703 fmtcnt = PyString_GET_SIZE(format) - \
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4704 (fmt - PyString_AS_STRING(format));
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4705 format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4706 if (format == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4707 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4708 v = PyUnicode_Format(format, args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4709 Py_DECREF(format);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4710 if (v == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4711 goto error;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4712 /* Paste what we have (result) to what the Unicode formatting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4713 function returned (v) and return the result (or error) */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4714 w = PyUnicode_Concat(result, v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4715 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4716 Py_DECREF(v);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4717 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4718 return w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4719 #endif /* Py_USING_UNICODE */
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4720
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4721 error:
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4722 Py_DECREF(result);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4723 if (args_owned) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4724 Py_DECREF(args);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4725 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4726 return NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4727 }
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4728
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4729 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4730 PyString_InternInPlace(PyObject **p)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4731 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4732 register PyStringObject *s = (PyStringObject *)(*p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4733 PyObject *t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4734 if (s == NULL || !PyString_Check(s))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4735 Py_FatalError("PyString_InternInPlace: strings only please!");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4736 /* If it's a string subclass, we don't really know what putting
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4737 it in the interned dict might do. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4738 if (!PyString_CheckExact(s))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4739 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4740 if (PyString_CHECK_INTERNED(s))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4741 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4742 if (interned == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4743 interned = PyDict_New();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4744 if (interned == NULL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4745 PyErr_Clear(); /* Don't leave an exception */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4746 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4747 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4748 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4749 t = PyDict_GetItem(interned, (PyObject *)s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4750 if (t) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4751 Py_INCREF(t);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4752 Py_DECREF(*p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4753 *p = t;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4754 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4755 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4756
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4757 if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4758 PyErr_Clear();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4759 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4760 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4761 /* The two references in interned are not counted by refcnt.
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4762 The string deallocator will take care of this */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4763 Py_REFCNT(s) -= 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4764 PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4765 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4766
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4767 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4768 PyString_InternImmortal(PyObject **p)
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4769 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4770 PyString_InternInPlace(p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4771 if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4772 PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4773 Py_INCREF(*p);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4774 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4775 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4776
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4777
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4778 PyObject *
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4779 PyString_InternFromString(const char *cp)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4780 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4781 PyObject *s = PyString_FromString(cp);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4782 if (s == NULL)
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4783 return NULL;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4784 PyString_InternInPlace(&s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4785 return s;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4786 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4787
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4788 void
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
4789 PyString_Fini(void)
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4790 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4791 int i;
81933
4d120cea3507 Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81737
diff changeset
4792 for (i = 0; i < UCHAR_MAX + 1; i++)
4d120cea3507 Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81737
diff changeset
4793 Py_CLEAR(characters[i]);
4d120cea3507 Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81737
diff changeset
4794 Py_CLEAR(nullstring);
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4795 }
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4796
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4797 void _Py_ReleaseInternedStrings(void)
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4798 {
75133
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4799 PyObject *keys;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4800 PyStringObject *s;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4801 Py_ssize_t i, n;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4802 Py_ssize_t immortal_size = 0, mortal_size = 0;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4803
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4804 if (interned == NULL || !PyDict_Check(interned))
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4805 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4806 keys = PyDict_Keys(interned);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4807 if (keys == NULL || !PyList_Check(keys)) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4808 PyErr_Clear();
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4809 return;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4810 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4811
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4812 /* Since _Py_ReleaseInternedStrings() is intended to help a leak
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4813 detector, interned strings are not forcibly deallocated; rather, we
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4814 give them their stolen references back, and then clear and DECREF
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4815 the interned dict. */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4816
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4817 n = PyList_GET_SIZE(keys);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4818 fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n",
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4819 n);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4820 for (i = 0; i < n; i++) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4821 s = (PyStringObject *) PyList_GET_ITEM(keys, i);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4822 switch (s->ob_sstate) {
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4823 case SSTATE_NOT_INTERNED:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4824 /* XXX Shouldn't happen */
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4825 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4826 case SSTATE_INTERNED_IMMORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4827 Py_REFCNT(s) += 1;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4828 immortal_size += Py_SIZE(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4829 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4830 case SSTATE_INTERNED_MORTAL:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4831 Py_REFCNT(s) += 2;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4832 mortal_size += Py_SIZE(s);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4833 break;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4834 default:
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4835 Py_FatalError("Inconsistent interned string state.");
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4836 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4837 s->ob_sstate = SSTATE_NOT_INTERNED;
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4838 }
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4839 fprintf(stderr, "total size of all interned strings: "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4840 "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d "
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4841 "mortal/immortal\n", mortal_size, immortal_size);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4842 Py_DECREF(keys);
357e268e7c5f ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents: 47560
diff changeset
4843 PyDict_Clear(interned);
81933
4d120cea3507 Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents: 81737
diff changeset
4844 Py_CLEAR(interned);
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4845 }