Mercurial > cpython-lru_cache-weakref
annotate Python/codecs.c @ 57960:ffd800ef46ea
Recorded merge of revisions 77086 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77086 | georg.brandl | 2009-12-28 09:09:32 +0100 (Mo, 28 Dez 2009) | 1 line
#7381: consistency update, and backport avoiding ``None >= 0`` check from py3k.
........
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Mon, 28 Dec 2009 08:10:38 +0000 |
| parents | b085fd0611de |
| children | cab14be0ada1 |
| 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 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
17 codec search function. |
|
11972
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; |
|
43603
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
58 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
59 if (len > PY_SSIZE_T_MAX) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
60 PyErr_SetString(PyExc_OverflowError, "string is too large"); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
61 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
62 } |
|
43603
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
63 |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
64 p = PyMem_Malloc(len + 1); |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
65 if (p == NULL) |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
66 return NULL; |
|
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
|
67 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
|
68 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
|
69 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
|
70 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 else |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
72 ch = tolower(Py_CHARMASK(ch)); |
|
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
|
73 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
|
74 } |
|
43603
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
75 p[i] = '\0'; |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
76 v = PyUnicode_FromString(p); |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
77 if (v == NULL) |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
78 return NULL; |
|
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
79 PyMem_Free(p); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 return v; |
|
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 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 /* 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
|
84 facilities. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 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
|
87 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
|
88 effectively case-insensitive. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
90 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
|
91 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
92 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
|
93 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
|
94 package. |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
95 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
96 */ |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 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
|
99 { |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
100 PyInterpreterState *interp; |
| 12048 | 101 PyObject *result, *args = NULL, *v; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
102 Py_ssize_t i, len; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
104 if (encoding == NULL) { |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
105 PyErr_BadArgument(); |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
106 goto onError; |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
107 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
108 |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
109 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
|
110 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
|
111 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
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
|
113 /* 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
|
114 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
|
115 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
|
116 v = normalizestring(encoding); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 goto onError; |
|
43603
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
119 PyUnicode_InternInPlace(&v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 /* 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
|
122 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
|
123 if (result != NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 Py_INCREF(result); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
125 Py_DECREF(v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 } |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
128 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 /* 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
|
130 args = PyTuple_New(1); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 PyTuple_SET_ITEM(args,0,v); |
| 12048 | 134 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
135 len = PyList_Size(interp->codec_search_path); |
| 12048 | 136 if (len < 0) |
| 137 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
|
138 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
|
139 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
|
140 "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
|
141 "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
|
142 goto onError; |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
143 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 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
|
146 PyObject *func; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
147 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
148 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
|
149 if (func == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 goto onError; |
| 12048 | 151 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
|
152 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 if (result == Py_None) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 continue; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 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
|
159 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 "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
|
161 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 break; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 if (i == len) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 /* 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
|
168 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
|
169 "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
|
170 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 /* Cache and return the result */ |
| 42534 | 174 if (PyDict_SetItem(interp->codec_search_cache, v, result) < 0) { |
| 175 Py_DECREF(result); | |
| 176 goto onError; | |
| 177 } | |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 return NULL; |
|
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 |
|
47496
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
186 /* Codec registry encoding check API. */ |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
187 |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
188 int PyCodec_KnownEncoding(const char *encoding) |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
189 { |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
190 PyObject *codecs; |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
191 |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
192 codecs = _PyCodec_Lookup(encoding); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
193 if (!codecs) { |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
194 PyErr_Clear(); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
195 return 0; |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
196 } |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
197 else { |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
198 Py_DECREF(codecs); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
199 return 1; |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
200 } |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
201 } |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
202 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 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
|
205 const char *errors) |
|
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 PyObject *args; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
208 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 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
|
210 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 Py_INCREF(object); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 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
|
214 if (errors) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 PyObject *v; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
216 |
|
43603
dd03aa741f56
This is the uncontroversial half of patch 1263 by Thomas Lee:
Guido van Rossum <guido@python.org>
parents:
42534
diff
changeset
|
217 v = PyUnicode_FromString(errors); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 if (v == NULL) { |
|
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 NULL; |
|
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 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
|
223 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 return args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
227 /* Helper function to get a codec item */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
228 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
229 static |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
230 PyObject *codec_getitem(const char *encoding, int index) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
231 { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
232 PyObject *codecs; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
233 PyObject *v; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
234 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
235 codecs = _PyCodec_Lookup(encoding); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
236 if (codecs == NULL) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
237 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
238 v = PyTuple_GET_ITEM(codecs, index); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
239 Py_DECREF(codecs); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
240 Py_INCREF(v); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
241 return v; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
242 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
243 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
244 /* Helper function to create an incremental codec. */ |
|
11972
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 static |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
247 PyObject *codec_getincrementalcodec(const char *encoding, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
248 const char *errors, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
249 const char *attrname) |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
251 PyObject *codecs, *ret, *inccodec; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
253 codecs = _PyCodec_Lookup(encoding); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
254 if (codecs == NULL) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
255 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
256 inccodec = PyObject_GetAttrString(codecs, attrname); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
257 Py_DECREF(codecs); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
258 if (inccodec == NULL) |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 return NULL; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
260 if (errors) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
261 ret = PyObject_CallFunction(inccodec, "s", errors); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
262 else |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
263 ret = PyObject_CallFunction(inccodec, NULL); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
264 Py_DECREF(inccodec); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
265 return ret; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
266 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
267 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
268 /* Helper function to create a stream codec. */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
269 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
270 static |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
271 PyObject *codec_getstreamcodec(const char *encoding, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
272 PyObject *stream, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
273 const char *errors, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
274 const int index) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
275 { |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
276 PyObject *codecs, *streamcodec, *codeccls; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
277 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
278 codecs = _PyCodec_Lookup(encoding); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
279 if (codecs == NULL) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
280 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
281 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
282 codeccls = PyTuple_GET_ITEM(codecs, index); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
283 if (errors != NULL) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
284 streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
285 else |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
286 streamcodec = PyObject_CallFunction(codeccls, "O", stream); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
287 Py_DECREF(codecs); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
288 return streamcodec; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
291 /* Convenience APIs to query the Codec registry. |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
292 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 All APIs return a codec object with incremented refcount. |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
294 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 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
|
298 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
299 return codec_getitem(encoding, 0); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 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
|
303 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
304 return codec_getitem(encoding, 1); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 |
|
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
|
307 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
|
308 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
|
309 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
310 return codec_getincrementalcodec(encoding, errors, "incrementalencoder"); |
|
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
|
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 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
313 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
|
314 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
|
315 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
316 return codec_getincrementalcodec(encoding, errors, "incrementaldecoder"); |
|
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
|
317 } |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36751
diff
changeset
|
318 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 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
|
320 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
323 return codec_getstreamcodec(encoding, stream, errors, 2); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 } |
|
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 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
|
327 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
330 return codec_getstreamcodec(encoding, stream, errors, 3); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 /* 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
|
334 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
|
335 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 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
|
337 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 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
|
339 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 PyObject *encoder = NULL; |
|
35939
79374eb6db51
Fix a few more ref leaks. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents:
32533
diff
changeset
|
343 PyObject *args = NULL, *result = NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
344 PyObject *v = NULL; |
|
11972
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 encoder = PyCodec_Encoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 if (encoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 goto onError; |
|
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 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
|
351 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 goto onError; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
353 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
354 result = PyEval_CallObject(encoder, args); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
358 if (!PyTuple_Check(result) || |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 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
|
360 PyErr_SetString(PyExc_TypeError, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
361 "encoder must return a tuple (object, integer)"); |
|
11972
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 } |
|
47496
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
364 v = PyTuple_GET_ITEM(result,0); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
365 Py_INCREF(v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 /* 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
|
367 |
|
47496
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
368 Py_DECREF(args); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
369 Py_DECREF(encoder); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
370 Py_DECREF(result); |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
371 return v; |
|
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
372 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 onError: |
|
35939
79374eb6db51
Fix a few more ref leaks. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents:
32533
diff
changeset
|
374 Py_XDECREF(result); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 Py_XDECREF(encoder); |
|
47496
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
377 return NULL; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 /* 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
|
381 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
|
382 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 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
|
384 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 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
|
386 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 PyObject *decoder = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 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
|
391 PyObject *v; |
|
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 decoder = PyCodec_Decoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 if (decoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 goto onError; |
|
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 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
|
398 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 goto onError; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
400 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
401 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
|
402 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 goto onError; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
404 if (!PyTuple_Check(result) || |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 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
|
406 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 "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
|
408 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 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
|
411 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 /* 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
|
413 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 Py_DECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 return v; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43603
diff
changeset
|
418 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 Py_XDECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 Py_XDECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
426 /* 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
|
427 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
|
428 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
|
429 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
|
430 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
|
431 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
|
432 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
|
433 { |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
434 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
|
435 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
|
436 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
|
437 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
|
438 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
|
439 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
|
440 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
441 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
|
442 (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
|
443 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
444 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
445 /* 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
|
446 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
|
447 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
|
448 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
|
449 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
450 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
|
451 |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
452 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
|
453 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
|
454 return NULL; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
455 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
456 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
|
457 name = "strict"; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
458 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
|
459 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
|
460 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
|
461 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
462 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
|
463 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
|
464 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
465 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
466 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
|
467 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
468 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
|
469 if (type != NULL) { |
|
41878
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
470 PyObject *name = PyObject_GetAttrString(type, "__name__"); |
|
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
471 Py_DECREF(type); |
|
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
472 if (name != NULL) { |
|
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
473 PyErr_Format(PyExc_TypeError, |
|
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
474 "don't know how to handle %S in error callback", name); |
|
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
475 Py_DECREF(name); |
|
089f449a20cd
Change PyErr_Format() to generate a unicode string (by using
Walter Dörwald <walter@livinglogic.de>
parents:
41587
diff
changeset
|
476 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
477 } |
|
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 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
|
481 { |
|
36751
8e04de2480e4
PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents:
36712
diff
changeset
|
482 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
|
483 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
|
484 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
485 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
|
486 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
|
487 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
488 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
489 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
490 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
|
491 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
492 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
|
493 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
|
494 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
|
495 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
|
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 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
|
498 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
|
499 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
|
500 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
501 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
|
502 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
|
503 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
|
504 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
505 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
506 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
|
507 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
|
508 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
509 /* 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
|
510 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
|
511 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
512 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
513 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
514 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
|
515 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
516 PyObject *restuple; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
517 Py_ssize_t start; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
518 Py_ssize_t end; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
519 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
|
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 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
|
522 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
|
523 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
|
524 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
|
525 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
|
526 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
|
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 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
|
529 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
|
530 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
|
531 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
|
532 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
|
533 *p = '?'; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
534 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
|
535 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
|
536 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
|
537 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
538 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
|
539 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
|
540 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
|
541 return NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
542 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
|
543 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
544 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
|
545 PyObject *res; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
546 Py_UNICODE *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
547 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
|
548 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
|
549 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
|
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 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
|
552 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
|
553 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
554 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
|
555 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
|
556 *p = Py_UNICODE_REPLACEMENT_CHARACTER; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
557 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
|
558 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
|
559 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
|
560 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
561 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
562 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
|
563 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
|
564 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
565 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
566 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
567 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
|
568 { |
|
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 (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
|
570 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
|
571 PyObject *object; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
572 Py_ssize_t start; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
573 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
|
574 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
|
575 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
|
576 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
|
577 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
|
578 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
|
579 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
|
580 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
|
581 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
|
582 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
|
583 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
|
584 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
|
585 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
|
586 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
|
587 if (*p<10) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
588 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
|
589 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
|
590 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
|
591 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
|
592 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
|
593 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
|
594 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
|
595 #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
|
596 else |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
597 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
|
598 #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
|
599 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
|
600 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
|
601 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
|
602 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
|
603 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
604 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
|
605 #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
|
606 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
607 /* 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
|
608 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
|
609 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
|
610 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
|
611 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
|
612 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
613 /* 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
|
614 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
|
615 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
|
616 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
|
617 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
|
618 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
|
619 *outp++ = '&'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
620 *outp++ = '#'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
621 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
|
622 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
|
623 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
|
624 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
625 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
|
626 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
|
627 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
|
628 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
629 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
|
630 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
|
631 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
|
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 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
|
634 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
|
635 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
|
636 } |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
637 #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
|
638 else { |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
639 digits = 5; |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
640 base = 10000; |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
641 } |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
642 #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
|
643 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
|
644 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
|
645 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
|
646 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
647 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
|
648 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
|
649 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
|
650 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
651 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
652 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
|
653 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
|
654 } |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
655 #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
|
656 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
|
657 *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
|
658 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
|
659 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
|
660 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
661 *outp++ = ';'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
662 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
663 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
|
664 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
|
665 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
|
666 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
|
667 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
668 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
669 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
|
670 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
|
671 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
672 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
673 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
674 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
|
675 '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
|
676 '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
|
677 }; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
678 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
679 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
|
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 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
|
682 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
|
683 PyObject *object; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
684 Py_ssize_t start; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
685 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
|
686 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
|
687 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
|
688 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
|
689 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
|
690 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
|
691 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
|
692 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
|
693 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
|
694 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
|
695 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
|
696 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
|
697 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
|
698 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
|
699 #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
|
700 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
|
701 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
|
702 else |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
703 #endif |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
704 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
|
705 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
|
706 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
707 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
708 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
|
709 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
710 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
|
711 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
|
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 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
|
714 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
|
715 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
|
716 *outp++ = '\\'; |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
717 #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
|
718 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
|
719 *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
|
720 *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
|
721 *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
|
722 *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
|
723 *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
|
724 *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
|
725 *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
|
726 } |
|
31273
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
727 else |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
728 #endif |
|
25519d6c97b8
Fix gcc 3.3 warnings related to Py_UNICODE_WIDE.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
28325
diff
changeset
|
729 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
|
730 *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
|
731 *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
|
732 *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
|
733 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
734 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
735 *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
|
736 *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
|
737 *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
|
738 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
739 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35939
diff
changeset
|
740 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
|
741 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
|
742 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
|
743 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
|
744 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
745 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
746 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
|
747 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
|
748 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
749 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
750 |
|
53687
52a89d723b6b
Make PyCodec_SurrogateErrors static.
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
751 /* This handler is declared static until someone demonstrates |
|
52a89d723b6b
Make PyCodec_SurrogateErrors static.
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
752 a need to call it directly. */ |
|
52a89d723b6b
Make PyCodec_SurrogateErrors static.
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
753 static PyObject * |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53767
diff
changeset
|
754 PyCodec_SurrogatePassErrors(PyObject *exc) |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
755 { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
756 PyObject *restuple; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
757 PyObject *object; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
758 Py_ssize_t start; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
759 Py_ssize_t end; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
760 PyObject *res; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
761 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
762 Py_UNICODE *p; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
763 Py_UNICODE *startp; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
764 char *outp; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
765 if (PyUnicodeEncodeError_GetStart(exc, &start)) |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
766 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
767 if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
768 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
769 if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
770 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
771 startp = PyUnicode_AS_UNICODE(object); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
772 res = PyBytes_FromStringAndSize(NULL, 3*(end-start)); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
773 if (!res) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
774 Py_DECREF(object); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
775 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
776 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
777 outp = PyBytes_AsString(res); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
778 for (p = startp+start; p < startp+end; p++) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
779 Py_UNICODE ch = *p; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
780 if (ch < 0xd800 || ch > 0xdfff) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
781 /* Not a surrogate, fail with original exception */ |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
782 PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
783 Py_DECREF(res); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
784 Py_DECREF(object); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
785 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
786 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
787 *outp++ = (char)(0xe0 | (ch >> 12)); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
788 *outp++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
789 *outp++ = (char)(0x80 | (ch & 0x3f)); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
790 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
791 restuple = Py_BuildValue("(On)", res, end); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
792 Py_DECREF(res); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
793 Py_DECREF(object); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
794 return restuple; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
795 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
796 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
797 unsigned char *p; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
798 Py_UNICODE ch = 0; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
799 if (PyUnicodeDecodeError_GetStart(exc, &start)) |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
800 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
801 if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
802 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
803 if (!(p = (unsigned char*)PyBytes_AsString(object))) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
804 Py_DECREF(object); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
805 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
806 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
807 /* Try decoding a single surrogate character. If |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
808 there are more, let the codec call us again. */ |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
809 p += start; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
810 if ((p[0] & 0xf0) == 0xe0 || |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
811 (p[1] & 0xc0) == 0x80 || |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
812 (p[2] & 0xc0) == 0x80) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
813 /* it's a three-byte code */ |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
814 ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
815 if (ch < 0xd800 || ch > 0xdfff) |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
816 /* it's not a surrogate - fail */ |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
817 ch = 0; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
818 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
819 Py_DECREF(object); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
820 if (ch == 0) { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
821 PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
822 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
823 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
824 return Py_BuildValue("(u#n)", &ch, 1, start+3); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
825 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
826 else { |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
827 wrong_exception_type(exc); |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
828 return NULL; |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
829 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
830 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
831 |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
832 static PyObject * |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
833 PyCodec_SurrogateEscapeErrors(PyObject *exc) |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
834 { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
835 PyObject *restuple; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
836 PyObject *object; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
837 Py_ssize_t start; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
838 Py_ssize_t end; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
839 PyObject *res; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
840 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
841 Py_UNICODE *p; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
842 Py_UNICODE *startp; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
843 char *outp; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
844 if (PyUnicodeEncodeError_GetStart(exc, &start)) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
845 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
846 if (PyUnicodeEncodeError_GetEnd(exc, &end)) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
847 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
848 if (!(object = PyUnicodeEncodeError_GetObject(exc))) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
849 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
850 startp = PyUnicode_AS_UNICODE(object); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
851 res = PyBytes_FromStringAndSize(NULL, end-start); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
852 if (!res) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
853 Py_DECREF(object); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
854 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
855 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
856 outp = PyBytes_AsString(res); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
857 for (p = startp+start; p < startp+end; p++) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
858 Py_UNICODE ch = *p; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
859 if (ch < 0xdc80 || ch > 0xdcff) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
860 /* Not a UTF-8b surrogate, fail with original exception */ |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
861 PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
862 Py_DECREF(res); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
863 Py_DECREF(object); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
864 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
865 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
866 *outp++ = ch - 0xdc00; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
867 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
868 restuple = Py_BuildValue("(On)", res, end); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
869 Py_DECREF(res); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
870 Py_DECREF(object); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
871 return restuple; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
872 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
873 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
874 unsigned char *p; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
875 Py_UNICODE ch[4]; /* decode up to 4 bad bytes. */ |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
876 int consumed = 0; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
877 if (PyUnicodeDecodeError_GetStart(exc, &start)) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
878 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
879 if (PyUnicodeDecodeError_GetEnd(exc, &end)) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
880 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
881 if (!(object = PyUnicodeDecodeError_GetObject(exc))) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
882 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
883 if (!(p = (unsigned char*)PyBytes_AsString(object))) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
884 Py_DECREF(object); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
885 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
886 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
887 while (consumed < 4 && consumed < end-start) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
888 /* Refuse to escape ASCII bytes. */ |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
889 if (p[start+consumed] < 128) |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
890 break; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
891 ch[consumed] = 0xdc00 + p[start+consumed]; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
892 consumed++; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
893 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
894 Py_DECREF(object); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
895 if (!consumed) { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
896 /* codec complained about ASCII byte. */ |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
897 PyErr_SetObject(PyExceptionInstance_Class(exc), exc); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
898 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
899 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
900 return Py_BuildValue("(u#n)", ch, consumed, start+consumed); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
901 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
902 else { |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
903 wrong_exception_type(exc); |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
904 return NULL; |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
905 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
906 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
907 |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
908 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
909 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
|
910 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
911 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
|
912 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
913 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
914 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
915 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
|
916 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
917 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
|
918 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
919 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
920 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
921 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
|
922 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
923 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
|
924 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
925 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
926 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
927 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
|
928 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
929 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
|
930 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
931 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
932 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
933 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
|
934 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
935 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
|
936 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
937 |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53767
diff
changeset
|
938 static PyObject *surrogatepass_errors(PyObject *self, PyObject *exc) |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
939 { |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53767
diff
changeset
|
940 return PyCodec_SurrogatePassErrors(exc); |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
941 } |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
942 |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
943 static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc) |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
944 { |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
945 return PyCodec_SurrogateEscapeErrors(exc); |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
946 } |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
947 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
948 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
|
949 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
950 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
|
951 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
|
952 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
|
953 } methods[] = |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
954 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
955 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
956 "strict", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
957 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
958 "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
|
959 strict_errors, |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
960 METH_O, |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
961 PyDoc_STR("Implements the 'strict' error handling, which " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
962 "raises a UnicodeError on coding errors.") |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
963 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
964 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
965 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
966 "ignore", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
967 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
968 "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
|
969 ignore_errors, |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
970 METH_O, |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
971 PyDoc_STR("Implements the 'ignore' error handling, which " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
972 "ignores malformed data and continues.") |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
973 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
974 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
975 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
976 "replace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
977 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
978 "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
|
979 replace_errors, |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
980 METH_O, |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
981 PyDoc_STR("Implements the 'replace' error handling, which " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
982 "replaces malformed data with a replacement marker.") |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
983 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
984 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
985 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
986 "xmlcharrefreplace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
987 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
988 "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
|
989 xmlcharrefreplace_errors, |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
990 METH_O, |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
991 PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
992 "which replaces an unencodable character with the " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
993 "appropriate XML character reference.") |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
994 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
995 }, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
996 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
997 "backslashreplace", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
998 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
999 "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
|
1000 backslashreplace_errors, |
|
56894
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
1001 METH_O, |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
1002 PyDoc_STR("Implements the 'backslashreplace' error handling, " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
1003 "which replaces an unencodable character with a " |
|
b085fd0611de
Merged revisions 75797 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53966
diff
changeset
|
1004 "backslashed escape sequence.") |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
1005 } |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
1006 }, |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
1007 { |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53767
diff
changeset
|
1008 "surrogatepass", |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
1009 { |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53767
diff
changeset
|
1010 "surrogatepass", |
|
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53767
diff
changeset
|
1011 surrogatepass_errors, |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
1012 METH_O |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
49613
diff
changeset
|
1013 } |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
1014 }, |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
1015 { |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1016 "surrogateescape", |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
1017 { |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1018 "surrogateescape", |
|
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1019 surrogateescape_errors, |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
1020 METH_O |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53687
diff
changeset
|
1021 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
1022 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
1023 }; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1024 |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31273
diff
changeset
|
1025 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
|
1026 PyObject *mod; |
|
32533
201986d6d9d6
Fix a couple of signed/unsigned comparison warnings
Neal Norwitz <nnorwitz@gmail.com>
parents:
31797
diff
changeset
|
1027 unsigned i; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1028 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1029 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
|
1030 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
|
1031 |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1032 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
|
1033 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
|
1034 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
|
1035 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1036 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
|
1037 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
|
1038 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
|
1039 int res; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1040 if (!func) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1041 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
|
1042 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
|
1043 Py_DECREF(func); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1044 if (res) |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1045 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
|
1046 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
24307
diff
changeset
|
1047 } |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1048 |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1049 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
|
1050 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
|
1051 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
|
1052 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
|
1053 |
|
44442
fa2f7183b1af
More PyImport_ImportModule -> PyImport_ImportModuleNoBlock
Christian Heimes <christian@cheimes.de>
parents:
44351
diff
changeset
|
1054 mod = PyImport_ImportModuleNoBlock("encodings"); |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1055 if (mod == NULL) { |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1056 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
|
1057 /* 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
|
1058 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
|
1059 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
|
1060 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
|
1061 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
|
1062 PyErr_Clear(); |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1063 return 0; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1064 } |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1065 return -1; |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1066 } |
|
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1067 Py_DECREF(mod); |
|
49613
efef5fc45a9a
Issue 3723: Fixed initialization of subinterpreters
Christian Heimes <christian@cheimes.de>
parents:
47496
diff
changeset
|
1068 interp->codecs_initialized = 1; |
|
28325
850626d47574
Fixed SF bug #663074. The codec system was using global static
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
27898
diff
changeset
|
1069 return 0; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1070 } |
