Mercurial > cpython-lru_cache-weakref
annotate Python/codecs.c @ 37875:a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
(branch-creation time) up to 43067. 43068 and 43069 contain a little
swapping action between re.py and sre.py, and this mightily confuses svn
merge, so later changes are going in separately.
This merge should break no additional tests.
The last-merged revision is going in a 'last_merge' property on '.' (the
branch directory.) Arbitrarily chosen, really; if there's a BCP for this, I
couldn't find it, but we can easily change it afterwards ;)
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Fri, 21 Apr 2006 09:43:23 +0000 |
| parents | 8e04de2480e4 |
| children | c449944068d2 f731859e9b3b |
| 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 { |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
31 PyInterpreterState *interp = PyThreadState_GET()->interp; |
|
28325
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)) { |
|
35939
79374eb6db51
Fix a few more ref leaks. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents:
32533
diff
changeset
|
39 PyErr_SetString(PyExc_TypeError, "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
|
40 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
42 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
|
43 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
44 onError: |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
45 return -1; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
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
|
48 /* 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
|
49 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
|
50 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 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
|
52 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
|
53 { |
|
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
|
54 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
|
55 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
|
56 char *p; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
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
|
59 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
|
60 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
|
61 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
|
62 } |
|
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 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
|
65 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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 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
|
73 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
|
74 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
|
75 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } |
|
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 /* 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
|
80 facilities. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 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
|
83 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
|
84 effectively case-insensitive. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
86 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
|
87 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
88 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
|
89 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
|
90 package. |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
91 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
92 */ |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 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
|
95 { |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
96 PyInterpreterState *interp; |
| 12048 | 97 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
|
98 int i, len; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
100 if (encoding == NULL) { |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
101 PyErr_BadArgument(); |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
102 goto onError; |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
103 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
104 |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
105 interp = PyThreadState_GET()->interp; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
106 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
|
107 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
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
|
109 /* 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
|
110 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
|
111 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
|
112 v = normalizestring(encoding); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 PyString_InternInPlace(&v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 /* 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
|
118 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
|
119 if (result != NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 Py_INCREF(result); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
121 Py_DECREF(v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 } |
|
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 /* 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
|
126 args = PyTuple_New(1); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 PyTuple_SET_ITEM(args,0,v); |
| 12048 | 130 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
131 len = PyList_Size(interp->codec_search_path); |
| 12048 | 132 if (len < 0) |
| 133 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
|
134 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
|
135 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
|
136 "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
|
137 "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
|
138 goto onError; |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
139 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 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
|
142 PyObject *func; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
144 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
|
145 if (func == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 goto onError; |
| 12048 | 147 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
|
148 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 if (result == Py_None) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 continue; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 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
|
155 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 "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
|
157 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 break; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 if (i == len) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 /* 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
|
164 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
|
165 "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
|
166 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 } |
|
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 /* 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
|
170 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
|
171 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 } |
|
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 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 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
|
181 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 PyObject *args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 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
|
186 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 Py_INCREF(object); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 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
|
190 if (errors) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 v = PyString_FromString(errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 if (v == NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 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
|
199 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 return args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 } |
|
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 /* 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
|
204 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
|
205 non-NULL. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 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
|
209 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 PyObject *args, *codec; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 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
|
215 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 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
|
219 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 return codec; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 } |
|
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 /* 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
|
224 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 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
|
226 |
|
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 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
|
230 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 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
|
238 Py_DECREF(codecs); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 } |
|
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 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
|
247 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 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
|
255 Py_DECREF(codecs); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
263 PyObject *PyCodec_IncrementalEncoder(const char *encoding, |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
264 const char *errors) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
265 { |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
266 PyObject *codecs, *ret, *encoder; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
267 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
268 codecs = _PyCodec_Lookup(encoding); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
269 if (codecs == NULL) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
270 goto onError; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
271 encoder = PyObject_GetAttrString(codecs, "incrementalencoder"); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
272 if (encoder == NULL) { |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
273 Py_DECREF(codecs); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
274 return NULL; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
275 } |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
276 if (errors) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
277 ret = PyObject_CallFunction(encoder, "O", errors); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
278 else |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
279 ret = PyObject_CallFunction(encoder, NULL); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
280 Py_DECREF(encoder); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
281 Py_DECREF(codecs); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
282 return ret; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
283 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
284 onError: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
285 return NULL; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
286 } |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
287 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
288 PyObject *PyCodec_IncrementalDecoder(const char *encoding, |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
289 const char *errors) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
290 { |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
291 PyObject *codecs, *ret, *decoder; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
292 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
293 codecs = _PyCodec_Lookup(encoding); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
294 if (codecs == NULL) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
295 goto onError; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
296 decoder = PyObject_GetAttrString(codecs, "incrementaldecoder"); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
297 if (decoder == NULL) { |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
298 Py_DECREF(codecs); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
299 return NULL; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
300 } |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
301 if (errors) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
302 ret = PyObject_CallFunction(decoder, "O", errors); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
303 else |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
304 ret = PyObject_CallFunction(decoder, NULL); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
305 Py_DECREF(decoder); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
306 Py_DECREF(codecs); |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
307 return ret; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
308 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
309 onError: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
310 return NULL; |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
311 } |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
312 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 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
|
314 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 { |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
317 PyObject *codecs, *ret; |
|
11972
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 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 goto onError; |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
322 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
|
323 Py_DECREF(codecs); |
|
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
324 return ret; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 return NULL; |
|
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 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 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
|
331 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 { |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
334 PyObject *codecs, *ret; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 goto onError; |
|
24307
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
339 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
|
340 Py_DECREF(codecs); |
|
416654e27d88
Fix bug [ 549731 ] Unicode encoders appears to leak references.
Mark Hammond <mhammond@skippinet.com.au>
parents:
14869
diff
changeset
|
341 return ret; |
|
11972
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 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 } |
|
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 /* 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
|
348 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
|
349 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 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
|
351 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 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
|
353 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 PyObject *encoder = NULL; |
|
35939
79374eb6db51
Fix a few more ref leaks. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents:
32533
diff
changeset
|
357 PyObject *args = NULL, *result = NULL; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 encoder = PyCodec_Encoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 if (encoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 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
|
365 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 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
|
369 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 if (!PyTuple_Check(result) || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 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
|
374 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 "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
|
376 goto onError; |
|
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 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
|
379 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 /* 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
|
381 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 Py_DECREF(encoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 onError: |
|
35939
79374eb6db51
Fix a few more ref leaks. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents:
32533
diff
changeset
|
388 Py_XDECREF(result); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 Py_XDECREF(encoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 /* 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
|
395 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
|
396 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 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
|
398 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 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
|
400 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 PyObject *decoder = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
404 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
|
405 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 decoder = PyCodec_Decoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 if (decoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 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
|
412 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 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
|
416 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 if (!PyTuple_Check(result) || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 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
|
420 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 "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
|
422 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 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
|
425 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 /* 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
|
427 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 Py_DECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 Py_XDECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 Py_XDECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
440 /* 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
|
441 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
|
442 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
|
443 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
|
444 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
|
445 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
|
446 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
|
447 { |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
448 PyInterpreterState *interp = PyThreadState_GET()->interp; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
449 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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
455 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
|
456 (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
|
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 /* 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
|
460 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
|
461 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
|
462 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
|
463 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
464 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
|
465 |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
466 PyInterpreterState *interp = PyThreadState_GET()->interp; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
467 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
|
468 return NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
469 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
470 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
|
471 name = "strict"; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
472 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
|
473 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
|
474 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
|
475 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
476 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
|
477 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
|
478 } |
|
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 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
|
481 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 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
|
488 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
|
489 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
|
490 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
|
491 "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
|
492 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
|
493 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
|
494 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
495 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
496 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
497 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
498 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
499 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
|
500 { |
|
36751
8e04de2480e4
PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents:
36712
diff
changeset
|
501 if (PyExceptionInstance_Check(exc)) |
|
8e04de2480e4
PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents:
36712
diff
changeset
|
502 PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
503 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
504 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
|
505 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
|
506 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
507 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
508 |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
509 #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
|
510 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
|
511 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
512 Py_ssize_t end; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
513 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
|
514 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
|
515 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
|
516 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
517 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
|
518 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
|
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 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
521 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
|
522 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
|
523 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
|
524 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
525 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
526 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
|
527 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
|
528 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
529 /* ouch: passing NULL, 0, pos gives None instead of u'' */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
530 return Py_BuildValue("(u#n)", &end, 0, end); |
|
25091
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
533 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
534 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
|
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 PyObject *restuple; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
537 Py_ssize_t start; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
538 Py_ssize_t end; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
539 Py_ssize_t i; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
540 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
541 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
|
542 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
|
543 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 *p = '?'; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
554 restuple = Py_BuildValue("(On)", res, end); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
555 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
|
556 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
|
557 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
558 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
|
559 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
|
560 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
|
561 return NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
562 return Py_BuildValue("(u#n)", &res, 1, end); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
563 } |
|
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 (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
|
565 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
|
566 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
|
567 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
|
568 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
|
569 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
|
570 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
|
571 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 *p = Py_UNICODE_REPLACEMENT_CHARACTER; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
577 restuple = Py_BuildValue("(On)", res, end); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
578 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
|
579 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
|
580 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
581 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
582 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
|
583 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
|
584 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
585 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
586 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
587 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
|
588 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
589 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
|
590 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
|
591 PyObject *object; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
592 Py_ssize_t start; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
593 Py_ssize_t end; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
594 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
|
595 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
|
596 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
|
597 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
|
598 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
|
599 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
|
600 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
|
601 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 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
|
608 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
|
609 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
|
610 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
|
611 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
|
612 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
|
613 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
|
614 ressize += 2+4+1; |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
615 #ifndef Py_UNICODE_WIDE |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
616 else |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
617 ressize += 2+5+1; |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
618 #else |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
619 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
|
620 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
|
621 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
|
622 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
|
623 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
624 ressize += 2+7+1; |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
625 #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
|
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 /* 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
|
628 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
|
629 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
|
630 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
|
631 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
|
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 /* 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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 *outp++ = '&'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
640 *outp++ = '#'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
641 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
|
642 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
|
643 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
|
644 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
645 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
|
646 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
|
647 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
|
648 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
649 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
|
650 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
|
651 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
|
652 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
653 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
|
654 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
|
655 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
|
656 } |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
657 #ifndef Py_UNICODE_WIDE |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
658 else { |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
659 digits = 5; |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
660 base = 10000; |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
661 } |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
662 #else |
|
25091
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 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
|
664 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
|
665 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
|
666 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
667 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
|
668 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
|
669 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
|
670 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
671 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
672 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
|
673 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
|
674 } |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
675 #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
|
676 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
|
677 *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
|
678 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
|
679 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
|
680 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
681 *outp++ = ';'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
682 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
683 restuple = Py_BuildValue("(On)", res, end); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
684 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
|
685 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
|
686 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
|
687 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
688 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
689 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
|
690 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
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
694 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
|
695 '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
|
696 '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
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
699 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
|
700 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
701 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
|
702 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
|
703 PyObject *object; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
704 Py_ssize_t start; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
705 Py_ssize_t end; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
706 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
|
707 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
|
708 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
|
709 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
|
710 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
|
711 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
|
712 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
|
713 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
|
714 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
|
715 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
|
716 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
|
717 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
|
718 for (p = startp+start, ressize = 0; p < startp+end; ++p) { |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
719 #ifdef Py_UNICODE_WIDE |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
720 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
|
721 ressize += 1+1+8; |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
722 else |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
723 #endif |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
724 if (*p >= 0x100) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
725 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
|
726 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
727 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
728 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
|
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 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
|
731 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
|
732 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
|
733 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
|
734 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
|
735 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
|
736 *outp++ = '\\'; |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
737 #ifdef Py_UNICODE_WIDE |
|
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 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
|
739 *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
|
740 *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
|
741 *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
|
742 *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
|
743 *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
|
744 *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
|
745 *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
|
746 } |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
747 else |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
748 #endif |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
749 if (c >= 0x100) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
750 *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
|
751 *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
|
752 *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
|
753 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
754 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
755 *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
|
756 *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
|
757 *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
|
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 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
760 restuple = Py_BuildValue("(On)", res, end); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
761 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
|
762 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
|
763 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
|
764 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
765 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
766 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
|
767 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
|
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 } |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
770 #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
|
771 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
772 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
|
773 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
774 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
|
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 |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
778 #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
|
779 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
|
780 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
781 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
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
784 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
785 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
|
786 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
787 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
|
788 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
789 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
790 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
791 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
|
792 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
793 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
|
794 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
795 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
796 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
797 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
|
798 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
799 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
|
800 } |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
801 #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
|
802 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
803 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
|
804 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
805 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
|
806 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
|
807 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
|
808 } methods[] = |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
809 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
810 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
811 "strict", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
812 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
813 "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
|
814 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
|
815 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
|
816 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
817 }, |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
818 #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
|
819 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
820 "ignore", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
821 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
822 "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
|
823 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
|
824 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
|
825 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
826 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
827 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
828 "replace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
829 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
830 "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
|
831 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
|
832 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
|
833 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
834 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
835 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
836 "xmlcharrefreplace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
837 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
838 "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
|
839 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
|
840 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
|
841 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
842 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
843 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
844 "backslashreplace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
845 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
846 "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
|
847 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
|
848 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
|
849 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
850 } |
|
26318
910fa739f4a4
Fix PEP 293 related problems with --disable-unicode builds
Walter Dörwald <walter@livinglogic.de>
parents:
25441
diff
changeset
|
851 #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
|
852 }; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
853 |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
854 PyInterpreterState *interp = PyThreadState_GET()->interp; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
855 PyObject *mod; |
|
32533
201986d6d9d6
Fix a couple of signed/unsigned comparison warnings
Neal Norwitz <nnorwitz@gmail.com>
parents:
31797
diff
changeset
|
856 unsigned i; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
857 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
858 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
|
859 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
|
860 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
861 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
|
862 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
|
863 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
|
864 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
865 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
|
866 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
|
867 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
|
868 int res; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
869 if (!func) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
870 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
|
871 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
|
872 Py_DECREF(func); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
873 if (res) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
874 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
|
875 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
876 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
877 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
878 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
|
879 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
|
880 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
|
881 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
|
882 |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
883 mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0); |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
884 if (mod == NULL) { |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
885 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
|
886 /* 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
|
887 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
|
888 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
|
889 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
|
890 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
|
891 PyErr_Clear(); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
892 return 0; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
893 } |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
894 return -1; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
895 } |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
896 Py_DECREF(mod); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
897 return 0; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
898 } |
