Mercurial > cpython-lru_cache-weakref
annotate Objects/stringobject.c @ 86317:d513149e0c19 2.7
Refer to strftime(3) manpage for platform specific format codes.
Suggested by Skip Montanaro on docs@.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 13 Oct 2013 18:28:25 +0200 |
| parents | ac2f59a6637f |
| children |
| 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 | 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 | 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 | 112 } |
| 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 | 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 | 165 } |
| 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 | 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 | 598 } |
| 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 | 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 | 788 } |
| 789 | |
| 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 | 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 | 834 } |
| 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 | 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 | 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 |
|
84540
2921f6c2009e
Issue #18427: str.replace could crash the interpreter with huge strings.
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
82938
diff
changeset
|
885 if (size) fwrite(data, (size_t)size, 1, fp); |
|
26550
b9c912e2c3c0
Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents:
26273
diff
changeset
|
886 #else |
|
84540
2921f6c2009e
Issue #18427: str.replace could crash the interpreter with huge strings.
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
82938
diff
changeset
|
887 fwrite(data, 1, (size_t)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 | 923 } |
| 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 | 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 | 985 } |
| 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 | 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 | 1012 } |
| 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 | 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 | 1070 #undef b |
| 1071 } | |
| 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 | 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 | 1124 } |
| 1125 | |
| 1126 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ | |
| 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 | 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 | 1147 } |
| 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 | 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 | 1188 } |
| 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 | 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 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0; |
| 6 | 1259 } |
| 1260 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1261 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
|
1262 string_hash(PyStringObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1263 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1264 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
|
1265 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
|
1266 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
|
1267 |
|
76189
a11a2bbd8241
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
75135
diff
changeset
|
1268 #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
|
1269 assert(_Py_HashSecret_Initialized); |
|
76189
a11a2bbd8241
fix build without Py_DEBUG and DNDEBUG (closes #14509)
Benjamin Peterson <benjamin@python.org>
parents:
75135
diff
changeset
|
1270 #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
|
1271 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
|
1272 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
|
1273 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
|
1274 /* |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1275 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
|
1276 (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
|
1277 */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1278 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
|
1279 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
|
1280 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
|
1281 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1282 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
|
1283 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
|
1284 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
|
1285 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
|
1286 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
|
1287 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
|
1288 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
|
1289 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
|
1290 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
|
1291 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
|
1292 return x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1293 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1294 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1295 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
|
1296 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
|
1297 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1298 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
|
1299 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
|
1300 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
|
1301 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
|
1302 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
|
1303 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
|
1304 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
|
1305 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1306 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
|
1307 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
|
1308 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
|
1309 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
|
1310 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
|
1311 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1312 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
|
1313 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
|
1314 &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
|
1315 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
|
1316 } |
|
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 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
|
1319 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
|
1320 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1321 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
|
1322 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
|
1323 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
|
1324 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
|
1325 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
|
1326 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1327 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
|
1328 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
|
1329 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
|
1330 slicelength); |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1331 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1332 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1333 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
|
1334 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
|
1335 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
|
1336 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
|
1337 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1338 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
|
1339 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
|
1340 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
|
1341 } |
|
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 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
|
1344 slicelength); |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1345 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
|
1346 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
|
1347 } |
|
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 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1350 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
|
1351 "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
|
1352 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
|
1353 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
|
1354 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1355 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1356 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1357 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
|
1358 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
|
1359 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1360 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
|
1361 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
|
1362 "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
|
1363 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
|
1364 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1365 *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
|
1366 return Py_SIZE(self); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1367 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1368 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1369 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
|
1370 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
|
1371 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1372 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
|
1373 "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
|
1374 return -1; |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1375 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1376 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1377 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
|
1378 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
|
1379 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1380 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
|
1381 *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
|
1382 return 1; |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1383 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1384 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1385 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
|
1386 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
|
1387 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1388 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
|
1389 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
|
1390 "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
|
1391 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
|
1392 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1393 *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
|
1394 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
|
1395 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1396 |
|
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
|
1397 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
|
1398 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
|
1399 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1400 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
|
1401 (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
|
1402 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
|
1403 } |
|
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1405 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
|
1406 (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
|
1407 (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
|
1408 (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
|
1409 (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
|
1410 (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
|
1411 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
|
1412 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
|
1413 (objobjproc)string_contains /*sq_contains*/ |
| 6 | 1414 }; |
| 1415 | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1416 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
|
1417 (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
|
1418 (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
|
1419 0, |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1420 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1421 |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1422 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
|
1423 (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
|
1424 (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
|
1425 (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
|
1426 (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
|
1427 (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
|
1428 0, /* XXX */ |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1429 }; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1430 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1431 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1432 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1433 #define LEFTSTRIP 0 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1434 #define RIGHTSTRIP 1 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1435 #define BOTHSTRIP 2 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1436 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1437 /* Arrays indexed by above */ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1438 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
|
1439 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1440 #define STRIPNAME(i) (stripformat[i]+3) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1441 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1442 PyDoc_STRVAR(split__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1443 "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
|
1444 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1445 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
|
1446 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
|
1447 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
|
1448 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
|
1449 from the result."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1450 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1451 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
|
1452 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
|
1453 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1454 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
|
1455 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
|
1456 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
|
1457 PyObject *subobj = Py_None; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1458 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1459 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
|
1460 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
|
1461 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
|
1462 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
|
1463 if (subobj == Py_None) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1464 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
|
1465 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
|
1466 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
|
1467 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
|
1468 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1469 #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
|
1470 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
|
1471 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
|
1472 #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
|
1473 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
|
1474 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
|
1475 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1476 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
|
1477 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1478 |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1479 PyDoc_STRVAR(partition__doc__, |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1480 "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
|
1481 \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
|
1482 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
|
1483 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
|
1484 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
|
1485 |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1486 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
|
1487 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
|
1488 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1489 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
|
1490 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
|
1491 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1492 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
|
1493 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
|
1494 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
|
1495 } |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38332
diff
changeset
|
1496 #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
|
1497 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
|
1498 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
|
1499 #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
|
1500 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
|
1501 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
|
1502 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1503 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
|
1504 (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
|
1505 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
|
1506 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
|
1507 ); |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1508 } |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1509 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1510 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
|
1511 "S.rpartition(sep) -> (head, sep, tail)\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1512 \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
|
1513 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
|
1514 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
|
1515 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
|
1516 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1517 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
|
1518 string_rpartition(PyStringObject *self, PyObject *sep_obj) |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1519 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1520 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
|
1521 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
|
1522 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1523 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
|
1524 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
|
1525 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
|
1526 } |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1527 #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
|
1528 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
|
1529 return PyUnicode_RPartition((PyObject *) self, sep_obj); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1530 #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
|
1531 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
|
1532 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
|
1533 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1534 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
|
1535 (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
|
1536 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
|
1537 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
|
1538 ); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1539 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1540 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1541 PyDoc_STRVAR(rsplit__doc__, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1542 "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
|
1543 \n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1544 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
|
1545 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
|
1546 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
|
1547 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
|
1548 is a separator."); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1549 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1550 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
|
1551 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
|
1552 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1553 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
|
1554 Py_ssize_t maxsplit = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1555 const char *s = PyString_AS_STRING(self), *sub; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1556 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
|
1557 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1558 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
|
1559 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
|
1560 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
|
1561 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
|
1562 if (subobj == Py_None) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1563 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
|
1564 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
|
1565 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
|
1566 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
|
1567 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1568 #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
|
1569 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
|
1570 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
|
1571 #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
|
1572 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
|
1573 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
|
1574 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1575 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
|
1576 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1577 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1578 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1579 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
|
1580 "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
|
1581 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1582 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
|
1583 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
|
1584 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1585 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
|
1586 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
|
1587 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1588 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
|
1589 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
|
1590 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
|
1591 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
|
1592 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
|
1593 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
|
1594 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
|
1595 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
|
1596 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1597 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
|
1598 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
|
1599 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
|
1600 } |
|
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 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
|
1603 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
|
1604 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
|
1605 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
|
1606 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1607 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
|
1608 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
|
1609 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
|
1610 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
|
1611 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
|
1612 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
|
1613 } |
|
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 /* 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
|
1617 * 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
|
1618 * 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
|
1619 * 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
|
1620 * 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
|
1621 */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1622 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
|
1623 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
|
1624 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
|
1625 if (!PyString_Check(item)){ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1626 #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
|
1627 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
|
1628 /* 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
|
1629 * 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
|
1630 * 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
|
1631 * 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
|
1632 */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1633 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
|
1634 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
|
1635 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
|
1636 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
|
1637 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1638 #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
|
1639 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
|
1640 "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
|
1641 " %.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
|
1642 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
|
1643 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
|
1644 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
|
1645 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1646 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
|
1647 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
|
1648 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
|
1649 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
|
1650 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
|
1651 "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
|
1652 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
|
1653 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
|
1654 } |
|
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 /* 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
|
1658 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
|
1659 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
|
1660 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
|
1661 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
|
1662 } |
|
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 /* 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
|
1665 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
|
1666 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
|
1667 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
|
1668 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
|
1669 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
|
1670 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
|
1671 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
|
1672 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
|
1673 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
|
1674 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
|
1675 } |
|
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 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
|
1679 return res; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1680 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1681 |
|
18130
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1682 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
|
1683 _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
|
1684 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1685 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
|
1686 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
|
1687 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
|
1688 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1689 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
58090
diff
changeset
|
1690 /* 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
|
1691 #define ADJUST_INDICES(start, end, len) \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1692 if (end > len) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1693 end = len; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1694 else if (end < 0) { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1695 end += len; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1696 if (end < 0) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1697 end = 0; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1698 } \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1699 if (start < 0) { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1700 start += len; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1701 if (start < 0) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1702 start = 0; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
1703 } |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1704 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1705 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
|
1706 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
|
1707 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1708 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
|
1709 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
|
1710 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
|
1711 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
|
1712 |
|
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 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
|
1714 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
|
1715 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
|
1716 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1717 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
|
1718 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
|
1719 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
|
1720 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1721 #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
|
1722 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
|
1723 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
|
1724 (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
|
1725 #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
|
1726 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
|
1727 /* 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
|
1728 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
|
1729 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
|
1730 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1731 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
|
1732 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
|
1733 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
|
1734 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
|
1735 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1736 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
|
1737 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
|
1738 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
|
1739 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1742 PyDoc_STRVAR(find__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1743 "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
|
1744 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1745 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
|
1746 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
|
1747 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
|
1748 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1749 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1750 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1751 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
|
1752 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
|
1753 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1754 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
|
1755 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
|
1756 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
|
1757 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
|
1758 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1761 PyDoc_STRVAR(index__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1762 "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
|
1763 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1764 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
|
1765 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1766 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
|
1767 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
|
1768 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1769 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
|
1770 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
|
1771 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
|
1772 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
|
1773 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
|
1774 "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
|
1775 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
|
1776 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1777 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
|
1778 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1781 PyDoc_STRVAR(rfind__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1782 "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
|
1783 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1784 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
|
1785 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
|
1786 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
|
1787 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1788 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1789 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1790 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
|
1791 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
|
1792 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1793 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
|
1794 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
|
1795 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
|
1796 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
|
1797 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1800 PyDoc_STRVAR(rindex__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1801 "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
|
1802 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1803 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
|
1804 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1805 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
|
1806 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
|
1807 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1808 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
|
1809 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
|
1810 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
|
1811 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
|
1812 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
|
1813 "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
|
1814 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
|
1815 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1816 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
|
1817 } |
|
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 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1820 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
|
1821 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
|
1822 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1823 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
|
1824 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
|
1825 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
|
1826 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
|
1827 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
|
1828 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1829 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
|
1830 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
|
1831 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
|
1832 i++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1833 } |
|
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 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
|
1837 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
|
1838 do { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1839 j--; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1840 } 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
|
1841 j++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1842 } |
|
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 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
|
1845 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
|
1846 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
|
1847 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1848 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1849 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
|
1850 } |
|
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 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1853 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
|
1854 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
|
1855 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1856 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
|
1857 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
|
1858 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1859 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
|
1860 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
|
1861 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
|
1862 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 } |
|
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 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
|
1867 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
|
1868 do { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1869 j--; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1870 } 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
|
1871 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 } |
|
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 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
|
1875 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
|
1876 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
|
1877 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1878 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1879 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
|
1880 } |
|
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 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1883 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
|
1884 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
|
1885 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1886 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
|
1887 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1888 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
|
1889 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
|
1890 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1891 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
|
1892 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
|
1893 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
|
1894 #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
|
1895 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
|
1896 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
|
1897 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
|
1898 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
|
1899 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
|
1900 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
|
1901 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
|
1902 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
|
1903 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
|
1904 } |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
1905 #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
|
1906 PyErr_Format(PyExc_TypeError, |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
1907 #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
|
1908 "%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
|
1909 #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
|
1910 "%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
|
1911 #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
|
1912 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
|
1913 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
|
1914 } |
|
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 return do_strip(self, striptype); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1917 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1920 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
|
1921 "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
|
1922 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1923 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
|
1924 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
|
1925 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
|
1926 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
|
1927 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1928 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
|
1929 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
|
1930 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1931 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
|
1932 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
|
1933 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1934 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
|
1935 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1938 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
|
1939 "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
|
1940 \n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1941 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
|
1942 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
|
1943 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
|
1944 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1945 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
|
1946 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
|
1947 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1948 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
|
1949 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
|
1950 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1951 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
|
1952 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1955 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
|
1956 "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
|
1957 \n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1958 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
|
1959 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
|
1960 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
|
1961 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1962 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
|
1963 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
|
1964 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1965 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
|
1966 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
|
1967 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1968 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
|
1969 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1972 PyDoc_STRVAR(lower__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1973 "S.lower() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1974 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1975 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
|
1976 |
|
38294
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
1977 /* _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
|
1978 #ifndef _tolower |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
1979 #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
|
1980 #endif |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
1981 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1982 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
|
1983 string_lower(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1984 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1985 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
|
1986 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
|
1987 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
|
1988 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1989 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
|
1990 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
|
1991 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
|
1992 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1993 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
|
1994 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1995 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
|
1996 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
1997 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
|
1998 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
|
1999 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
|
2000 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
|
2001 } |
|
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 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2004 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2005 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2006 PyDoc_STRVAR(upper__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2007 "S.upper() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2008 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2009 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
|
2010 |
|
38294
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2011 #ifndef _toupper |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2012 #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
|
2013 #endif |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2014 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2015 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
|
2016 string_upper(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2017 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2018 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
|
2019 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
|
2020 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
|
2021 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2022 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
|
2023 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
|
2024 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
|
2025 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2026 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
|
2027 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2028 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
|
2029 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2030 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
|
2031 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
|
2032 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
|
2033 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
|
2034 } |
|
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 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2037 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2038 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2039 PyDoc_STRVAR(title__doc__, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2040 "S.title() -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2041 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2042 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
|
2043 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
|
2044 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2045 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
|
2046 string_title(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2047 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2048 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
|
2049 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
|
2050 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
|
2051 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
|
2052 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2053 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
|
2054 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
|
2055 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
|
2056 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
|
2057 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
|
2058 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
|
2059 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
|
2060 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
|
2061 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
|
2062 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
|
2063 } 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
|
2064 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
|
2065 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
|
2066 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
|
2067 } else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2068 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
|
2069 *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
|
2070 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2071 return newobj; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2072 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2073 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2074 PyDoc_STRVAR(capitalize__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2075 "S.capitalize() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2076 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2077 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
|
2078 capitalized."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2079 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2080 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
|
2081 string_capitalize(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2082 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2083 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
|
2084 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
|
2085 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
|
2086 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2087 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
|
2088 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
|
2089 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
|
2090 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
|
2091 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
|
2092 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
|
2093 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
|
2094 *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
|
2095 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2096 *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
|
2097 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
|
2098 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2099 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
|
2100 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
|
2101 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
|
2102 *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
|
2103 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2104 *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
|
2105 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
|
2106 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2107 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2108 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2111 PyDoc_STRVAR(count__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2112 "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
|
2113 \n\ |
|
38236
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38173
diff
changeset
|
2114 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
|
2115 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
|
2116 as in slice notation."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2117 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2118 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
|
2119 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
|
2120 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2121 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
|
2122 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
|
2123 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
|
2124 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
|
2125 |
|
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
|
2126 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
|
2127 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
|
2128 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2129 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
|
2130 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
|
2131 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
|
2132 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2133 #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
|
2134 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
|
2135 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
|
2136 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
|
2137 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
|
2138 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
|
2139 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2140 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
|
2141 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2142 #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
|
2143 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
|
2144 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
|
2145 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2146 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
|
2147 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2148 return PyInt_FromSsize_t( |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2149 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
|
2150 ); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2151 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2152 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2153 PyDoc_STRVAR(swapcase__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2154 "S.swapcase() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2155 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2156 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
|
2157 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
|
2158 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2159 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
|
2160 string_swapcase(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2161 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2162 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
|
2163 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
|
2164 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
|
2165 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2166 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
|
2167 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
|
2168 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
|
2169 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
|
2170 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
|
2171 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
|
2172 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
|
2173 *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
|
2174 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2175 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
|
2176 *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
|
2177 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2178 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2179 *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
|
2180 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
|
2181 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2182 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2183 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2186 PyDoc_STRVAR(translate__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2187 "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
|
2188 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2189 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
|
2190 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
|
2191 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
|
2192 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
|
2193 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
|
2194 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
|
2195 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2196 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
|
2197 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
|
2198 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2199 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
|
2200 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
|
2201 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
|
2202 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
|
2203 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
|
2204 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
|
2205 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
|
2206 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
|
2207 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
|
2208 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2209 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
|
2210 &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
|
2211 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
|
2212 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2213 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
|
2214 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
|
2215 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
|
2216 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2217 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
|
2218 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
|
2219 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
|
2220 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2221 #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
|
2222 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
|
2223 /* 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
|
2224 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
|
2225 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
|
2226 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
|
2227 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
|
2228 "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
|
2229 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
|
2230 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2231 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
|
2232 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2233 #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
|
2234 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
|
2235 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
|
2236 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2237 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
|
2238 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
|
2239 "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
|
2240 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
|
2241 } |
|
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 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
|
2244 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
|
2245 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
|
2246 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
|
2247 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2248 #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
|
2249 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
|
2250 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
|
2251 "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
|
2252 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
|
2253 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2254 #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
|
2255 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
|
2256 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
|
2257 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2258 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2259 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
|
2260 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
|
2261 } |
|
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 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
|
2264 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
|
2265 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
|
2266 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
|
2267 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
|
2268 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
|
2269 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2270 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
|
2271 /* 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
|
2272 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
|
2273 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
|
2274 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
|
2275 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
|
2276 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2277 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
|
2278 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
|
2279 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
|
2280 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
|
2281 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
|
2282 } |
|
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 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
|
2285 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
|
2286 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
|
2287 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2288 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
|
2289 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
|
2290 } |
|
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 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
|
2293 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
|
2294 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2295 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
|
2296 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
|
2297 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
|
2298 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
|
2299 continue; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2300 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
|
2301 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2302 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
|
2303 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
|
2304 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
|
2305 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
|
2306 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2307 /* Fix the size of the resulting string */ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2308 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
|
2309 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
|
2310 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2311 } |
|
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 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2314 /* 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
|
2315 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2316 #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
|
2317 ((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
|
2318 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2319 /* 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
|
2320 /* 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
|
2321 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
|
2322 return_self(PyStringObject *self) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2323 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2324 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
|
2325 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
|
2326 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
|
2327 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2328 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
|
2329 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
|
2330 PyString_GET_SIZE(self)); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2331 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2332 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
2333 Py_LOCAL_INLINE(Py_ssize_t) |
|
84540
2921f6c2009e
Issue #18427: str.replace could crash the interpreter with huge strings.
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
82938
diff
changeset
|
2334 countchar(const char *target, Py_ssize_t 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
|
2335 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2336 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
|
2337 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
|
2338 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
|
2339 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2340 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
|
2341 count++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2342 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
|
2343 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2344 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
|
2345 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2346 return count; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2347 } |
|
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 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2350 /* 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
|
2351 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2352 /* 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
|
2353 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
|
2354 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
|
2355 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
|
2356 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2357 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2358 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
|
2359 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
|
2360 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
|
2361 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
|
2362 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2363 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
|
2364 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2365 /* 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
|
2366 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
|
2367 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
|
2368 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
|
2369 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2370 /* 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
|
2371 /* 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
|
2372 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
|
2373 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
|
2374 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
|
2375 "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
|
2376 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
|
2377 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2378 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
|
2379 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
|
2380 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
|
2381 "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
|
2382 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
|
2383 } |
|
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 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
|
2386 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
|
2387 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
|
2388 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2389 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
|
2390 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
|
2391 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2392 /* 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
|
2393 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2394 /* 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
|
2395 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
|
2396 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
|
2397 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
|
2398 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2399 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
|
2400 *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
|
2401 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
|
2402 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
|
2403 } |
|
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 /* 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
|
2406 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
|
2407 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2408 return result; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2409 } |
|
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 /* 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
|
2412 /* 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
|
2413 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
|
2414 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
|
2415 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
|
2416 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2417 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
|
2418 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
|
2419 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
|
2420 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
|
2421 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
|
2422 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2423 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
|
2424 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
|
2425 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2426 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
|
2427 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
|
2428 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
|
2429 } |
|
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 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
|
2432 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
|
2433 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2434 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
|
2435 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
|
2436 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
|
2437 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
|
2438 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2439 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
|
2440 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
|
2441 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
|
2442 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
|
2443 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
|
2444 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2445 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
|
2446 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
|
2447 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
|
2448 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2449 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
|
2450 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2451 return result; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2452 } |
|
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 /* 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
|
2455 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2456 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
|
2457 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
|
2458 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
|
2459 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
|
2460 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
|
2461 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
|
2462 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
|
2463 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
|
2464 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
|
2465 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2466 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
|
2467 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
|
2468 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2469 count = stringlib_count(self_s, self_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2470 from_s, from_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2471 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
|
2472 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2473 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
|
2474 /* 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
|
2475 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
|
2476 } |
|
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 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
|
2479 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
|
2480 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2481 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
|
2482 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
|
2483 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
|
2484 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2485 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
|
2486 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2487 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
|
2488 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
|
2489 while (count-- > 0) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2490 offset = stringlib_find(start, end-start, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2491 from_s, from_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2492 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
|
2493 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
|
2494 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2495 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
|
2496 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2497 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
|
2498 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2499 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
|
2500 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
|
2501 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2502 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
|
2503 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2504 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2505 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2506 /* 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
|
2507 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
|
2508 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
|
2509 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
|
2510 Py_ssize_t maxcount) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2511 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2512 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
|
2513 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
|
2514 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
|
2515 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2516 /* 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
|
2517 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
|
2518 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
|
2519 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2520 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
|
2521 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2522 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
|
2523 /* 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
|
2524 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
|
2525 } |
|
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 /* 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
|
2528 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
|
2529 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
|
2530 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
|
2531 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
|
2532 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
|
2533 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2534 /* 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
|
2535 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
|
2536 *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
|
2537 start++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2538 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
|
2539 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2540 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
|
2541 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
|
2542 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
|
2543 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2544 *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
|
2545 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
|
2546 } |
|
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 return result; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2549 } |
|
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 /* 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
|
2552 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
|
2553 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
|
2554 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
|
2555 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
|
2556 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2557 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2558 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
|
2559 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
|
2560 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
|
2561 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
|
2562 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2563 /* 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
|
2564 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2565 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
|
2566 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
|
2567 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2568 offset = stringlib_find(self_s, self_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2569 from_s, from_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2570 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
|
2571 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
|
2572 /* 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
|
2573 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
|
2574 } |
|
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 /* 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
|
2577 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
|
2578 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
|
2579 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
|
2580 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
|
2581 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
|
2582 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2583 /* 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
|
2584 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
|
2585 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
|
2586 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
|
2587 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
|
2588 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2589 while ( --maxcount > 0) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2590 offset = stringlib_find(start, end-start, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2591 from_s, from_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2592 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
|
2593 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
|
2594 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2595 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
|
2596 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
|
2597 } |
|
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 return result; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2600 } |
|
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 /* 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
|
2603 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
|
2604 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
|
2605 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
|
2606 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
|
2607 Py_ssize_t maxcount) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2608 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2609 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
|
2610 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
|
2611 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
|
2612 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
|
2613 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
|
2614 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2615 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
|
2616 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
|
2617 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2618 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
|
2619 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
|
2620 /* 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
|
2621 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
|
2622 } |
|
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 /* 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
|
2625 /* 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
|
2626 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
|
2627 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
|
2628 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
|
2629 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
|
2630 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2631 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
|
2632 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
|
2633 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
|
2634 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
|
2635 } |
|
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 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
|
2638 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
|
2639 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
|
2640 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
|
2641 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2642 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
|
2643 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
|
2644 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
|
2645 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
|
2646 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
|
2647 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2648 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2649 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
|
2650 /* 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
|
2651 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
|
2652 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
|
2653 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
|
2654 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2655 /* 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
|
2656 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
|
2657 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
|
2658 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
|
2659 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
|
2660 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
|
2661 } |
|
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 /* 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
|
2664 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
|
2665 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2666 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2667 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2668 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2669 /* 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
|
2670 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
|
2671 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
|
2672 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
|
2673 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
|
2674 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
|
2675 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
|
2676 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
|
2677 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
|
2678 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
|
2679 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
|
2680 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2681 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
|
2682 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
|
2683 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2684 count = stringlib_count(self_s, self_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2685 from_s, from_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2686 maxcount); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2687 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2688 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
|
2689 /* 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
|
2690 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
|
2691 } |
|
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 /* 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
|
2694 /* 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
|
2695 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
|
2696 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
|
2697 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
|
2698 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
|
2699 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2700 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
|
2701 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
|
2702 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
|
2703 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
|
2704 } |
|
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 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
|
2707 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
|
2708 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
|
2709 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
|
2710 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2711 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
|
2712 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
|
2713 while (count-- > 0) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2714 offset = stringlib_find(start, end-start, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2715 from_s, from_len, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2716 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
|
2717 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
|
2718 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2719 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
|
2720 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
|
2721 /* 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
|
2722 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
|
2723 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
|
2724 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
|
2725 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2726 /* 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
|
2727 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
|
2728 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
|
2729 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
|
2730 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
|
2731 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
|
2732 } |
|
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 /* 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
|
2735 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
|
2736 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2737 return result; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2738 } |
|
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 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2741 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
|
2742 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
|
2743 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
|
2744 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
|
2745 Py_ssize_t maxcount) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2746 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2747 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
|
2748 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
|
2749 } 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
|
2750 /* 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
|
2751 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
|
2752 } |
|
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 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
|
2755 (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
|
2756 /* 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
|
2757 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
|
2758 } |
|
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 /* 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
|
2761 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2762 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
|
2763 /* 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
|
2764 /* >>> "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
|
2765 /* '.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
|
2766 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
|
2767 } |
|
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 /* 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
|
2770 /* 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
|
2771 /* 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
|
2772 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
|
2773 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
|
2774 } |
|
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 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
|
2777 /* 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
|
2778 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
|
2779 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
|
2780 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
|
2781 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2782 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
|
2783 } |
|
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 /* 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
|
2787 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2788 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
|
2789 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
|
2790 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
|
2791 self, |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2792 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
|
2793 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
|
2794 maxcount); |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2795 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2796 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
|
2797 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
|
2798 } |
|
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 /* 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
|
2802 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
|
2803 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
|
2804 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
|
2805 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2806 /* 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
|
2807 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
|
2808 } |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2809 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2810 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2811 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
|
2812 "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
|
2813 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2814 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
|
2815 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
|
2816 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
|
2817 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2818 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
|
2819 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
|
2820 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2821 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
|
2822 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
|
2823 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
|
2824 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
|
2825 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2826 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
|
2827 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
|
2828 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2829 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
|
2830 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
|
2831 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
|
2832 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2833 #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
|
2834 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
|
2835 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
|
2836 from, to, count); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2837 #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
|
2838 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
|
2839 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
|
2840 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2841 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
|
2842 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
|
2843 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
|
2844 } |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2845 #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
|
2846 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
|
2847 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
|
2848 from, to, count); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2849 #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
|
2850 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
|
2851 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
|
2852 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2853 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
|
2854 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
|
2855 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
|
2856 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2857 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2858 /** End DALKE **/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2859 |
|
38738
0a8a392e1fae
Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
2860 /* 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
|
2861 * 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
|
2862 * -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
|
2863 */ |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
2864 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
|
2865 _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
|
2866 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
|
2867 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2868 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
|
2869 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
|
2870 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
|
2871 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
|
2872 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2873 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
|
2874 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
|
2875 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
|
2876 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
2877 #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
|
2878 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
|
2879 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
|
2880 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
|
2881 #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
|
2882 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
|
2883 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
|
2884 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
|
2885 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
2886 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
|
2887 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2888 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
|
2889 /* startswith */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2890 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
|
2891 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
|
2892 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2893 /* endswith */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2894 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
|
2895 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
|
2896 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2897 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
|
2898 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
|
2899 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2900 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
|
2901 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
|
2902 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
|
2903 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2906 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
|
2907 "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
|
2908 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28478
diff
changeset
|
2909 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
|
2910 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
|
2911 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
|
2912 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
|
2913 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2914 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
|
2915 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
|
2916 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2917 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
|
2918 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
|
2919 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
|
2920 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
|
2921 |
|
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
|
2922 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
|
2923 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
|
2924 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
|
2925 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
|
2926 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
|
2927 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
|
2928 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
|
2929 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
|
2930 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
|
2931 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
|
2932 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
|
2933 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
|
2934 } |
|
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 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
|
2937 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2938 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
|
2939 if (result == -1) { |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
2940 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
|
2941 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
|
2942 "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
|
2943 return NULL; |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
2944 } |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2945 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2946 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2947 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2950 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
|
2951 "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
|
2952 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28478
diff
changeset
|
2953 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
|
2954 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
|
2955 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
|
2956 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
|
2957 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2958 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
|
2959 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
|
2960 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2961 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
|
2962 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
|
2963 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
|
2964 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
|
2965 |
|
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
|
2966 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
|
2967 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
|
2968 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
|
2969 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
|
2970 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
|
2971 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
|
2972 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
|
2973 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
|
2974 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
|
2975 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
|
2976 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
|
2977 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
|
2978 } |
|
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 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
|
2981 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2982 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
|
2983 if (result == -1) { |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
2984 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
|
2985 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
|
2986 "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
|
2987 return NULL; |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
2988 } |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2989 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
2990 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2991 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2994 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
|
2995 "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
|
2996 \n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2997 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
|
2998 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
|
2999 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
|
3000 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
|
3001 '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
|
3002 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
|
3003 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3004 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3005 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
|
3006 { |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3007 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
|
3008 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
|
3009 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3010 PyObject *v; |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
3011 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3012 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3013 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
|
3014 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
|
3015 v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3016 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3017 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
|
3018 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
|
3019 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3020 "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
|
3021 "(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
|
3022 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
|
3023 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3024 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3025 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3026 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3027 |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3028 onError: |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3029 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
|
3030 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3033 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
|
3034 "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
|
3035 \n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3036 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
|
3037 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
|
3038 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
|
3039 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
|
3040 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
|
3041 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
|
3042 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3043 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3044 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
|
3045 { |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3046 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
|
3047 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
|
3048 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3049 PyObject *v; |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
3050 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3051 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3052 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
|
3053 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
|
3054 v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3055 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3056 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
|
3057 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
|
3058 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3059 "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
|
3060 "(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
|
3061 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
|
3062 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3063 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3064 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3065 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3066 |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3067 onError: |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3068 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
|
3069 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3072 PyDoc_STRVAR(expandtabs__doc__, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3073 "S.expandtabs([tabsize]) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3074 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3075 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
|
3076 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
|
3077 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3078 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
|
3079 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
|
3080 { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3081 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
|
3082 char *q; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3083 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
|
3084 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3085 int tabsize = 8; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3086 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3087 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
|
3088 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3089 |
|
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
|
3090 /* 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
|
3091 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
|
3092 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
|
3093 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
|
3094 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
|
3095 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
|
3096 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
|
3097 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
|
3098 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
|
3099 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
|
3100 j += incr; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3101 } |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3102 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3103 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3104 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
|
3105 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
|
3106 j++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3107 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
|
3108 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
|
3109 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
|
3110 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
|
3111 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
|
3112 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3113 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3114 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3115 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
|
3116 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
|
3117 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3118 /* 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
|
3119 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
|
3120 if (!u) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3121 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3122 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3123 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
|
3124 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
|
3125 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
|
3126 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3127 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
|
3128 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
|
3129 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
|
3130 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
|
3131 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
|
3132 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
|
3133 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
|
3134 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
|
3135 *q++ = ' '; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3136 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3137 } |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3138 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3139 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3140 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
|
3141 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
|
3142 *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
|
3143 j++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3144 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
|
3145 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
|
3146 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3147 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3148 return u; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3149 |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3150 overflow2: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3151 Py_DECREF(u); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3152 overflow1: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3153 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
|
3154 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3155 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3156 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
3157 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
|
3158 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
|
3159 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3160 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3161 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3162 if (left < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3163 left = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3164 if (right < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3165 right = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3166 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3167 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
|
3168 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3169 return (PyObject *)self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3170 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3171 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3172 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
|
3173 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
|
3174 if (u) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3175 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
|
3176 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
|
3177 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
|
3178 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
|
3179 PyString_GET_SIZE(self)); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3180 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
|
3181 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
|
3182 fill, right); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3183 } |
|
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 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3186 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3187 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3188 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
|
3189 "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
|
3190 "\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
|
3191 "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
|
3192 "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
|
3193 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3194 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
|
3195 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
|
3196 { |
|
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
|
3197 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
|
3198 char fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3199 |
|
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
|
3200 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
|
3201 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3202 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3203 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
|
3204 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3205 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3206 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3207 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3208 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
|
3209 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3212 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
|
3213 "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
|
3214 "\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
|
3215 "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
|
3216 "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
|
3217 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3218 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
|
3219 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
|
3220 { |
|
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
|
3221 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
|
3222 char fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3223 |
|
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
|
3224 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
|
3225 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3226 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3227 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
|
3228 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3229 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3230 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3231 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3232 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
|
3233 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3236 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
|
3237 "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
|
3238 "\n" |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3239 "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
|
3240 "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
|
3241 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3242 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
|
3243 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
|
3244 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3245 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
|
3246 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
|
3247 char fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3248 |
|
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
|
3249 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
|
3250 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3251 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3252 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
|
3253 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3254 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3255 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3256 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3257 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
|
3258 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
|
3259 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3260 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
|
3261 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3262 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3263 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
|
3264 "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
|
3265 "\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 "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
|
3267 "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
|
3268 |
|
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 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
|
3270 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
|
3271 { |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
3272 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
|
3273 PyObject *s; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3274 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
|
3275 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
|
3276 |
|
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 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
|
3278 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
|
3279 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3280 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
|
3281 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
|
3282 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
|
3283 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
|
3284 } |
|
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 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
|
3286 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
|
3287 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
|
3288 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
|
3289 ); |
|
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
|
3290 } |
|
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 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3292 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
|
3293 |
|
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 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
|
3295 |
|
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 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
|
3297 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
|
3298 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3299 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
|
3300 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
|
3301 /* 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
|
3302 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
|
3303 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
|
3304 } |
|
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 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
|
3307 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3309 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
|
3310 "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
|
3311 \n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3312 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
|
3313 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
|
3314 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3315 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
|
3316 string_isspace(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3317 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3318 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
|
3319 = (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
|
3320 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
|
3321 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3322 /* 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
|
3323 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
|
3324 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
|
3325 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3326 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3327 /* 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
|
3328 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
|
3329 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3330 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3331 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
|
3332 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
|
3333 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
|
3334 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3335 } |
|
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
|
3336 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3337 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3340 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
|
3341 "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
|
3342 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3343 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
|
3344 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
|
3345 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3346 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
|
3347 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
|
3348 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3349 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
|
3350 = (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
|
3351 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
|
3352 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3353 /* 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
|
3354 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
|
3355 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
|
3356 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
|
3357 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3358 /* 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
|
3359 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
|
3360 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
|
3361 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3362 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
|
3363 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
|
3364 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
|
3365 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
|
3366 } |
|
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
|
3367 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
|
3368 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3371 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
|
3372 "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
|
3373 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3374 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
|
3375 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
|
3376 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3377 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
|
3378 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
|
3379 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3380 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
|
3381 = (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
|
3382 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
|
3383 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3384 /* 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
|
3385 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
|
3386 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
|
3387 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
|
3388 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3389 /* 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
|
3390 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
|
3391 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
|
3392 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3393 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
|
3394 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
|
3395 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
|
3396 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
|
3397 } |
|
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
|
3398 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
|
3399 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3402 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
|
3403 "S.isdigit() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3404 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3405 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
|
3406 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
|
3407 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3408 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
|
3409 string_isdigit(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3410 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3411 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
|
3412 = (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
|
3413 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
|
3414 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3415 /* 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
|
3416 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
|
3417 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
|
3418 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3419 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3420 /* 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
|
3421 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
|
3422 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3423 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3424 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
|
3425 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
|
3426 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
|
3427 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3428 } |
|
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
|
3429 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3430 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3433 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
|
3434 "S.islower() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3435 \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
|
3436 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
|
3437 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
|
3438 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3439 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
|
3440 string_islower(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3441 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3442 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
|
3443 = (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
|
3444 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
|
3445 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3446 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3447 /* 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
|
3448 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
|
3449 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
|
3450 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3451 /* 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
|
3452 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
|
3453 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3454 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3455 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
|
3456 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3457 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
|
3458 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
|
3459 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
|
3460 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
|
3461 cased = 1; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3462 } |
|
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
|
3463 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3464 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3467 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
|
3468 "S.isupper() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3469 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3470 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
|
3471 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
|
3472 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3473 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
|
3474 string_isupper(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3475 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3476 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
|
3477 = (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
|
3478 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
|
3479 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3480 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3481 /* 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
|
3482 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
|
3483 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
|
3484 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3485 /* 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
|
3486 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
|
3487 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3488 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3489 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
|
3490 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3491 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
|
3492 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
|
3493 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
|
3494 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
|
3495 cased = 1; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3496 } |
|
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
|
3497 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3498 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3501 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
|
3502 "S.istitle() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3503 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3504 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
|
3505 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
|
3506 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
|
3507 otherwise."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3508 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3509 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
|
3510 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
|
3511 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3512 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
|
3513 = (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
|
3514 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
|
3515 int cased, previous_is_cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3516 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3517 /* 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
|
3518 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
|
3519 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
|
3520 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3521 /* 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
|
3522 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
|
3523 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3524 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3525 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
|
3526 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3527 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3528 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
|
3529 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
|
3530 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3531 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
|
3532 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
|
3533 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
|
3534 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
|
3535 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 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3537 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
|
3538 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
|
3539 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
|
3540 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
|
3541 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 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3543 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3544 previous_is_cased = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3545 } |
|
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
|
3546 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3547 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3550 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
|
3551 "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
|
3552 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3553 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12371 | 3554 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
|
3555 is given and true."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3556 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3557 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
|
3558 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
|
3559 { |
| 12371 | 3560 int keepends = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3561 |
| 12371 | 3562 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
|
3563 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3564 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
58090
diff
changeset
|
3565 return stringlib_splitlines( |
|
62208
ba2f2d08504b
Issue #8930: fix some C code indentation
Antoine Pitrou <solipsis@pitrou.net>
parents:
61463
diff
changeset
|
3566 (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
|
3567 keepends |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3568 ); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3569 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3570 |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
3571 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
|
3572 "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
|
3573 |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
3574 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
|
3575 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
|
3576 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3577 Py_ssize_t res; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3578 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
|
3579 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
|
3580 } |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
3581 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
3582 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
|
3583 string_getnewargs(PyStringObject *v) |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
3584 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3585 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
|
3586 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
3587 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
3588 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
3589 #include "stringlib/string_format.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
3590 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
3591 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
|
3592 "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
|
3593 \n\ |
|
66044
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63374
diff
changeset
|
3594 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
|
3595 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
|
3596 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3597 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
|
3598 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
|
3599 { |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3600 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
|
3601 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
|
3602 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
|
3603 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3604 /* 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
|
3605 /* 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
|
3606 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
|
3607 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
|
3608 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
|
3609 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
|
3610 "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
|
3611 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
|
3612 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3613 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
|
3614 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
|
3615 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
|
3616 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
|
3617 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3618 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
|
3619 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
|
3620 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
|
3621 done: |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3622 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
|
3623 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
|
3624 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
3625 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
3626 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
|
3627 "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
|
3628 \n\ |
|
66044
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63374
diff
changeset
|
3629 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
|
3630 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3631 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3632 static PyMethodDef |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3633 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
|
3634 /* 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
|
3635 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
|
3636 {"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
|
3637 {"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
|
3638 {"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
|
3639 {"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
|
3640 {"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
|
3641 {"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
|
3642 {"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
|
3643 {"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
|
3644 {"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
|
3645 {"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
|
3646 {"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
|
3647 {"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
|
3648 {"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
|
3649 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
|
3650 {"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
|
3651 {"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
|
3652 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
|
3653 {"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
|
3654 {"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
|
3655 {"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
|
3656 {"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
|
3657 {"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
|
3658 {"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
|
3659 {"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
|
3660 {"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
|
3661 {"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
|
3662 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
|
3663 {"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
|
3664 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
|
3665 {"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
|
3666 {"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
|
3667 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
|
3668 {"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
|
3669 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
|
3670 {"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
|
3671 {"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
|
3672 {"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
|
3673 {"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
|
3674 {"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
|
3675 {"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
|
3676 {"__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
|
3677 {"_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
|
3678 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3679 {"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
|
3680 {"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
|
3681 {"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
|
3682 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
|
3683 {"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
|
3684 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
|
3685 {"__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
|
3686 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
|
3687 {"__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
|
3688 {NULL, NULL} /* sentinel */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3689 }; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3690 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24184
diff
changeset
|
3691 static PyObject * |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3692 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
|
3693 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3694 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3695 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
|
3696 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3697 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
|
3698 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
|
3699 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3700 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
|
3701 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
|
3702 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
|
3703 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
|
3704 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
|
3705 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
|
3706 return PyObject_Str(x); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3707 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3708 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3709 static PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3710 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
|
3711 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3712 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
|
3713 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
|
3714 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3715 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
|
3716 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
|
3717 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
|
3718 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
|
3719 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
|
3720 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
|
3721 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
|
3722 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
|
3723 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
|
3724 ((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
|
3725 ((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
|
3726 ((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
|
3727 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3728 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
|
3729 return pnew; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3730 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3731 |
|
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
|
3732 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
|
3733 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
|
3734 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3735 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
|
3736 "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
|
3737 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
|
3738 } |
|
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 |
|
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
|
3740 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
|
3741 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
|
3742 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3743 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
|
3744 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
|
3745 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
|
3746 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3747 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
|
3748 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3750 PyDoc_STRVAR(basestring_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3751 "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
|
3752 |
|
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
|
3753 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
|
3754 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
|
3755 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
|
3756 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
|
3757 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
|
3758 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
|
3759 }; |
|
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 |
|
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
|
3762 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
|
3763 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
|
3764 "basestring", |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3765 0, |
|
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, /* 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
|
3768 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
|
3769 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
|
3770 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
|
3771 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
|
3772 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
|
3773 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
|
3774 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
|
3775 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
|
3776 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
|
3777 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
|
3778 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
|
3779 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
|
3780 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
|
3781 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
|
3782 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
|
3783 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
|
3784 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
|
3785 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
|
3786 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
|
3787 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
|
3788 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
|
3789 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
|
3790 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
|
3791 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
|
3792 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
|
3793 &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
|
3794 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
|
3795 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
|
3796 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
|
3797 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
|
3798 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
|
3799 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
|
3800 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
|
3801 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
|
3802 }; |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3804 PyDoc_STRVAR(string_doc, |
|
79590
3b484f53f91b
Issue #14783: Backport changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
78792
diff
changeset
|
3805 "str(object='') -> string\n\ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3806 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3807 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
|
3808 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
|
3809 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3810 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
|
3811 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
|
3812 "str", |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3813 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
|
3814 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
|
3815 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
|
3816 (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
|
3817 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
|
3818 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
|
3819 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
|
3820 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
|
3821 &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
|
3822 &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
|
3823 &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
|
3824 (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
|
3825 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
|
3826 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
|
3827 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
|
3828 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
|
3829 &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
|
3830 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
|
3831 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
|
3832 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
|
3833 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
|
3834 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
|
3835 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
|
3836 (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
|
3837 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
|
3838 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
|
3839 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
|
3840 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
|
3841 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
|
3842 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
|
3843 &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
|
3844 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
|
3845 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
|
3846 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
|
3847 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
|
3848 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
|
3849 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
|
3850 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
|
3851 PyObject_Del, /* tp_free */ |
| 6 | 3852 }; |
| 3853 | |
| 3854 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
|
3855 PyString_Concat(register PyObject **pv, register PyObject *w) |
| 6 | 3856 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3857 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
|
3858 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
|
3859 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3860 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
|
3861 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
|
3862 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3863 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3864 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
|
3865 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
|
3866 *pv = v; |
| 6 | 3867 } |
| 3868 | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3869 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
|
3870 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
|
3871 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3872 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
|
3873 Py_XDECREF(w); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3874 } |
|
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 |
| 6 | 3877 /* The following function breaks the notion that strings are immutable: |
| 3878 it changes the size of a string. We get away with this only if there | |
| 3879 is only one module referencing the object. You can also think of it | |
| 3880 as creating a new string object and destroying the old one, only | |
| 3881 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
|
3882 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
|
3883 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
|
3884 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
|
3885 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
|
3886 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
|
3887 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
|
3888 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
|
3889 */ |
| 6 | 3890 |
| 3891 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
|
3892 _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
| 6 | 3893 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3894 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
|
3895 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
|
3896 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
|
3897 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
|
3898 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
|
3899 *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
|
3900 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
|
3901 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
|
3902 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
|
3903 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3904 /* 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
|
3905 _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
|
3906 _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
|
3907 *pv = (PyObject *) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3908 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
|
3909 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
|
3910 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
|
3911 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
|
3912 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
|
3913 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3914 _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
|
3915 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
|
3916 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
|
3917 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
|
3918 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
|
3919 return 0; |
| 6 | 3920 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3921 |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3922 /* Helpers for formatstring */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3923 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
3924 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
|
3925 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
|
3926 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3927 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
|
3928 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
|
3929 (*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
|
3930 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
|
3931 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
|
3932 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3933 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
|
3934 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3935 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
|
3936 "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
|
3937 return NULL; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3938 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3939 |
|
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
|
3940 /* 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
|
3941 * 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
|
3942 * 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
|
3943 * 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
|
3944 * 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
|
3945 * 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
|
3946 */ |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3947 #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
|
3948 #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
|
3949 #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
|
3950 #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
|
3951 #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
|
3952 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
57277
diff
changeset
|
3953 /* 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
|
3954 |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
57277
diff
changeset
|
3955 static PyObject * |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
57277
diff
changeset
|
3956 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
|
3957 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3958 char *p; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3959 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
|
3960 double x; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3961 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3962 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
|
3963 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
|
3964 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
|
3965 "not %.200s", Py_TYPE(v)->tp_name); |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3966 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
|
3967 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3968 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
3969 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
|
3970 prec = 6; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3971 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3972 p = PyOS_double_to_string(x, type, prec, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3973 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3974 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3975 if (p == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3976 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3977 result = PyString_FromStringAndSize(p, strlen(p)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3978 PyMem_Free(p); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
3979 return result; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3980 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3981 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3982 /* _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
|
3983 * 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
|
3984 * 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
|
3985 * 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
|
3986 * . *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
|
3987 * *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
|
3988 * 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
|
3989 * 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
|
3990 * "-"? ("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
|
3991 * "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
|
3992 * 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
|
3993 * 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
|
3994 * 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
|
3995 * 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
|
3996 * 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
|
3997 * 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
|
3998 * 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
|
3999 * |
|
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 * 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
|
4001 * 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
|
4002 */ |
|
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 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
|
4004 _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
|
4005 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
|
4006 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4007 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
|
4008 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
|
4009 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
|
4010 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
|
4011 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
|
4012 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
|
4013 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
|
4014 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
|
4015 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4016 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
|
4017 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
|
4018 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
|
4019 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
|
4020 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4021 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
|
4022 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
|
4023 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4024 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
|
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 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
|
4027 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
|
4028 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4029 default: |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4030 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
|
4031 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4032 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
|
4033 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
|
4034 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4035 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
|
4036 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
|
4037 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
|
4038 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
|
4039 } |
|
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 /* 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
|
4042 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
|
4043 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
|
4044 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
|
4045 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4046 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
|
4047 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
|
4048 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
|
4049 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
|
4050 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4051 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
|
4052 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
|
4053 --len; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4054 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
|
4055 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4056 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
|
4057 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
|
4058 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
|
4059 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
|
4060 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4061 /* 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
|
4062 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
|
4063 /* 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
|
4064 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
|
4065 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
|
4066 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
|
4067 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
|
4068 /* 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
|
4069 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
|
4070 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
|
4071 --numdigits; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4072 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4073 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4074 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
|
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 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
|
4077 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
|
4078 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
|
4079 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
|
4080 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4081 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4082 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
|
4083 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
|
4084 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
|
4085 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
|
4086 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
|
4087 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4088 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
|
4089 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
|
4090 } |
|
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 /* 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
|
4093 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
|
4094 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
|
4095 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
|
4096 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
|
4097 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
|
4098 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
|
4099 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
|
4100 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4101 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
|
4102 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
|
4103 *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
|
4104 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
|
4105 *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
|
4106 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
|
4107 *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
|
4108 *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
|
4109 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
|
4110 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
|
4111 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
|
4112 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
|
4113 } |
|
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 /* 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
|
4116 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
|
4117 /* 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
|
4118 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
|
4119 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
|
4120 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
|
4121 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
|
4122 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4123 *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
|
4124 *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
|
4125 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
|
4126 } |
|
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 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
4128 Py_LOCAL_INLINE(int) |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4129 formatint(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4130 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
|
4131 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4132 /* 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
|
4133 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
|
4134 + 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
|
4135 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
|
4136 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
|
4137 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
|
4138 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4139 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
|
4140 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
|
4141 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
|
4142 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
|
4143 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
|
4144 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4145 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
|
4146 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
|
4147 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4148 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
|
4149 sign = "-"; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4150 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4151 sign = ""; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4152 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
|
4153 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
|
4154 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4155 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
|
4156 (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
|
4157 /* 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
|
4158 * 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
|
4159 * - 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
|
4160 * 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
|
4161 * %#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
|
4162 * 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
|
4163 * - 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
|
4164 * 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
|
4165 * (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
|
4166 * - 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
|
4167 * 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
|
4168 * 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
|
4169 * |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4170 * 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
|
4171 * 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
|
4172 * 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
|
4173 * |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4174 * 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
|
4175 * 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
|
4176 */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4177 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
|
4178 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
|
4179 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4180 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4181 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
|
4182 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
|
4183 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
|
4184 } |
|
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 /* 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
|
4187 * 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
|
4188 */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4189 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
|
4190 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
|
4191 "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
|
4192 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
|
4193 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4194 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
|
4195 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
|
4196 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4197 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
|
4198 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
|
4199 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4200 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
4201 Py_LOCAL_INLINE(int) |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4202 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
|
4203 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4204 /* 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
|
4205 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
|
4206 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
|
4207 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
|
4208 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4209 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4210 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
|
4211 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
|
4212 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4213 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
|
4214 return 1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4215 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4216 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4217 /* 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
|
4218 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
57277
diff
changeset
|
4219 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
|
4220 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
|
4221 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
|
4222 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
|
4223 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
|
4224 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4225 #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
|
4226 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4227 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
|
4228 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
|
4229 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4230 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
|
4231 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
|
4232 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
|
4233 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
|
4234 PyObject *result, *orig_args; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4235 #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
|
4236 PyObject *v, *w; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4237 #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
|
4238 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
|
4239 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
|
4240 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
|
4241 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
|
4242 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4243 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
|
4244 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
|
4245 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
|
4246 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
|
4247 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
|
4248 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
|
4249 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
|
4250 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
|
4251 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
|
4252 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
|
4253 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
|
4254 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4255 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4256 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
|
4257 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
|
4258 } |
|
82938
391e3a7db1a3
allow any type with __getitem__ to be a mapping for the purposes of % (#15801)
Benjamin Peterson <benjamin@python.org>
parents:
81933
diff
changeset
|
4259 if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript && |
|
391e3a7db1a3
allow any type with __getitem__ to be a mapping for the purposes of % (#15801)
Benjamin Peterson <benjamin@python.org>
parents:
81933
diff
changeset
|
4260 !PyTuple_Check(args) && !PyObject_TypeCheck(args, &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
|
4261 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
|
4262 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
|
4263 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
|
4264 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
|
4265 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
|
4266 reslen += rescnt; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4267 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
|
4268 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
|
4269 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
|
4270 + 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
|
4271 --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 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4273 *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
|
4274 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4275 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4276 /* 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
|
4277 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
|
4278 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
|
4279 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
|
4280 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
|
4281 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
|
4282 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
|
4283 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
|
4284 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
|
4285 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
|
4286 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
|
4287 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
|
4288 char formatbuf[FORMATBUFLEN]; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4289 /* For format{int,char}() */ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4290 #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
|
4291 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
|
4292 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
|
4293 #endif |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
4294 |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4295 fmt++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4296 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
|
4297 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
|
4298 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
|
4299 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
|
4300 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
|
4301 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4302 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
|
4303 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
|
4304 "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
|
4305 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
|
4306 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4307 ++fmt; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4308 --fmtcnt; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4309 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
|
4310 /* 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
|
4311 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
|
4312 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
|
4313 --pcount; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4314 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
|
4315 ++pcount; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4316 fmt++; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4317 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4318 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
|
4319 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
|
4320 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
|
4321 "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
|
4322 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
|
4323 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4324 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
|
4325 keylen); |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4326 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
|
4327 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
|
4328 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
|
4329 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
|
4330 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
|
4331 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4332 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
|
4333 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
|
4334 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
|
4335 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
|
4336 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4337 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
|
4338 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
|
4339 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
|
4340 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4341 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
|
4342 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
|
4343 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
|
4344 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
|
4345 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
|
4346 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
|
4347 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
|
4348 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4349 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4350 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4351 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
|
4352 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
|
4353 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
|
4354 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
|
4355 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
|
4356 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
|
4357 "* 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
|
4358 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
|
4359 } |
|
81605
ee93a89b4e0f
Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81245
diff
changeset
|
4360 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
|
4361 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
|
4362 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
|
4363 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
|
4364 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
|
4365 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
|
4366 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4367 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
|
4368 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
|
4369 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4370 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
|
4371 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
|
4372 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
|
4373 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
|
4374 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
|
4375 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
|
4376 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
|
4377 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
|
4378 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
|
4379 "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
|
4380 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
|
4381 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4382 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
|
4383 } |
|
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 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
|
4386 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
|
4387 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
|
4388 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
|
4389 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
|
4390 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
|
4391 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
|
4392 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
|
4393 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
|
4394 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
|
4395 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
|
4396 "* 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
|
4397 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
|
4398 } |
|
81605
ee93a89b4e0f
Issue #15989: Fix possible integer overflow in str formatting as in unicode formatting.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81245
diff
changeset
|
4399 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
|
4400 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
|
4401 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
|
4402 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
|
4403 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 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
|
4405 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
|
4406 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4407 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
|
4408 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
|
4409 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
|
4410 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
|
4411 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
|
4412 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
|
4413 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
|
4414 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
|
4415 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
|
4416 "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
|
4417 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
|
4418 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4419 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
|
4420 } |
|
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 } /* prec */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4423 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
|
4424 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
|
4425 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
|
4426 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
|
4427 } |
|
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 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
|
4430 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
|
4431 "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
|
4432 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
|
4433 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4434 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
|
4435 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
|
4436 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
|
4437 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
|
4438 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4439 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
|
4440 fill = ' '; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4441 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
|
4442 case '%': |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4443 pbuf = "%"; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4444 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
|
4445 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4446 case 's': |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4447 #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
|
4448 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
|
4449 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
|
4450 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
|
4451 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
|
4452 } |
|
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
|
4453 #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
|
4454 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
|
4455 #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
|
4456 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
|
4457 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
|
4458 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
|
4459 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
|
4460 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
|
4461 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4462 #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
|
4463 /* 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
|
4464 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
|
4465 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
|
4466 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
|
4467 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
|
4468 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
|
4469 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
|
4470 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
|
4471 "%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
|
4472 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
|
4473 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
|
4474 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4475 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
|
4476 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
|
4477 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
|
4478 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 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4480 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
|
4481 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
|
4482 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
|
4483 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
|
4484 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
|
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 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
|
4487 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
|
4488 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
|
4489 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
|
4490 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
|
4491 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4492 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
|
4493 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
|
4494 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
|
4495 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4496 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4497 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
|
4498 if (iobj==NULL) { |
| 81245 | 4499 PyErr_Clear(); |
| 4500 iobj = PyNumber_Long(v); | |
| 4501 } | |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4502 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4503 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
|
4504 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
|
4505 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
|
4506 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
|
4507 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
|
4508 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
|
4509 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
|
4510 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
|
4511 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
|
4512 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
|
4513 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
|
4514 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4515 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
|
4516 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
|
4517 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4518 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
|
4519 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
|
4520 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
|
4521 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
|
4522 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
|
4523 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
|
4524 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
|
4525 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
|
4526 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4527 else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4528 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
|
4529 } |
|
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 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
|
4533 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
|
4534 "%%%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
|
4535 "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
|
4536 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
|
4537 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4538 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
|
4539 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
|
4540 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4541 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
|
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 'f': |
|
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 'g': |
|
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': |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4547 temp = formatfloat(v, flags, prec, c); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4548 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
|
4549 goto error; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4550 pbuf = PyString_AS_STRING(temp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4551 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
|
4552 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
|
4553 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
|
4554 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
|
4555 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4556 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
|
4557 #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
|
4558 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
|
4559 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
|
4560 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
|
4561 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
|
4562 } |
|
28478
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4563 #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
|
4564 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
|
4565 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
|
4566 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
|
4567 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
|
4568 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4569 default: |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4570 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
|
4571 "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
|
4572 "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
|
4573 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
|
4574 (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
|
4575 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
|
4576 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
|
4577 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4578 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
|
4579 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
|
4580 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
|
4581 len--; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4582 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4583 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
|
4584 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 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
|
4586 sign = ' '; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4587 else |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4588 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
|
4589 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4590 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
|
4591 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 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
|
4593 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
|
4594 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
|
4595 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
|
4596 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
|
4597 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
|
4598 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
|
4599 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
|
4600 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4601 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
|
4602 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
|
4603 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
|
4604 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4605 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
|
4606 + 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
|
4607 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4608 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
|
4609 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
|
4610 *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
|
4611 rescnt--; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4612 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
|
4613 width--; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4614 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4615 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
|
4616 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
|
4617 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
|
4618 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
|
4619 *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
|
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 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4622 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
|
4623 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
|
4624 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
|
4625 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 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
|
4627 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4628 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
|
4629 do { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4630 --rescnt; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4631 *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
|
4632 } 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
|
4633 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4634 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
|
4635 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
|
4636 *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
|
4637 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
|
4638 (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
|
4639 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
|
4640 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
|
4641 *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
|
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 } |
|
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 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
|
4646 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
|
4647 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
|
4648 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
|
4649 --rescnt; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4650 *res++ = ' '; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4651 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4652 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
|
4653 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
|
4654 "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
|
4655 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
|
4656 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
|
4657 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4658 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
|
4659 } /* '%' */ |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4660 } /* 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
|
4661 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
|
4662 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
|
4663 "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
|
4664 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
|
4665 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4666 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
|
4667 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
|
4668 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4669 if (_PyString_Resize(&result, reslen - rescnt)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60200
diff
changeset
|
4670 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
|
4671 return result; |
| 12328 | 4672 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4673 #ifdef Py_USING_UNICODE |
| 12328 | 4674 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
|
4675 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
|
4676 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
|
4677 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
|
4678 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4679 /* 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
|
4680 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
|
4681 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
|
4682 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
|
4683 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
|
4684 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
|
4685 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
|
4686 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
|
4687 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
|
4688 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
|
4689 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
|
4690 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4691 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
|
4692 } else { |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4693 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
|
4694 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
|
4695 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4696 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
|
4697 /* 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
|
4698 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
|
4699 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
|
4700 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
|
4701 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
|
4702 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
|
4703 (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
|
4704 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
|
4705 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
|
4706 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
|
4707 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
|
4708 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
|
4709 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
|
4710 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
|
4711 /* 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
|
4712 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
|
4713 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
|
4714 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
|
4715 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
|
4716 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
|
4717 return w; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4718 #endif /* Py_USING_UNICODE */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
4719 |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4720 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
|
4721 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
|
4722 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
|
4723 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
|
4724 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4725 return NULL; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4726 } |
| 5005 | 4727 |
| 4728 void | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
4729 PyString_InternInPlace(PyObject **p) |
| 5005 | 4730 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4731 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
|
4732 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
|
4733 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
|
4734 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
|
4735 /* 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
|
4736 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
|
4737 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
|
4738 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4739 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
|
4740 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4741 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
|
4742 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
|
4743 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
|
4744 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
|
4745 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4746 } |
|
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 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
|
4749 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
|
4750 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
|
4751 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
|
4752 *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
|
4753 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4754 } |
|
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 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
|
4757 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
|
4758 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4759 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4760 /* 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
|
4761 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
|
4762 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
|
4763 PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 5005 | 4764 } |
| 4765 | |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
4766 void |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
4767 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
|
4768 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4769 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
|
4770 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
|
4771 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 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
|
4773 } |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
4774 } |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
4775 |
| 5005 | 4776 |
| 4777 PyObject * | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
4778 PyString_InternFromString(const char *cp) |
| 5005 | 4779 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4780 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
|
4781 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
|
4782 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
|
4783 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
|
4784 return s; |
| 5005 | 4785 } |
| 4786 | |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
4787 void |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
4788 PyString_Fini(void) |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
4789 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4790 int i; |
|
81933
4d120cea3507
Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81737
diff
changeset
|
4791 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
|
4792 Py_CLEAR(characters[i]); |
|
4d120cea3507
Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81737
diff
changeset
|
4793 Py_CLEAR(nullstring); |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
4794 } |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
4795 |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
4796 void _Py_ReleaseInternedStrings(void) |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
4797 { |
|
75133
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4798 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
|
4799 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
|
4800 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
|
4801 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
|
4802 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4803 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
|
4804 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4805 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
|
4806 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
|
4807 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
|
4808 return; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4809 } |
|
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 /* 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
|
4812 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
|
4813 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
|
4814 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
|
4815 |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4816 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
|
4817 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
|
4818 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 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
|
4820 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
|
4821 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
|
4822 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
|
4823 /* 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
|
4824 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4825 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
|
4826 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
|
4827 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
|
4828 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4829 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
|
4830 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
|
4831 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
|
4832 break; |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4833 default: |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4834 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
|
4835 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4836 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
|
4837 } |
|
357e268e7c5f
ensure no one tries to hash things before the random seed is found
Benjamin Peterson <benjamin@python.org>
parents:
47560
diff
changeset
|
4838 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
|
4839 "%" 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
|
4840 "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
|
4841 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
|
4842 PyDict_Clear(interned); |
|
81933
4d120cea3507
Issue #17034: Use Py_CLEAR() in stringobject.c.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81737
diff
changeset
|
4843 Py_CLEAR(interned); |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
4844 } |
