Mercurial > cpython-lru_cache-weakref
annotate Python/codecs.c @ 21100:0feac45ee662 legacy-trunk
SF bug 486278 SystemError: Python/getargs.c:1086: bad.
vgetargskeywords(): Now that this routine is checking for bad input
(rather than dump core in some cases), some bad calls are raising errors
that previously "worked". This patch makes the error strings more
revealing, and changes the exceptions from SystemError to RuntimeError
(under the theory that SystemError is more of a "can't happen!" assert-
like thing, and so inappropriate for bad arguments to a public C API
function).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2001 03:26:37 +0000 |
| parents | 25ddd3dc7a7a |
| children | 898304f6da23 416654e27d88 |
| 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 /* --- Globals ------------------------------------------------------------ */ |
|
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 static PyObject *_PyCodec_SearchPath; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 static PyObject *_PyCodec_SearchCache; |
|
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 /* Flag used for lazy import of the standard encodings package */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 static int import_encodings_called = 0; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 /* --- Codec Registry ----------------------------------------------------- */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 /* Import the standard encodings package which will register the first |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 codec search function. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 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
|
28 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
|
29 |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
30 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
|
31 made. |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 static |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
36 int import_encodings(void) |
|
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 PyObject *mod; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 import_encodings_called = 1; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 mod = PyImport_ImportModule("encodings"); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 if (mod == NULL) { |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
43 if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
44 /* Ignore ImportErrors... this is done so that |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
45 distributions can disable the encodings package. Note |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
46 that other errors are not masked, e.g. SystemErrors |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
47 raised to inform the user of an error in the Python |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
48 configuration are still reported back to the user. */ |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
49 PyErr_Clear(); |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
50 return 0; |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
51 } |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
52 return -1; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 Py_DECREF(mod); |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
55 return 0; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 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
|
59 { |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
60 if (!import_encodings_called) { |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
61 if (import_encodings()) |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
62 goto onError; |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
63 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 if (search_function == NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 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
|
66 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 if (!PyCallable_Check(search_function)) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 "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
|
71 goto onError; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 return PyList_Append(_PyCodec_SearchPath, 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
|
74 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
75 onError: |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
76 return -1; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
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
|
79 /* 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
|
80 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
|
81 |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 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
|
83 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
|
84 { |
|
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
|
85 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
|
86 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
|
87 char *p; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
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
|
90 if (len > INT_MAX) { |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
91 PyErr_SetString(PyExc_OverflowError, "string is too large"); |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
92 return NULL; |
|
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12648
diff
changeset
|
93 } |
|
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
|
94 |
|
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
|
95 v = PyString_FromStringAndSize(NULL, (int)len); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 p = PyString_AS_STRING(v); |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 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
|
103 else |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
104 ch = tolower(ch); |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12195
diff
changeset
|
105 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
|
106 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 /* 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
|
111 facilities. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 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
|
114 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
|
115 effectively case-insensitive. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
117 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
|
118 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
119 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
|
120 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
|
121 package. |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
122 |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
123 */ |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 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
|
126 { |
| 12048 | 127 PyObject *result, *args = NULL, *v; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 int i, len; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
12648
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
130 if (encoding == NULL) { |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
131 PyErr_BadArgument(); |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
132 goto onError; |
|
2a53f5dc0590
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
133 } |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
134 if (_PyCodec_SearchCache == NULL || |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
135 _PyCodec_SearchPath == NULL) { |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
136 PyErr_SetString(PyExc_SystemError, |
|
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
137 "codec module not properly initialized"); |
|
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
138 goto onError; |
|
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
139 } |
|
12195
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
140 if (!import_encodings_called) { |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
141 if (import_encodings()) |
|
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 |
|
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
|
145 /* 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
|
146 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
|
147 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
|
148 v = normalizestring(encoding); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 if (v == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 PyString_InternInPlace(&v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 /* First, try to lookup the name in the registry dictionary */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 result = PyDict_GetItem(_PyCodec_SearchCache, v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 if (result != NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 Py_INCREF(result); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
157 Py_DECREF(v); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 /* 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
|
162 args = PyTuple_New(1); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 PyTuple_SET_ITEM(args,0,v); |
| 12048 | 166 |
| 167 len = PyList_Size(_PyCodec_SearchPath); | |
| 168 if (len < 0) | |
| 169 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
|
170 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
|
171 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
|
172 "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
|
173 "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
|
174 goto onError; |
|
bc861add5d71
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
Guido van Rossum <guido@python.org>
parents:
12048
diff
changeset
|
175 } |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 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
|
178 PyObject *func; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 func = PyList_GetItem(_PyCodec_SearchPath, i); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 if (func == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 goto onError; |
| 12048 | 183 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
|
184 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 if (result == Py_None) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 continue; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 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
|
191 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 "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
|
193 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 break; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 if (i == len) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 /* XXX Perhaps we should cache misses too ? */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 PyErr_SetString(PyExc_LookupError, |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
201 "unknown encoding"); |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 /* Cache and return the result */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 PyDict_SetItem(_PyCodec_SearchCache, v, result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 return result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 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
|
217 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 PyObject *args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 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
|
222 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 Py_INCREF(object); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 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
|
226 if (errors) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 v = PyString_FromString(errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 if (v == NULL) { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 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
|
235 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 return args; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 /* Build a codec by calling factory(stream[,errors]) or just |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 factory(errors) depending on whether the given parameters are |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 non-NULL. */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 static |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 PyObject *build_stream_codec(PyObject *factory, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 PyObject *args, *codec; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 args = args_tuple(stream, errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 codec = PyEval_CallObject(factory, args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 return codec; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 /* Convenience APIs to query the Codec registry. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 All APIs return a codec object with incremented refcount. |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 */ |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 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
|
266 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 v = PyTuple_GET_ITEM(codecs,0); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 PyObject *PyCodec_Decoder(const char *encoding) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 v = PyTuple_GET_ITEM(codecs,1); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 return NULL; |
|
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_StreamReader(const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 const char *errors) |
|
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 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 return build_stream_codec(PyTuple_GET_ITEM(codecs,2),stream,errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 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
|
313 PyObject *stream, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 PyObject *codecs; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 codecs = _PyCodec_Lookup(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 if (codecs == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 return build_stream_codec(PyTuple_GET_ITEM(codecs,3),stream,errors); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 return NULL; |
|
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 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 /* 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
|
328 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
|
329 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 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
|
331 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 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
|
333 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 const char *errors) |
|
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 PyObject *encoder = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 PyObject *args = NULL, *result; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 encoder = PyCodec_Encoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 if (encoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 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
|
345 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 result = PyEval_CallObject(encoder,args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 if (!PyTuple_Check(result) || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 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
|
354 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 "encoder must return a tuple (object,integer)"); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 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
|
359 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 /* 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
|
361 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 Py_DECREF(encoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 Py_XDECREF(encoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 /* 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
|
374 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
|
375 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 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
|
377 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 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
|
379 const char *encoding, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 const char *errors) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 PyObject *decoder = NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 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
|
384 PyObject *v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 decoder = PyCodec_Decoder(encoding); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 if (decoder == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 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
|
391 if (args == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 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
|
395 if (result == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 if (!PyTuple_Check(result) || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 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
|
399 PyErr_SetString(PyExc_TypeError, |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 "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
|
401 goto onError; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 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
|
404 Py_INCREF(v); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 /* 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
|
406 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 Py_DECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 Py_DECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 Py_DECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 return v; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 onError: |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 Py_XDECREF(args); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 Py_XDECREF(decoder); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 Py_XDECREF(result); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 return NULL; |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
419 void _PyCodecRegistry_Init(void) |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
420 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 if (_PyCodec_SearchPath == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 _PyCodec_SearchPath = PyList_New(0); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 if (_PyCodec_SearchCache == NULL) |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 _PyCodec_SearchCache = PyDict_New(); |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 if (_PyCodec_SearchPath == NULL || |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 _PyCodec_SearchCache == 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
|
427 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
|
428 } |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
430 void _PyCodecRegistry_Fini(void) |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
431 { |
|
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 Py_XDECREF(_PyCodec_SearchPath); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
433 _PyCodec_SearchPath = NULL; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 Py_XDECREF(_PyCodec_SearchCache); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11972
diff
changeset
|
435 _PyCodec_SearchCache = NULL; |
|
11972
d0e06efb3165
Python Codec Registry and support functions, written by Marc-Andre
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 } |
