From 8d4acf67131b9a1241bbda784905c5730c382d60 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Feb 2011 12:07:37 +0000 Subject: Issue #11272: Fix input() and sys.stdin for Windows newline On Windows, input() strips '\r' (and not only '\n'), and sys.stdin uses universal newline (replace '\r\n' by '\n'). --- Python/pythonrun.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 33f187386a..8e97d7fc24 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -778,6 +778,7 @@ create_stdio(PyObject* io, { PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; const char* mode; + const char* newline; PyObject *line_buffering; int buffering, isatty; @@ -828,9 +829,17 @@ create_stdio(PyObject* io, Py_CLEAR(raw); Py_CLEAR(text); + newline = "\n"; +#ifdef MS_WINDOWS + if (!write_mode) { + /* translate \r\n to \n for sys.stdin on Windows */ + newline = NULL; + } +#endif + stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO", buf, encoding, errors, - "\n", line_buffering); + newline, line_buffering); Py_CLEAR(buf); if (stream == NULL) goto error; -- cgit v1.2.1 From 8c709c419d16486b76126195864ed9376e8b849b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 20 Mar 2011 23:09:03 +0100 Subject: Fix #11586: typo in initfsencoding() Patch written by Ray Allen. --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 8e97d7fc24..38b2ab84fb 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -147,7 +147,7 @@ get_codec_name(const char *encoding) goto error; name_utf8 = _PyUnicode_AsString(name); - if (name == NULL) + if (name_utf8 == NULL) goto error; name_str = strdup(name_utf8); Py_DECREF(name); -- cgit v1.2.1 From d3c216b4c8eb33b04cff0f81e2233fb2d662df64 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 31 Mar 2011 01:31:06 +0200 Subject: Issue #11393: Add the new faulthandler module --- Python/pythonrun.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 38b2ab84fb..f787a4f8b3 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -70,6 +70,8 @@ extern void _PyUnicode_Init(void); extern void _PyUnicode_Fini(void); extern int _PyLong_Init(void); extern void PyLong_Fini(void); +extern int _PyFaulthandler_Init(void); +extern void _PyFaulthandler_Fini(void); #ifdef WITH_THREAD extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); @@ -286,6 +288,10 @@ Py_InitializeEx(int install_sigs) _PyImportHooks_Init(); + /* initialize the faulthandler module */ + if (_PyFaulthandler_Init()) + Py_FatalError("Py_Initialize: can't initialize faulthandler"); + /* Initialize _warnings. */ _PyWarnings_Init(); @@ -454,6 +460,9 @@ Py_Finalize(void) /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ _PyImport_Fini(); + /* unload faulthandler module */ + _PyFaulthandler_Fini(); + /* Debugging stuff */ #ifdef COUNT_ALLOCS dump_counts(stdout); @@ -2100,11 +2109,23 @@ cleanup: void Py_FatalError(const char *msg) { + const int fd = fileno(stderr); + PyThreadState *tstate; + fprintf(stderr, "Fatal Python error: %s\n", msg); fflush(stderr); /* it helps in Windows debug build */ if (PyErr_Occurred()) { PyErr_PrintEx(0); } + else { + tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current); + if (tstate != NULL) { + fputc('\n', stderr); + fflush(stderr); + _Py_DumpTraceback(fd, tstate); + } + } + #ifdef MS_WINDOWS { size_t len = strlen(msg); -- cgit v1.2.1 From eb92e63267528242069ef100470afec9147700ab Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 1 Apr 2011 12:13:55 +0200 Subject: Issue #11393: The fault handler handles also SIGABRT --- Python/pythonrun.c | 1 + 1 file changed, 1 insertion(+) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f787a4f8b3..1c36e63ac4 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -2124,6 +2124,7 @@ Py_FatalError(const char *msg) fflush(stderr); _Py_DumpTraceback(fd, tstate); } + _PyFaulthandler_Fini(); } #ifdef MS_WINDOWS -- cgit v1.2.1 From 8136044f569744c1f5fbb37a093e2728db859ee0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 5 Apr 2011 00:39:01 +0200 Subject: Issue #10785: Store the filename as Unicode in the Python parser. --- Python/pythonrun.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 1c36e63ac4..a6787c4fc7 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -62,6 +62,7 @@ static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, PyCompilerFlags *); static void err_input(perrdetail *); +static void err_free(perrdetail *); static void initsigs(void); static void call_py_exitfuncs(void); static void wait_for_thread_shutdown(void); @@ -1887,12 +1888,13 @@ PyParser_ASTFromString(const char *s, const char *filename, int start, flags->cf_flags |= iflags & PyCF_MASK; mod = PyAST_FromNode(n, flags, filename, arena); PyNode_Free(n); - return mod; } else { err_input(&err); - return NULL; + mod = NULL; } + err_free(&err); + return mod; } mod_ty @@ -1917,14 +1919,15 @@ PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc, flags->cf_flags |= iflags & PyCF_MASK; mod = PyAST_FromNode(n, flags, filename, arena); PyNode_Free(n); - return mod; } else { err_input(&err); if (errcode) *errcode = err.error; - return NULL; + mod = NULL; } + err_free(&err); + return mod; } /* Simplified interface to parsefile -- return node or set exception */ @@ -1938,6 +1941,7 @@ PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int fla start, NULL, NULL, &err, flags); if (n == NULL) err_input(&err); + err_free(&err); return n; } @@ -1952,6 +1956,7 @@ PyParser_SimpleParseStringFlags(const char *str, int start, int flags) start, &err, flags); if (n == NULL) err_input(&err); + err_free(&err); return n; } @@ -1964,6 +1969,7 @@ PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename, &_PyParser_Grammar, start, &err, flags); if (n == NULL) err_input(&err); + err_free(&err); return n; } @@ -1976,12 +1982,24 @@ PyParser_SimpleParseStringFilename(const char *str, const char *filename, int st /* May want to move a more generalized form of this to parsetok.c or even parser modules. */ +void +PyParser_ClearError(perrdetail *err) +{ + err_free(err); +} + void PyParser_SetError(perrdetail *err) { err_input(err); } +static void +err_free(perrdetail *err) +{ + Py_CLEAR(err->filename); +} + /* Set the error appropriate to the given input error code (see errcode.h) */ static void @@ -1989,7 +2007,6 @@ err_input(perrdetail *err) { PyObject *v, *w, *errtype, *errtext; PyObject *msg_obj = NULL; - PyObject *filename; char *msg = NULL; errtype = PyExc_SyntaxError; @@ -2075,17 +2092,8 @@ err_input(perrdetail *err) errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text), "replace"); } - if (err->filename != NULL) - filename = PyUnicode_DecodeFSDefault(err->filename); - else { - Py_INCREF(Py_None); - filename = Py_None; - } - if (filename != NULL) - v = Py_BuildValue("(NiiN)", filename, - err->lineno, err->offset, errtext); - else - v = NULL; + v = Py_BuildValue("(OiiN)", err->filename, + err->lineno, err->offset, errtext); if (v != NULL) { if (msg_obj) w = Py_BuildValue("(OO)", msg_obj, v); -- cgit v1.2.1 From db32b7ada2bd63927e2ecd2c32427baec6e44fda Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 27 Apr 2011 00:20:27 +0200 Subject: Issue #10914: Py_NewInterpreter() uses PyErr_PrintEx(0) ... instead of PyErr_Print() because we don't need to set sys attributes, the sys module is destroyed just after printing the error. --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a6787c4fc7..99bd66d7ad 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -632,7 +632,7 @@ Py_NewInterpreter(void) handle_error: /* Oops, it didn't work. Undo it all. */ - PyErr_Print(); + PyErr_PrintEx(0); PyThreadState_Clear(tstate); PyThreadState_Swap(save_tstate); PyThreadState_Delete(tstate); -- cgit v1.2.1 From 94efbe0ac4d55d812c49fa29accc75e135526975 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 27 Apr 2011 00:24:21 +0200 Subject: Issue #10914: Initialize correctly the filesystem codec when creating a new subinterpreter to fix a bootstrap issue with codecs implemented in Python, as the ISO-8859-15 codec. Add fscodec_initialized attribute to the PyInterpreterState structure. --- Python/pythonrun.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 99bd66d7ad..ad31613651 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -53,7 +53,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */ /* Forward */ static void initmain(void); -static void initfsencoding(void); +static int initfsencoding(PyInterpreterState *interp); static void initsite(void); static int initstdio(void); static void flush_io(void); @@ -298,7 +298,8 @@ Py_InitializeEx(int install_sigs) _PyTime_Init(); - initfsencoding(); + if (initfsencoding(interp) < 0) + Py_FatalError("Py_Initialize: unable to load the file system codec"); if (install_sigs) initsigs(); /* Signal handling stuff, including initintr() */ @@ -618,6 +619,10 @@ Py_NewInterpreter(void) Py_DECREF(pstderr); _PyImportHooks_Init(); + + if (initfsencoding(interp) < 0) + goto handle_error; + if (initstdio() < 0) Py_FatalError( "Py_Initialize: can't initialize sys standard streams"); @@ -730,8 +735,8 @@ initmain(void) } } -static void -initfsencoding(void) +static int +initfsencoding(PyInterpreterState *interp) { PyObject *codec; #if defined(HAVE_LANGINFO_H) && defined(CODESET) @@ -748,7 +753,8 @@ initfsencoding(void) Py_FileSystemDefaultEncoding = codeset; Py_HasFileSystemDefaultEncoding = 0; - return; + interp->fscodec_initialized = 1; + return 0; } #endif @@ -758,10 +764,11 @@ initfsencoding(void) /* Such error can only occurs in critical situations: no more * memory, import a module of the standard library failed, * etc. */ - Py_FatalError("Py_Initialize: unable to load the file system codec"); - } else { - Py_DECREF(codec); + return -1; } + Py_DECREF(codec); + interp->fscodec_initialized = 1; + return 0; } /* Import the site module (not into __main__ though) */ -- cgit v1.2.1 From ac8f9652916d3c88b8d08aaf32fa6942531cd712 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 7 May 2011 12:43:00 +0200 Subject: faulthandler: dump all threads by default * Set the default value of all_threads arguments to True * Py_FatalError() dumps all threads, instead of only the current thread Dump only the current thread is not reliable. In some cases, Python is unable to retrieve the state of the current thread and so is unable to dump the traceback. faulthandler keeps a reference to the interpreter and so is always able to dump the traceback of all threads. --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ebc4f1cf17..6ebc823b80 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -2144,7 +2144,7 @@ Py_FatalError(const char *msg) if (tstate != NULL) { fputc('\n', stderr); fflush(stderr); - _Py_DumpTraceback(fd, tstate); + _Py_DumpTracebackThreads(fd, tstate->interp, tstate); } _PyFaulthandler_Fini(); } -- cgit v1.2.1 From 48f7ac826a35230300859e8bfba3b7339efab679 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 May 2011 14:25:13 +0200 Subject: print_exception(): handle correctly PyObject_GetAttrString() failure Bug found by the Clang Static Analyzer. --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index b55dc5b201..232d7befa0 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1593,7 +1593,7 @@ print_exception(PyObject *f, PyObject *value) moduleName = PyObject_GetAttrString(type, "__module__"); if (moduleName == NULL || !PyUnicode_Check(moduleName)) { - Py_DECREF(moduleName); + Py_XDECREF(moduleName); err = PyFile_WriteString("", f); } else { -- cgit v1.2.1 From 841e3742a787ea782d53c357b9894deff2ad363d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 4 Jul 2011 13:48:30 +0200 Subject: Issue #9642: Fix filesystem encoding initialization: use the ANSI code page on Windows if the mbcs codec is not available, and fail with a fatal error if we cannot get the locale encoding (if nl_langinfo(CODESET) is not available) instead of using UTF-8. --- Python/pythonrun.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 232d7befa0..5649e86c51 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -168,18 +168,25 @@ error: return NULL; } -#if defined(HAVE_LANGINFO_H) && defined(CODESET) static char* -get_codeset(void) +get_locale_encoding(void) { +#ifdef MS_WINDOWS + char codepage[100]; + PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP()); + return get_codec_name(codepage); +#elif defined(HAVE_LANGINFO_H) && defined(CODESET) char* codeset = nl_langinfo(CODESET); if (!codeset || codeset[0] == '\0') { PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); return NULL; } return get_codec_name(codeset); -} +#else + PyErr_SetNone(PyExc_NotImplementedError); + return NULL; #endif +} void Py_InitializeEx(int install_sigs) @@ -746,24 +753,17 @@ static int initfsencoding(PyInterpreterState *interp) { PyObject *codec; -#if defined(HAVE_LANGINFO_H) && defined(CODESET) - char *codeset = NULL; - - if (Py_FileSystemDefaultEncoding == NULL) { - /* On Unix, set the file system encoding according to the - user's preference, if the CODESET names a well-known - Python codec, and Py_FileSystemDefaultEncoding isn't - initialized by other means. */ - codeset = get_codeset(); - if (codeset == NULL) + + if (Py_FileSystemDefaultEncoding == NULL) + { + Py_FileSystemDefaultEncoding = get_locale_encoding(); + if (Py_FileSystemDefaultEncoding == NULL) Py_FatalError("Py_Initialize: Unable to get the locale encoding"); - Py_FileSystemDefaultEncoding = codeset; Py_HasFileSystemDefaultEncoding = 0; interp->fscodec_initialized = 1; return 0; } -#endif /* the encoding is mbcs, utf-8 or ascii */ codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); -- cgit v1.2.1 From dcacb24f386ae1b107462181af42b6826ec15fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sun, 9 Oct 2011 10:38:36 +0200 Subject: =?UTF-8?q?Add=20API=20for=20static=20strings,=20primarily=20good?= =?UTF-8?q?=20for=20identifiers.=20Thanks=20to=20Konrad=20Sch=C3=B6bel=20a?= =?UTF-8?q?nd=20Jasper=20Schulz=20for=20helping=20with=20the=20mass-editin?= =?UTF-8?q?g.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Python/pythonrun.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 1888fba9b9..0ef36bbb8c 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -352,9 +352,10 @@ flush_std_files(void) PyObject *fout = PySys_GetObject("stdout"); PyObject *ferr = PySys_GetObject("stderr"); PyObject *tmp; + _Py_identifier(flush); if (fout != NULL && fout != Py_None) { - tmp = PyObject_CallMethod(fout, "flush", ""); + tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); if (tmp == NULL) PyErr_WriteUnraisable(fout); else @@ -362,7 +363,7 @@ flush_std_files(void) } if (ferr != NULL && ferr != Py_None) { - tmp = PyObject_CallMethod(ferr, "flush", ""); + tmp = _PyObject_CallMethodId(ferr, &PyId_flush, ""); if (tmp == NULL) PyErr_Clear(); else @@ -805,6 +806,9 @@ create_stdio(PyObject* io, const char* newline; PyObject *line_buffering; int buffering, isatty; + _Py_identifier(open); + _Py_identifier(isatty); + _Py_identifier(TextIOWrapper); /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper @@ -819,9 +823,9 @@ create_stdio(PyObject* io, mode = "wb"; else mode = "rb"; - buf = PyObject_CallMethod(io, "open", "isiOOOi", - fd, mode, buffering, - Py_None, Py_None, Py_None, 0); + buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", + fd, mode, buffering, + Py_None, Py_None, Py_None, 0); if (buf == NULL) goto error; @@ -838,7 +842,7 @@ create_stdio(PyObject* io, text = PyUnicode_FromString(name); if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0) goto error; - res = PyObject_CallMethod(raw, "isatty", ""); + res = _PyObject_CallMethodId(raw, &PyId_isatty, ""); if (res == NULL) goto error; isatty = PyObject_IsTrue(res); @@ -861,9 +865,9 @@ create_stdio(PyObject* io, } #endif - stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO", - buf, encoding, errors, - newline, line_buffering); + stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", + buf, encoding, errors, + newline, line_buffering); Py_CLEAR(buf); if (stream == NULL) goto error; @@ -1759,13 +1763,14 @@ flush_io(void) { PyObject *f, *r; PyObject *type, *value, *traceback; + _Py_identifier(flush); /* Save the current exception */ PyErr_Fetch(&type, &value, &traceback); f = PySys_GetObject("stderr"); if (f != NULL) { - r = PyObject_CallMethod(f, "flush", ""); + r = _PyObject_CallMethodId(f, &PyId_flush, ""); if (r) Py_DECREF(r); else @@ -1773,7 +1778,7 @@ flush_io(void) } f = PySys_GetObject("stdout"); if (f != NULL) { - r = PyObject_CallMethod(f, "flush", ""); + r = _PyObject_CallMethodId(f, &PyId_flush, ""); if (r) Py_DECREF(r); else @@ -2205,6 +2210,7 @@ static void wait_for_thread_shutdown(void) { #ifdef WITH_THREAD + _Py_identifier(_shutdown); PyObject *result; PyThreadState *tstate = PyThreadState_GET(); PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, @@ -2214,7 +2220,7 @@ wait_for_thread_shutdown(void) PyErr_Clear(); return; } - result = PyObject_CallMethod(threading, "_shutdown", ""); + result = _PyObject_CallMethodId(threading, &PyId__shutdown, ""); if (result == NULL) { PyErr_WriteUnraisable(threading); } -- cgit v1.2.1 From c3eb93fe1ce5f5ff2666fb9f34a6fd7c46279554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Mon, 10 Oct 2011 18:11:30 +0200 Subject: Use identifier API for PyObject_GetAttrString. --- Python/pythonrun.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0ef36bbb8c..df0d1b34e1 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -141,12 +141,13 @@ get_codec_name(const char *encoding) { char *name_utf8, *name_str; PyObject *codec, *name = NULL; + _Py_identifier(name); codec = _PyCodec_Lookup(encoding); if (!codec) goto error; - name = PyObject_GetAttrString(codec, "name"); + name = _PyObject_GetAttrId(codec, &PyId_name); Py_CLEAR(codec); if (!name) goto error; @@ -830,7 +831,8 @@ create_stdio(PyObject* io, goto error; if (buffering) { - raw = PyObject_GetAttrString(buf, "raw"); + _Py_identifier(raw); + raw = _PyObject_GetAttrId(buf, &PyId_raw); if (raw == NULL) goto error; } @@ -1115,13 +1117,14 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags PyArena *arena; char *ps1 = "", *ps2 = "", *enc = NULL; int errcode = 0; + _Py_identifier(encoding); if (fp == stdin) { /* Fetch encoding from sys.stdin */ v = PySys_GetObject("stdin"); if (v == NULL || v == Py_None) return -1; - oenc = PyObject_GetAttrString(v, "encoding"); + oenc = _PyObject_GetAttrId(v, &PyId_encoding); if (!oenc) return -1; enc = _PyUnicode_AsString(oenc); @@ -1318,6 +1321,11 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, { long hold; PyObject *v; + _Py_identifier(msg); + _Py_identifier(filename); + _Py_identifier(lineno); + _Py_identifier(offset); + _Py_identifier(text); /* old style errors */ if (PyTuple_Check(err)) @@ -1326,11 +1334,11 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, /* new style errors. `err' is an instance */ - if (! (v = PyObject_GetAttrString(err, "msg"))) + if (! (v = _PyObject_GetAttrId(err, &PyId_msg))) goto finally; *message = v; - if (!(v = PyObject_GetAttrString(err, "filename"))) + if (!(v = _PyObject_GetAttrId(err, &PyId_filename))) goto finally; if (v == Py_None) *filename = NULL; @@ -1338,7 +1346,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, goto finally; Py_DECREF(v); - if (!(v = PyObject_GetAttrString(err, "lineno"))) + if (!(v = _PyObject_GetAttrId(err, &PyId_lineno))) goto finally; hold = PyLong_AsLong(v); Py_DECREF(v); @@ -1347,7 +1355,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, goto finally; *lineno = (int)hold; - if (!(v = PyObject_GetAttrString(err, "offset"))) + if (!(v = _PyObject_GetAttrId(err, &PyId_offset))) goto finally; if (v == Py_None) { *offset = -1; @@ -1362,7 +1370,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, *offset = (int)hold; } - if (!(v = PyObject_GetAttrString(err, "text"))) + if (!(v = _PyObject_GetAttrId(err, &PyId_text))) goto finally; if (v == Py_None) *text = NULL; @@ -1431,7 +1439,8 @@ handle_system_exit(void) goto done; if (PyExceptionInstance_Check(value)) { /* The error code should be in the `code' attribute. */ - PyObject *code = PyObject_GetAttrString(value, "code"); + _Py_identifier(code); + PyObject *code = _PyObject_GetAttrId(value, &PyId_code); if (code) { Py_DECREF(value); value = code; @@ -1588,6 +1597,7 @@ print_exception(PyObject *f, PyObject *value) else { PyObject* moduleName; char* className; + _Py_identifier(__module__); assert(PyExceptionClass_Check(type)); className = PyExceptionClass_Name(type); if (className != NULL) { @@ -1596,7 +1606,7 @@ print_exception(PyObject *f, PyObject *value) className = dot+1; } - moduleName = PyObject_GetAttrString(type, "__module__"); + moduleName = _PyObject_GetAttrId(type, &PyId___module__); if (moduleName == NULL || !PyUnicode_Check(moduleName)) { Py_XDECREF(moduleName); -- cgit v1.2.1 From f5ad3b280b43227eb0e3fa63d89490a58ba9c28b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Fri, 14 Oct 2011 10:20:37 +0200 Subject: Rename _Py_identifier to _Py_IDENTIFIER. --- Python/pythonrun.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index df0d1b34e1..0ce61a5558 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -141,7 +141,7 @@ get_codec_name(const char *encoding) { char *name_utf8, *name_str; PyObject *codec, *name = NULL; - _Py_identifier(name); + _Py_IDENTIFIER(name); codec = _PyCodec_Lookup(encoding); if (!codec) @@ -353,7 +353,7 @@ flush_std_files(void) PyObject *fout = PySys_GetObject("stdout"); PyObject *ferr = PySys_GetObject("stderr"); PyObject *tmp; - _Py_identifier(flush); + _Py_IDENTIFIER(flush); if (fout != NULL && fout != Py_None) { tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); @@ -807,9 +807,9 @@ create_stdio(PyObject* io, const char* newline; PyObject *line_buffering; int buffering, isatty; - _Py_identifier(open); - _Py_identifier(isatty); - _Py_identifier(TextIOWrapper); + _Py_IDENTIFIER(open); + _Py_IDENTIFIER(isatty); + _Py_IDENTIFIER(TextIOWrapper); /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper @@ -831,7 +831,7 @@ create_stdio(PyObject* io, goto error; if (buffering) { - _Py_identifier(raw); + _Py_IDENTIFIER(raw); raw = _PyObject_GetAttrId(buf, &PyId_raw); if (raw == NULL) goto error; @@ -1117,7 +1117,7 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags PyArena *arena; char *ps1 = "", *ps2 = "", *enc = NULL; int errcode = 0; - _Py_identifier(encoding); + _Py_IDENTIFIER(encoding); if (fp == stdin) { /* Fetch encoding from sys.stdin */ @@ -1321,11 +1321,11 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, { long hold; PyObject *v; - _Py_identifier(msg); - _Py_identifier(filename); - _Py_identifier(lineno); - _Py_identifier(offset); - _Py_identifier(text); + _Py_IDENTIFIER(msg); + _Py_IDENTIFIER(filename); + _Py_IDENTIFIER(lineno); + _Py_IDENTIFIER(offset); + _Py_IDENTIFIER(text); /* old style errors */ if (PyTuple_Check(err)) @@ -1439,7 +1439,7 @@ handle_system_exit(void) goto done; if (PyExceptionInstance_Check(value)) { /* The error code should be in the `code' attribute. */ - _Py_identifier(code); + _Py_IDENTIFIER(code); PyObject *code = _PyObject_GetAttrId(value, &PyId_code); if (code) { Py_DECREF(value); @@ -1597,7 +1597,7 @@ print_exception(PyObject *f, PyObject *value) else { PyObject* moduleName; char* className; - _Py_identifier(__module__); + _Py_IDENTIFIER(__module__); assert(PyExceptionClass_Check(type)); className = PyExceptionClass_Name(type); if (className != NULL) { @@ -1773,7 +1773,7 @@ flush_io(void) { PyObject *f, *r; PyObject *type, *value, *traceback; - _Py_identifier(flush); + _Py_IDENTIFIER(flush); /* Save the current exception */ PyErr_Fetch(&type, &value, &traceback); @@ -2220,7 +2220,7 @@ static void wait_for_thread_shutdown(void) { #ifdef WITH_THREAD - _Py_identifier(_shutdown); + _Py_IDENTIFIER(_shutdown); PyObject *result; PyThreadState *tstate = PyThreadState_GET(); PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, -- cgit v1.2.1 From 406e4d8f1d3d003e8754d9e65607a697e7f5c865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Fri, 14 Oct 2011 15:16:45 +0200 Subject: Port SetAttrString/HasAttrString to SetAttrId/GetAttrId. --- Python/pythonrun.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0ce61a5558..a6e7c46568 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -810,6 +810,8 @@ create_stdio(PyObject* io, _Py_IDENTIFIER(open); _Py_IDENTIFIER(isatty); _Py_IDENTIFIER(TextIOWrapper); + _Py_IDENTIFIER(name); + _Py_IDENTIFIER(mode); /* stdin is always opened in buffered mode, first because it shouldn't make a difference in common use cases, second because TextIOWrapper @@ -842,7 +844,7 @@ create_stdio(PyObject* io, } text = PyUnicode_FromString(name); - if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0) + if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) goto error; res = _PyObject_CallMethodId(raw, &PyId_isatty, ""); if (res == NULL) @@ -879,7 +881,7 @@ create_stdio(PyObject* io, else mode = "r"; text = PyUnicode_FromString(mode); - if (!text || PyObject_SetAttrString(stream, "mode", text) < 0) + if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) goto error; Py_CLEAR(text); return stream; @@ -1547,6 +1549,7 @@ print_exception(PyObject *f, PyObject *value) { int err = 0; PyObject *type, *tb; + _Py_IDENTIFIER(print_file_and_line); if (!PyExceptionInstance_Check(value)) { PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f); @@ -1562,7 +1565,7 @@ print_exception(PyObject *f, PyObject *value) if (tb && tb != Py_None) err = PyTraceBack_Print(tb, f); if (err == 0 && - PyObject_HasAttrString(value, "print_file_and_line")) + _PyObject_HasAttrId(value, &PyId_print_file_and_line)) { PyObject *message; const char *filename, *text; -- cgit v1.2.1 From 9bde2716a2b2d48153a8e2bb99db27e7929a1e92 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2011 21:21:00 +0200 Subject: Issue #12281: Rewrite the MBCS codec to handle correctly replace and ignore error handlers on all Windows versions. The MBCS codec is now supporting all error handlers, instead of only replace to encode and ignore to decode. --- Python/pythonrun.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a6e7c46568..0f2f0501cd 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -67,7 +67,7 @@ static void initsigs(void); static void call_py_exitfuncs(void); static void wait_for_thread_shutdown(void); static void call_ll_exitfuncs(void); -extern void _PyUnicode_Init(void); +extern int _PyUnicode_Init(void); extern void _PyUnicode_Fini(void); extern int _PyLong_Init(void); extern void PyLong_Fini(void); @@ -261,7 +261,8 @@ Py_InitializeEx(int install_sigs) Py_FatalError("Py_Initialize: can't make modules_reloading dictionary"); /* Init Unicode implementation; relies on the codec registry */ - _PyUnicode_Init(); + if (_PyUnicode_Init() < 0) + Py_FatalError("Py_Initialize: can't initialize unicode"); bimod = _PyBuiltin_Init(); if (bimod == NULL) -- cgit v1.2.1 From 20c31ce6ca51dcf5219497f8f4bb07110b9144a2 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 18 Nov 2011 20:14:34 +0100 Subject: Issue #10227: Add an allocation cache for a single slice object. Patch by Stefan Behnel. --- Python/pythonrun.c | 1 + 1 file changed, 1 insertion(+) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0f2f0501cd..0c267fc832 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -531,6 +531,7 @@ Py_Finalize(void) PyLong_Fini(); PyFloat_Fini(); PyDict_Fini(); + PySlice_Fini(); /* Cleanup Unicode implementation */ _PyUnicode_Fini(); -- cgit v1.2.1 From fe1c10998b6142e028a7cd63a8f42e5fecb406e0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 20 Nov 2011 19:20:00 +0100 Subject: print_exception() uses PyUnicode_GetLength() instead of PyUnicode_GetSize() --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 0c267fc832..389bcd08ec 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1641,7 +1641,7 @@ print_exception(PyObject *f, PyObject *value) if (s == NULL) err = -1; else if (!PyUnicode_Check(s) || - PyUnicode_GetSize(s) != 0) + PyUnicode_GetLength(s) != 0) err = PyFile_WriteString(": ", f); if (err == 0) err = PyFile_WriteObject(s, f, Py_PRINT_RAW); -- cgit v1.2.1 From b15cf649f348f5af9bd8f38405377c5be866a6d8 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Mon, 12 Dec 2011 18:54:29 +0100 Subject: Issue #13575: there is only one class type. --- Python/pythonrun.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index abc9ffc994..c8d1e90074 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -474,7 +474,7 @@ Py_Finalize(void) flush_std_files(); /* Collect final garbage. This disposes of cycles created by - * new-style class definitions, for example. + * class definitions, for example. * XXX This is disabled because it caused too many problems. If * XXX a __del__ or weakref callback triggers here, Python code has * XXX a hard time running, because even the sys module has been @@ -1348,11 +1348,6 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, _Py_IDENTIFIER(offset); _Py_IDENTIFIER(text); - /* old style errors */ - if (PyTuple_Check(err)) - return PyArg_ParseTuple(err, "O(ziiz)", message, filename, - lineno, offset, text); - /* new style errors. `err' is an instance */ if (! (v = _PyObject_GetAttrId(err, &PyId_msg))) -- cgit v1.2.1 From 1434e995657f5525a55584535d023dbe0ce2af24 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 13 Jan 2012 18:52:16 +0100 Subject: Issue #13645: pyc files now contain the size of the corresponding source code, to avoid timestamp collisions (especially on filesystems with a low timestamp resolution) when checking for freshness of the bytecode. --- Python/pythonrun.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index c8d1e90074..761110a86c 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1844,6 +1844,8 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals, "Bad magic number in .pyc file"); return NULL; } + /* Skip mtime and size */ + (void) PyMarshal_ReadLongFromFile(fp); (void) PyMarshal_ReadLongFromFile(fp); v = PyMarshal_ReadLastObjectFromFile(fp); fclose(fp); -- cgit v1.2.1 From fb993800cf884241d1b83007707e5290e29c1d37 Mon Sep 17 00:00:00 2001 From: Meador Inge Date: Thu, 19 Jan 2012 01:08:41 -0600 Subject: Issue #12705: Raise SyntaxError when compiling multiple statements as single interactive statement --- Python/pythonrun.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index bff04de599..44b817f546 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -2129,6 +2129,9 @@ err_input(perrdetail *err) case E_IDENTIFIER: msg = "invalid character in identifier"; break; + case E_BADSINGLE: + msg = "multiple statements found while compiling a single statement"; + break; default: fprintf(stderr, "error=%d\n", err->error); msg = "unknown parsing error"; -- cgit v1.2.1 From fc7f17dd7a77350fd6906e2082f2375647b57c6a Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 26 Feb 2012 17:49:52 +1000 Subject: Close issue #6210: Implement PEP 409 --- Python/pythonrun.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a642c0b0da..f4e7e7b9b2 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1698,7 +1698,11 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) else if (PyExceptionInstance_Check(value)) { cause = PyException_GetCause(value); context = PyException_GetContext(value); - if (cause) { + if (cause && cause == Py_None) { + /* print neither cause nor context */ + ; + } + else if (cause) { res = PySet_Contains(seen, cause); if (res == -1) PyErr_Clear(); -- cgit v1.2.1 From e6ecc6d13e7b3c963d6cabfdacca8138f968e970 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sat, 14 Apr 2012 14:10:13 -0400 Subject: Issue #2377: Make importlib the implementation of __import__(). importlib._bootstrap is now frozen into Python/importlib.h and stored as _frozen_importlib in sys.modules. Py_Initialize() loads the frozen code along with sys and imp and then uses _frozen_importlib._install() to set builtins.__import__() w/ _frozen_importlib.__import__(). --- Python/pythonrun.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index b68bf9db06..2757eba927 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -190,6 +190,58 @@ get_locale_encoding(void) #endif } +static void +import_init(PyInterpreterState *interp, PyObject *sysmod) +{ + PyObject *importlib; + PyObject *impmod; + PyObject *sys_modules; + PyObject *value; + + /* Import _importlib through its frozen version, _frozen_importlib. */ + /* XXX(bcannon): The file path for _frozen_importlib is completely off + */ + if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { + Py_FatalError("Py_Initialize: can't import _frozen_importlib"); + } + else if (Py_VerboseFlag) { + PySys_FormatStderr("import _frozen_importlib # frozen\n"); + } + importlib = PyImport_AddModule("_frozen_importlib"); + if (importlib == NULL) { + Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from " + "sys.modules"); + } + interp->importlib = importlib; + Py_INCREF(interp->importlib); + + /* Install _importlib as __import__ */ + impmod = PyInit_imp(); + if (impmod == NULL) { + Py_FatalError("Py_Initialize: can't import imp"); + } + else if (Py_VerboseFlag) { + PySys_FormatStderr("import imp # builtin\n"); + } + sys_modules = PyImport_GetModuleDict(); + if (Py_VerboseFlag) { + PySys_FormatStderr("import sys # builtin\n"); + } + if (PyDict_SetItemString(sys_modules, "imp", impmod) < 0) { + Py_FatalError("Py_Initialize: can't save imp to sys.modules"); + } + + value = PyObject_CallMethod(importlib, "_setup", "OO", sysmod, impmod); + if (value == NULL) { + PyErr_Print(); + Py_FatalError("Py_Initialize: importlib install failed"); + } + Py_DECREF(value); + + _PyImportZip_Init(); +} + + void Py_InitializeEx(int install_sigs) { @@ -281,7 +333,7 @@ Py_InitializeEx(int install_sigs) Py_INCREF(interp->builtins); /* initialize builtin exceptions */ - _PyExc_Init(); + _PyExc_Init(bimod); sysmod = _PySys_Init(); if (sysmod == NULL) @@ -315,6 +367,8 @@ Py_InitializeEx(int install_sigs) /* Initialize _warnings. */ _PyWarnings_Init(); + import_init(interp, sysmod); + _PyTime_Init(); if (initfsencoding(interp) < 0) @@ -638,11 +692,12 @@ Py_NewInterpreter(void) } /* initialize builtin exceptions */ - _PyExc_Init(); + _PyExc_Init(bimod); sysmod = _PyImport_FindBuiltin("sys"); if (bimod != NULL && sysmod != NULL) { PyObject *pstderr; + interp->sysdict = PyModule_GetDict(sysmod); if (interp->sysdict == NULL) goto handle_error; @@ -661,6 +716,8 @@ Py_NewInterpreter(void) _PyImportHooks_Init(); + import_init(interp, sysmod); + if (initfsencoding(interp) < 0) goto handle_error; -- cgit v1.2.1 From a2d45fe8cf0b00cc2ec7ed268ea3de424db42649 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 15 Apr 2012 01:35:05 -0400 Subject: Plug a refleak. --- Python/pythonrun.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 2757eba927..b64a9bf796 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -199,8 +199,6 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) PyObject *value; /* Import _importlib through its frozen version, _frozen_importlib. */ - /* XXX(bcannon): The file path for _frozen_importlib is completely off - */ if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { Py_FatalError("Py_Initialize: can't import _frozen_importlib"); } @@ -237,6 +235,7 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) Py_FatalError("Py_Initialize: importlib install failed"); } Py_DECREF(value); + Py_DECREF(impmod); _PyImportZip_Init(); } -- cgit v1.2.1 From 6d17cfc162c054f5c39bd2c7dbde2a6513924722 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 15 Apr 2012 16:08:47 -0400 Subject: Issue #13959: Rename imp to _imp and add Lib/imp.py and begin rewriting functionality in pure Python. To start, imp.new_module() has been rewritten in pure Python, put into importlib (privately) and then publicly exposed in imp. --- Python/pythonrun.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index b64a9bf796..44a85bb355 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -225,8 +225,8 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) if (Py_VerboseFlag) { PySys_FormatStderr("import sys # builtin\n"); } - if (PyDict_SetItemString(sys_modules, "imp", impmod) < 0) { - Py_FatalError("Py_Initialize: can't save imp to sys.modules"); + if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) { + Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); } value = PyObject_CallMethod(importlib, "_setup", "OO", sysmod, impmod); -- cgit v1.2.1 From 134077c0558bbe9199cce0dd1815523802bb968d Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 25 Apr 2012 20:54:04 -0400 Subject: Issue #14605: Make explicit the entries on sys.path_hooks that used to be implicit. Added a warning for when sys.path_hooks is found to be empty. Also changed the meaning of None in sys.path_importer_cache to represent trying sys.path_hooks again (an interpretation of previous semantics). Also added a warning for when None was found. The long-term goal is for None in sys.path_importer_cache to represent the same as imp.NullImporter: no finder found for that sys.path entry. --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 44a85bb355..9e20e4a051 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -229,7 +229,7 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); } - value = PyObject_CallMethod(importlib, "_setup", "OO", sysmod, impmod); + value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); if (value == NULL) { PyErr_Print(); Py_FatalError("Py_Initialize: importlib install failed"); -- cgit v1.2.1 From 50f76bdf6a49c9e80d8c092db2e93fc8c02ecb5c Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 29 Apr 2012 14:38:11 -0400 Subject: Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py. Thanks to Eric Snow for the patch. --- Python/pythonrun.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 9e20e4a051..cd3cf5c53c 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -314,9 +314,6 @@ Py_InitializeEx(int install_sigs) interp->modules = PyDict_New(); if (interp->modules == NULL) Py_FatalError("Py_Initialize: can't make modules dictionary"); - interp->modules_reloading = PyDict_New(); - if (interp->modules_reloading == NULL) - Py_FatalError("Py_Initialize: can't make modules_reloading dictionary"); /* Init Unicode implementation; relies on the codec registry */ if (_PyUnicode_Init() < 0) @@ -680,7 +677,6 @@ Py_NewInterpreter(void) /* XXX The following is lax in error checking */ interp->modules = PyDict_New(); - interp->modules_reloading = PyDict_New(); bimod = _PyImport_FindBuiltin("builtins"); if (bimod != NULL) { -- cgit v1.2.1 From 9031c8f35aa4af460cb1507b2fc1ec8f78601327 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 14 May 2012 22:09:31 -0700 Subject: PEP 415: Implement suppression of __context__ display with an exception attribute This replaces the original PEP 409 implementation. See #14133. --- Python/pythonrun.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index cd3cf5c53c..d9eb5e7d9f 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1761,11 +1761,7 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) else if (PyExceptionInstance_Check(value)) { cause = PyException_GetCause(value); context = PyException_GetContext(value); - if (cause && cause == Py_None) { - /* print neither cause nor context */ - ; - } - else if (cause) { + if (cause) { res = PySet_Contains(seen, cause); if (res == -1) PyErr_Clear(); @@ -1776,7 +1772,8 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) cause_message, f); } } - else if (context) { + else if (context && + !((PyBaseExceptionObject *)value)->suppress_context) { res = PySet_Contains(seen, context); if (res == -1) PyErr_Clear(); -- cgit v1.2.1 From acf001dca7afd219dde2b5abd928f4258c1b71e7 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 19 Jun 2012 22:29:35 +0200 Subject: Issue #14928: Fix importlib bootstrap issues by using a custom executable (Modules/_freeze_importlib) to build Python/importlib.h. --- Python/pythonrun.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index d9eb5e7d9f..d9d2fdda81 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -242,7 +242,7 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) void -Py_InitializeEx(int install_sigs) +_Py_InitializeEx_Private(int install_sigs, int install_importlib) { PyInterpreterState *interp; PyThreadState *tstate; @@ -363,6 +363,9 @@ Py_InitializeEx(int install_sigs) /* Initialize _warnings. */ _PyWarnings_Init(); + if (!install_importlib) + return; + import_init(interp, sysmod); _PyTime_Init(); @@ -392,6 +395,12 @@ Py_InitializeEx(int install_sigs) initsite(); /* Module site */ } +void +Py_InitializeEx(int install_sigs) +{ + _Py_InitializeEx_Private(install_sigs, 1); +} + void Py_Initialize(void) { -- cgit v1.2.1 From c66679874d42ebaddde9958928171a8bb318b116 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 22 Jun 2012 14:55:41 -0400 Subject: Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index d9d2fdda81..465aa7dbfe 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -642,7 +642,7 @@ Py_Finalize(void) #endif /* Py_TRACE_REFS */ #ifdef PYMALLOC_DEBUG if (Py_GETENV("PYTHONMALLOCSTATS")) - _PyObject_DebugMallocStats(); + _PyObject_DebugMallocStats(stderr); #endif call_ll_exitfuncs(); -- cgit v1.2.1 From 5eb145a959563baff35f1fa07f8495aef7cb4171 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 15 Jul 2012 18:09:52 +1000 Subject: Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357) --- Python/pythonrun.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 7 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a9ed588dd3..970834e0bb 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -52,7 +52,7 @@ extern wchar_t *Py_GetPath(void); extern grammar _PyParser_Grammar; /* From graminit.c */ /* Forward */ -static void initmain(void); +static void initmain(PyInterpreterState *interp); static int initfsencoding(PyInterpreterState *interp); static void initsite(void); static int initstdio(void); @@ -376,7 +376,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib) if (install_sigs) initsigs(); /* Signal handling stuff, including initintr() */ - initmain(); /* Module __main__ */ + initmain(interp); /* Module __main__ */ if (initstdio() < 0) Py_FatalError( "Py_Initialize: can't initialize sys standard streams"); @@ -728,7 +728,7 @@ Py_NewInterpreter(void) if (initstdio() < 0) Py_FatalError( "Py_Initialize: can't initialize sys standard streams"); - initmain(); + initmain(interp); if (!Py_NoSiteFlag) initsite(); } @@ -825,7 +825,7 @@ Py_GetPythonHome(void) /* Create __main__ module */ static void -initmain(void) +initmain(PyInterpreterState *interp) { PyObject *m, *d; m = PyImport_AddModule("__main__"); @@ -834,11 +834,31 @@ initmain(void) d = PyModule_GetDict(m); if (PyDict_GetItemString(d, "__builtins__") == NULL) { PyObject *bimod = PyImport_ImportModule("builtins"); - if (bimod == NULL || - PyDict_SetItemString(d, "__builtins__", bimod) != 0) - Py_FatalError("can't add __builtins__ to __main__"); + if (bimod == NULL) { + Py_FatalError("Failed to retrieve builtins module"); + } + if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) { + Py_FatalError("Failed to initialize __main__.__builtins__"); + } Py_DECREF(bimod); } + /* Main is a little special - imp.is_builtin("__main__") will return + * False, but BuiltinImporter is still the most appropriate initial + * setting for its __loader__ attribute. A more suitable value will + * be set if __main__ gets further initialized later in the startup + * process. + */ + if (PyDict_GetItemString(d, "__loader__") == NULL) { + PyObject *loader = PyObject_GetAttrString(interp->importlib, + "BuiltinImporter"); + if (loader == NULL) { + Py_FatalError("Failed to retrieve BuiltinImporter"); + } + if (PyDict_SetItemString(d, "__loader__", loader) < 0) { + Py_FatalError("Failed to initialize __main__.__loader__"); + } + Py_DECREF(loader); + } } static int @@ -1330,6 +1350,24 @@ maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit) return 0; } +int +set_main_loader(PyObject *d, const char *filename, const char *loader_name) +{ + PyInterpreterState *interp; + PyThreadState *tstate; + PyObject *loader; + /* Get current thread state and interpreter pointer */ + tstate = PyThreadState_GET(); + interp = tstate->interp; + loader = PyObject_GetAttrString(interp->importlib, loader_name); + if (loader == NULL || + (PyDict_SetItemString(d, "__loader__", loader) < 0)) { + return -1; + } + Py_DECREF(loader); + return 0; +} + int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags) @@ -1373,8 +1411,21 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, /* Turn on optimization if a .pyo file is given */ if (strcmp(ext, ".pyo") == 0) Py_OptimizeFlag = 1; + + if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) { + fprintf(stderr, "python: failed to set __main__.__loader__\n"); + ret = -1; + goto done; + } v = run_pyc_file(fp, filename, d, d, flags); } else { + /* When running from stdin, leave __main__.__loader__ alone */ + if (strcmp(filename, "") != 0 && + set_main_loader(d, filename, "SourceFileLoader") < 0) { + fprintf(stderr, "python: failed to set __main__.__loader__\n"); + ret = -1; + goto done; + } v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, closeit, flags); } -- cgit v1.2.1 From 142944102f6bd2b4cdcf8e80035167c181422502 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 15 Jul 2012 19:10:39 +1000 Subject: Actually initialize __main__.__loader__ with loader instances, not the corresponding type objects --- Python/pythonrun.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 970834e0bb..8130cc5289 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1355,11 +1355,15 @@ set_main_loader(PyObject *d, const char *filename, const char *loader_name) { PyInterpreterState *interp; PyThreadState *tstate; - PyObject *loader; + PyObject *loader_type, *loader; /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET(); interp = tstate->interp; - loader = PyObject_GetAttrString(interp->importlib, loader_name); + loader_type = PyObject_GetAttrString(interp->importlib, loader_name); + if (loader_type == NULL) { + return -1; + } + loader = PyObject_CallFunction(loader_type, "ss", "__main__", filename); if (loader == NULL || (PyDict_SetItemString(d, "__loader__", loader) < 0)) { return -1; -- cgit v1.2.1 From b0f23648aae46ca1a30bfe7c5ba52744b3701b89 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 15 Jul 2012 23:18:08 +1000 Subject: Make set_main_loader static (noticed by Antoine Pitrou) --- Python/pythonrun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 8130cc5289..33ac741de0 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1351,7 +1351,7 @@ maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit) } int -set_main_loader(PyObject *d, const char *filename, const char *loader_name) +static set_main_loader(PyObject *d, const char *filename, const char *loader_name) { PyInterpreterState *interp; PyThreadState *tstate; -- cgit v1.2.1 From 3a67a0088184bdada808beae41c68096b9b566bf Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 15 Jul 2012 23:21:08 +1000 Subject: Refcounting fixes --- Python/pythonrun.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 33ac741de0..6ee9a5fddf 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1356,6 +1356,7 @@ static set_main_loader(PyObject *d, const char *filename, const char *loader_nam PyInterpreterState *interp; PyThreadState *tstate; PyObject *loader_type, *loader; + int result = 0; /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET(); interp = tstate->interp; @@ -1364,12 +1365,15 @@ static set_main_loader(PyObject *d, const char *filename, const char *loader_nam return -1; } loader = PyObject_CallFunction(loader_type, "ss", "__main__", filename); - if (loader == NULL || - (PyDict_SetItemString(d, "__loader__", loader) < 0)) { + Py_DECREF(loader_type); + if (loader == NULL) { return -1; } + if (PyDict_SetItemString(d, "__loader__", loader) < 0) { + result = -1; + } Py_DECREF(loader); - return 0; + return result; } int -- cgit v1.2.1 From b1097e6aa71b0eba3281eaeae2c7c5177fedfb3f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 31 Jul 2012 02:55:49 +0200 Subject: Fix initialization of the faulthandler module faulthandler requires the importlib if "-X faulthandler" option is present on the command line, so initialize faulthandler after importlib. Add also an unit test. --- Python/pythonrun.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 6ee9a5fddf..cafc09ac3f 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -356,10 +356,6 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib) _PyImportHooks_Init(); - /* initialize the faulthandler module */ - if (_PyFaulthandler_Init()) - Py_FatalError("Py_Initialize: can't initialize faulthandler"); - /* Initialize _warnings. */ _PyWarnings_Init(); @@ -368,6 +364,10 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib) import_init(interp, sysmod); + /* initialize the faulthandler module */ + if (_PyFaulthandler_Init()) + Py_FatalError("Py_Initialize: can't initialize faulthandler"); + _PyTime_Init(); if (initfsencoding(interp) < 0) -- cgit v1.2.1 From 18f37321ab18672f72c9fa42a9fd143e4b731fb5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 4 Aug 2012 01:28:00 +0200 Subject: Close #13119: use "\r\n" newline for sys.stdout/err on Windows sys.stdout and sys.stderr are now using "\r\n" newline on Windows, as Python 2. --- Python/pythonrun.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index cafc09ac3f..05dfb8e1d0 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -971,12 +971,15 @@ create_stdio(PyObject* io, Py_CLEAR(raw); Py_CLEAR(text); - newline = "\n"; #ifdef MS_WINDOWS - if (!write_mode) { - /* translate \r\n to \n for sys.stdin on Windows */ - newline = NULL; - } + /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r" + newlines to "\n". + sys.stdout and sys.stderr: translate "\n" to "\r\n". */ + newline = NULL; +#else + /* sys.stdin: split lines at "\n". + sys.stdout and sys.stderr: don't translate newlines (use "\n"). */ + newline = "\n"; #endif stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", -- cgit v1.2.1 From e9af11a58c7e0d1facc54f24a97cf1ecbf3a56bb Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 11 Sep 2012 14:11:03 +0200 Subject: Issue #15895: Fix FILE pointer leak in PyRun_SimpleFileExFlags() when filename points to a pyc/pyo file and closeit is false. --- Python/pythonrun.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 05dfb8e1d0..7e9f6545e2 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1385,7 +1385,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, { PyObject *m, *d, *v; const char *ext; - int set_file_name = 0, ret; + int set_file_name = 0, close_own_fp = 0, ret; size_t len; m = PyImport_AddModule("__main__"); @@ -1419,6 +1419,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, ret = -1; goto done; } + close_own_fp = 1; /* Turn on optimization if a .pyo file is given */ if (strcmp(ext, ".pyo") == 0) Py_OptimizeFlag = 1; @@ -1449,6 +1450,9 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, Py_DECREF(v); ret = 0; done: + if (close_own_fp) { + fclose(fp); + } if (set_file_name && PyDict_DelItemString(d, "__file__")) PyErr_Clear(); return ret; -- cgit v1.2.1 From e9f8d79a728051ea0254a6885d2ce11a48faa80f Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 11 Sep 2012 15:47:28 +0200 Subject: Issue #15895: my analysis was slightly off. The FILE pointer is only leaked when set_main_loader() fails for a pyc file with closeit=0. In the success case run_pyc_file() does its own cleanup of the fp. I've changed the code to use another FILE ptr for pyc files and moved the fclose() to PyRun_SimpleFileExFlags() to make it more obvious what's happening. --- Python/pythonrun.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 7e9f6545e2..b1ca125781 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1385,7 +1385,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, { PyObject *m, *d, *v; const char *ext; - int set_file_name = 0, close_own_fp = 0, ret; + int set_file_name = 0, ret; size_t len; m = PyImport_AddModule("__main__"); @@ -1411,15 +1411,15 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, len = strlen(filename); ext = filename + len - (len > 4 ? 4 : 0); if (maybe_pyc_file(fp, filename, ext, closeit)) { + FILE *pyc_fp; /* Try to run a pyc file. First, re-open in binary */ if (closeit) fclose(fp); - if ((fp = fopen(filename, "rb")) == NULL) { + if ((pyc_fp = fopen(filename, "rb")) == NULL) { fprintf(stderr, "python: Can't reopen .pyc file\n"); ret = -1; goto done; } - close_own_fp = 1; /* Turn on optimization if a .pyo file is given */ if (strcmp(ext, ".pyo") == 0) Py_OptimizeFlag = 1; @@ -1427,9 +1427,11 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) { fprintf(stderr, "python: failed to set __main__.__loader__\n"); ret = -1; + fclose(pyc_fp); goto done; } - v = run_pyc_file(fp, filename, d, d, flags); + v = run_pyc_file(pyc_fp, filename, d, d, flags); + fclose(pyc_fp); } else { /* When running from stdin, leave __main__.__loader__ alone */ if (strcmp(filename, "") != 0 && @@ -1450,9 +1452,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, Py_DECREF(v); ret = 0; done: - if (close_own_fp) { - fclose(fp); - } if (set_file_name && PyDict_DelItemString(d, "__file__")) PyErr_Clear(); return ret; @@ -1999,7 +1998,6 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals, (void) PyMarshal_ReadLongFromFile(fp); (void) PyMarshal_ReadLongFromFile(fp); v = PyMarshal_ReadLastObjectFromFile(fp); - fclose(fp); if (v == NULL || !PyCode_Check(v)) { Py_XDECREF(v); PyErr_SetString(PyExc_RuntimeError, -- cgit v1.2.1 From f5c8faafee817beaa30d7ac28c1e49bc2ffe3b48 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Thu, 1 Nov 2012 14:51:14 +0200 Subject: Issue #16218: Support non ascii characters in python launcher. Patch by Serhiy Storchaka. --- Python/pythonrun.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'Python/pythonrun.c') diff --git a/Python/pythonrun.c b/Python/pythonrun.c index b1ca125781..7c4fb4a3c9 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1358,16 +1358,21 @@ static set_main_loader(PyObject *d, const char *filename, const char *loader_nam { PyInterpreterState *interp; PyThreadState *tstate; - PyObject *loader_type, *loader; + PyObject *filename_obj, *loader_type, *loader; int result = 0; + + filename_obj = PyUnicode_DecodeFSDefault(filename); + if (filename_obj == NULL) + return -1; /* Get current thread state and interpreter pointer */ tstate = PyThreadState_GET(); interp = tstate->interp; loader_type = PyObject_GetAttrString(interp->importlib, loader_name); if (loader_type == NULL) { + Py_DECREF(filename_obj); return -1; } - loader = PyObject_CallFunction(loader_type, "ss", "__main__", filename); + loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj); Py_DECREF(loader_type); if (loader == NULL) { return -1; -- cgit v1.2.1