/* Convert a Postgres encoding into Python encoding and decoding functions. * * Set clean_encoding to a clean version of the Postgres encoding name * and pyenc and pydec to python codec functions. * * Return 0 on success, else -1 and set an exception. */ RAISES_NEG static int conn_get_python_codec(const char *encoding, char **clean_encoding, PyObject **pyenc, PyObject **pydec) { int rv = -1; char *pgenc = NULL; PyObject *encname = NULL; PyObject *enc_tmp = NULL, *dec_tmp = NULL; /* get the Python name of the encoding as a C string */ if (!(encname = conn_pgenc_to_pyenc(encoding, &pgenc))) { goto exit; } if (!(encname = psyco_ensure_bytes(encname))) { goto exit; } /* Look up the codec functions */ if (!(enc_tmp = PyCodec_Encoder(Bytes_AS_STRING(encname)))) { goto exit; } if (!(dec_tmp = PyCodec_Decoder(Bytes_AS_STRING(encname)))) { goto exit; } /* success */ *pyenc = enc_tmp; enc_tmp = NULL; *pydec = dec_tmp; dec_tmp = NULL; *clean_encoding = pgenc; pgenc = NULL; rv = 0; exit: Py_XDECREF(enc_tmp); Py_XDECREF(dec_tmp); Py_XDECREF(encname); PyMem_Free(pgenc); return rv; }
/* Generic encoding/decoding API */ PyObject *PyCodec_Encode(PyObject *object, const char *encoding, const char *errors) { PyObject *encoder; encoder = PyCodec_Encoder(encoding); if (encoder == NULL) return NULL; return _PyCodec_EncodeInternal(object, encoder, encoding, errors); }
PyObject *PyCodec_Encode(PyObject *object, const char *encoding, const char *errors) { PyObject *encoder = NULL; PyObject *args = NULL, *result = NULL; PyObject *v; encoder = PyCodec_Encoder(encoding); if (encoder == NULL) goto onError; args = args_tuple(object, errors); if (args == NULL) goto onError; result = PyEval_CallObject(encoder,args); if (result == NULL) goto onError; if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 2) { PyErr_SetString(PyExc_TypeError, "encoder must return a tuple (object,integer)"); goto onError; } v = PyTuple_GET_ITEM(result,0); Py_INCREF(v); /* We don't check or use the second (integer) entry. */ Py_DECREF(args); Py_DECREF(encoder); Py_DECREF(result); return v; onError: Py_XDECREF(result); Py_XDECREF(args); Py_XDECREF(encoder); return NULL; }
void setEncodingAndErrors() { // Adapted from pythonrun.c in CPython, with modifications for Pyston. char* p; char* icodeset = nullptr; char* codeset = nullptr; char* errors = nullptr; int free_codeset = 0; int overridden = 0; PyObject* sys_stream, *sys_isatty; char* saved_locale, *loc_codeset; if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') { p = icodeset = codeset = strdup(p); free_codeset = 1; errors = strchr(p, ':'); if (errors) { *errors = '\0'; errors++; } overridden = 1; } #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) /* 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. Also set the encoding of stdin and stdout if these are terminals, unless overridden. */ if (!overridden || !Py_FileSystemDefaultEncoding) { saved_locale = strdup(setlocale(LC_CTYPE, NULL)); setlocale(LC_CTYPE, ""); loc_codeset = nl_langinfo(CODESET); if (loc_codeset && *loc_codeset) { PyObject* enc = PyCodec_Encoder(loc_codeset); if (enc) { loc_codeset = strdup(loc_codeset); Py_DECREF(enc); } else { if (PyErr_ExceptionMatches(PyExc_LookupError)) { PyErr_Clear(); loc_codeset = NULL; } else { PyErr_Print(); exit(1); } } } else loc_codeset = NULL; setlocale(LC_CTYPE, saved_locale); free(saved_locale); if (!overridden) { codeset = icodeset = loc_codeset; free_codeset = 1; } /* Initialize Py_FileSystemDefaultEncoding from locale even if PYTHONIOENCODING is set. */ if (!Py_FileSystemDefaultEncoding) { Py_FileSystemDefaultEncoding = loc_codeset; if (!overridden) free_codeset = 0; } } #endif #ifdef MS_WINDOWS if (!overridden) { icodeset = ibuf; codeset = buf; sprintf(ibuf, "cp%d", GetConsoleCP()); sprintf(buf, "cp%d", GetConsoleOutputCP()); } #endif if (codeset) { sys_stream = PySys_GetObject("stdin"); sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); if (!sys_isatty) PyErr_Clear(); if ((overridden || (sys_isatty && PyObject_IsTrue(sys_isatty))) && PyFile_Check(sys_stream)) { if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors)) Py_FatalError("Cannot set codeset of stdin"); } Py_XDECREF(sys_isatty); sys_stream = PySys_GetObject("stdout"); sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); if (!sys_isatty) PyErr_Clear(); if ((overridden || (sys_isatty && PyObject_IsTrue(sys_isatty))) && PyFile_Check(sys_stream)) { if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) Py_FatalError("Cannot set codeset of stdout"); } Py_XDECREF(sys_isatty); sys_stream = PySys_GetObject("stderr"); sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); if (!sys_isatty) PyErr_Clear(); if ((overridden || (sys_isatty && PyObject_IsTrue(sys_isatty))) && PyFile_Check(sys_stream)) { if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) Py_FatalError("Cannot set codeset of stderr"); } Py_XDECREF(sys_isatty); if (free_codeset) free(codeset); } }
void Py_InitializeEx(int install_sigs) { PyInterpreterState *interp; PyThreadState *tstate; PyObject *bimod, *sysmod; char *p; #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) char *codeset; char *saved_locale; PyObject *sys_stream, *sys_isatty; #endif extern void _Py_ReadyTypes(void); if (initialized) return; initialized = 1; if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') Py_DebugFlag = add_flag(Py_DebugFlag, p); if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') Py_VerboseFlag = add_flag(Py_VerboseFlag, p); if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); interp = PyInterpreterState_New(); if (interp == NULL) Py_FatalError("Py_Initialize: can't make first interpreter"); tstate = PyThreadState_New(interp); if (tstate == NULL) Py_FatalError("Py_Initialize: can't make first thread"); (void) PyThreadState_Swap(tstate); _Py_ReadyTypes(); if (!_PyFrame_Init()) Py_FatalError("Py_Initialize: can't init frames"); if (!_PyInt_Init()) Py_FatalError("Py_Initialize: can't init ints"); _PyFloat_Init(); 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"); #ifdef Py_USING_UNICODE /* Init Unicode implementation; relies on the codec registry */ _PyUnicode_Init(); #endif bimod = _PyBuiltin_Init(); if (bimod == NULL) Py_FatalError("Py_Initialize: can't initialize __builtin__"); interp->builtins = PyModule_GetDict(bimod); if (interp->builtins == NULL) Py_FatalError("Py_Initialize: can't initialize builtins dict"); Py_INCREF(interp->builtins); sysmod = _PySys_Init(); if (sysmod == NULL) Py_FatalError("Py_Initialize: can't initialize sys"); interp->sysdict = PyModule_GetDict(sysmod); if (interp->sysdict == NULL) Py_FatalError("Py_Initialize: can't initialize sys dict"); Py_INCREF(interp->sysdict); _PyImport_FixupExtension("sys", "sys"); PySys_SetPath(Py_GetPath()); PyDict_SetItemString(interp->sysdict, "modules", interp->modules); _PyImport_Init(); /* initialize builtin exceptions */ _PyExc_Init(); _PyImport_FixupExtension("exceptions", "exceptions"); /* phase 2 of builtins */ _PyImport_FixupExtension("__builtin__", "__builtin__"); _PyImportHooks_Init(); if (install_sigs) initsigs(); /* Signal handling stuff, including initintr() */ initmain(); /* Module __main__ */ if (!Py_NoSiteFlag) initsite(); /* Module site */ /* auto-thread-state API, if available */ #ifdef WITH_THREAD _PyGILState_Init(interp, tstate); #endif /* WITH_THREAD */ warnings_module = PyImport_ImportModule("warnings"); if (!warnings_module) PyErr_Clear(); #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) /* 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. Also set the encoding of stdin and stdout if these are terminals. */ saved_locale = strdup(setlocale(LC_CTYPE, NULL)); setlocale(LC_CTYPE, ""); codeset = nl_langinfo(CODESET); if (codeset && *codeset) { PyObject *enc = PyCodec_Encoder(codeset); if (enc) { codeset = strdup(codeset); Py_DECREF(enc); } else { codeset = NULL; PyErr_Clear(); } } else codeset = NULL; setlocale(LC_CTYPE, saved_locale); free(saved_locale); if (codeset) { sys_stream = PySys_GetObject("stdin"); sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); if (!sys_isatty) PyErr_Clear(); if(sys_isatty && PyObject_IsTrue(sys_isatty) && PyFile_Check(sys_stream)) { if (!PyFile_SetEncoding(sys_stream, codeset)) Py_FatalError("Cannot set codeset of stdin"); } Py_XDECREF(sys_isatty); sys_stream = PySys_GetObject("stdout"); sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); if (!sys_isatty) PyErr_Clear(); if(sys_isatty && PyObject_IsTrue(sys_isatty) && PyFile_Check(sys_stream)) { if (!PyFile_SetEncoding(sys_stream, codeset)) Py_FatalError("Cannot set codeset of stdout"); } Py_XDECREF(sys_isatty); sys_stream = PySys_GetObject("stderr"); sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); if (!sys_isatty) PyErr_Clear(); if(sys_isatty && PyObject_IsTrue(sys_isatty) && PyFile_Check(sys_stream)) { if (!PyFile_SetEncoding(sys_stream, codeset)) Py_FatalError("Cannot set codeset of stderr"); } Py_XDECREF(sys_isatty); if (!Py_FileSystemDefaultEncoding) Py_FileSystemDefaultEncoding = codeset; else free(codeset); } #endif }