static int initfsencoding(PyInterpreterState *interp) { PyObject *codec; if (Py_FileSystemDefaultEncoding == NULL) { Py_FileSystemDefaultEncoding = get_locale_encoding(); if (Py_FileSystemDefaultEncoding == NULL) Py_FatalError("Py_Initialize: Unable to get the locale encoding"); Py_HasFileSystemDefaultEncoding = 0; interp->fscodec_initialized = 1; return 0; } /* the encoding is mbcs, utf-8 or ascii */ codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); if (!codec) { /* Such error can only occurs in critical situations: no more * memory, import a module of the standard library failed, * etc. */ return -1; } Py_DECREF(codec); interp->fscodec_initialized = 1; return 0; }
static char* get_codec_name(const char *encoding) { char *name_utf8, *name_str; PyObject *codec, *name = NULL; codec = _PyCodec_Lookup(encoding); if (!codec) goto error; name = _PyObject_GetAttrId(codec, &PyId_name); Py_CLEAR(codec); if (!name) goto error; name_utf8 = _PyUnicode_AsString(name); if (name_utf8 == NULL) goto error; name_str = _PyMem_RawStrdup(name_utf8); Py_DECREF(name); if (name_str == NULL) { PyErr_NoMemory(); return NULL; } return name_str; error: Py_XDECREF(codec); Py_XDECREF(name); return NULL; }
static PyObject *codec_lookup(PyObject *self, PyObject *args) { char *encoding; if (!PyArg_ParseTuple(args, "s:lookup", &encoding)) return NULL; return _PyCodec_Lookup(encoding); }
static PyObject* codec_getitem(const char* encoding, int index) { PyObject* codecs; PyObject* v; codecs = _PyCodec_Lookup(encoding); if (codecs == NULL) return NULL; v = PyTuple_GET_ITEM(codecs, index); Py_DECREF(codecs); Py_INCREF(v); return v; }
static PyObject *codec_getincrementalcodec(const char *encoding, const char *errors, const char *attrname) { PyObject *codec_info, *ret; codec_info = _PyCodec_Lookup(encoding); if (codec_info == NULL) return NULL; ret = codec_makeincrementalcodec(codec_info, errors, attrname); Py_DECREF(codec_info); return ret; }
int PyCodec_KnownEncoding(const char *encoding) { PyObject *codecs; codecs = _PyCodec_Lookup(encoding); if (!codecs) { PyErr_Clear(); return 0; } else { Py_DECREF(codecs); return 1; } }
static PyObject* codec_getstreamcodec(const char* encoding, PyObject* stream, const char* errors, const int index) { PyObject* codecs, *streamcodec, *codeccls; codecs = _PyCodec_Lookup(encoding); if (codecs == NULL) return NULL; codeccls = PyTuple_GET_ITEM(codecs, index); if (errors != NULL) streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors); else streamcodec = PyObject_CallFunction(codeccls, "O", stream); Py_DECREF(codecs); return streamcodec; }
/* Text encoding/decoding API */ static PyObject *codec_getitem_checked(const char *encoding, const char *operation_name, int index) { _Py_IDENTIFIER(_is_text_encoding); PyObject *codec; PyObject *attr; PyObject *v; int is_text_codec; codec = _PyCodec_Lookup(encoding); if (codec == NULL) return NULL; /* Backwards compatibility: assume any raw tuple describes a text * encoding, and the same for anything lacking the private * attribute. */ if (!PyTuple_CheckExact(codec)) { attr = _PyObject_GetAttrId(codec, &PyId__is_text_encoding); if (attr == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); } else { Py_DECREF(codec); return NULL; } } else { is_text_codec = PyObject_IsTrue(attr); Py_DECREF(attr); if (!is_text_codec) { Py_DECREF(codec); PyErr_Format(PyExc_LookupError, "'%.400s' is not a text encoding; " "use codecs.%s() to handle arbitrary codecs", encoding, operation_name); return NULL; } } } v = PyTuple_GET_ITEM(codec, index); Py_DECREF(codec); Py_INCREF(v); return v; }
static PyObject* codec_getincrementalcodec(const char* encoding, const char* errors, const char* attrname) { PyObject* codecs, *ret, *inccodec; codecs = _PyCodec_Lookup(encoding); if (codecs == NULL) return NULL; inccodec = PyObject_GetAttrString(codecs, attrname); Py_DECREF(codecs); if (inccodec == NULL) return NULL; if (errors) ret = PyObject_CallFunction(inccodec, "s", errors); else ret = PyObject_CallFunction(inccodec, NULL); Py_DECREF(inccodec); return ret; }
/* Text encoding/decoding API */ PyObject * _PyCodec_LookupTextEncoding(const char *encoding, const char *alternate_command) { _Py_IDENTIFIER(_is_text_encoding); PyObject *codec; PyObject *attr; int is_text_codec; codec = _PyCodec_Lookup(encoding); if (codec == NULL) return NULL; /* Backwards compatibility: assume any raw tuple describes a text * encoding, and the same for anything lacking the private * attribute. */ if (!PyTuple_CheckExact(codec)) { attr = _PyObject_GetAttrId(codec, &PyId__is_text_encoding); if (attr == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); } else { Py_DECREF(codec); return NULL; } } else { is_text_codec = PyObject_IsTrue(attr); Py_DECREF(attr); if (is_text_codec <= 0) { Py_DECREF(codec); if (!is_text_codec) PyErr_Format(PyExc_LookupError, "'%.400s' is not a text encoding; " "use %s to handle arbitrary codecs", encoding, alternate_command); return NULL; } } } /* This appears to be a valid text encoding */ return codec; }
static int initfsencoding(PyInterpreterState *interp) { PyObject *codec; #ifdef MS_WINDOWS if (Py_LegacyWindowsFSEncodingFlag) { Py_FileSystemDefaultEncoding = "mbcs"; Py_FileSystemDefaultEncodeErrors = "replace"; } else { Py_FileSystemDefaultEncoding = "utf-8"; Py_FileSystemDefaultEncodeErrors = "surrogatepass"; } #else if (Py_FileSystemDefaultEncoding == NULL) { Py_FileSystemDefaultEncoding = get_locale_encoding(); if (Py_FileSystemDefaultEncoding == NULL) Py_FatalError("Py_Initialize: Unable to get the locale encoding"); Py_HasFileSystemDefaultEncoding = 0; interp->fscodec_initialized = 1; return 0; } #endif /* the encoding is mbcs, utf-8 or ascii */ codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); if (!codec) { /* Such error can only occurs in critical situations: no more * memory, import a module of the standard library failed, * etc. */ return -1; } Py_DECREF(codec); interp->fscodec_initialized = 1; return 0; }
/* Initialize sys.stdin, stdout, stderr and builtins.open */ static int initstdio(void) { PyObject *iomod = NULL, *wrapper; PyObject *bimod = NULL; PyObject *m; PyObject *std = NULL; int status = 0, fd; PyObject * encoding_attr; char *pythonioencoding = NULL, *encoding, *errors; /* Hack to avoid a nasty recursion issue when Python is invoked in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { goto error; } Py_DECREF(m); if (!(m = PyImport_ImportModule("encodings.latin_1"))) { goto error; } Py_DECREF(m); if (!(bimod = PyImport_ImportModule("builtins"))) { goto error; } if (!(iomod = PyImport_ImportModule("io"))) { goto error; } if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { goto error; } /* Set builtins.open */ if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { Py_DECREF(wrapper); goto error; } Py_DECREF(wrapper); encoding = _Py_StandardStreamEncoding; errors = _Py_StandardStreamErrors; if (!encoding || !errors) { pythonioencoding = Py_GETENV("PYTHONIOENCODING"); if (pythonioencoding) { char *err; pythonioencoding = _PyMem_Strdup(pythonioencoding); if (pythonioencoding == NULL) { PyErr_NoMemory(); goto error; } err = strchr(pythonioencoding, ':'); if (err) { *err = '\0'; err++; if (*err && !errors) { errors = err; } } if (*pythonioencoding && !encoding) { encoding = pythonioencoding; } } if (!errors && !(pythonioencoding && *pythonioencoding)) { /* When the LC_CTYPE locale is the POSIX locale ("C locale"), stdin and stdout use the surrogateescape error handler by default, instead of the strict error handler. */ char *loc = setlocale(LC_CTYPE, NULL); if (loc != NULL && strcmp(loc, "C") == 0) errors = "surrogateescape"; } } /* Set sys.stdin */ fd = fileno(stdin); /* Under some conditions stdin, stdout and stderr may not be connected * and fileno() may point to an invalid file descriptor. For example * GUI apps don't have valid standard streams by default. */ std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); if (std == NULL) goto error; PySys_SetObject("__stdin__", std); _PySys_SetObjectId(&PyId_stdin, std); Py_DECREF(std); /* Set sys.stdout */ fd = fileno(stdout); std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); if (std == NULL) goto error; PySys_SetObject("__stdout__", std); _PySys_SetObjectId(&PyId_stdout, std); Py_DECREF(std); #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ /* Set sys.stderr, replaces the preliminary stderr */ fd = fileno(stderr); std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); if (std == NULL) goto error; /* Same as hack above, pre-import stderr's codec to avoid recursion when import.c tries to write to stderr in verbose mode. */ encoding_attr = PyObject_GetAttrString(std, "encoding"); if (encoding_attr != NULL) { const char * std_encoding; std_encoding = _PyUnicode_AsString(encoding_attr); if (std_encoding != NULL) { PyObject *codec_info = _PyCodec_Lookup(std_encoding); Py_XDECREF(codec_info); } Py_DECREF(encoding_attr); } PyErr_Clear(); /* Not a fatal error if codec isn't available */ if (PySys_SetObject("__stderr__", std) < 0) { Py_DECREF(std); goto error; } if (_PySys_SetObjectId(&PyId_stderr, std) < 0) { Py_DECREF(std); goto error; } Py_DECREF(std); #endif if (0) { error: status = -1; } /* We won't need them anymore. */ if (_Py_StandardStreamEncoding) { PyMem_RawFree(_Py_StandardStreamEncoding); _Py_StandardStreamEncoding = NULL; } if (_Py_StandardStreamErrors) { PyMem_RawFree(_Py_StandardStreamErrors); _Py_StandardStreamErrors = NULL; } PyMem_Free(pythonioencoding); Py_XDECREF(bimod); Py_XDECREF(iomod); return status; }
static PyObject * _codecs_lookup_impl(PyObject *module, const char *encoding) /*[clinic end generated code: output=9f0afa572080c36d input=3c572c0db3febe9c]*/ { return _PyCodec_Lookup(encoding); }