Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5590)

Side by Side Diff: Python/sysmodule.c

Issue 9198: Should repr() print unicode characters outside the BMP?
Patch Set: Created 8 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* System module */ 2 /* System module */
3 3
4 /* 4 /*
5 Various bits of information used by the interpreter are collected in 5 Various bits of information used by the interpreter are collected in
6 module 'sys'. 6 module 'sys'.
7 Function member: 7 Function member:
8 - exit(sts): raise SystemExit 8 - exit(sts): raise SystemExit
9 Data members: 9 Data members:
10 - stdin, stdout, stderr: standard file objects 10 - stdin, stdout, stderr: standard file objects
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 return PyDict_SetItemString(sd, name, v); 67 return PyDict_SetItemString(sd, name, v);
68 } 68 }
69 69
70 static PyObject * 70 static PyObject *
71 sys_displayhook(PyObject *self, PyObject *o) 71 sys_displayhook(PyObject *self, PyObject *o)
72 { 72 {
73 PyObject *outf; 73 PyObject *outf;
74 PyInterpreterState *interp = PyThreadState_GET()->interp; 74 PyInterpreterState *interp = PyThreadState_GET()->interp;
75 PyObject *modules = interp->modules; 75 PyObject *modules = interp->modules;
76 PyObject *builtins = PyDict_GetItemString(modules, "builtins"); 76 PyObject *builtins = PyDict_GetItemString(modules, "builtins");
77 PyObject *encoded, *escaped_str, *repr_str, *stdout_encoding, *args;
78 char *stdout_encoding_str;
79 PyObject *buffer, *writer, *result;
77 80
78 if (builtins == NULL) { 81 if (builtins == NULL) {
79 PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); 82 PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
80 return NULL; 83 return NULL;
81 } 84 }
82 85
83 /* Print value except if None */ 86 /* Print value except if None */
84 /* After printing, also assign to '_' */ 87 /* After printing, also assign to '_' */
85 /* Before, set '_' to None to avoid recursion */ 88 /* Before, set '_' to None to avoid recursion */
86 if (o == Py_None) { 89 if (o == Py_None) {
87 Py_INCREF(Py_None); 90 Py_INCREF(Py_None);
88 return Py_None; 91 return Py_None;
89 } 92 }
90 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0) 93 if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
91 return NULL; 94 return NULL;
92 outf = PySys_GetObject("stdout"); 95 outf = PySys_GetObject("stdout");
93 if (outf == NULL || outf == Py_None) { 96 if (outf == NULL || outf == Py_None) {
94 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); 97 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
95 return NULL; 98 return NULL;
96 } 99 }
97 if (PyFile_WriteObject(o, outf, 0) != 0) 100 if (PyFile_WriteObject(o, outf, 0) != 0) {
98 return NULL; 101 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
102 /* if the encoding used by sys.stdout is not able to encode
103 the string, a UnicodeEncodeError will be raised.
104 In this case encode the string using 'backslashreplace',
105 check for sys.stdout.buffer and try to write it there,
106 or decode it again and send it to PyFile_WriteObject(). */
107 stdout_encoding = PyObject_GetAttrString(outf, "encoding");
108 if (stdout_encoding == NULL)
109 return NULL; /* with no encoding just reraise the error */
110 stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
111 if (stdout_encoding_str == NULL)
112 return NULL;
113 PyErr_Clear();
114 repr_str = PyObject_Repr(o);
115 if (repr_str == NULL) {
116 Py_DECREF(stdout_encoding);
117 return NULL;
118 }
119 encoded = PyUnicode_AsEncodedString(repr_str,
120 stdout_encoding_str,
121 "backslashreplace");
122 Py_DECREF(repr_str);
123 if (encoded == NULL)
124 return NULL;
125 buffer = PyObject_GetAttrString(outf, "buffer");
126 if (buffer) {
127 Py_DECREF(stdout_encoding);
128 writer = PyObject_GetAttrString(buffer, "write");
129 Py_DECREF(buffer);
130 if (writer == NULL) {
131 Py_DECREF(encoded);
132 return NULL;
133 }
134 args = PyTuple_Pack(1, encoded);
135 if (args == NULL) {
136 Py_DECREF(encoded);
137 Py_DECREF(writer);
138 return NULL;
139 }
140 result = PyEval_CallObject(writer, args);
141 Py_DECREF(args);
142 Py_DECREF(encoded);
143 Py_DECREF(writer);
144 if (result == NULL)
145 return NULL;
146 Py_DECREF(result);
147 }
148 else {
149 escaped_str = PyUnicode_FromEncodedObject(encoded,
150 stdout_encoding_str,
151 "backslashreplace");
152 Py_DECREF(encoded);
153 Py_DECREF(stdout_encoding);
154 if (PyFile_WriteObject(escaped_str, outf, Py_PRINT_RAW) != 0) {
155 Py_DECREF(escaped_str);
156 return NULL;
157 }
158 Py_DECREF(escaped_str);
159 }
160 }
161 else {
162 return NULL;
163 }
164 }
99 if (PyFile_WriteString("\n", outf) != 0) 165 if (PyFile_WriteString("\n", outf) != 0)
100 return NULL; 166 return NULL;
101 if (PyObject_SetAttrString(builtins, "_", o) != 0) 167 if (PyObject_SetAttrString(builtins, "_", o) != 0)
102 return NULL; 168 return NULL;
103 Py_INCREF(Py_None); 169 Py_INCREF(Py_None);
104 return Py_None; 170 return Py_None;
105 } 171 }
106 172
107 PyDoc_STRVAR(displayhook_doc, 173 PyDoc_STRVAR(displayhook_doc,
108 "displayhook(object) -> None\n" 174 "displayhook(object) -> None\n"
(...skipping 1829 matching lines...) Expand 10 before | Expand all | Expand 10 after
1938 2004
1939 void 2005 void
1940 PySys_WriteStderr(const char *format, ...) 2006 PySys_WriteStderr(const char *format, ...)
1941 { 2007 {
1942 va_list va; 2008 va_list va;
1943 2009
1944 va_start(va, format); 2010 va_start(va, format);
1945 mywrite("stderr", stderr, format, va); 2011 mywrite("stderr", stderr, format, va);
1946 va_end(va); 2012 va_end(va);
1947 } 2013 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+