/* PyInterpreterConfig API */ #include "Python.h" #include "pycore_pylifecycle.h" #include #include "config_common.h" static const char * gil_flag_to_str(int flag) { switch (flag) { case PyInterpreterConfig_DEFAULT_GIL: return "default"; case PyInterpreterConfig_SHARED_GIL: return "shared"; case PyInterpreterConfig_OWN_GIL: return "own"; default: PyErr_SetString(PyExc_SystemError, "invalid interpreter config 'gil' value"); return NULL; } } static int gil_flag_from_str(const char *str, int *p_flag) { int flag; if (str == NULL) { flag = PyInterpreterConfig_DEFAULT_GIL; } else if (strcmp(str, "default") == 0) { flag = PyInterpreterConfig_DEFAULT_GIL; } else if (strcmp(str, "shared") == 0) { flag = PyInterpreterConfig_SHARED_GIL; } else if (strcmp(str, "own") == 0) { flag = PyInterpreterConfig_OWN_GIL; } else { PyErr_Format(PyExc_ValueError, "unsupported interpreter config .gil value '%s'", str); return -1; } *p_flag = flag; return 0; } PyObject * _PyInterpreterConfig_AsDict(PyInterpreterConfig *config) { PyObject *dict = PyDict_New(); if (dict == NULL) { return NULL; } #define ADD(NAME, OBJ) \ do { \ int res = PyDict_SetItemString(dict, NAME, (OBJ)); \ Py_DECREF(OBJ); \ if (res < 0) { \ goto error; \ } \ } while (0) #define ADD_BOOL(FIELD) \ ADD(#FIELD, Py_NewRef(config->FIELD ? Py_True : Py_False)) #define ADD_STR(FIELD, STR) \ do { \ if (STR == NULL) { \ goto error; \ } \ PyObject *obj = PyUnicode_FromString(STR); \ if (obj == NULL) { \ goto error; \ } \ ADD(#FIELD, obj); \ } while (0) ADD_BOOL(use_main_obmalloc); ADD_BOOL(allow_fork); ADD_BOOL(allow_exec); ADD_BOOL(allow_threads); ADD_BOOL(allow_daemon_threads); ADD_BOOL(check_multi_interp_extensions); ADD_STR(gil, gil_flag_to_str(config->gil)); #undef ADD_STR #undef ADD_BOOL #undef ADD return dict; error: Py_DECREF(dict); return NULL; } static int _config_dict_get_bool(PyObject *dict, const char *name, int *p_flag) { PyObject *item; if (_config_dict_get(dict, name, &item) < 0) { return -1; } // For now we keep things strict, rather than using PyObject_IsTrue(). int flag = item == Py_True; if (!flag && item != Py_False) { Py_DECREF(item); config_dict_invalid_type(name); return -1; } Py_DECREF(item); *p_flag = flag; return 0; } static int _config_dict_copy_str(PyObject *dict, const char *name, char *buf, size_t bufsize) { PyObject *item; if (_config_dict_get(dict, name, &item) < 0) { return -1; } if (!PyUnicode_Check(item)) { Py_DECREF(item); config_dict_invalid_type(name); return -1; } strncpy(buf, PyUnicode_AsUTF8(item), bufsize-1); buf[bufsize-1] = '\0'; Py_DECREF(item); return 0; } static int interp_config_from_dict(PyObject *origdict, PyInterpreterConfig *config, bool missing_allowed) { PyObject *dict = PyDict_New(); if (dict == NULL) { return -1; } if (PyDict_Update(dict, origdict) < 0) { goto error; } #define CHECK(NAME) \ do { \ if (PyErr_Occurred()) { \ goto error; \ } \ else { \ if (!missing_allowed) { \ (void)config_dict_get(dict, NAME); \ assert(PyErr_Occurred()); \ goto error; \ } \ } \ } while (0) #define COPY_BOOL(FIELD) \ do { \ int flag; \ if (_config_dict_get_bool(dict, #FIELD, &flag) < 0) { \ CHECK(#FIELD); \ } \ else { \ config->FIELD = flag; \ (void)PyDict_PopString(dict, #FIELD, NULL); \ } \ } while (0) COPY_BOOL(use_main_obmalloc); COPY_BOOL(allow_fork); COPY_BOOL(allow_exec); COPY_BOOL(allow_threads); COPY_BOOL(allow_daemon_threads); COPY_BOOL(check_multi_interp_extensions); // PyInterpreterConfig.gil char buf[20]; if (_config_dict_copy_str(dict, "gil", buf, 20) < 0) { CHECK("gil"); } else { int flag; if (gil_flag_from_str(buf, &flag) < 0) { goto error; } config->gil = flag; (void)PyDict_PopString(dict, "gil", NULL); } #undef COPY_BOOL #undef CHECK Py_ssize_t unused = PyDict_GET_SIZE(dict); if (unused == 1) { PyErr_Format(PyExc_ValueError, "config dict has 1 extra item (%R)", dict/**************************************************************************** ** ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the documentation of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** You may use this file under the terms of the BSD license as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor ** the names of its contributors may be used to endorse or promote ** products derived from this software without specific prior written ** permission. ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** $QT_END_LICENSE$ ** ****************************************************************************/ //! [0] var ba = new ByteArray(); // constructs an empty ByteArray var ba2 = new ByteArray(10); // constructs a ByteArray of length 10 (all bytes initialized to 0) //! [0] //! [1] for (var i = 0; i < ba.length; ++i) ba[i] = 123; //! [1] //! [2] ba[0] = 257; print(ba[0]); // 1 //! [2] //! [3] var ba3 = new ByteArray(); print(ba3.length); // 0 ba[0] = 64; print(ba3.length); // 1 //! [3] //! [4] ba["foo"] = "Hello"; //! [4] //! [5] var ba64 = ba.toBase64(); print(ba64.toLatin1String()); //! [5]