Mercurial > cpython-lru_cache-weakref
annotate Python/codecs.c @ 30783:4833a9cd377e 2.3
regressing the performance bugfix -- Guido wants the performance bug left
alone, because there can be no guarantee re the semantics of += vs + .
| author | Alex Martelli <aleaxit@gmail.com> |
|---|---|
| date | Sat, 25 Oct 2003 23:22:55 +0000 |
| parents | 850626d47574 |
| children | 25519d6c97b8 |
| rev | line source |
|---|---|
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* ------------------------------------------------------------------------ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 Python Codec Registry and support functions |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 Written by Marc-Andre Lemburg (mal@lemburg.com). |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
13828
254f8b7c4a83
Changing the CNRI copyright notice according to CNRI's instructions.
Guido van Rossum <guido@python.org>
parents:
13658
diff
changeset
|
7 Copyright (c) Corporation for National Research Initiatives. |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 ------------------------------------------------------------------------ */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 #include "Python.h" |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 #include <ctype.h> |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 /* --- Codec Registry ----------------------------------------------------- */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 /* Import the standard encodings package which will register the first |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 codec search function. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 This is done in a lazy way so that the Unicode implementation does |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 not downgrade startup time of scripts not needing it. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
22 ImportErrors are silently ignored by this function. Only one try is |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
23 made. |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
27 static int _PyCodecRegistry_Init(void); /* Forward */ |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 int PyCodec_Register(PyObject *search_function) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 { |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
31 PyInterpreterState *interp = PyThreadState_Get()->interp; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
32 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
33 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 if (search_function == NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 PyErr_BadArgument(); |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
36 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 if (!PyCallable_Check(search_function)) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 "argument must be callable"); |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
41 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
43 return PyList_Append(interp->codec_search_path, search_function); |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
44 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
45 onError: |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
46 return -1; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
49 /* Convert a string to a normalized Python string: all characters are |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
50 converted to lower case, spaces are replaced with underscores. */ |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
51 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 static |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
53 PyObject *normalizestring(const char *string) |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 { |
|
13128
cb928d926f27
Change the loop index in normalizestring() to size_t too, to avoid a
Guido van Rossum <guido@python.org>
parents:
13096
diff
changeset
|
55 register size_t i; |
|
13096
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
56 size_t len = strlen(string); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 char *p; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
13096
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
60 if (len > INT_MAX) { |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
61 PyErr_SetString(PyExc_OverflowError, "string is too large"); |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
62 return NULL; |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
63 } |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
64 |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
65 v = PyString_FromStringAndSize(NULL, (int)len); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 p = PyString_AS_STRING(v); |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
69 for (i = 0; i < len; i++) { |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
70 register char ch = string[i]; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
71 if (ch == ' ') |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
72 ch = '-'; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
73 else |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
74 ch = tolower(ch); |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
75 p[i] = ch; |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
76 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 /* Lookup the given encoding and return a tuple providing the codec |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 facilities. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 The encoding string is looked up converted to all lower-case |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 characters. This makes encodings looked up through this mechanism |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 effectively case-insensitive. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
87 If no codec is found, a LookupError is set and NULL returned. |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
88 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
89 As side effect, this tries to load the encodings package, if not |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
90 yet done. This is part of the lazy load strategy for the encodings |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
91 package. |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
92 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
93 */ |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 PyObject *_PyCodec_Lookup(const char *encoding) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 { |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
97 PyInterpreterState *interp; |
| 12048 | 98 PyObject *result, *args = NULL, *v; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 int i, len; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
101 if (encoding == NULL) { |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
102 PyErr_BadArgument(); |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
103 goto onError; |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
104 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
105 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
106 interp = PyThreadState_Get()->interp; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
107 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
108 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
110 /* Convert the encoding to a normalized Python string: all |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13330
diff
changeset
|
111 characters are converted to lower case, spaces and hyphens are |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
112 replaced with underscores. */ |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
113 v = normalizestring(encoding); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 PyString_InternInPlace(&v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 /* First, try to lookup the name in the registry dictionary */ |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
119 result = PyDict_GetItem(interp->codec_search_cache, v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 if (result != NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 Py_INCREF(result); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
122 Py_DECREF(v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 /* Next, scan the search functions in order of registration */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 args = PyTuple_New(1); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 PyTuple_SET_ITEM(args,0,v); |
| 12048 | 131 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
132 len = PyList_Size(interp->codec_search_path); |
| 12048 | 133 if (len < 0) |
| 134 goto onError; | |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
135 if (len == 0) { |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
136 PyErr_SetString(PyExc_LookupError, |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
137 "no codec search functions registered: " |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
138 "can't find encoding"); |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
139 goto onError; |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
140 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 for (i = 0; i < len; i++) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 PyObject *func; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
145 func = PyList_GetItem(interp->codec_search_path, i); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 if (func == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 goto onError; |
| 12048 | 148 result = PyEval_CallObject(func, args); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 if (result == Py_None) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 continue; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 "codec search functions must return 4-tuples"); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 break; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 if (i == len) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 /* XXX Perhaps we should cache misses too ? */ |
|
25441
d32588c88633
Add encoding name in LookupError. Fixes #615013. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents:
25095
diff
changeset
|
165 PyErr_Format(PyExc_LookupError, |
|
d32588c88633
Add encoding name in LookupError. Fixes #615013. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents:
25095
diff
changeset
|
166 "unknown encoding: %s", encoding); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 /* Cache and return the result */ |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
171 PyDict_SetItem(interp->codec_search_cache, v, result); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 PyObject *args_tuple(PyObject *object, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 PyObject *args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 args = PyTuple_New(1 + (errors != NULL)); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 Py_INCREF(object); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 PyTuple_SET_ITEM(args,0,object); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 if (errors) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 v = PyString_FromString(errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 if (v == NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 PyTuple_SET_ITEM(args, 1, v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 return args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 /* Build a codec by calling factory(stream[,errors]) or just |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 factory(errors) depending on whether the given parameters are |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 non-NULL. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 PyObject *build_stream_codec(PyObject *factory, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 PyObject *args, *codec; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 args = args_tuple(stream, errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 codec = PyEval_CallObject(factory, args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 return codec; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 /* Convenience APIs to query the Codec registry. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 All APIs return a codec object with incremented refcount. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 PyObject *PyCodec_Encoder(const char *encoding) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 v = PyTuple_GET_ITEM(codecs,0); |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
239 Py_DECREF(codecs); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 PyObject *PyCodec_Decoder(const char *encoding) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 v = PyTuple_GET_ITEM(codecs,1); |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
256 Py_DECREF(codecs); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 PyObject *PyCodec_StreamReader(const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 { |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
268 PyObject *codecs, *ret; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 goto onError; |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
273 ret = build_stream_codec(PyTuple_GET_ITEM(codecs,2),stream,errors); |
|
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
274 Py_DECREF(codecs); |
|
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
275 return ret; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 PyObject *PyCodec_StreamWriter(const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 { |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
285 PyObject *codecs, *ret; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 goto onError; |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
290 ret = build_stream_codec(PyTuple_GET_ITEM(codecs,3),stream,errors); |
|
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
291 Py_DECREF(codecs); |
|
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
292 return ret; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 /* Encode an object (e.g. an Unicode object) using the given encoding |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 and return the resulting encoded object (usually a Python string). |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 errors is passed to the encoder factory as argument if non-NULL. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 PyObject *PyCodec_Encode(PyObject *object, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 PyObject *encoder = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 PyObject *args = NULL, *result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 encoder = PyCodec_Encoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 if (encoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 args = args_tuple(object, errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 result = PyEval_CallObject(encoder,args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 if (!PyTuple_Check(result) || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 PyTuple_GET_SIZE(result) != 2) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 "encoder must return a tuple (object,integer)"); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 v = PyTuple_GET_ITEM(result,0); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 /* We don't check or use the second (integer) entry. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 Py_DECREF(encoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 Py_XDECREF(encoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 /* Decode an object (usually a Python string) using the given encoding |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 and return an equivalent object (e.g. an Unicode object). |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 errors is passed to the decoder factory as argument if non-NULL. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 PyObject *PyCodec_Decode(PyObject *object, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 PyObject *decoder = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 PyObject *args = NULL, *result = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 decoder = PyCodec_Decoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 if (decoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 args = args_tuple(object, errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 result = PyEval_CallObject(decoder,args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 if (!PyTuple_Check(result) || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 PyTuple_GET_SIZE(result) != 2) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 "decoder must return a tuple (object,integer)"); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 v = PyTuple_GET_ITEM(result,0); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 /* We don't check or use the second (integer) entry. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 Py_DECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 Py_XDECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 Py_XDECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
390 /* Register the error handling callback function error under the name |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
391 name. This function will be called by the codec when it encounters |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
392 an unencodable characters/undecodable bytes and doesn't know the |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
393 callback name, when name is specified as the error parameter |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
394 in the call to the encode/decode function. |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
395 Return 0 on success, -1 on error */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
396 int PyCodec_RegisterError(const char *name, PyObject *error) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
397 { |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
398 PyInterpreterState *interp = PyThreadState_Get()->interp; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
399 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
400 return -1; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
401 if (!PyCallable_Check(error)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
402 PyErr_SetString(PyExc_TypeError, "handler must be callable"); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
403 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
404 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
405 return PyDict_SetItemString(interp->codec_error_registry, |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
406 (char *)name, error); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
407 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
408 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
409 /* Lookup the error handling callback function registered under the |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
410 name error. As a special case NULL can be passed, in which case |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
411 the error handling callback for strict encoding will be returned. */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
412 PyObject *PyCodec_LookupError(const char *name) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
413 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
414 PyObject *handler = NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
415 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
416 PyInterpreterState *interp = PyThreadState_Get()->interp; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
417 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
418 return NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
419 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
420 if (name==NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
421 name = "strict"; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
422 handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
423 if (!handler) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
424 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
425 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
426 Py_INCREF(handler); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
427 return handler; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
428 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
429 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
430 static void wrong_exception_type(PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
431 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
432 PyObject *type = PyObject_GetAttrString(exc, "__class__"); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
433 if (type != NULL) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
434 PyObject *name = PyObject_GetAttrString(type, "__name__"); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
435 Py_DECREF(type); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
436 if (name != NULL) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
437 PyObject *string = PyObject_Str(name); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
438 Py_DECREF(name); |
|
25095
1b80e0fd5a48
Check string for NULL before using it to format the error message.
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
439 if (string != NULL) { |
|
1b80e0fd5a48
Check string for NULL before using it to format the error message.
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
440 PyErr_Format(PyExc_TypeError, |
|
1b80e0fd5a48
Check string for NULL before using it to format the error message.
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
441 "don't know how to handle %.400s in error callback", |
|
1b80e0fd5a48
Check string for NULL before using it to format the error message.
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
442 PyString_AS_STRING(string)); |
|
1b80e0fd5a48
Check string for NULL before using it to format the error message.
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
443 Py_DECREF(string); |
|
1b80e0fd5a48
Check string for NULL before using it to format the error message.
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
444 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
445 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
446 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
447 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
448 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
449 PyObject *PyCodec_StrictErrors(PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
450 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
451 if (PyInstance_Check(exc)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
452 PyErr_SetObject((PyObject*)((PyInstanceObject*)exc)->in_class, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
453 exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
454 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
455 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance"); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
456 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
457 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
458 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
459 |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
460 #ifdef Py_USING_UNICODE |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
461 PyObject *PyCodec_IgnoreErrors(PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
462 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
463 int end; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
464 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
465 if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
466 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
467 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
468 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
469 if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
470 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
471 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
472 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
473 if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
474 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
475 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
476 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
477 wrong_exception_type(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
478 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
479 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
480 /* ouch: passing NULL, 0, pos gives None instead of u'' */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
481 return Py_BuildValue("(u#i)", &end, 0, end); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
482 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
483 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
484 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
485 PyObject *PyCodec_ReplaceErrors(PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
486 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
487 PyObject *restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
488 int start; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
489 int end; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
490 int i; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
491 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
492 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
493 PyObject *res; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
494 Py_UNICODE *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
495 if (PyUnicodeEncodeError_GetStart(exc, &start)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
496 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
497 if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
498 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
499 res = PyUnicode_FromUnicode(NULL, end-start); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
500 if (res == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
501 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
502 for (p = PyUnicode_AS_UNICODE(res), i = start; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
503 i<end; ++p, ++i) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
504 *p = '?'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
505 restuple = Py_BuildValue("(Oi)", res, end); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
506 Py_DECREF(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
507 return restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
508 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
509 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
510 Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
511 if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
512 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
513 return Py_BuildValue("(u#i)", &res, 1, end); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
514 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
515 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
516 PyObject *res; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
517 Py_UNICODE *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
518 if (PyUnicodeTranslateError_GetStart(exc, &start)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
519 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
520 if (PyUnicodeTranslateError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
521 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
522 res = PyUnicode_FromUnicode(NULL, end-start); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
523 if (res == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
524 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
525 for (p = PyUnicode_AS_UNICODE(res), i = start; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
526 i<end; ++p, ++i) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
527 *p = Py_UNICODE_REPLACEMENT_CHARACTER; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
528 restuple = Py_BuildValue("(Oi)", res, end); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
529 Py_DECREF(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
530 return restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
531 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
532 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
533 wrong_exception_type(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
534 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
535 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
536 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
537 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
538 PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
539 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
540 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
541 PyObject *restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
542 PyObject *object; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
543 int start; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
544 int end; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
545 PyObject *res; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
546 Py_UNICODE *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
547 Py_UNICODE *startp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
548 Py_UNICODE *outp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
549 int ressize; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
550 if (PyUnicodeEncodeError_GetStart(exc, &start)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
551 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
552 if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
553 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
554 if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
555 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
556 startp = PyUnicode_AS_UNICODE(object); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
557 for (p = startp+start, ressize = 0; p < startp+end; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
558 if (*p<10) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
559 ressize += 2+1+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
560 else if (*p<100) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
561 ressize += 2+2+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
562 else if (*p<1000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
563 ressize += 2+3+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
564 else if (*p<10000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
565 ressize += 2+4+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
566 else if (*p<100000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
567 ressize += 2+5+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
568 else if (*p<1000000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
569 ressize += 2+6+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
570 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
571 ressize += 2+7+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
572 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
573 /* allocate replacement */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
574 res = PyUnicode_FromUnicode(NULL, ressize); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
575 if (res == NULL) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
576 Py_DECREF(object); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
577 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
578 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
579 /* generate replacement */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
580 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
581 p < startp+end; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
582 Py_UNICODE c = *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
583 int digits; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
584 int base; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
585 *outp++ = '&'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
586 *outp++ = '#'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
587 if (*p<10) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
588 digits = 1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
589 base = 1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
590 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
591 else if (*p<100) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
592 digits = 2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
593 base = 10; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
594 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
595 else if (*p<1000) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
596 digits = 3; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
597 base = 100; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
598 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
599 else if (*p<10000) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
600 digits = 4; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
601 base = 1000; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
602 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
603 else if (*p<100000) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
604 digits = 5; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
605 base = 10000; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
606 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
607 else if (*p<1000000) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
608 digits = 6; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
609 base = 100000; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
610 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
611 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
612 digits = 7; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
613 base = 1000000; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
614 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
615 while (digits-->0) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
616 *outp++ = '0' + c/base; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
617 c %= base; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
618 base /= 10; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
619 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
620 *outp++ = ';'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
621 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
622 restuple = Py_BuildValue("(Oi)", res, end); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
623 Py_DECREF(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
624 Py_DECREF(object); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
625 return restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
626 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
627 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
628 wrong_exception_type(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
629 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
630 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
631 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
632 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
633 static Py_UNICODE hexdigits[] = { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
634 '0', '1', '2', '3', '4', '5', '6', '7', |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
635 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
636 }; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
637 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
638 PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
639 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
640 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
641 PyObject *restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
642 PyObject *object; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
643 int start; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
644 int end; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
645 PyObject *res; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
646 Py_UNICODE *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
647 Py_UNICODE *startp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
648 Py_UNICODE *outp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
649 int ressize; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
650 if (PyUnicodeEncodeError_GetStart(exc, &start)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
651 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
652 if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
653 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
654 if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
655 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
656 startp = PyUnicode_AS_UNICODE(object); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
657 for (p = startp+start, ressize = 0; p < startp+end; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
658 if (*p >= 0x00010000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
659 ressize += 1+1+8; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
660 else if (*p >= 0x100) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
661 ressize += 1+1+4; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
662 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
663 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
664 ressize += 1+1+2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
665 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
666 res = PyUnicode_FromUnicode(NULL, ressize); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
667 if (res==NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
668 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
669 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
670 p < startp+end; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
671 Py_UNICODE c = *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
672 *outp++ = '\\'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
673 if (c >= 0x00010000) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
674 *outp++ = 'U'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
675 *outp++ = hexdigits[(c>>28)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
676 *outp++ = hexdigits[(c>>24)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
677 *outp++ = hexdigits[(c>>20)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
678 *outp++ = hexdigits[(c>>16)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
679 *outp++ = hexdigits[(c>>12)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
680 *outp++ = hexdigits[(c>>8)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
681 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
682 else if (c >= 0x100) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
683 *outp++ = 'u'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
684 *outp++ = hexdigits[(c>>12)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
685 *outp++ = hexdigits[(c>>8)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
686 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
687 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
688 *outp++ = 'x'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
689 *outp++ = hexdigits[(c>>4)&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
690 *outp++ = hexdigits[c&0xf]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
691 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
692 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
693 restuple = Py_BuildValue("(Oi)", res, end); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
694 Py_DECREF(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
695 Py_DECREF(object); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
696 return restuple; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
697 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
698 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
699 wrong_exception_type(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
700 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
701 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
702 } |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
703 #endif |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
704 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
705 static PyObject *strict_errors(PyObject *self, PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
706 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
707 return PyCodec_StrictErrors(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
708 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
709 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
710 |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
711 #ifdef Py_USING_UNICODE |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
712 static PyObject *ignore_errors(PyObject *self, PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
713 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
714 return PyCodec_IgnoreErrors(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
715 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
716 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
717 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
718 static PyObject *replace_errors(PyObject *self, PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
719 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
720 return PyCodec_ReplaceErrors(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
721 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
722 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
723 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
724 static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
725 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
726 return PyCodec_XMLCharRefReplaceErrors(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
727 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
728 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
729 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
730 static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
731 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
732 return PyCodec_BackslashReplaceErrors(exc); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
733 } |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
734 #endif |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
735 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
736 static int _PyCodecRegistry_Init(void) |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
737 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
738 static struct { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
739 char *name; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
740 PyMethodDef def; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
741 } methods[] = |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
742 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
743 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
744 "strict", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
745 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
746 "strict_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
747 strict_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
748 METH_O |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
749 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
750 }, |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
751 #ifdef Py_USING_UNICODE |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
752 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
753 "ignore", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
754 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
755 "ignore_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
756 ignore_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
757 METH_O |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
758 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
759 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
760 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
761 "replace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
762 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
763 "replace_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
764 replace_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
765 METH_O |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
766 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
767 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
768 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
769 "xmlcharrefreplace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
770 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
771 "xmlcharrefreplace_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
772 xmlcharrefreplace_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
773 METH_O |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
774 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
775 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
776 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
777 "backslashreplace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
778 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
779 "backslashreplace_errors", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
780 backslashreplace_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
781 METH_O |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
782 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
783 } |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
784 #endif |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
785 }; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
786 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
787 PyInterpreterState *interp = PyThreadState_Get()->interp; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
788 PyObject *mod; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
789 int i; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
790 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
791 if (interp->codec_search_path != NULL) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
792 return 0; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
793 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
794 interp->codec_search_path = PyList_New(0); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
795 interp->codec_search_cache = PyDict_New(); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
796 interp->codec_error_registry = PyDict_New(); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
797 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
798 if (interp->codec_error_registry) { |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
799 for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) { |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
800 PyObject *func = PyCFunction_New(&methods[i].def, NULL); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
801 int res; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
802 if (!func) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
803 Py_FatalError("can't initialize codec error registry"); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
804 res = PyCodec_RegisterError(methods[i].name, func); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
805 Py_DECREF(func); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
806 if (res) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
807 Py_FatalError("can't initialize codec error registry"); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
808 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
809 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
810 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
811 if (interp->codec_search_path == NULL || |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
812 interp->codec_search_cache == NULL || |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
813 interp->codec_error_registry == NULL) |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13330
diff
changeset
|
814 Py_FatalError("can't initialize codec registry"); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
815 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
816 mod = PyImport_ImportModuleEx("encodings", NULL, NULL, NULL); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
817 if (mod == NULL) { |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
818 if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
819 /* Ignore ImportErrors... this is done so that |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
820 distributions can disable the encodings package. Note |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
821 that other errors are not masked, e.g. SystemErrors |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
822 raised to inform the user of an error in the Python |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
823 configuration are still reported back to the user. */ |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
824 PyErr_Clear(); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
825 return 0; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
826 } |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
827 return -1; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
828 } |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
829 Py_DECREF(mod); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
830 return 0; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
831 } |
