diff options
Diffstat (limited to 'Python/bltinmodule.c')
| -rw-r--r-- | Python/bltinmodule.c | 91 | 
1 files changed, 46 insertions, 45 deletions
| diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 6258167..e68f025 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -6,6 +6,9 @@  #include "node.h"  #include "code.h" +#include "asdl.h" +#include "ast.h" +  #include <ctype.h>  #ifdef HAVE_LANGINFO_H @@ -18,26 +21,27 @@     Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the     values for Py_FileSystemDefaultEncoding!  */ -#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) +#ifdef HAVE_MBCS  const char *Py_FileSystemDefaultEncoding = "mbcs";  int Py_HasFileSystemDefaultEncoding = 1;  #elif defined(__APPLE__)  const char *Py_FileSystemDefaultEncoding = "utf-8";  int Py_HasFileSystemDefaultEncoding = 1; -#elif defined(HAVE_LANGINFO_H) && defined(CODESET) +#else  const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */  int Py_HasFileSystemDefaultEncoding = 0; -#else -const char *Py_FileSystemDefaultEncoding = "utf-8"; -int Py_HasFileSystemDefaultEncoding = 1;  #endif +_Py_IDENTIFIER(fileno); +_Py_IDENTIFIER(flush); +  static PyObject *  builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)  {      PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;      PyObject *cls = NULL; -    Py_ssize_t nargs, nbases; +    Py_ssize_t nargs; +    _Py_IDENTIFIER(__prepare__);      assert(args != NULL);      if (!PyTuple_Check(args)) { @@ -61,7 +65,6 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)      bases = PyTuple_GetSlice(args, 2, nargs);      if (bases == NULL)          return NULL; -    nbases = nargs - 2;      if (kwds == NULL) {          meta = NULL; @@ -93,7 +96,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)          }          Py_INCREF(meta);      } -    prep = PyObject_GetAttrString(meta, "__prepare__"); +    prep = _PyObject_GetAttrId(meta, &PyId___prepare__);      if (prep == NULL) {          if (PyErr_ExceptionMatches(PyExc_AttributeError)) {              PyErr_Clear(); @@ -156,17 +159,14 @@ builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)  {      static char *kwlist[] = {"name", "globals", "locals", "fromlist",                               "level", 0}; -    char *name; -    PyObject *globals = NULL; -    PyObject *locals = NULL; -    PyObject *fromlist = NULL; +    PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;      int level = -1; -    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__", +    if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",                      kwlist, &name, &globals, &locals, &fromlist, &level))          return NULL; -    return PyImport_ImportModuleLevel(name, globals, locals, -                                      fromlist, level); +    return PyImport_ImportModuleLevelObject(name, globals, locals, +                                            fromlist, level);  }  PyDoc_STRVAR(import_doc, @@ -512,8 +512,8 @@ source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)      if (PyUnicode_Check(cmd)) {          cf->cf_flags |= PyCF_IGNORE_COOKIE; -        cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL); -        if (cmd == NULL) +        str = PyUnicode_AsUTF8AndSize(cmd, &size); +        if (str == NULL)              return NULL;      }      else if (!PyObject_CheckReadBuffer(cmd)) { @@ -522,9 +522,10 @@ source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)            funcname, what);          return NULL;      } -    if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) { +    else if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {          return NULL;      } +      if (strlen(str) != size) {          PyErr_SetString(PyExc_TypeError,                          "source code string cannot contain null bytes"); @@ -611,6 +612,10 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)                  PyArena_Free(arena);                  goto error;              } +            if (!PyAST_Validate(mod)) { +                PyArena_Free(arena); +                goto error; +            }              result = (PyObject*)PyAST_CompileEx(mod, filename,                                                  &cf, optimize, arena);              PyArena_Free(arena); @@ -766,7 +771,6 @@ builtin_exec(PyObject *self, PyObject *args)  {      PyObject *v;      PyObject *prog, *globals = Py_None, *locals = Py_None; -    int plain = 0;      if (!PyArg_UnpackTuple(args, "exec", 1, 3, &prog, &globals, &locals))          return NULL; @@ -775,7 +779,6 @@ builtin_exec(PyObject *self, PyObject *args)          globals = PyEval_GetGlobals();          if (locals == Py_None) {              locals = PyEval_GetLocals(); -            plain = 1;          }          if (!globals || !locals) {              PyErr_SetString(PyExc_SystemError, @@ -1397,24 +1400,13 @@ builtin_ord(PyObject *self, PyObject* obj)          }      }      else if (PyUnicode_Check(obj)) { -        size = PyUnicode_GET_SIZE(obj); +        if (PyUnicode_READY(obj) == -1) +            return NULL; +        size = PyUnicode_GET_LENGTH(obj);          if (size == 1) { -            ord = (long)*PyUnicode_AS_UNICODE(obj); +            ord = (long)PyUnicode_READ_CHAR(obj, 0);              return PyLong_FromLong(ord);          } -#ifndef Py_UNICODE_WIDE -        if (size == 2) { -            /* Decode a valid surrogate pair */ -            int c0 = PyUnicode_AS_UNICODE(obj)[0]; -            int c1 = PyUnicode_AS_UNICODE(obj)[1]; -            if (0xD800 <= c0 && c0 <= 0xDBFF && -                0xDC00 <= c1 && c1 <= 0xDFFF) { -                ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) + -                       0x00010000); -                return PyLong_FromLong(ord); -            } -        } -#endif      }      else if (PyByteArray_Check(obj)) {          /* XXX Hopefully this is temporary */ @@ -1579,7 +1571,7 @@ builtin_input(PyObject *self, PyObject *args)      }      /* First of all, flush stderr */ -    tmp = PyObject_CallMethod(ferr, "flush", ""); +    tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");      if (tmp == NULL)          PyErr_Clear();      else @@ -1588,7 +1580,7 @@ builtin_input(PyObject *self, PyObject *args)      /* We should only use (GNU) readline if Python's sys.stdin and         sys.stdout are the same as C's stdin and stdout, because we         need to pass it those. */ -    tmp = PyObject_CallMethod(fin, "fileno", ""); +    tmp = _PyObject_CallMethodId(fin, &PyId_fileno, "");      if (tmp == NULL) {          PyErr_Clear();          tty = 0; @@ -1601,7 +1593,7 @@ builtin_input(PyObject *self, PyObject *args)          tty = fd == fileno(stdin) && isatty(fd);      }      if (tty) { -        tmp = PyObject_CallMethod(fout, "fileno", ""); +        tmp = _PyObject_CallMethodId(fout, &PyId_fileno, "");          if (tmp == NULL)              PyErr_Clear();          else { @@ -1622,8 +1614,9 @@ builtin_input(PyObject *self, PyObject *args)          char *stdin_encoding_str;          PyObject *result;          size_t len; +        _Py_IDENTIFIER(encoding); -        stdin_encoding = PyObject_GetAttrString(fin, "encoding"); +        stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);          if (!stdin_encoding)              /* stdin is a text stream, so it must have an                 encoding. */ @@ -1633,7 +1626,7 @@ builtin_input(PyObject *self, PyObject *args)              Py_DECREF(stdin_encoding);              return NULL;          } -        tmp = PyObject_CallMethod(fout, "flush", ""); +        tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");          if (tmp == NULL)              PyErr_Clear();          else @@ -1642,7 +1635,7 @@ builtin_input(PyObject *self, PyObject *args)              PyObject *stringpo;              PyObject *stdout_encoding;              char *stdout_encoding_str; -            stdout_encoding = PyObject_GetAttrString(fout, "encoding"); +            stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);              if (stdout_encoding == NULL) {                  Py_DECREF(stdin_encoding);                  return NULL; @@ -1715,7 +1708,7 @@ builtin_input(PyObject *self, PyObject *args)          if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)              return NULL;      } -    tmp = PyObject_CallMethod(fout, "flush", ""); +    tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");      if (tmp == NULL)          PyErr_Clear();      else @@ -1797,6 +1790,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)      PyObject *callable;      static char *kwlist[] = {"iterable", "key", "reverse", 0};      int reverse; +    _Py_IDENTIFIER(sort);      /* args 1-3 should match listsort in Objects/listobject.c */      if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted", @@ -1807,7 +1801,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)      if (newlist == NULL)          return NULL; -    callable = PyObject_GetAttrString(newlist, "sort"); +    callable = _PyObject_GetAttrId(newlist, &PyId_sort);      if (callable == NULL) {          Py_DECREF(newlist);          return NULL; @@ -1853,7 +1847,8 @@ builtin_vars(PyObject *self, PyObject *args)              Py_INCREF(d);      }      else { -        d = PyObject_GetAttrString(v, "__dict__"); +        _Py_IDENTIFIER(__dict__); +        d = _PyObject_GetAttrId(v, &PyId___dict__);          if (d == NULL) {              PyErr_SetString(PyExc_TypeError,                  "vars() argument must have __dict__ attribute"); @@ -1897,12 +1892,18 @@ builtin_sum(PyObject *self, PyObject *args)              Py_DECREF(iter);              return NULL;          } -        if (PyByteArray_Check(result)) { +        if (PyBytes_Check(result)) {              PyErr_SetString(PyExc_TypeError,                  "sum() can't sum bytes [use b''.join(seq) instead]");              Py_DECREF(iter);              return NULL;          } +        if (PyByteArray_Check(result)) { +            PyErr_SetString(PyExc_TypeError, +                "sum() can't sum bytearray [use b''.join(seq) instead]"); +            Py_DECREF(iter); +            return NULL; +        }          Py_INCREF(result);      } | 
