comparison Python/pythonrun.c @ 72835:5e33219492bc

Add API for static strings, primarily good for identifiers. Thanks to Konrad Schöbel and Jasper Schulz for helping with the mass-editing.
author Martin v. Löwis <martin@v.loewis.de>
date Sun, 09 Oct 2011 10:38:36 +0200
parents ad0804d3dbd0
children 81380082d216
comparison
equal deleted inserted replaced
72829:a52b5be3676a 72835:5e33219492bc
350 flush_std_files(void) 350 flush_std_files(void)
351 { 351 {
352 PyObject *fout = PySys_GetObject("stdout"); 352 PyObject *fout = PySys_GetObject("stdout");
353 PyObject *ferr = PySys_GetObject("stderr"); 353 PyObject *ferr = PySys_GetObject("stderr");
354 PyObject *tmp; 354 PyObject *tmp;
355 _Py_identifier(flush);
355 356
356 if (fout != NULL && fout != Py_None) { 357 if (fout != NULL && fout != Py_None) {
357 tmp = PyObject_CallMethod(fout, "flush", ""); 358 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
358 if (tmp == NULL) 359 if (tmp == NULL)
359 PyErr_WriteUnraisable(fout); 360 PyErr_WriteUnraisable(fout);
360 else 361 else
361 Py_DECREF(tmp); 362 Py_DECREF(tmp);
362 } 363 }
363 364
364 if (ferr != NULL && ferr != Py_None) { 365 if (ferr != NULL && ferr != Py_None) {
365 tmp = PyObject_CallMethod(ferr, "flush", ""); 366 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
366 if (tmp == NULL) 367 if (tmp == NULL)
367 PyErr_Clear(); 368 PyErr_Clear();
368 else 369 else
369 Py_DECREF(tmp); 370 Py_DECREF(tmp);
370 } 371 }
803 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; 804 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
804 const char* mode; 805 const char* mode;
805 const char* newline; 806 const char* newline;
806 PyObject *line_buffering; 807 PyObject *line_buffering;
807 int buffering, isatty; 808 int buffering, isatty;
809 _Py_identifier(open);
810 _Py_identifier(isatty);
811 _Py_identifier(TextIOWrapper);
808 812
809 /* stdin is always opened in buffered mode, first because it shouldn't 813 /* stdin is always opened in buffered mode, first because it shouldn't
810 make a difference in common use cases, second because TextIOWrapper 814 make a difference in common use cases, second because TextIOWrapper
811 depends on the presence of a read1() method which only exists on 815 depends on the presence of a read1() method which only exists on
812 buffered streams. 816 buffered streams.
817 buffering = -1; 821 buffering = -1;
818 if (write_mode) 822 if (write_mode)
819 mode = "wb"; 823 mode = "wb";
820 else 824 else
821 mode = "rb"; 825 mode = "rb";
822 buf = PyObject_CallMethod(io, "open", "isiOOOi", 826 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
823 fd, mode, buffering, 827 fd, mode, buffering,
824 Py_None, Py_None, Py_None, 0); 828 Py_None, Py_None, Py_None, 0);
825 if (buf == NULL) 829 if (buf == NULL)
826 goto error; 830 goto error;
827 831
828 if (buffering) { 832 if (buffering) {
829 raw = PyObject_GetAttrString(buf, "raw"); 833 raw = PyObject_GetAttrString(buf, "raw");
836 } 840 }
837 841
838 text = PyUnicode_FromString(name); 842 text = PyUnicode_FromString(name);
839 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0) 843 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
840 goto error; 844 goto error;
841 res = PyObject_CallMethod(raw, "isatty", ""); 845 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
842 if (res == NULL) 846 if (res == NULL)
843 goto error; 847 goto error;
844 isatty = PyObject_IsTrue(res); 848 isatty = PyObject_IsTrue(res);
845 Py_DECREF(res); 849 Py_DECREF(res);
846 if (isatty == -1) 850 if (isatty == -1)
859 /* translate \r\n to \n for sys.stdin on Windows */ 863 /* translate \r\n to \n for sys.stdin on Windows */
860 newline = NULL; 864 newline = NULL;
861 } 865 }
862 #endif 866 #endif
863 867
864 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO", 868 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
865 buf, encoding, errors, 869 buf, encoding, errors,
866 newline, line_buffering); 870 newline, line_buffering);
867 Py_CLEAR(buf); 871 Py_CLEAR(buf);
868 if (stream == NULL) 872 if (stream == NULL)
869 goto error; 873 goto error;
870 874
871 if (write_mode) 875 if (write_mode)
1757 static void 1761 static void
1758 flush_io(void) 1762 flush_io(void)
1759 { 1763 {
1760 PyObject *f, *r; 1764 PyObject *f, *r;
1761 PyObject *type, *value, *traceback; 1765 PyObject *type, *value, *traceback;
1766 _Py_identifier(flush);
1762 1767
1763 /* Save the current exception */ 1768 /* Save the current exception */
1764 PyErr_Fetch(&type, &value, &traceback); 1769 PyErr_Fetch(&type, &value, &traceback);
1765 1770
1766 f = PySys_GetObject("stderr"); 1771 f = PySys_GetObject("stderr");
1767 if (f != NULL) { 1772 if (f != NULL) {
1768 r = PyObject_CallMethod(f, "flush", ""); 1773 r = _PyObject_CallMethodId(f, &PyId_flush, "");
1769 if (r) 1774 if (r)
1770 Py_DECREF(r); 1775 Py_DECREF(r);
1771 else 1776 else
1772 PyErr_Clear(); 1777 PyErr_Clear();
1773 } 1778 }
1774 f = PySys_GetObject("stdout"); 1779 f = PySys_GetObject("stdout");
1775 if (f != NULL) { 1780 if (f != NULL) {
1776 r = PyObject_CallMethod(f, "flush", ""); 1781 r = _PyObject_CallMethodId(f, &PyId_flush, "");
1777 if (r) 1782 if (r)
1778 Py_DECREF(r); 1783 Py_DECREF(r);
1779 else 1784 else
1780 PyErr_Clear(); 1785 PyErr_Clear();
1781 } 1786 }
2203 "threading" threads have completed. */ 2208 "threading" threads have completed. */
2204 static void 2209 static void
2205 wait_for_thread_shutdown(void) 2210 wait_for_thread_shutdown(void)
2206 { 2211 {
2207 #ifdef WITH_THREAD 2212 #ifdef WITH_THREAD
2213 _Py_identifier(_shutdown);
2208 PyObject *result; 2214 PyObject *result;
2209 PyThreadState *tstate = PyThreadState_GET(); 2215 PyThreadState *tstate = PyThreadState_GET();
2210 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, 2216 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2211 "threading"); 2217 "threading");
2212 if (threading == NULL) { 2218 if (threading == NULL) {
2213 /* threading not imported */ 2219 /* threading not imported */
2214 PyErr_Clear(); 2220 PyErr_Clear();
2215 return; 2221 return;
2216 } 2222 }
2217 result = PyObject_CallMethod(threading, "_shutdown", ""); 2223 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
2218 if (result == NULL) { 2224 if (result == NULL) {
2219 PyErr_WriteUnraisable(threading); 2225 PyErr_WriteUnraisable(threading);
2220 } 2226 }
2221 else { 2227 else {
2222 Py_DECREF(result); 2228 Py_DECREF(result);