Example #1
0
int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThreadState *interpreter, int app_type) {

    PyObject *app_list = NULL, *applications = NULL;


    if (uwsgi_apps_cnt >= uwsgi.max_apps) {
        uwsgi_log("ERROR: you cannot load more than %d apps in a worker\n", uwsgi.max_apps);
        return -1;
    }


    int id = uwsgi_apps_cnt;

    int multiapp = 0;

    int i;

    struct uwsgi_app *wi;

    time_t now = uwsgi_now();

    if (uwsgi_get_app_id(NULL, wsgi_req->appid, wsgi_req->appid_len, -1) != -1) {
        uwsgi_log( "mountpoint %.*s already configured. skip.\n", wsgi_req->appid_len, wsgi_req->appid);
        return -1;
    }

    wi = &uwsgi_apps[id];

    memset(wi, 0, sizeof(struct uwsgi_app));
    wi->modifier1 = python_plugin.modifier1;
    wi->mountpoint_len = wsgi_req->appid_len < 0xff ? wsgi_req->appid_len : (0xff-1);
    strncpy(wi->mountpoint, wsgi_req->appid, wi->mountpoint_len);

    // dynamic chdir ?
    if (wsgi_req->chdir_len > 0) {
        strncpy(wi->chdir, wsgi_req->chdir, wsgi_req->chdir_len < 0xff ? wsgi_req->chdir_len : (0xff-1));
#ifdef UWSGI_DEBUG
        uwsgi_debug("chdir to %s\n", wi->chdir);
#endif
        if (chdir(wi->chdir)) {
            uwsgi_error("chdir()");
        }
    }

    // Initialize a new environment for the new interpreter

    // reload "os" environ to allow dynamic setenv()
    if (up.reload_os_env) {

        char **e, *p;
        PyObject *k, *env_value;

        PyObject *os_module = PyImport_ImportModule("os");
        if (os_module) {
            PyObject *os_module_dict = PyModule_GetDict(os_module);
            PyObject *py_environ = PyDict_GetItemString(os_module_dict, "environ");
            if (py_environ) {
                for (e = environ; *e != NULL; e++) {
                    p = strchr(*e, '=');
                    if (p == NULL) continue;

                    k = PyString_FromStringAndSize(*e, (int)(p-*e));
                    if (k == NULL) {
                        PyErr_Print();
                        continue;
                    }

                    env_value = PyString_FromString(p+1);
                    if (env_value == NULL) {
                        PyErr_Print();
                        Py_DECREF(k);
                        continue;
                    }

#ifdef UWSGI_DEBUG
                    uwsgi_log("%s = %s\n", PyString_AsString(k), PyString_AsString(env_value));
#endif

                    if (PyObject_SetItem(py_environ, k, env_value)) {
                        PyErr_Print();
                    }

                    Py_DECREF(k);
                    Py_DECREF(env_value);

                }

            }
        }
    }

    if (interpreter == NULL && id) {

        wi->interpreter = Py_NewInterpreter();
        if (!wi->interpreter) {
            uwsgi_log( "unable to initialize the new python interpreter\n");
            exit(1);
        }
        PyThreadState_Swap(wi->interpreter);
        init_pyargv();

        // we need to inizialize an embedded module for every interpreter
        init_uwsgi_embedded_module();
        init_uwsgi_vars();

    }
    else if (interpreter) {
        wi->interpreter = interpreter;
    }
    else {
        wi->interpreter = up.main_thread;
    }

    if (wsgi_req->home_len) {
        set_dyn_pyhome(wsgi_req->home, wsgi_req->home_len);
    }

    if (wsgi_req->touch_reload_len > 0 && wsgi_req->touch_reload_len < 0xff) {
        struct stat trst;
        strncpy(wi->touch_reload, wsgi_req->touch_reload, wsgi_req->touch_reload_len);
        if (!stat(wi->touch_reload, &trst)) {
            wi->touch_reload_mtime = trst.st_mtime;
        }
    }

    wi->callable = up.loaders[loader](arg1);

    if (!wi->callable) {
        uwsgi_log("unable to load app %d (mountpoint='%s') (callable not found or import error)\n", id, wi->mountpoint);
        goto doh;
    }

    // the module contains multiple apps
    if (PyDict_Check((PyObject *)wi->callable)) {
        applications = wi->callable;
        uwsgi_log("found a multiapp module...\n");
        app_list = PyDict_Keys(applications);
        multiapp = PyList_Size(app_list);
        if (multiapp < 1) {
            uwsgi_log("you have to define at least one app in the apllications dictionary\n");
            goto doh;
        }

        PyObject *app_mnt = PyList_GetItem(app_list, 0);
        if (!PyString_Check(app_mnt)) {
            uwsgi_log("the app mountpoint must be a string\n");
            goto doh;
        }
        char *tmp_mountpoint = PyString_AsString(app_mnt);
        wi->mountpoint_len = strlen(wi->mountpoint) < 0xff ? strlen(wi->mountpoint) : (0xff-1);
        strncpy(wi->mountpoint, tmp_mountpoint, wi->mountpoint_len);
        wsgi_req->appid = wi->mountpoint;
        wsgi_req->appid_len = wi->mountpoint_len;
#ifdef UWSGI_DEBUG
        uwsgi_log("main mountpoint = %s\n", wi->mountpoint);
#endif
        wi->callable = PyDict_GetItem(applications, app_mnt);
        if (PyString_Check((PyObject *) wi->callable)) {
            PyObject *callables_dict = get_uwsgi_pydict((char *)arg1);
            if (callables_dict) {
                wi->callable = PyDict_GetItem(callables_dict, (PyObject *)wi->callable);
            }
        }
    }

    Py_INCREF((PyObject *)wi->callable);

    wi->environ = malloc(sizeof(PyObject*)*uwsgi.cores);
    if (!wi->environ) {
        uwsgi_error("malloc()");
        exit(1);
    }

    for(i=0; i<uwsgi.cores; i++) {
        wi->environ[i] = PyDict_New();
        if (!wi->environ[i]) {
            uwsgi_log("unable to allocate new env dictionary for app\n");
            exit(1);
        }
    }

    wi->argc = 1;

    if (app_type == PYTHON_APP_TYPE_WSGI) {
#ifdef UWSGI_DEBUG
        uwsgi_log("-- WSGI callable selected --\n");
#endif
        wi->request_subhandler = uwsgi_request_subhandler_wsgi;
        wi->response_subhandler = uwsgi_response_subhandler_wsgi;
        wi->argc = 2;
    }
    else if (app_type == PYTHON_APP_TYPE_WEB3) {
#ifdef UWSGI_DEBUG
        uwsgi_log("-- Web3 callable selected --\n");
#endif
        wi->request_subhandler = uwsgi_request_subhandler_web3;
        wi->response_subhandler = uwsgi_response_subhandler_web3;
    }
    else if (app_type == PYTHON_APP_TYPE_PUMP) {
#ifdef UWSGI_DEBUG
        uwsgi_log("-- Pump callable selected --\n");
#endif
        wi->request_subhandler = uwsgi_request_subhandler_pump;
        wi->response_subhandler = uwsgi_response_subhandler_pump;
    }

    wi->args = malloc(sizeof(PyObject*)*uwsgi.cores);
    if (!wi->args) {
        uwsgi_error("malloc()");
        exit(1);
    }

    for(i=0; i<uwsgi.cores; i++) {
        wi->args[i] = PyTuple_New(wi->argc);
        if (!wi->args[i]) {
            uwsgi_log("unable to allocate new tuple for app args\n");
            exit(1);
        }

        // add start_response on WSGI app
        Py_INCREF((PyObject *)up.wsgi_spitout);
        if (app_type == PYTHON_APP_TYPE_WSGI) {
            if (PyTuple_SetItem(wi->args[i], 1, up.wsgi_spitout)) {
                uwsgi_log("unable to set start_response in args tuple\n");
                exit(1);
            }
        }
    }

    if (app_type == PYTHON_APP_TYPE_WSGI) {
        // prepare sendfile() for WSGI app
        wi->sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL);

        wi->eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL);
        wi->eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL);
    }

    // cache most used values
    wi->error = PyFile_FromFile(stderr, "wsgi_errors", "w", NULL);
    Py_INCREF((PyObject *)wi->error);

    wi->gateway_version = PyTuple_New(2);
    PyTuple_SetItem(wi->gateway_version, 0, PyInt_FromLong(1));
    PyTuple_SetItem(wi->gateway_version, 1, PyInt_FromLong(0));
    Py_INCREF((PyObject *)wi->gateway_version);

    wi->uwsgi_version = PyString_FromString(UWSGI_VERSION);
    Py_INCREF((PyObject *)wi->uwsgi_version);

    wi->uwsgi_node = PyString_FromString(uwsgi.hostname);
    Py_INCREF((PyObject *)wi->uwsgi_node);

    if (uwsgi.threads > 1 && id) {
        // if we have multiple threads we need to initialize a PyThreadState for each one
        for(i=0; i<uwsgi.threads; i++) {
            //uwsgi_log("%p\n", uwsgi.core[i]->ts[id]);
            uwsgi.workers[uwsgi.mywid].cores[i].ts[id] = PyThreadState_New( ((PyThreadState *)wi->interpreter)->interp);
            if (!uwsgi.workers[uwsgi.mywid].cores[i].ts[id]) {
                uwsgi_log("unable to allocate new PyThreadState structure for app %s", wi->mountpoint);
                goto doh;
            }
        }
        PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key) );
    }
    else if (interpreter == NULL && id) {
        PyThreadState_Swap(up.main_thread);
    }

    const char *default_app = "";

    if ((wsgi_req->appid_len == 0 || (wsgi_req->appid_len = 1 && wsgi_req->appid[0] == '/')) && uwsgi.default_app == -1) {
        default_app = " (default app)" ;
        uwsgi.default_app = id;
    }

    wi->started_at = now;
    wi->startup_time = uwsgi_now() - now;

    if (app_type == PYTHON_APP_TYPE_WSGI) {
        uwsgi_log( "WSGI app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app);
    }
    else if (app_type == PYTHON_APP_TYPE_WEB3) {
        uwsgi_log( "Web3 app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app);
    }
    else if (app_type == PYTHON_APP_TYPE_PUMP) {
        uwsgi_log( "Pump app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app);
    }


    uwsgi_apps_cnt++;

    if (multiapp > 1) {
        for(i=1; i<multiapp; i++) {
            PyObject *app_mnt = PyList_GetItem(app_list, i);
            if (!PyString_Check(app_mnt)) {
                uwsgi_log("applications dictionary key must be a string, skipping.\n");
                continue;
            }

            wsgi_req->appid = PyString_AsString(app_mnt);
            wsgi_req->appid_len = strlen(wsgi_req->appid);
            PyObject *a_callable = PyDict_GetItem(applications, app_mnt);
            if (PyString_Check(a_callable)) {

                PyObject *callables_dict = get_uwsgi_pydict((char *)arg1);
                if (callables_dict) {
                    a_callable = PyDict_GetItem(callables_dict, a_callable);
                }
            }
            if (!a_callable) {
                uwsgi_log("skipping broken app %s\n", wsgi_req->appid);
                continue;
            }
            init_uwsgi_app(LOADER_CALLABLE, a_callable, wsgi_req, wi->interpreter, app_type);
        }
    }

    // emulate COW
    uwsgi_emulate_cow_for_apps(id);

    return id;

doh:
    if (PyErr_Occurred())
        PyErr_Print();
    if (interpreter == NULL && id) {
        Py_EndInterpreter(wi->interpreter);
        if (uwsgi.threads > 1) {
            PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key));
        }
        else {
            PyThreadState_Swap(up.main_thread);
        }
    }
    return -1;
}
Example #2
0
 bool addType(const char *name, PyObject *klass) {
   
   bool singleton = false;
   std::string desc;
   
   PyObject *sg = PyObject_GetAttrString(klass, "Singleton");
   if (!sg) {
     PyErr_Clear();
   } else {
     if (PyBool_Check(sg)) {
       singleton = (sg == Py_True);
     }
     Py_DECREF(sg);
   }
   
   PyObject *de = PyObject_GetAttrString(klass, "Description");
   if (!de) {
     PyErr_Clear();
   } else {
     if (PyString_Check(de)) {
       desc = PyString_AsString(de);
     }
     Py_DECREF(de);
   }
   
   PyObject *mt = PyObject_GetAttrString(klass, "Methods");
   
   if (!mt || !PyDict_Check(mt)) {
     std::cout << "pyloader: Missing \"Methods\" class member" << std::endl;
     Py_XDECREF(mt);
     return false;
   }
   
   if (PyDict_Size(mt) == 0) {
     std::cout << "pyloader: No keys in \"Methods\" dict" << std::endl;
     Py_DECREF(mt);
     return false;
   }
   
   lwc::MethodsTable *typeMethods = 0;
   
   PyObject *key;
   PyObject *value;
   Py_ssize_t pos = 0;
   
   while (PyDict_Next(mt, &pos, &key, &value)) {
     
     if (!PyString_Check(key) || !PyTuple_Check(value)) {
       std::cout << "pyloader: \"Methods\" must be a dict of tuples" << std::endl;
       continue;
     }
     
     bool add = true;
     char *mname = PyString_AsString(key);
     char *mdesc = NULL;
     
     long n = PyTuple_Size(value);
     if (n < 1 || n > 2) {
       std::cout << "pyloader: \"Methods\" dict values must be tuples with 1 to 2 elements" << std::endl;
       continue;
     }
     
     PyObject *args = PyTuple_GetItem(value, 0);
     if (!PyList_Check(args)) {
       std::cout << "pyloader: \"Methods\" dict values' tuple first element must be a list" << std::endl;
       continue;
     }
     
     if (n == 2) {
       PyObject *pdesc = PyTuple_GetItem(value, 1);
       if (!PyString_Check(pdesc)) {
         std::cout << "pyloader: \"Methods\" dict values' tuple second element must be a string" << std::endl;
         continue;
       }
       mdesc = PyString_AsString(pdesc);
     }
     
     lwc::Method meth;
     
     if (mdesc) {
       meth.setDescription(mdesc);
     }
     
     n = PyList_Size(args);
     
     for (long i=0; i<n; ++i) {
       PyObject *arg = PyList_GetItem(args, i);
       Py_ssize_t ts = PyTuple_Size(arg);
       if (!PyTuple_Check(arg) || ts < 2 || ts > 6) {
         std::cout << "pyloader: Arguments must be tuples with 2 to 6 elements" << std::endl;
         add = false;
         break;
       }
       lwc::Argument a;
       a.setDir(lwc::Direction(PyInt_AsLong(PyTuple_GetItem(arg, 0))));
       a.setType(lwc::Type(PyInt_AsLong(PyTuple_GetItem(arg, 1))));
       if (ts >= 3) {
         a.setArraySizeArg(PyInt_AsLong(PyTuple_GetItem(arg, 2)));
       }
       if (ts >= 4) {
         PyObject *ahd = PyTuple_GetItem(arg, 3);
         if (!PyBool_Check(ahd)) {
           std::cout << "pyloader: Argument's tuple 4th element must be a boolean" << std::endl;
           add = false;
           break;
         }
         if (ahd == Py_True) {
           if (ts < 5) {
             std::cout << "pyloader: Argument is missing default value" << std::endl;
             add = false;
             break;
           } else {
             PyObject *pdv = PyTuple_GetItem(arg, 4);
             py::SetArgDefault(a, pdv);
           }
         }
         // hasDefault (yes, no)
         // if yes -> must have at least 5
       }
       if (ts >= 6) {
         PyObject *aname = PyTuple_GetItem(arg, 5);
         if (!PyString_Check(aname)) {
           std::cout << "pyloader: Arguments name must be a string" << std::endl;
           add = false;
           break;
         }
         a.setName(PyString_AsString(aname));
       }
       try {
         meth.addArg(a);
       } catch (std::exception &e) {
         std::cout << "pyloader: " << e.what() << std::endl;
         add = false;
         break;
       }
     }
     
     if (!add) {
       std::cout << "pyloader: Skipped method (1) \"" << mname << "\" for type \"" << name << "\"" << std::endl;
       continue;
     
     } else {
       if (!typeMethods) {
         PyTypeObject *to = (PyTypeObject*) klass;
         Py_ssize_t nbases = PyTuple_Size(to->tp_bases);
         if (nbases > 1) {
           std::cout << "pyloader: Skipped type \"" << name << "\" (Multiple inheritance not supported)" << std::endl;
           return false;
         }
         if (nbases == 1 && PyTuple_GetItem(to->tp_bases, 0) != (PyObject*)&py::PyLWCObjectType) {
           PyObject *pto = PyTuple_GetItem(to->tp_bases, 0);
           std::map<std::string, TypeEntry>::iterator it = mTypes.begin();
           while (it != mTypes.end()) {
             if (it->second.klass == pto) {
               typeMethods = new lwc::MethodsTable(it->second.methods);
               break;
             }
             ++it;
           }
           if (!typeMethods) {
             std::cout << "pyloader: Skipped type \"" << name << "\" (Super class not registered yet)" << std::endl;
             return false;
           }
         } else {
           typeMethods = new lwc::MethodsTable();
         }
       }
       try {
         typeMethods->addMethod(mname, meth);
       } catch (std::runtime_error &e) {
         std::cout << "pyloader: Skipped method (2) \"" << mname << "\" for type \"" << name << "\"" << std::endl;
         std::cout << e.what() << std::endl;
         continue;
       }
     }
   }
   
   Py_DECREF(mt);
   
   if (typeMethods) {
     if (typeMethods->numMethods() == 0) {
       std::cout << "pyloader: MethodsTable is empty for type \"" << name << "\"" << std::endl;
       delete typeMethods;
       return false;
     } else {
       TypeEntry te;
       Py_INCREF(klass);
       te.klass = klass;
       te.singleton = singleton;
       te.methods = typeMethods;
       te.desc = desc;
       mTypes[name] = te;
       return true;
     }
   } else {
     std::cout << "pyloader: MethodsTable not allocated for type \"" << name << "\"" << std::endl;
     return false;
   }
   
 }
Example #3
0
File: pydsdb.c Project: GSam/samba
/*
  convert a python string to a DRSUAPI drsuapi_DsReplicaAttribute attribute
 */
static PyObject *py_dsdb_DsReplicaAttribute(PyObject *self, PyObject *args)
{
	PyObject *py_ldb, *el_list, *ret;
	struct ldb_context *ldb;
	char *ldap_display_name;
	const struct dsdb_attribute *a;
	struct dsdb_schema *schema;
	struct dsdb_syntax_ctx syntax_ctx;
	struct ldb_message_element *el;
	struct drsuapi_DsReplicaAttribute *attr;
	TALLOC_CTX *tmp_ctx;
	WERROR werr;
	Py_ssize_t i;

	if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
		return NULL;
	}

	PyErr_LDB_OR_RAISE(py_ldb, ldb);

	schema = dsdb_get_schema(ldb, NULL);
	if (!schema) {
		PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
		return NULL;
	}

	a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
	if (a == NULL) {
		PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
		return NULL;
	}

	dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
	syntax_ctx.is_schema_nc = false;

	tmp_ctx = talloc_new(ldb);
	if (tmp_ctx == NULL) {
		PyErr_NoMemory();
		return NULL;
	}

	/* If we were not given an LdbMessageElement */
	if (!PyList_Check(el_list)) {
		if (!py_check_dcerpc_type(el_list, "ldb", "MessageElement")) {
			PyErr_SetString(py_ldb_get_exception(),
					"list of strings or ldb MessageElement object required");
			return NULL;
		}
		/*
		 * NOTE:
		 * el may not be a valid talloc context, it
		 * could be part of an array
		 */
		el = pyldb_MessageElement_AsMessageElement(el_list);
	} else {
		el = talloc_zero(tmp_ctx, struct ldb_message_element);
		if (el == NULL) {
			PyErr_NoMemory();
			talloc_free(tmp_ctx);
			return NULL;
		}

		el->name = ldap_display_name;
		el->num_values = PyList_Size(el_list);

		el->values = talloc_array(el, struct ldb_val, el->num_values);
		if (el->values == NULL) {
			PyErr_NoMemory();
			talloc_free(tmp_ctx);
			return NULL;
		}

		for (i = 0; i < el->num_values; i++) {
			PyObject *item = PyList_GetItem(el_list, i);
			if (!PyString_Check(item)) {
				PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
				talloc_free(tmp_ctx);
				return NULL;
			}
			el->values[i].data = (uint8_t *)PyString_AsString(item);
			el->values[i].length = PyString_Size(item);
		}
	}

	attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
	if (attr == NULL) {
		PyErr_NoMemory();
		talloc_free(tmp_ctx);
		return NULL;
	}

	werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
	PyErr_WERROR_NOT_OK_RAISE(werr);

	ret = py_return_ndr_struct("samba.dcerpc.drsuapi", "DsReplicaAttribute", attr, attr);

	talloc_free(tmp_ctx);

	return ret;
}
QString QgsPythonUtilsImpl::getTraceback()
{
#define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;}

  // acquire global interpreter lock to ensure we are in a consistent state
  PyGILState_STATE gstate;
  gstate = PyGILState_Ensure();

  QString errMsg;
  QString result;

  PyObject *modStringIO = NULL;
  PyObject *modTB = NULL;
  PyObject *obStringIO = NULL;
  PyObject *obResult = NULL;

  PyObject *type, *value, *traceback;

  PyErr_Fetch( &type, &value, &traceback );
  PyErr_NormalizeException( &type, &value, &traceback );

  modStringIO = PyImport_ImportModule( "cStringIO" );
  if ( modStringIO == NULL )
    TRACEBACK_FETCH_ERROR( "can't import cStringIO" );

  obStringIO = PyObject_CallMethod( modStringIO, ( char* ) "StringIO", NULL );

  /* Construct a cStringIO object */
  if ( obStringIO == NULL )
    TRACEBACK_FETCH_ERROR( "cStringIO.StringIO() failed" );

  modTB = PyImport_ImportModule( "traceback" );
  if ( modTB == NULL )
    TRACEBACK_FETCH_ERROR( "can't import traceback" );

  obResult = PyObject_CallMethod( modTB, ( char* ) "print_exception",
                                  ( char* ) "OOOOO",
                                  type, value ? value : Py_None,
                                  traceback ? traceback : Py_None,
                                  Py_None,
                                  obStringIO );

  if ( obResult == NULL )
    TRACEBACK_FETCH_ERROR( "traceback.print_exception() failed" );
  Py_DECREF( obResult );

  obResult = PyObject_CallMethod( obStringIO, ( char* ) "getvalue", NULL );
  if ( obResult == NULL )
    TRACEBACK_FETCH_ERROR( "getvalue() failed." );

  /* And it should be a string all ready to go - duplicate it. */
  if ( !PyString_Check( obResult ) )
    TRACEBACK_FETCH_ERROR( "getvalue() did not return a string" );

  result = PyString_AsString( obResult );

done:

  // All finished - first see if we encountered an error
  if ( result.isEmpty() && !errMsg.isEmpty() )
  {
    result = errMsg;
  }

  Py_XDECREF( modStringIO );
  Py_XDECREF( modTB );
  Py_XDECREF( obStringIO );
  Py_XDECREF( obResult );
  Py_XDECREF( value );
  Py_XDECREF( traceback );
  Py_XDECREF( type );

  // we are done calling python API, release global interpreter lock
  PyGILState_Release( gstate );

  return result;
}
Example #5
0
RowSet *
ConvertPythonToRowset(PyObject * v)
{
    RowSet *pRow;
    Py_ssize_t row, col;
    int i, j;
    if (PyInt_Check(v)) {
        if (PyInt_AsLong(v) != 0)
            outputerrf(_("unexpected rowset error"));
        return NULL;
    }

    if (!PySequence_Check(v)) {
        outputerrf(_("invalid Python return"));
        return NULL;
    }

    row = PySequence_Size(v);
    col = 0;
    if (row > 0) {
        PyObject *cols = PySequence_GetItem(v, 0);
        if (!PySequence_Check(cols)) {
            outputerrf(_("invalid Python return"));
            return NULL;
        } else
            col = PySequence_Size(cols);
    }

    pRow = MallocRowset((size_t) row, (size_t) col);
    for (i = 0; i < (int) pRow->rows; i++) {
        PyObject *e = PySequence_GetItem(v, i);

        if (!e) {
            outputf(_("Error getting item %d\n"), i);
            continue;
        }

        if (PySequence_Check(e)) {
            for (j = 0; j < (int) pRow->cols; j++) {
                char buf[1024];
                PyObject *e2 = PySequence_GetItem(e, j);

                if (!e2) {
                    outputf(_("Error getting sub item (%d, %d)\n"), i, j);
                    continue;
                }
                if (PyUnicode_Check(e2))
                    strcpy(buf, PyString_AsString(PyUnicode_AsUTF8String(e2)));
                else if (PyString_Check(e2))
                    strcpy(buf, PyString_AsString(e2));
                else if (PyInt_Check(e2) || PyLong_Check(e2)
                         || !StrCaseCmp(e2->ob_type->tp_name, "Decimal"))       /* Not sure how to check for decimal type directly */
                    sprintf(buf, "%d", (int) PyInt_AsLong(e2));
                else if (PyFloat_Check(e2))
                    sprintf(buf, "%.4f", PyFloat_AsDouble(e2));
                else if (e2 == Py_None)
                    sprintf(buf, "[%s]", _("none"));
                else
                    sprintf(buf, "[%s]", _("unknown type"));

                SetRowsetData(pRow, (size_t) i, (size_t) j, buf);

                Py_DECREF(e2);
            }
        } else {
            outputf(_("Item %d is not a list\n"), i);
        }

        Py_DECREF(e);
    }
    return pRow;
}
static PyObject*
automaton_search_iter_set(PyObject* self, PyObject* args) {
	PyObject* object;
	PyObject* flag;
	ssize_t len;
	bool reset;

	// first argument - required string or buffer
	object = PyTuple_GetItem(args, 0);
	if (object) {
#ifdef PY3K
    #ifdef AHOCORASICK_UNICODE
        if (PyUnicode_Check(object))
            len = PyUnicode_GET_SIZE(object);
        else {
            PyErr_SetString(PyExc_TypeError, "string required");
            return NULL;
        }
    #else
        if (PyBytes_Check(object))
            len = PyBytes_GET_SIZE(object);
        else {
            PyErr_SetString(PyExc_TypeError, "string or bytes object required");
            return NULL;
        }
    #endif
#else
        if (PyString_Check(object)) {
            len = PyString_GET_SIZE(object);
        } else {
            PyErr_SetString(PyExc_TypeError, "string required 2");
            return NULL;
        }
#endif
	}
	else
		return NULL;

	// second argument - optional bool
	flag = PyTuple_GetItem(args, 1);
	if (flag) {
		switch (PyObject_IsTrue(flag)) {
			case 0:
				reset = false;
				break;
			case 1:
				reset = true;
				break;
			default:
				return NULL;
		}
	}
	else {
		PyErr_Clear();
		reset = false;
	}

	// update internal state
	Py_XDECREF(iter->object);
	Py_INCREF(object);
	iter->object	= object;
#ifdef AHOCORASICK_UNICODE
	iter->data = PyUnicode_AS_UNICODE(object);
#else
	iter->data = (uint8_t*)PyBytes_AS_STRING(object);
#endif

	if (!reset)
		iter->shift += (iter->index >= 0) ? iter->index : 0;

	iter->index		= -1;
	iter->end		= (int)len;

	if (reset) {
		iter->state  = iter->automaton->root;
		iter->shift  = 0;
		iter->output = NULL;
	}

	Py_RETURN_NONE;
}
Example #7
0
static int
tb_displayline(PyObject *f, char *filename, int lineno, char *name)
{
	int err = 0;
	FILE *xfp;
	char linebuf[2000];
	int i;
	if (filename == NULL || name == NULL)
		return -1;
#ifdef MPW
	/* This is needed by MPW's File and Line commands */
#define FMT "  File \"%.500s\"; line %d # in %.500s\n"
#else
	/* This is needed by Emacs' compile command */
#define FMT "  File \"%.500s\", line %d, in %.500s\n"
#endif
	xfp = fopen(filename, "r");
	if (xfp == NULL) {
		/* Search tail of filename in sys.path before giving up */
		PyObject *path;
		char *tail = strrchr(filename, SEP);
		if (tail == NULL)
			tail = filename;
		else
			tail++;
		path = PySys_GetObject("path");
		if (path != NULL && PyList_Check(path)) {
			int npath = PyList_Size(path);
			size_t taillen = strlen(tail);
			char namebuf[MAXPATHLEN+1];
			for (i = 0; i < npath; i++) {
				PyObject *v = PyList_GetItem(path, i);
				if (v == NULL) {
					PyErr_Clear();
					break;
				}
				if (PyString_Check(v)) {
					size_t len;
					len = PyString_Size(v);
					if (len + 1 + taillen >= MAXPATHLEN)
						continue; /* Too long */
					strcpy(namebuf, PyString_AsString(v));
					if (strlen(namebuf) != len)
						continue; /* v contains '\0' */
					if (len > 0 && namebuf[len-1] != SEP)
						namebuf[len++] = SEP;
					strcpy(namebuf+len, tail);
					xfp = fopen(namebuf, "r");
					if (xfp != NULL) {
						filename = namebuf;
						break;
					}
				}
			}
		}
	}
	PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
	err = PyFile_WriteString(linebuf, f);
	if (xfp == NULL || err != 0)
		return err;
	for (i = 0; i < lineno; i++) {
		char* pLastChar = &linebuf[sizeof(linebuf)-2];
		do {
			*pLastChar = '\0';
			if (fgets(linebuf, sizeof linebuf, xfp) == NULL)
				break;
			/* fgets read *something*; if it didn't get as
			   far as pLastChar, it must have found a newline
			   or hit the end of the file;	if pLastChar is \n,
			   it obviously found a newline; else we haven't
			   yet seen a newline, so must continue */
		} while (*pLastChar != '\0' && *pLastChar != '\n');
	}
	if (i == lineno) {
		char *p = linebuf;
		while (*p == ' ' || *p == '\t' || *p == '\014')
			p++;
		err = PyFile_WriteString("    ", f);
		if (err == 0) {
			err = PyFile_WriteString(p, f);
			if (err == 0 && strchr(p, '\n') == NULL)
				err = PyFile_WriteString("\n", f);
		}
	}
	fclose(xfp);
	return err;
}
Example #8
0
/*
 * Extract a Python traceback from the current exception.
 *
 * The exception error message is returned in xmsg, the traceback in
 * tbmsg (both as palloc'd strings) and the traceback depth in
 * tb_depth.
 */
static void
PLy_traceback(char **xmsg, char **tbmsg, int *tb_depth)
{
	PyObject   *e,
			   *v,
			   *tb;
	PyObject   *e_type_o;
	PyObject   *e_module_o;
	char	   *e_type_s = NULL;
	char	   *e_module_s = NULL;
	PyObject   *vob = NULL;
	char	   *vstr;
	StringInfoData xstr;
	StringInfoData tbstr;

	/*
	 * get the current exception
	 */
	PyErr_Fetch(&e, &v, &tb);

	/*
	 * oops, no exception, return
	 */
	if (e == NULL)
	{
		*xmsg = NULL;
		*tbmsg = NULL;
		*tb_depth = 0;

		return;
	}

	PyErr_NormalizeException(&e, &v, &tb);

	/*
	 * Format the exception and its value and put it in xmsg.
	 */

	e_type_o = PyObject_GetAttrString(e, "__name__");
	e_module_o = PyObject_GetAttrString(e, "__module__");
	if (e_type_o)
		e_type_s = PyString_AsString(e_type_o);
	if (e_type_s)
		e_module_s = PyString_AsString(e_module_o);

	if (v && ((vob = PyObject_Str(v)) != NULL))
		vstr = PyString_AsString(vob);
	else
		vstr = "unknown";

	initStringInfo(&xstr);
	if (!e_type_s || !e_module_s)
	{
		if (PyString_Check(e))
			/* deprecated string exceptions */
			appendStringInfoString(&xstr, PyString_AsString(e));
		else
			/* shouldn't happen */
			appendStringInfoString(&xstr, "unrecognized exception");
	}
	/* mimics behavior of traceback.format_exception_only */
	else if (strcmp(e_module_s, "builtins") == 0
			 || strcmp(e_module_s, "__main__") == 0
			 || strcmp(e_module_s, "exceptions") == 0)
		appendStringInfo(&xstr, "%s", e_type_s);
	else
		appendStringInfo(&xstr, "%s.%s", e_module_s, e_type_s);
	appendStringInfo(&xstr, ": %s", vstr);

	*xmsg = xstr.data;

	/*
	 * Now format the traceback and put it in tbmsg.
	 */

	*tb_depth = 0;
	initStringInfo(&tbstr);
	/* Mimick Python traceback reporting as close as possible. */
	appendStringInfoString(&tbstr, "Traceback (most recent call last):");
	while (tb != NULL && tb != Py_None)
	{
		PyObject   *volatile tb_prev = NULL;
		PyObject   *volatile frame = NULL;
		PyObject   *volatile code = NULL;
		PyObject   *volatile name = NULL;
		PyObject   *volatile lineno = NULL;
		PyObject   *volatile filename = NULL;

		PG_TRY();
		{
			lineno = PyObject_GetAttrString(tb, "tb_lineno");
			if (lineno == NULL)
				elog(ERROR, "could not get line number from Python traceback");

			frame = PyObject_GetAttrString(tb, "tb_frame");
			if (frame == NULL)
				elog(ERROR, "could not get frame from Python traceback");

			code = PyObject_GetAttrString(frame, "f_code");
			if (code == NULL)
				elog(ERROR, "could not get code object from Python frame");

			name = PyObject_GetAttrString(code, "co_name");
			if (name == NULL)
				elog(ERROR, "could not get function name from Python code object");

			filename = PyObject_GetAttrString(code, "co_filename");
			if (filename == NULL)
				elog(ERROR, "could not get file name from Python code object");
		}
		PG_CATCH();
		{
			Py_XDECREF(frame);
			Py_XDECREF(code);
			Py_XDECREF(name);
			Py_XDECREF(lineno);
			Py_XDECREF(filename);
			PG_RE_THROW();
		}
		PG_END_TRY();

		/* The first frame always points at <module>, skip it. */
		if (*tb_depth > 0)
		{
			PLyExecutionContext	*exec_ctx = PLy_current_execution_context();
			char	   *proname;
			char	   *fname;
			char	   *line;
			char	   *plain_filename;
			long		plain_lineno;

			/*
			 * The second frame points at the internal function, but to mimick
			 * Python error reporting we want to say <module>.
			 */
			if (*tb_depth == 1)
				fname = "<module>";
			else
				fname = PyString_AsString(name);

			proname = PLy_procedure_name(exec_ctx->curr_proc);
			plain_filename = PyString_AsString(filename);
			plain_lineno = PyInt_AsLong(lineno);

			if (proname == NULL)
				appendStringInfo(
				&tbstr, "\n  PL/Python anonymous code block, line %ld, in %s",
								 plain_lineno - 1, fname);
			else
				appendStringInfo(
					&tbstr, "\n  PL/Python function \"%s\", line %ld, in %s",
								 proname, plain_lineno - 1, fname);

			/*
			 * function code object was compiled with "<string>" as the
			 * filename
			 */
			if (exec_ctx->curr_proc && plain_filename != NULL &&
				strcmp(plain_filename, "<string>") == 0)
			{
				/*
				 * If we know the current procedure, append the exact line
				 * from the source, again mimicking Python's traceback.py
				 * module behavior.  We could store the already line-split
				 * source to avoid splitting it every time, but producing a
				 * traceback is not the most important scenario to optimize
				 * for.  But we do not go as far as traceback.py in reading
				 * the source of imported modules.
				 */
				line = get_source_line(exec_ctx->curr_proc->src, plain_lineno);
				if (line)
				{
					appendStringInfo(&tbstr, "\n    %s", line);
					pfree(line);
				}
			}
		}

		Py_DECREF(frame);
		Py_DECREF(code);
		Py_DECREF(name);
		Py_DECREF(lineno);
		Py_DECREF(filename);

		/* Release the current frame and go to the next one. */
		tb_prev = tb;
		tb = PyObject_GetAttrString(tb, "tb_next");
		Assert(tb_prev != Py_None);
		Py_DECREF(tb_prev);
		if (tb == NULL)
			elog(ERROR, "could not traverse Python traceback");
		(*tb_depth)++;
	}

	/* Return the traceback. */
	*tbmsg = tbstr.data;

	Py_XDECREF(e_type_o);
	Py_XDECREF(e_module_o);
	Py_XDECREF(vob);
	Py_XDECREF(v);
	Py_DECREF(e);
}
Example #9
0
static PyObject *cboodle_init(PyObject *self, PyObject *args)
{
    char *devname = NULL;
    int ratewanted = 0;
    int verbose = 0;
    int ix, res;
    PyObject *extras = NULL;
    extraopt_t *opts = NULL;
    extraopt_t dummyopt = {NULL, NULL};

    if (!PyArg_ParseTuple(args, "|ziiO:init", &devname, &ratewanted, &verbose, &extras))
        return NULL;

    res = noteq_init();
    if (!res) {
        PyErr_SetString(PyExc_IOError, "unable to initialize note queue");
        return NULL;
    }

    if (extras && PyList_Check(extras)) {
        int count = PyList_Size(extras);

        opts = (extraopt_t *)malloc(sizeof(extraopt_t) * (count+1));
        if (!opts) {
            PyErr_SetString(PyExc_IOError, "unable to initialize extra options");
            return NULL;
        }

        for (ix=0; ix<count; ix++) {
            PyObject *tup = PyList_GetItem(extras, ix);
            PyObject *tkey, *tval;
            if (!tup)
                return NULL;
            if (!PyTuple_Check(tup) || PyTuple_Size(tup) != 2) {
                PyErr_SetString(PyExc_TypeError, "extraopts must be a list of 2-tuples");
                return NULL;
            }

            tkey = PyTuple_GetItem(tup, 0);
            if (!tkey)
                return NULL;
            tval = PyTuple_GetItem(tup, 1);
            if (!tval)
                return NULL;
            if (!PyString_Check(tkey)
                    || !(tval == Py_None || PyString_Check(tval))) {
                PyErr_SetString(PyExc_TypeError, "extraopts must be (string, string) or (string, None)");
                return NULL;
            }

            opts[ix].key = PyString_AsString(tkey);
            if (tval == Py_None)
                opts[ix].val = NULL;
            else
                opts[ix].val = PyString_AsString(tval);
        }

        opts[count].key = NULL;
        opts[count].val = NULL;
    }

    res = audev_init_device(devname, ratewanted, (verbose!=0),
                            (opts?opts:(&dummyopt)));
    if (!res) {
        PyErr_SetString(PyExc_IOError, "unable to initialize audio device");
        if (opts) {
            free(opts);
        }
        return NULL;
    }

    if (opts) {
        free(opts);
    }

    Py_INCREF(Py_None);
    return Py_None;
}
static PyObject *
wrap_getattro(PyObject *self, PyObject *name)
{
    PyObject *wrapped;
    PyObject *descriptor;
    PyObject *res = NULL;
    char *name_as_string;
    int maybe_special_name;

#ifdef Py_USING_UNICODE
    /* The Unicode to string conversion is done here because the
       existing tp_getattro slots expect a string object as name
       and we wouldn't want to break those. */
    if (PyUnicode_Check(name)) {
        name = PyUnicode_AsEncodedString(name, NULL, NULL);
        if (name == NULL)
            return NULL;
    }
    else
#endif
    if (!PyString_Check(name)){
        PyErr_SetString(PyExc_TypeError, "attribute name must be string");
        return NULL;
    }
    else
        Py_INCREF(name);

    name_as_string = PyString_AS_STRING(name);
    wrapped = Proxy_GET_OBJECT(self);
    if (wrapped == NULL) {
        PyErr_Format(PyExc_RuntimeError,
            "object is NULL; requested to get attribute '%s'",
            name_as_string);
        goto finally;
    }

    maybe_special_name = name_as_string[0] == '_' && name_as_string[1] == '_';

    if (!(maybe_special_name && strcmp(name_as_string, "__class__") == 0)) {

        descriptor = WrapperType_Lookup(self->ob_type, name);

        if (descriptor != NULL) {
            if (PyType_HasFeature(descriptor->ob_type, Py_TPFLAGS_HAVE_CLASS)
                && descriptor->ob_type->tp_descr_get != NULL) {
                res = descriptor->ob_type->tp_descr_get(
                        descriptor,
                        self,
                        (PyObject *)self->ob_type);
            } else {
                Py_INCREF(descriptor);
                res = descriptor;
            }
            goto finally;
        }
    }
    res = PyObject_GetAttr(wrapped, name);

finally:
    Py_DECREF(name);
    return res;
}
Example #11
0
void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
{
	painter.clip(eRect(offset, m_itemsize));

	int marked = 0;

	if (m_current_marked && selected)
		marked = 2;
	else if (cursorValid() && isMarked(*m_cursor))
	{
		if (selected)
			marked = 2;
		else
			marked = 1;
	}
	else
		style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);

	eListboxStyle *local_style = 0;

		/* get local listbox style, if present */
	if (m_listbox)
		local_style = m_listbox->getLocalStyle();

	if (marked == 1)  // marked
	{
		style.setStyle(painter, eWindowStyle::styleListboxMarked);
		if (m_color_set[markedForeground])
			painter.setForegroundColor(m_color[markedForeground]);
		if (m_color_set[markedBackground])
			painter.setBackgroundColor(m_color[markedBackground]);
	}
	else if (marked == 2) // marked and selected
	{
		style.setStyle(painter, eWindowStyle::styleListboxMarkedAndSelected);
		if (m_color_set[markedForegroundSelected])
			painter.setForegroundColor(m_color[markedForegroundSelected]);
		if (m_color_set[markedBackgroundSelected])
			painter.setBackgroundColor(m_color[markedBackgroundSelected]);
	}
	else if (local_style)
	{
		if (selected)
		{
			/* if we have a local background color set, use that. */
			if (local_style->m_background_color_selected_set)
				painter.setBackgroundColor(local_style->m_background_color_selected);
			/* same for foreground */
			if (local_style->m_foreground_color_selected_set)
				painter.setForegroundColor(local_style->m_foreground_color_selected);
		}
		else
		{
			/* if we have a local background color set, use that. */
			if (local_style->m_background_color_set)
				painter.setBackgroundColor(local_style->m_background_color);
			/* same for foreground */
			if (local_style->m_foreground_color_set)
				painter.setForegroundColor(local_style->m_foreground_color);
		}
	}

	if (!local_style || !local_style->m_transparent_background)
		/* if we have no transparent background */
	{
		/* blit background picture, if available (otherwise, clear only) */
		if (local_style && local_style->m_background)
			painter.blit(local_style->m_background, offset, eRect(), 0);
		else
			painter.clear();
	} else
	{
		if (local_style->m_background)
			painter.blit(local_style->m_background, offset, eRect(), gPainter::BT_ALPHABLEND);
		else if (selected && !local_style->m_selection)
			painter.clear();
	}

	if (cursorValid())
	{
		/* get service information */
		ePtr<iStaticServiceInformation> service_info;
		m_service_center->info(*m_cursor, service_info);
		eServiceReference ref = *m_cursor;
		bool isMarker = ref.flags & eServiceReference::isMarker;
		bool isPlayable = !(ref.flags & eServiceReference::isDirectory || isMarker);
		bool isRecorded = m_record_indicator_mode && isPlayable && checkServiceIsRecorded(ref,pNavigation::RecordType(pNavigation::isRealRecording|pNavigation::isUnknownRecording));
		bool isStreamed = m_record_indicator_mode && isPlayable && checkServiceIsRecorded(ref,pNavigation::isStreaming);
		bool isPseudoRecorded = m_record_indicator_mode && isPlayable && checkServiceIsRecorded(ref,pNavigation::isPseudoRecording);
		ePtr<eServiceEvent> evt;
		bool serviceAvail = true;
		bool serviceFallback = false;
		int isplayable_value;

		if (!marked && isPlayable && service_info && m_is_playable_ignore.valid())
		{
			isplayable_value = service_info->isPlayable(*m_cursor, m_is_playable_ignore);

			if (isplayable_value == 0) // service unavailable
			{
				if (m_color_set[serviceNotAvail])
					painter.setForegroundColor(m_color[serviceNotAvail]);
				else
					painter.setForegroundColor(gRGB(0xbbbbbb));
				serviceAvail = false;
			}
			else
			{
				if (isplayable_value == 2) // fallback receiver service
				{
					if (m_color_set[serviceItemFallback])
						painter.setForegroundColor(m_color[serviceItemFallback]);
					serviceFallback = true;
				}
			}
		}
		if (m_record_indicator_mode == 3 && isPseudoRecorded)
		{
			if (m_color_set[servicePseudoRecorded])
				painter.setForegroundColor(m_color[servicePseudoRecorded]);
			else
				painter.setForegroundColor(gRGB(0x41b1ec));
		}
		if (m_record_indicator_mode == 3 && isStreamed)
		{
			if (m_color_set[serviceStreamed])
				painter.setForegroundColor(m_color[serviceStreamed]);
			else
				painter.setForegroundColor(gRGB(0xf56712));
		}
		if (m_record_indicator_mode == 3 && isRecorded)
		{
			if (m_color_set[serviceRecorded])
				painter.setForegroundColor(m_color[serviceRecorded]);
			else
				painter.setForegroundColor(gRGB(0xb40431));
		}

		if (selected && local_style && local_style->m_selection)
			painter.blit(local_style->m_selection, offset, eRect(), gPainter::BT_ALPHABLEND);

		int xoffset=0;  // used as offset when painting the folder/marker symbol or the serviceevent progress
		time_t now = time(0);

		for (int e = 0; e != celServiceTypePixmap; ++e)
		{
			if (m_element_font[e])
			{
				int flags=gPainter::RT_VALIGN_CENTER;
				int yoffs = 0;
				eRect area = m_element_position[e];
				std::string text = "<n/a>";
				switch (e)
				{
				case celServiceNumber:
				{
					if (area.width() <= 0)
						continue; // no point in going on if we won't paint anything

					if( m_cursor->getChannelNum() == 0 )
						continue;

					char buffer[15];
					snprintf(buffer, sizeof(buffer), "%d", m_cursor->getChannelNum() );
					text = buffer;
					flags|=gPainter::RT_HALIGN_RIGHT;
					if (isPlayable && serviceFallback && selected && m_color_set[serviceSelectedFallback])
						painter.setForegroundColor(m_color[serviceSelectedFallback]);
					break;
				}
				case celServiceName:
				{
					if (service_info)
						service_info->getName(*m_cursor, text);
					if (!isPlayable)
					{
						area.setWidth(area.width() + m_element_position[celServiceEventProgressbar].width() +  m_nonplayable_margins);
						if (m_element_position[celServiceEventProgressbar].left() == 0)
							area.setLeft(0);
						if (m_element_position[celServiceNumber].width() && m_element_position[celServiceEventProgressbar].left() == m_element_position[celServiceNumber].width() +  m_nonplayable_margins)
							area.setLeft(m_element_position[celServiceNumber].width() +  m_nonplayable_margins);
					}
					if (!(m_record_indicator_mode == 3 && isRecorded) && isPlayable && serviceFallback && selected && m_color_set[serviceSelectedFallback])
						painter.setForegroundColor(m_color[serviceSelectedFallback]);
					break;
				}
				case celServiceInfo:
				{
					if ( isPlayable && service_info && !service_info->getEvent(*m_cursor, evt) )
					{
						std::string name = evt->getEventName();
						if (name.empty())
							continue;
						text = evt->getEventName();
						if (serviceAvail)
						{
							if (!selected && m_color_set[eventForeground])
								painter.setForegroundColor(m_color[eventForeground]);
							else if (selected && m_color_set[eventForegroundSelected])
								painter.setForegroundColor(m_color[eventForegroundSelected]);
							else
								painter.setForegroundColor(gRGB(0xe7b53f));

							if (serviceFallback && !selected && m_color_set[eventForegroundFallback]) // fallback receiver
								painter.setForegroundColor(m_color[eventForegroundFallback]);
							else if (serviceFallback && selected && m_color_set[eventForegroundSelectedFallback])
								painter.setForegroundColor(m_color[eventForegroundSelectedFallback]);

						}
						break;
					}
					continue;
				}
				case celServiceEventProgressbar:
				{
					if (area.width() > 0 && isPlayable && service_info && !service_info->getEvent(*m_cursor, evt))
					{
						char buffer[15];
						snprintf(buffer, sizeof(buffer), "%d %%", (int)(100 * (now - evt->getBeginTime()) / evt->getDuration()));
						text = buffer;
						flags|=gPainter::RT_HALIGN_RIGHT;
						break;
					}
					continue;
				}
				}

				eRect tmp = area;
				int xoffs = 0;
				ePtr<gPixmap> piconPixmap;

				if (e == celServiceName)
				{
					//picon stuff
					if (isPlayable && PyCallable_Check(m_GetPiconNameFunc))
					{
						ePyObject pArgs = PyTuple_New(1);
						PyTuple_SET_ITEM(pArgs, 0, PyString_FromString(ref.toString().c_str()));
						ePyObject pRet = PyObject_CallObject(m_GetPiconNameFunc, pArgs);
						Py_DECREF(pArgs);
						if (pRet)
						{
							if (PyString_Check(pRet))
							{
								std::string piconFilename = PyString_AS_STRING(pRet);
								if (!piconFilename.empty())
									loadPNG(piconPixmap, piconFilename.c_str());
							}
							Py_DECREF(pRet);
						}
					}
					xoffs = xoffset;
					tmp.setWidth(((!isPlayable || m_column_width == -1 || (!piconPixmap && !m_column_width)) ? tmp.width() : m_column_width) - xoffs);
				}

				eTextPara *para = new eTextPara(tmp);
				para->setFont(m_element_font[e]);
				para->renderString(text.c_str());

				if (e == celServiceName)
				{
					eRect bbox = para->getBoundBox();

					int servicenameWidth = ((!isPlayable || m_column_width == -1 || (!piconPixmap && !m_column_width)) ? bbox.width() : m_column_width);
					m_element_position[celServiceInfo].setLeft(area.left() + servicenameWidth + m_items_distances + xoffs);
					m_element_position[celServiceInfo].setTop(area.top());
					m_element_position[celServiceInfo].setWidth(area.width() - (servicenameWidth + m_items_distances + xoffs));
					m_element_position[celServiceInfo].setHeight(area.height());

					if (isPlayable)
					{
						//picon stuff
						if (PyCallable_Check(m_GetPiconNameFunc) and (m_column_width || piconPixmap))
						{
							eRect area = m_element_position[celServiceInfo];
							/* PIcons are usually about 100:60. Make it a
							 * bit wider in case the icons are diffently
							 * shaped, and to add a bit of margin between
							 * icon and text. */
							const int iconWidth = (area.height() + m_service_picon_downsize * 2) * 1.67 + m_items_distances;
							m_element_position[celServiceInfo].setLeft(area.left() + iconWidth);
							m_element_position[celServiceInfo].setWidth(area.width() - iconWidth);
							area = m_element_position[celServiceName];
							xoffs += iconWidth;
							if (piconPixmap)
							{
								area.moveBy(offset);
								painter.clip(area);
								painter.blitScale(piconPixmap,
									eRect(area.left(), area.top() - m_service_picon_downsize, iconWidth, area.height() + m_service_picon_downsize * 2),
									area,
									gPainter::BT_ALPHABLEND | gPainter::BT_KEEP_ASPECT_RATIO);
								painter.clippop();
							}
						}

						//service type marker stuff
						if (m_servicetype_icon_mode)
						{
							int orbpos = m_cursor->getUnsignedData(4) >> 16;
							const char *filename = ref.path.c_str();
							ePtr<gPixmap> &pixmap =
								(m_cursor->flags & eServiceReference::isGroup) ? m_pixmaps[picServiceGroup] :
								(strstr(filename, "://")) ? m_pixmaps[picStream] :
								(orbpos == 0xFFFF) ? m_pixmaps[picDVB_C] :
								(orbpos == 0xEEEE) ? m_pixmaps[picDVB_T] : m_pixmaps[picDVB_S];
							if (pixmap)
							{
								eSize pixmap_size = pixmap->size();
								eRect area = m_element_position[celServiceInfo];
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								int offs = 0;
								if (m_servicetype_icon_mode == 1)
								{
									area = m_element_position[celServiceName];
									offs = xoffs;
									xoffs += pixmap_size.width() + m_items_distances;
								}
								else if (m_crypto_icon_mode == 1 && m_pixmaps[picCrypto])
									offs = offs + m_pixmaps[picCrypto]->size().width() + m_items_distances;
								int correction = (area.height() - pixmap_size.height()) / 2;
								area.moveBy(offset);
								painter.clip(area);
								painter.blit(pixmap, ePoint(area.left() + offs, offset.y() + correction), area, gPainter::BT_ALPHABLEND);
								painter.clippop();
							}
						}

						//crypto icon stuff
						if (m_crypto_icon_mode && m_pixmaps[picCrypto])
						{
							eSize pixmap_size = m_pixmaps[picCrypto]->size();
							eRect area = m_element_position[celServiceInfo];
							int offs = 0;
							if (m_crypto_icon_mode == 1)
							{
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								area = m_element_position[celServiceName];
								offs = xoffs;
								xoffs += pixmap_size.width() + m_items_distances;
							}
							int correction = (area.height() - pixmap_size.height()) / 2;
							area.moveBy(offset);
							if (service_info->isCrypted())
							{
								if (m_crypto_icon_mode == 2)
								{
									m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
									m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								}
								painter.clip(area);
								painter.blit(m_pixmaps[picCrypto], ePoint(area.left() + offs, offset.y() + correction), area, gPainter::BT_ALPHABLEND);
								painter.clippop();
							}
						}

						//record icon stuff
						if (isRecorded && m_record_indicator_mode < 3 && m_pixmaps[picRecord])
						{
							eSize pixmap_size = m_pixmaps[picRecord]->size();
							eRect area = m_element_position[celServiceInfo];
							int offs = 0;
							if (m_record_indicator_mode == 1)
							{
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
								area = m_element_position[celServiceName];
								offs = xoffs;
								xoffs += pixmap_size.width() + m_items_distances;
							}
							int correction = (area.height() - pixmap_size.height()) / 2;
							area.moveBy(offset);
							if (m_record_indicator_mode == 2)
							{
								m_element_position[celServiceInfo].setLeft(area.left() + pixmap_size.width() + m_items_distances);
								m_element_position[celServiceInfo].setWidth(area.width() - pixmap_size.width() - m_items_distances);
							}
							painter.clip(area);
							painter.blit(m_pixmaps[picRecord], ePoint(area.left() + offs, offset.y() + correction), area, gPainter::BT_ALPHABLEND);
							painter.clippop();
						}
					}
				}

				if (flags & gPainter::RT_HALIGN_RIGHT)
					para->realign(eTextPara::dirRight);
				else if (flags & gPainter::RT_HALIGN_CENTER)
					para->realign(eTextPara::dirCenter);
				else if (flags & gPainter::RT_HALIGN_BLOCK)
					para->realign(eTextPara::dirBlock);

				if (flags & gPainter::RT_VALIGN_CENTER)
				{
					eRect bbox = para->getBoundBox();
					yoffs = (area.height() - bbox.height()) / 2 - bbox.top();
				}

				painter.renderPara(para, offset+ePoint(xoffs, yoffs));
			}
			else if ((e == celFolderPixmap && m_cursor->flags & eServiceReference::isDirectory) ||
				(e == celMarkerPixmap && m_cursor->flags & eServiceReference::isMarker &&
				!(m_cursor->flags & eServiceReference::isNumberedMarker)))
			{
				ePtr<gPixmap> &pixmap =
					(e == celFolderPixmap) ? m_pixmaps[picFolder] : m_pixmaps[picMarker];
				if (pixmap)
				{
					eSize pixmap_size = pixmap->size();
					eRect area = m_element_position[e == celFolderPixmap ? celServiceName: celServiceNumber];
					int correction = (area.height() - pixmap_size.height()) / 2;
					if (e == celFolderPixmap)
						if (m_element_position[celServiceEventProgressbar].left() == 0)
							area.setLeft(0);
						xoffset = pixmap_size.width() + m_items_distances;
					area.moveBy(offset);
					painter.clip(area);
					painter.blit(pixmap, ePoint(area.left(), offset.y() + correction), area, gPainter::BT_ALPHABLEND);
					painter.clippop();
				}
			}
		}
Example #12
0
static LIST*
call_python_function(RULE* r, FRAME* frame)
{
    LIST* result = 0;
    PyObject* arguments = PyTuple_New(frame->args->count);
    int i ;
    PyObject* py_result;

    for(i = 0; i < frame->args->count; ++i)
    {
        PyObject* arg = PyList_New(0);
        LIST* l = lol_get( frame->args, i);

        for(; l; l = l->next)
        {
            PyObject* v = PyString_FromString(l->string);
            /* Steals reference to 'v' */
            PyList_Append(arg, v);            
        }
        /* Steals reference to 'arg' */
        PyTuple_SetItem(arguments, i, arg);
    }

    py_result = PyObject_CallObject(r->python_function, arguments);
    Py_DECREF(arguments);
    if (py_result != NULL) {
        
        if (PyList_Check(py_result)) {
            int size = PyList_Size(py_result);
            int i;
            for(i = 0; i < size; ++i)
            {
                PyObject* item = PyList_GetItem(py_result, i);
                if (PyString_Check(item))
                {
                    result = list_new(result, 
                                      newstr(PyString_AsString(item)));
                }
                else
                {
                    fprintf(stderr, "Non-string object returned by Python call\n");
                }
            }
        }
        else if (PyInstance_Check(py_result))
        {
            static char instance_name[1000];
            static char imported_method_name[1000];
            module_t* m;
            PyObject* method;
            PyObject* method_name = PyString_FromString("foo");
            RULE* r;

            fprintf(stderr, "Got instance!\n");

            snprintf(instance_name, 1000,
                     "pyinstance%d", python_instance_number);
            snprintf(imported_method_name, 1000,
                     "pyinstance%d.foo", python_instance_number);
            ++python_instance_number;
            
            m = bindmodule(instance_name);

            /* This is expected to get bound method. */
            method = PyObject_GetAttr(py_result, method_name);
            
            r = bindrule( imported_method_name, root_module() );

            r->python_function = method;

            result = list_new(0, newstr(instance_name));    

            Py_DECREF(method_name);
        }
        else if (py_result == Py_None)
        {
            result = L0;
        }
        else
        {
            fprintf(stderr, "Non-list object returned by Python call\n");
        }

        Py_DECREF(py_result);
    }
    else {
        PyErr_Print();
        fprintf(stderr,"Call failed\n");
    }
    
    return result;
}
Example #13
0
int
exceptionlist__initmodule(PyObject* module, struct exceptionlist* exclist) {
	const struct exceptionlist* exc;
	char* longname = NULL;
	PyObject* name;
	Py_ssize_t size;
	Py_ssize_t maxsize;

	if ((name = PyObject_GetAttrString(module, "__name__")) == NULL)
		return -1;
	if (!PyString_Check(name)) {
		PyErr_SetString(PyExc_TypeError, "__name__ required an str()");
		goto failed;
	}
	size = PyString_GET_SIZE(name);
	for (maxsize = 0, exc = exclist; exc->exc_name != NULL; exc++) {
		register int i = strlen(exc->exc_name);
		if (i > maxsize)
			maxsize = i;
	}
	if ((longname = PyMem_MALLOC(size + sizeof(".") + maxsize + sizeof("\0"))) == NULL) {
		PyErr_NoMemory();
		goto failed;
	}
	memcpy(longname, PyString_AS_STRING(name), size);
	Py_DECREF(name);
	longname[size++] = '.';
	for (exc = exclist; exc->exc_name != NULL; exc++) {
		PyObject* dict;
		PyObject* s;
		
		if ((dict = PyDict_New()) == NULL)
			goto failed;
		if ((s = PyString_FromString(exc->exc_doc)) == NULL) {
			Py_DECREF(dict);
			goto failed;
		}
		if (PyDict_SetItemString(dict, "__doc__", s) < 0) {
			Py_DECREF(dict);
			Py_DECREF(s);
			goto failed;
		}
		Py_DECREF(s);
		strcpy(&longname[size], exc->exc_name);
		if (*exc->exc_this == NULL &&
		   (*exc->exc_this = PyErr_NewException(longname, *exc->exc_base, dict)) == NULL) {
			Py_DECREF(dict);
			goto failed;
		}
		Py_DECREF(dict);
	}
	PyMem_FREE(longname);
	for (exc = exclist; exc->exc_name != NULL; exc++) {
		Py_INCREF(*exc->exc_this);
		if (PyModule_AddObject(module, exc->exc_name, *exc->exc_this) < 0)
			return -1;
	}
	return 0;
failed:
	if (longname != NULL)
		PyMem_FREE(longname);
	Py_DECREF(name);
	return -1;
}
Example #14
0
dbtype_t
from_python(pgctx_t *ctx, PyObject *ob)
{
    dbtype_t db;
    char *buf;
    Py_ssize_t length;
    PyObject *items;
    struct tm tm;
    long usec;
    //int i;
    
    if (PyObject_HasAttrString(ob, "__topongo__")) {
        ob = PyObject_CallMethod(ob, "__topongo__", NULL);
        if (PyErr_Occurred())
            return DBNULL;
    }
    if (ob == Py_None) {
        db = DBNULL;
    } else if (ob == pongo_id) {
        db = dbuuid_new(ctx, NULL);
    } else if (ob == pongo_utcnow) {
        db = dbtime_now(ctx);
    } else if (PyBool_Check(ob)) {
        db = dbboolean_new(ctx, ob == Py_True);
    } else if (PyInt_Check(ob)) {
        db = dbint_new(ctx, PyInt_AsLong(ob));
    } else if (PyLong_Check(ob)) {
        db = dbint_new(ctx, PyLong_AsLongLong(ob));
    } else if (PyFloat_Check(ob)) {
        db = dbfloat_new(ctx, PyFloat_AsDouble(ob));
    } else if (PyString_Check(ob)) {
        PyString_AsStringAndSize(ob, &buf, &length);
        // FIXME:
        //db = dbbuffer_new(ctx, buf, length);
        db = dbstring_new(ctx, buf, length);
    } else if (PyUnicode_Check(ob)) {
        ob = PyUnicode_AsUTF8String(ob);
        if (ob) {
            PyString_AsStringAndSize(ob, &buf, &length);
            db = dbstring_new(ctx, buf, length);
            Py_DECREF(ob);
        }
    } else if (PyDateTime_Check(ob)) {
        memset(&tm, 0, sizeof(tm));
        tm.tm_year = PyDateTime_GET_YEAR(ob);
        tm.tm_mon = PyDateTime_GET_MONTH(ob);
        tm.tm_mday = PyDateTime_GET_DAY(ob);
        tm.tm_hour = PyDateTime_DATE_GET_HOUR(ob);
        tm.tm_min = PyDateTime_DATE_GET_MINUTE(ob);
        tm.tm_sec = PyDateTime_DATE_GET_SECOND(ob);
        usec = PyDateTime_DATE_GET_MICROSECOND(ob);
        tm.tm_year -= 1900;
        db = dbtime_newtm(ctx, &tm, usec);
#ifdef WANT_UUID_TYPE
    } else if (PyObject_TypeCheck(ob, uuid_class)) {
        ob = PyObject_CallMethod(ob, "get_bytes", NULL);
        PyString_AsStringAndSize(ob, &buf, &length);
        db = dbuuid_new(ctx, (uint8_t*)buf);
#endif
    } else if (Py_TYPE(ob) == &PongoList_Type) {
        // Resolve proxy types back to their original dbtype
        PongoList *p = (PongoList*)ob;
        db = p->dbptr;
    } else if (Py_TYPE(ob) == &PongoDict_Type) {
        // Resolve proxy types back to their original dbtype
        PongoDict *p = (PongoDict*)ob;
        db = p->dbptr;
    } else if (Py_TYPE(ob) == &PongoCollection_Type) {
        // Resolve proxy types back to their original dbtype
        PongoCollection *p = (PongoCollection*)ob;
        db = p->dbptr;
    } else if (PyMapping_Check(ob)) {
        length = PyMapping_Length(ob);
        items = PyMapping_Items(ob);
        if (items) {
            // mapping object implements "items"
            db = dbobject_new(ctx);
            dbobject_update(ctx, db, length, _py_mapping_cb, items, NOSYNC);
            Py_XDECREF(items);
        } else {
            // mapping object implements iterator protocol
            // don't have to decref the iterator object cuz it self-decrefs
            // upon StopIteration
            PyErr_Clear();
            items = PyObject_GetIter(ob);
            db = dbobject_new(ctx);
            dbobject_update(ctx, db, length, _py_itermapping_cb, items, NOSYNC);
        }
    } else if (PySequence_Check(ob)) {
        length = PySequence_Length(ob);
        db = dblist_new(ctx);
        dblist_extend(ctx, db, length, _py_sequence_cb, ob, NOSYNC);
    } else {
        // FIXME: Unknown object type
        PyErr_SetObject(PyExc_TypeError, (PyObject*)Py_TYPE(ob));
        db = DBNULL;
    }
    return db;
}
Example #15
0
PyObject* JSONToObj(PyObject* self, PyObject *arg)
{
    PyObject *ret;
    PyObject *sarg;
    JSONObjectDecoder decoder = 
    {
        Object_newString,
        Object_newDateTime,
        Object_objectAddKey,
        Object_arrayAddItem,
        Object_newTrue,
        Object_newFalse,
        Object_newNull,
        Object_newObject,
        Object_newArray,
        Object_newInteger,
        Object_newLong,
        Object_newDouble,
        Object_releaseObject,
        PyObject_Malloc,
        PyObject_Free,
        PyObject_Realloc
    };

    if (PyString_Check(arg))
    {
        sarg = arg;
    }
    else
    if (PyUnicode_Check(arg))
    {
        sarg = PyUnicode_AsUTF8String(arg);
        if (sarg == NULL)
        {
            //Exception raised above us by codec according to docs
            return NULL;
        }
    }
    else
    {
        PyErr_Format(PyExc_TypeError, "Expected String or Unicode");
        return NULL;
    }

    decoder.errorStr = NULL;
    decoder.errorOffset = NULL;
    
    ret = JSON_DecodeObject(&decoder, PyString_AS_STRING(sarg), PyString_GET_SIZE(sarg)); 

    if (sarg != arg)
    {
        Py_DECREF(sarg);
    }

    if (decoder.errorStr)
    {
        /*
        FIXME: It's possible to give a much nicer error message here with actual failing element in input etc*/
        
        PyErr_Format (PyExc_ValueError, "%s", decoder.errorStr);
        
        if (ret)
        {
            Py_DECREF( (PyObject *) ret);
        }
        
        return NULL;
    }
    
    return ret;
}
Example #16
0
static int LPX_init(LPXObject *self, PyObject *args, PyObject *kwds)
{
	char *mps_n=NULL, *freemps_n=NULL, *cpxlp_n=NULL;
	PyObject *model_obj=NULL;
	static char *kwlist[] = {"gmp","mps","freemps","cpxlp",NULL};
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Osss", kwlist, &model_obj, &mps_n, &freemps_n, &cpxlp_n)) {
		return -1;
	}
	int numargs = (mps_n?1:0)+(freemps_n?1:0)+(cpxlp_n?1:0)+(model_obj?1:0);
	if (numargs>1) {
		PyErr_SetString(PyExc_TypeError, "cannot specify multiple data sources");
		return -1;
	}
	if (numargs==0) {
		// No arguments.  Create an empty problem.
		self->lp = glp_create_prob();
	} else {
		// Some of these are pretty straightforward data reading routines.
		if (mps_n) {
#if GLPK_VERSION(4, 29)
			self->lp = glp_create_prob();
			int failure = glp_read_mps(self->lp, GLP_MPS_DECK, NULL, mps_n);
			if (failure) {
				PyErr_SetString(PyExc_RuntimeError, "MPS reader failed");
				return -1;
			}
#else
			self->lp = lpx_read_mps(mps_n);
#endif
		} else if (freemps_n) {
#if GLPK_VERSION(4, 29)
			self->lp = glp_create_prob();
			int failure = glp_read_mps(self->lp, GLP_MPS_FILE, NULL, mps_n);
			if (failure) {
				PyErr_SetString(PyExc_RuntimeError, "Free MPS reader failed");
				return -1;
			}
#else
			self->lp = lpx_read_freemps(freemps_n);
#endif
		} else if (cpxlp_n) {
#if GLPK_VERSION(4, 29)
			self->lp = glp_create_prob();
			int failure = glp_read_lp(self->lp, NULL, mps_n);
			if (failure) {
				PyErr_SetString(PyExc_RuntimeError, "CPLEX LP reader failed");
				return -1;
			}
#else
			self->lp = lpx_read_cpxlp(cpxlp_n);
#endif
		} else if (model_obj) {
			// This one can take a few possible values.
			char *model[] = {NULL,NULL,NULL};
			if (PyString_Check(model_obj)) {
				// Single string object.
				model[0] = PyString_AsString(model_obj);
				if (!model[0])
					return -1;
			} else if (PyTuple_Check(model_obj)) {
				// Possibly module arguments.
				int i,size = PyTuple_Size(model_obj);
				if (size < -1)
					return -1;
				if (size >  3) {
					PyErr_SetString(PyExc_ValueError, "model tuple must have length<=3");
					return -1;
				}
				for (i=0; i < size; ++i) {
					PyObject *so = PyTuple_GET_ITEM(model_obj,i);
					if (so == Py_None)
						continue;
					model[i] = PyString_AsString(so);
					if (model[i] == NULL)
						return -1;
				}
			} else {
				PyErr_SetString(PyExc_TypeError, "model arg must be string or tuple");
				return -1;
			}
			// Now, pass in that information.
			if (!model[0])
				return -1;
			self->lp = lpx_read_model(model[0], model[1], model[2]);
		}
	}
	// Any of the methods above may have failed, so the LP would be null.
	if (LP == NULL) {
		PyErr_SetString(numargs ? PyExc_RuntimeError : PyExc_MemoryError, "could not create problem");
		return -1;
	}
	// Create those rows and cols and things.
	self->cols = (PyObject*)BarCol_New(self, 0);
	self->rows = (PyObject*)BarCol_New(self, 1);
	self->obj = (PyObject*)Obj_New(self);
#ifdef USEPARAMS
	self->params = (PyObject*)Params_New(self);
#endif
	return 0;
}
Example #17
0
char *PyTraceback_AsString(PyObject *exc_tb)
{
	const char *errMsg = NULL; /* holds a local error message */
	char *result = NULL; /* a valid, allocated result. */
	PyObject *modStringIO = NULL;
	PyObject *modTB = NULL;
	PyObject *obFuncStringIO = NULL;
	PyObject *obStringIO = NULL;
	PyObject *obFuncTB = NULL;
	PyObject *argsTB = NULL;
	PyObject *obResult = NULL;

#if PY_MAJOR_VERSION <= 2
	/* Import the modules we need - cStringIO and traceback */
	modStringIO = PyImport_ImportModule("cStringIO");
	if (modStringIO==NULL)
		TRACEBACK_FETCH_ERROR("cant import cStringIO\n");

	modTB = PyImport_ImportModule("traceback");
	if (modTB==NULL)
		TRACEBACK_FETCH_ERROR("cant import traceback\n");
	/* Construct a cStringIO object */
	obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
	if (obFuncStringIO==NULL)
		TRACEBACK_FETCH_ERROR("cant find cStringIO.StringIO\n");
	obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
	if (obStringIO==NULL)
		TRACEBACK_FETCH_ERROR("cStringIO.StringIO() failed\n");
#else
	/* Import the modules we need - io and traceback */
	modStringIO = PyImport_ImportModule("io");
	if (modStringIO==NULL)
		TRACEBACK_FETCH_ERROR("cant import io\n");

	modTB = PyImport_ImportModule("traceback");
	if (modTB==NULL)
		TRACEBACK_FETCH_ERROR("cant import traceback\n");
	/* Construct a StringIO object */
	obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
	if (obFuncStringIO==NULL)
		TRACEBACK_FETCH_ERROR("cant find io.StringIO\n");
	obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
	if (obStringIO==NULL)
		TRACEBACK_FETCH_ERROR("io.StringIO() failed\n");
#endif
	/* Get the traceback.print_exception function, and call it. */
	obFuncTB = PyObject_GetAttrString(modTB, "print_tb");
	if (obFuncTB==NULL)
		TRACEBACK_FETCH_ERROR("cant find traceback.print_tb\n");

	argsTB = Py_BuildValue("OOO",
			exc_tb  ? exc_tb  : Py_None,
			Py_None,
			obStringIO);
	if (argsTB==NULL)
		TRACEBACK_FETCH_ERROR("cant make print_tb arguments\n");

	obResult = PyObject_CallObject(obFuncTB, argsTB);
	if (obResult==NULL)
		TRACEBACK_FETCH_ERROR("traceback.print_tb() failed\n");
	/* Now call the getvalue() method in the StringIO instance */
	Py_DECREF(obFuncStringIO);
	obFuncStringIO = PyObject_GetAttrString(obStringIO, "getvalue");
	if (obFuncStringIO==NULL)
		TRACEBACK_FETCH_ERROR("cant find getvalue function\n");
	Py_DECREF(obResult);
	obResult = PyObject_CallObject(obFuncStringIO, NULL);
	if (obResult==NULL)
		TRACEBACK_FETCH_ERROR("getvalue() failed.\n");

	/* And it should be a string all ready to go - duplicate it. */
#if PY_MAJOR_VERSION <= 2
	if (!PyString_Check(obResult))
#else
	if (!PyUnicode_Check(obResult))
#endif
			TRACEBACK_FETCH_ERROR("getvalue() did not return a string\n");

	{ // a temp scope so I can use temp locals.
#if PY_MAJOR_VERSION <= 2
	char *tempResult = PyString_AsString(obResult);
#else
	char *tempResult = PyUnicode_AsUTF8(obResult);
#endif
	result = (char *)PyMem_Malloc(strlen(tempResult)+1);
	if (result==NULL)
		TRACEBACK_FETCH_ERROR("memory error duplicating the traceback string\n");

	strcpy(result, tempResult);
	} // end of temp scope.
done:
	/* All finished - first see if we encountered an error */
	if (result==NULL && errMsg != NULL) {
		result = (char *)PyMem_Malloc(strlen(errMsg)+1);
		if (result != NULL)
			/* if it does, not much we can do! */
			strcpy(result, errMsg);
	}
	Py_XDECREF(modStringIO);
	Py_XDECREF(modTB);
	Py_XDECREF(obFuncStringIO);
	Py_XDECREF(obStringIO);
	Py_XDECREF(obFuncTB);
	Py_XDECREF(argsTB);
	Py_XDECREF(obResult);
	return result;
}
Example #18
0
PyObject *
PyString_Format(PyObject *format, PyObject *args)
{
	char *fmt, *res;
	int fmtcnt, rescnt, reslen, arglen, argidx;
	int args_owned = 0;
	PyObject *result, *orig_args;
	PyObject *dict = NULL;
	if (format == NULL || !PyString_Check(format) || args == NULL) {
		/* ERROR */
		return NULL;
	}
	orig_args = args;
	fmt = PyString_AS_STRING(format);
	fmtcnt = PyString_GET_SIZE(format);
	reslen = rescnt = fmtcnt + 100;
	result = PyString_FromStringAndSize((char *)NULL, reslen);
	if (result == NULL)
		return NULL;
	res = PyString_AsString(result);
	if (PyTuple_Check(args)) {
		arglen = PyTuple_GET_SIZE(args);
		argidx = 0;
	}
	else {
		arglen = -1;
		argidx = -2;
	}
	if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) &&
	    !PyObject_TypeCheck(args, &PyBaseString_Type))
		dict = args;
	while (--fmtcnt >= 0) {
		if (*fmt != '%') {
			if (--rescnt < 0) {
				rescnt = fmtcnt + 100;
				reslen += rescnt;
				if (_PyString_Resize(&result, reslen) < 0)
					return NULL;
				res = PyString_AS_STRING(result)
					+ reslen - rescnt;
				--rescnt;
			}
			*res++ = *fmt++;
		}
		else {
			/* Got a format specifier */
			int flags = 0;
			int width = -1;
			int prec = -1;
			int c = '\0';
			int fill;
			PyObject *v = NULL;
			PyObject *temp = NULL;
			char *pbuf;
			int sign;
			int len;
			char formatbuf[FORMATBUFLEN];
			     /* For format{float,int,char}() */

			fmt++;
			if (*fmt == '(') {
				/* TO DO */
			}
			while (--fmtcnt >= 0) {
				switch (c = *fmt++) {
				case '-': flags |= F_LJUST; continue;
				case '+': flags |= F_SIGN; continue;
				case ' ': flags |= F_BLANK; continue;
				case '#': flags |= F_ALT; continue;
				case '0': flags |= F_ZERO; continue;
				}
				break;
			}
			/* ... */
			if (fmtcnt >= 0) {
				if (c == 'h' || c == 'l' || c == 'L') {
					if (--fmtcnt >= 0)
						c = *fmt++;
				}
			}
			if (fmtcnt < 0) {
				/* ERROR */
				goto error;
			}
			if (c != '%') {
				v = getnextarg(args, arglen, &argidx);
				if (v == NULL)
					goto error;
			}
			sign = 0;
			fill = ' ';
			switch (c) {
			case '%':
				pbuf = "%";
				len = 1;
				break;
			case 's':
				/* Fall through */
			case 'r':
				if (c == 's')
					temp = PyObject_Str(v);
				else {
					/* TO DO */
					goto error;
				}
				if (temp == NULL)
					goto error;
				if (!PyString_Check(temp)) {
					/* XXX Note: this should never happen,
					   since PyObject_Repr() and
					   PyObject_Str() assure this */
					/* ERROR */
					Py_DECREF(temp);
					goto error;
				}
				pbuf = PyString_AS_STRING(temp);
				len = PyString_GET_SIZE(temp);
				if (prec >= 0 && len > prec)
					len = prec;
				break;
			case 'i':
			case 'd':
			case 'u':
			case 'o':
			case 'x':
			case 'X':
				pbuf = formatbuf;

				/* TO DO */
//				len = formatint(pbuf,
//						sizeof(formatbuf),
//						flags, prec, c, v);
				if (len < 0)
					goto error;
				/* only d conversion is signed */
				sign = c == 'd';
				if (flags & F_ZERO)
					fill = '0';
				break;
			case 'e':
			case 'E':
			case 'f':
			case 'g':
			case 'G':
			case 'c':

				printf("FORMAT NOT SUPPORTED YET\n");
				goto error;
			default:
				/* ERROR */
				goto error;
			}
			if (sign) {
				if (*pbuf == '-' || *pbuf == '+') {
					sign = *pbuf++;
					len--;
				}
				else if (flags & F_SIGN)
					sign = '+';
				else if (flags & F_BLANK)
					sign = ' ';
				else
					sign = 0;
			}
			if (width < len)
				width = len;
			if (rescnt - (sign != 0) < width) {
				reslen -= rescnt;
				rescnt = width + fmtcnt + 100;
				reslen += rescnt;
				if (reslen < 0) {
					Py_DECREF(result);
					/* ERROR No mem*/
					return NULL;
				}
				if (_PyString_Resize(&result, reslen) < 0)
					return NULL;
				res = PyString_AS_STRING(result)
					+ reslen - rescnt;
			}
			if (sign) {
				if (fill != ' ')
					*res++ = sign;
				rescnt--;
				if (width > len)
					width--;
			}
			if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
				if (fill != ' ') {
					*res++ = *pbuf++;
					*res++ = *pbuf++;
				}
				rescnt -= 2;
				width -= 2;
				if (width < 0)
					width = 0;
				len -= 2;
			}
			if (width > len && !(flags & F_LJUST)) {
				do {
					--rescnt;
					*res++ = fill;
				} while (--width > len);
			}
			if (fill == ' ') {
				if (sign)
					*res++ = sign;
				if ((flags & F_ALT) &&
				    (c == 'x' || c == 'X')) {
					*res++ = *pbuf++;
					*res++ = *pbuf++;
				}
			}
			memcpy(res, pbuf, len);
			res += len;
			rescnt -= len;
			while (--width >= len) {
				--rescnt;
				*res++ = ' ';
			}
                        if (dict && (argidx < arglen) && c != '%') {
				/* ERROR */
                                goto error;
                        }
			Py_XDECREF(temp);
		} /* '%' */
	} /* until end */
	if (argidx < arglen && !dict) {
		/* ERROR */
		goto error;
	}
	if (args_owned) {
		Py_DECREF(args);
	}
	_PyString_Resize(&result, reslen - rescnt);
	return result;

 error:
	Py_DECREF(result);
	if (args_owned) {
		Py_DECREF(args);
	}
	return NULL;
}
/**
 *********************************************************************
 * This function will invoke aerospike_batch_get_bins to get filtered
 * bins from all the records in a batches.
 *
 * @param self                    AerospikeClient object
 * @param py_keys                 List of keys passed on by user
 * @param py_bins                 List of filter bins passed on by user
 * @param py_policy               User specified Policy dictionary
 *
 *********************************************************************
 **/
static
PyObject * AerospikeClient_Select_Many_Invoke(
    AerospikeClient * self,
    PyObject * py_keys, PyObject * py_bins, PyObject * py_policy)
{
    // Python Return Value
    PyObject * py_recs = NULL;

    // Aerospike Client Arguments
    as_error err;
    as_policy_batch policy;
    as_policy_batch * batch_policy_p = NULL;
    Py_ssize_t bins_size = 0;
    char **filter_bins = NULL;
    bool has_batch_index = false;

    // Unicode object's pool
    UnicodePyObjects u_objs;
    u_objs.size = 0;
    int i = 0;

    // Initialize error
    as_error_init(&err);

    if (!self || !self->as) {
        as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
        goto CLEANUP;
    }

    if (!self->is_conn_16) {
        as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
        goto CLEANUP;
    }

    // Check the type of bins and get it's size
    // i.e. number of bins provided
    if (py_bins != NULL && PyList_Check(py_bins)) {
        bins_size    = PyList_Size(py_bins);
    }
    else if (py_bins != NULL && PyTuple_Check(py_bins)) {
        bins_size    = PyTuple_Size(py_bins);
    }
    else {
        as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter bins should be specified as a list or tuple.");
        goto CLEANUP;
    }

    filter_bins = (char **)malloc(sizeof(long int) * bins_size);

    for (i = 0; i < bins_size; i++) {
        PyObject *py_bin = NULL;
        if(PyList_Check(py_bins)) {
            py_bin = PyList_GetItem(py_bins, i);
        }
        if(PyTuple_Check(py_bins)) {
            py_bin = PyTuple_GetItem(py_bins, i);
        }
        if (PyUnicode_Check(py_bin)) {
            // Store the unicode object into a pool
            // It is DECREFed at later stages
            // So, no need of DECREF here.
            filter_bins[i] = PyString_AsString(
                                 store_unicode_bins(&u_objs, PyUnicode_AsUTF8String(py_bin)));
        }
        else if (PyString_Check(py_bin)) {
            filter_bins[i]    = PyString_AsString(py_bin);
        }
        else {
            as_error_update(&err, AEROSPIKE_ERR_PARAM, "Bin name should be a string or unicode string.");
            goto CLEANUP;
        }
    }

    // Convert python policy object to as_policy_batch
    pyobject_to_policy_batch(&err, py_policy, &policy, &batch_policy_p,
                             &self->as->config.policies.batch);
    if ( err.code != AEROSPIKE_OK ) {
        goto CLEANUP;
    }

    has_batch_index = aerospike_has_batch_index(self->as);
    if (has_batch_index) {
        py_recs = batch_select_aerospike_batch_read(&err, self, py_keys, batch_policy_p, filter_bins, bins_size);
    } else {
        py_recs = batch_select_aerospike_batch_get(&err, self, py_keys, batch_policy_p, filter_bins, bins_size);
    }

CLEANUP:

    if (filter_bins != NULL) {
        free(filter_bins);
    }

    // DECREFed all the unicode objects stored in Pool
    for ( i = 0; i< u_objs.size; i++) {
        Py_DECREF(u_objs.ob[i]);
    }

    if ( err.code != AEROSPIKE_OK ) {
        PyObject * py_err = NULL;
        error_to_pyobject(&err, &py_err);
        PyObject *exception_type = raise_exception(&err);
        if(PyObject_HasAttrString(exception_type, "key")) {
            PyObject_SetAttrString(exception_type, "key", py_keys);
        }
        if(PyObject_HasAttrString(exception_type, "bin")) {
            PyObject_SetAttrString(exception_type, "bin", Py_None);
        }
        PyErr_SetObject(exception_type, py_err);
        Py_DECREF(py_err);
        return NULL;
    }
    return py_recs;
}
Example #20
0
static int droprule_cvt(PyObject * input, int *value)
{
    PyObject *seq = NULL;
    int i;
    int rule = 0;

    if (input == Py_None) {
	/* Leave as default */
	return 1;
    }
    else if (PyInt_Check(input)) {
	*value = PyInt_AsLong(input);
	return 1;
    }
    else if (PyString_Check(input)) {
	/* Comma-separated string */
	seq = PyObject_CallMethod(input, "split", "s", ",");
	if (seq == NULL || !PySequence_Check(seq))
	    goto fail;
    }
    else if (PyUnicode_Check(input)) {
	/* Comma-separated string */
	PyObject *s;
	int ret;

	s = PyUnicode_AsASCIIString(input);
	if (s == NULL) {
	    goto fail;
	}
	ret = droprule_cvt(s, value);
	Py_DECREF(s);
	return ret;
    }
    else if (PySequence_Check(input)) {
	/* Sequence of strings or integers */
	seq = input;
	Py_INCREF(seq);
    }
    else {
	PyErr_SetString(PyExc_ValueError, "invalid value for drop rule");
	goto fail;
    }

    /* OR multiple values together */
    for (i = 0; i < PySequence_Size(seq); ++i) {
	PyObject *item;
	int one_value;

	item = PySequence_ITEM(seq, i);
	if (item == NULL) {
	    goto fail;
	}
	if (!droprule_one_cvt(item, &one_value)) {
	    Py_DECREF(item);
	    goto fail;
	}
	Py_DECREF(item);
	rule |= one_value;
    }
    Py_DECREF(seq);

    *value = rule;
    return 1;

  fail:
    Py_XDECREF(seq);
    return 0;
}
Example #21
0
void XBPyThread::Process()
{
  CLog::Log(LOGDEBUG,"Python thread: start processing");

  int m_Py_file_input = Py_file_input;

  // get the global lock
  PyEval_AcquireLock();
  PyThreadState* state = Py_NewInterpreter();
  if (!state)
  {
    PyEval_ReleaseLock();
    CLog::Log(LOGERROR,"Python thread: FAILED to get thread state!");
    return;
  }
  // swap in my thread state
  PyThreadState_Swap(state);

  m_pExecuter->InitializeInterpreter(addon);

  CLog::Log(LOGDEBUG, "%s - The source file to load is %s", __FUNCTION__, m_source);

  // get path from script file name and add python path's
  // this is used for python so it will search modules from script path first
  CStdString scriptDir;
  URIUtils::GetDirectory(_P(m_source), scriptDir);
  URIUtils::RemoveSlashAtEnd(scriptDir);
  CStdString path = scriptDir;

  // add on any addon modules the user has installed
  ADDON::VECADDONS addons;
  ADDON::CAddonMgr::Get().GetAddons(ADDON::ADDON_SCRIPT_MODULE, addons);
  for (unsigned int i = 0; i < addons.size(); ++i)
#ifdef TARGET_WINDOWS
  {
    CStdString strTmp(_P(addons[i]->LibPath()));
    g_charsetConverter.utf8ToSystem(strTmp);
    path += PY_PATH_SEP + strTmp;
  }
#else
    path += PY_PATH_SEP + _P(addons[i]->LibPath());
#endif

  // and add on whatever our default path is
  path += PY_PATH_SEP;

  // we want to use sys.path so it includes site-packages
  // if this fails, default to using Py_GetPath
  PyObject *sysMod(PyImport_ImportModule((char*)"sys")); // must call Py_DECREF when finished
  PyObject *sysModDict(PyModule_GetDict(sysMod)); // borrowed ref, no need to delete
  PyObject *pathObj(PyDict_GetItemString(sysModDict, "path")); // borrowed ref, no need to delete

  if( pathObj && PyList_Check(pathObj) )
  {
    for( int i = 0; i < PyList_Size(pathObj); i++ )
    {
      PyObject *e = PyList_GetItem(pathObj, i); // borrowed ref, no need to delete
      if( e && PyString_Check(e) )
      {
          path += PyString_AsString(e); // returns internal data, don't delete or modify
          path += PY_PATH_SEP;
      }
    }
  }
  else
  {
    path += Py_GetPath();
  }
  Py_DECREF(sysMod); // release ref to sysMod

  // set current directory and python's path.
  if (m_argv != NULL)
    PySys_SetArgv(m_argc, m_argv);

  CLog::Log(LOGDEBUG, "%s - Setting the Python path to %s", __FUNCTION__, path.c_str());

  PySys_SetPath((char *)path.c_str());

  CLog::Log(LOGDEBUG, "%s - Entering source directory %s", __FUNCTION__, scriptDir.c_str());

  PyObject* module = PyImport_AddModule((char*)"__main__");
  PyObject* moduleDict = PyModule_GetDict(module);

  // when we are done initing we store thread state so we can be aborted
  PyThreadState_Swap(NULL);
  PyEval_ReleaseLock();

  // we need to check if we was asked to abort before we had inited
  bool stopping = false;
  { CSingleLock lock(m_pExecuter->m_critSection);
    m_threadState = state;
    stopping = m_stopping;
  }

  PyEval_AcquireLock();
  PyThreadState_Swap(state);

  if (!stopping)
  {
    if (m_type == 'F')
    {
      // run script from file
      // We need to have python open the file because on Windows the DLL that python
      //  is linked against may not be the DLL that xbmc is linked against so
      //  passing a FILE* to python from an fopen has the potential to crash.
      PyObject* file = PyFile_FromString((char *) _P(m_source).c_str(), (char*)"r");
      FILE *fp = PyFile_AsFile(file);

      if (fp)
      {
        PyObject *f = PyString_FromString(_P(m_source).c_str());
        PyDict_SetItemString(moduleDict, "__file__", f);
        if (addon.get() != NULL)
        {
          PyObject *pyaddonid = PyString_FromString(addon->ID().c_str());
          PyDict_SetItemString(moduleDict, "__xbmcaddonid__", pyaddonid);

          CStdString version = ADDON::GetXbmcApiVersionDependency(addon);
          PyObject *pyxbmcapiversion = PyString_FromString(version.c_str());
          PyDict_SetItemString(moduleDict, "__xbmcapiversion__", pyxbmcapiversion);

          CLog::Log(LOGDEBUG,"Instantiating addon using automatically obtained id of \"%s\" dependent on version %s of the xbmc.python api",addon->ID().c_str(),version.c_str());
        }
        Py_DECREF(f);
        PyRun_FileExFlags(fp, _P(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict,1,NULL);
      }
      else
        CLog::Log(LOGERROR, "%s not found!", m_source);
    }
    else
    {
      //run script
      PyRun_String(m_source, m_Py_file_input, moduleDict, moduleDict);
    }
  }

  if (!PyErr_Occurred())
    CLog::Log(LOGINFO, "Scriptresult: Success");
  else if (PyErr_ExceptionMatches(PyExc_SystemExit))
    CLog::Log(LOGINFO, "Scriptresult: Aborted");
  else
  {
    PyObject* exc_type;
    PyObject* exc_value;
    PyObject* exc_traceback;
    PyObject* pystring;
    pystring = NULL;

    PyErr_Fetch(&exc_type, &exc_value, &exc_traceback);
    if (exc_type == 0 && exc_value == 0 && exc_traceback == 0)
    {
      CLog::Log(LOGINFO, "Strange: No Python exception occured");
    }
    else
    {
      if (exc_type != NULL && (pystring = PyObject_Str(exc_type)) != NULL && (PyString_Check(pystring)))
      {
          PyObject *tracebackModule;

          CLog::Log(LOGINFO, "-->Python script returned the following error<--");
          CLog::Log(LOGERROR, "Error Type: %s", PyString_AsString(PyObject_Str(exc_type)));
          if (PyObject_Str(exc_value))
            CLog::Log(LOGERROR, "Error Contents: %s", PyString_AsString(PyObject_Str(exc_value)));

          tracebackModule = PyImport_ImportModule((char*)"traceback");
          if (tracebackModule != NULL)
          {
            PyObject *tbList, *emptyString, *strRetval;

            tbList = PyObject_CallMethod(tracebackModule, (char*)"format_exception", (char*)"OOO", exc_type, exc_value == NULL ? Py_None : exc_value, exc_traceback == NULL ? Py_None : exc_traceback);
            emptyString = PyString_FromString("");
            strRetval = PyObject_CallMethod(emptyString, (char*)"join", (char*)"O", tbList);

            CLog::Log(LOGERROR, "%s", PyString_AsString(strRetval));

            Py_DECREF(tbList);
            Py_DECREF(emptyString);
            Py_DECREF(strRetval);
            Py_DECREF(tracebackModule);
          }
          CLog::Log(LOGINFO, "-->End of Python script error report<--");
      }
      else
      {
        pystring = NULL;
        CLog::Log(LOGINFO, "<unknown exception type>");
      }

      PYXBMC::PyXBMCGUILock();
      CGUIDialogKaiToast *pDlgToast = (CGUIDialogKaiToast*)g_windowManager.GetWindow(WINDOW_DIALOG_KAI_TOAST);
      if (pDlgToast)
      {
        CStdString desc;
        CStdString path;
        CStdString script;
        URIUtils::Split(m_source, path, script);
        if (script.Equals("default.py"))
        {
          CStdString path2;
          URIUtils::RemoveSlashAtEnd(path);
          URIUtils::Split(path, path2, script);
        }

        desc.Format(g_localizeStrings.Get(2100), script);
        pDlgToast->QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(257), desc);
      }
      PYXBMC::PyXBMCGUIUnlock();
    }

    Py_XDECREF(exc_type);
    Py_XDECREF(exc_value); // caller owns all 3
    Py_XDECREF(exc_traceback); // already NULL'd out
    Py_XDECREF(pystring);
  }

  PyObject *m = PyImport_AddModule((char*)"xbmc");
  if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1)))
    CLog::Log(LOGERROR, "Scriptresult: failed to set abortRequested");

  // make sure all sub threads have finished
  for(PyThreadState* s = state->interp->tstate_head, *old = NULL; s;)
  {
    if(s == state)
    {
      s = s->next;
      continue;
    }
    if(old != s)
    {
      CLog::Log(LOGINFO, "Scriptresult: Waiting on thread %"PRIu64, (uint64_t)s->thread_id);
      old = s;
    }

    CPyThreadState pyState;
    Sleep(100);
    pyState.Restore();

    s = state->interp->tstate_head;
  }

  // pending calls must be cleared out
  PyXBMC_ClearPendingCalls(state);

  PyThreadState_Swap(NULL);
  PyEval_ReleaseLock();

  //set stopped event - this allows ::stop to run and kill remaining threads
  //this event has to be fired without holding m_pExecuter->m_critSection
  //before
  //Also the GIL (PyEval_AcquireLock) must not be held
  //if not obeyed there is still no deadlock because ::stop waits with timeout (smart one!)
  stoppedEvent.Set();

  { CSingleLock lock(m_pExecuter->m_critSection);
    m_threadState = NULL;
  }

  PyEval_AcquireLock();
  PyThreadState_Swap(state);

  m_pExecuter->DeInitializeInterpreter();

  Py_EndInterpreter(state);
  PyThreadState_Swap(NULL);

  PyEval_ReleaseLock();
}
Example #22
0
static PyObject *
csv_writerow(WriterObj *self, PyObject *seq)
{
        DialectObj *dialect = self->dialect;
	int len, i;

	if (!PySequence_Check(seq))
		return PyErr_Format(error_obj, "sequence expected");

	len = PySequence_Length(seq);
	if (len < 0)
		return NULL;

	/* Join all fields in internal buffer.
	 */
	join_reset(self);
	for (i = 0; i < len; i++) {
		PyObject *field;
		int append_ok;
		int quoted;

		field = PySequence_GetItem(seq, i);
		if (field == NULL)
			return NULL;

		quoted = 0;
		if (dialect->quoting == QUOTE_NONNUMERIC) {
			PyObject *num;

			num = PyNumber_Float(field);
			if (num == NULL) {
				quoted = 1;
				PyErr_Clear();
			}
			else {
				Py_DECREF(num);
			}
		}

		if (PyString_Check(field)) {
			append_ok = join_append(self,
						PyString_AS_STRING(field),
                                                &quoted, len == 1);
			Py_DECREF(field);
		}
		else if (field == Py_None) {
			append_ok = join_append(self, "", &quoted, len == 1);
			Py_DECREF(field);
		}
		else {
			PyObject *str;

			str = PyObject_Str(field);
			Py_DECREF(field);
			if (str == NULL)
				return NULL;

			append_ok = join_append(self, PyString_AS_STRING(str), 
                                                &quoted, len == 1);
			Py_DECREF(str);
		}
		if (!append_ok)
			return NULL;
	}

	/* Add line terminator.
	 */
	if (!join_append_lineterminator(self))
		return 0;

	return PyObject_CallFunction(self->writeline, 
                                     "(s#)", self->rec, self->rec_len);
}
static PyObject *
strop_joinfields(PyObject *self, PyObject *args)
{
    PyObject *seq;
    char *sep = NULL;
    Py_ssize_t seqlen, seplen = 0;
    Py_ssize_t i, reslen = 0, slen = 0, sz = 100;
    PyObject *res = NULL;
    char* p = NULL;
    ssizeargfunc getitemfunc;

    WARN;
    if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen))
        return NULL;
    if (sep == NULL) {
        sep = " ";
        seplen = 1;
    }

    seqlen = PySequence_Size(seq);
    if (seqlen < 0 && PyErr_Occurred())
        return NULL;

    if (seqlen == 1) {
        /* Optimization if there's only one item */
        PyObject *item = PySequence_GetItem(seq, 0);
        if (item && !PyString_Check(item)) {
            PyErr_SetString(PyExc_TypeError,
                     "first argument must be sequence of strings");
            Py_DECREF(item);
            return NULL;
        }
        return item;
    }

    if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
        return NULL;
    p = PyString_AsString(res);

    /* optimize for lists, since it's the most common case.  all others
     * (tuples and arbitrary sequences) just use the sequence abstract
     * interface.
     */
    if (PyList_Check(seq)) {
        for (i = 0; i < seqlen; i++) {
            PyObject *item = PyList_GET_ITEM(seq, i);
            if (!PyString_Check(item)) {
                PyErr_SetString(PyExc_TypeError,
                "first argument must be sequence of strings");
                Py_DECREF(res);
                return NULL;
            }
            slen = PyString_GET_SIZE(item);
            if (slen > PY_SSIZE_T_MAX - reslen ||
                seplen > PY_SSIZE_T_MAX - reslen - seplen) {
                PyErr_SetString(PyExc_OverflowError,
                                "input too long");
                Py_DECREF(res);
                return NULL;
            }
            while (reslen + slen + seplen >= sz) {
                if (_PyString_Resize(&res, sz * 2) < 0)
                    return NULL;
                sz *= 2;
                p = PyString_AsString(res) + reslen;
            }
            if (i > 0) {
                memcpy(p, sep, seplen);
                p += seplen;
                reslen += seplen;
            }
            memcpy(p, PyString_AS_STRING(item), slen);
            p += slen;
            reslen += slen;
        }
        _PyString_Resize(&res, reslen);
        return res;
    }

    if (seq->ob_type->tp_as_sequence == NULL ||
             (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
    {
        PyErr_SetString(PyExc_TypeError,
                        "first argument must be a sequence");
        return NULL;
    }
    /* This is now type safe */
    for (i = 0; i < seqlen; i++) {
        PyObject *item = getitemfunc(seq, i);
        if (!item || !PyString_Check(item)) {
            PyErr_SetString(PyExc_TypeError,
                     "first argument must be sequence of strings");
            Py_DECREF(res);
            Py_XDECREF(item);
            return NULL;
        }
        slen = PyString_GET_SIZE(item);
        if (slen > PY_SSIZE_T_MAX - reslen ||
            seplen > PY_SSIZE_T_MAX - reslen - seplen) {
            PyErr_SetString(PyExc_OverflowError,
                            "input too long");
            Py_DECREF(res);
            Py_XDECREF(item);
            return NULL;
        }
        while (reslen + slen + seplen >= sz) {
            if (_PyString_Resize(&res, sz * 2) < 0) {
                Py_DECREF(item);
                return NULL;
            }
            sz *= 2;
            p = PyString_AsString(res) + reslen;
        }
        if (i > 0) {
            memcpy(p, sep, seplen);
            p += seplen;
            reslen += seplen;
        }
        memcpy(p, PyString_AS_STRING(item), slen);
        p += slen;
        reslen += slen;
        Py_DECREF(item);
    }
    _PyString_Resize(&res, reslen);
    return res;
}
Example #24
0
static int
dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs)
{
        PyObject *dialect = NULL, *name_obj, *value_obj;

	self->quotechar = '"';
	self->delimiter = ',';
	self->escapechar = '\0';
	self->skipinitialspace = 0;
        Py_XDECREF(self->lineterminator);
	self->lineterminator = PyString_FromString("\r\n");
        if (self->lineterminator == NULL)
                return -1;
	self->quoting = QUOTE_MINIMAL;
	self->doublequote = 1;
	self->strict = 0;

	if (!PyArg_UnpackTuple(args, "", 0, 1, &dialect))
                return -1;
        Py_XINCREF(dialect);
        if (kwargs != NULL) {
                PyObject * key = PyString_FromString("dialect");
                PyObject * d;

                d = PyDict_GetItem(kwargs, key);
                if (d) {
                        Py_INCREF(d);
                        Py_XDECREF(dialect);
                        PyDict_DelItem(kwargs, key);
                        dialect = d;
                }
                Py_DECREF(key);
        }
        if (dialect != NULL) {
                int i;
                PyObject * dir_list;

                /* If dialect is a string, look it up in our registry */
                if (PyString_Check(dialect)
#ifdef Py_USING_UNICODE
		    || PyUnicode_Check(dialect)
#endif
			) {
                        PyObject * new_dia;
                        new_dia = get_dialect_from_registry(dialect);
                        Py_DECREF(dialect);
                        if (new_dia == NULL)
                                return -1;
                        dialect = new_dia;
                }
                /* A class rather than an instance? Instantiate */
                if (PyObject_TypeCheck(dialect, &PyClass_Type)) {
                        PyObject * new_dia;
                        new_dia = PyObject_CallFunction(dialect, "");
                        Py_DECREF(dialect);
                        if (new_dia == NULL)
                                return -1;
                        dialect = new_dia;
                }
                /* Make sure we finally have an instance */
                if (!PyInstance_Check(dialect) ||
                    (dir_list = PyObject_Dir(dialect)) == NULL) {
                        PyErr_SetString(PyExc_TypeError,
                                        "dialect must be an instance");
                        Py_DECREF(dialect);
                        return -1;
                }
                /* And extract the attributes */
                for (i = 0; i < PyList_GET_SIZE(dir_list); ++i) {
			char *s;
                        name_obj = PyList_GET_ITEM(dir_list, i);
			s = PyString_AsString(name_obj);
			if (s == NULL)
				return -1;
                        if (s[0] == '_')
                                continue;
                        value_obj = PyObject_GetAttr(dialect, name_obj);
                        if (value_obj) {
                                if (PyObject_SetAttr((PyObject *)self, 
                                                     name_obj, value_obj)) {
					Py_DECREF(value_obj);
                                        Py_DECREF(dir_list);
					Py_DECREF(dialect);
                                        return -1;
                                }
                                Py_DECREF(value_obj);
                        }
                }
                Py_DECREF(dir_list);
                Py_DECREF(dialect);
        }
        if (kwargs != NULL) {
                int pos = 0;

                while (PyDict_Next(kwargs, &pos, &name_obj, &value_obj)) {
                        if (PyObject_SetAttr((PyObject *)self, 
                                             name_obj, value_obj))
                                return -1;
                }
        }
        return 0;
}
/**
 * Function for setting scan parameters in scan.
 * Like Scan Priority, Percentage, Concurrent, Nobins
 *
 * @param err                   The as_error to be populated by the function
 *                              with the encountered error if any.
 * @scan_p                      Scan parameter.
 * @py_options                  The user's optional scan options.
 */
void set_scan_options(as_error *err, as_scan* scan_p, PyObject * py_options)
{
	if (!scan_p) {
		as_error_update(err, AEROSPIKE_ERR_CLIENT, "Scan is not initialized");
		return ;
	}

	if (PyDict_Check(py_options)) {
		PyObject *key = NULL, *value = NULL;
		Py_ssize_t pos = 0;
		int64_t val = 0;
		while (PyDict_Next(py_options, &pos, &key, &value)) {
			char *key_name = PyString_AsString(key);
			if (!PyString_Check(key)) {
				as_error_update(err, AEROSPIKE_ERR_PARAM, "Policy key must be string");
				break;
			}

			if (strcmp("priority", key_name) == 0) {
				if (!PyInt_Check(value)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value(type) for priority");
					break;
				}
				val = (int64_t) PyInt_AsLong(value);
				if (!as_scan_set_priority(scan_p, val)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Unable to set scan priority");
					break;
				}
			} else if (strcmp("percent", key_name) == 0) {
				if (!PyInt_Check(value)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value(type) for percent");
					break;
				}
				val = (int64_t) PyInt_AsLong(value);
				if (val<0 || val>100) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value for scan percentage");
					break;
				}
				else if (!as_scan_set_percent(scan_p, val)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Unable to set scan percentage");
					break;
				}
			} else if (strcmp("concurrent", key_name) == 0) {
				if (!PyBool_Check(value)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value(type) for concurrent");
					break;
				}
				val = (int64_t)PyObject_IsTrue(value);
				if (val == -1 || (!as_scan_set_concurrent(scan_p, val))) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Unable to set scan percentage");
					break;
				}
			} else if (strcmp("nobins", key_name) == 0) {
				if (!PyBool_Check(value)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value(type) for nobins");
					break;
				}
				val = (int64_t)PyObject_IsTrue(value);
				if (val == -1 || (!as_scan_set_nobins(scan_p, val))) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Unable to set scan nobins");
					break;
				}
			} else if (strcmp("include_ldt", key_name) == 0) {
				if (!PyBool_Check(value)) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value(type) for nobins");
					break;
				}
				val = (int8_t)PyObject_IsTrue(value);
				if (val == -1) {
					as_error_update(err, AEROSPIKE_ERR_PARAM, "Unable to set scan nobins");
					break;
				}
				scan_p->include_ldt = val;
			} else {
				as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid value for scan options");
				break;
			}
		}
	} else {
		as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid option(type)");
	}
}
Example #26
0
PyCodeObject *
PyCode_New(int argcount, int nlocals, int stacksize, int flags,
	   PyObject *code, PyObject *consts, PyObject *names,
	   PyObject *varnames, PyObject *freevars, PyObject *cellvars,
	   PyObject *filename, PyObject *name, int firstlineno,
	   PyObject *lnotab)
{
	PyCodeObject *co;
	Py_ssize_t i;
	/* Check argument types */
	if (argcount < 0 || nlocals < 0 ||
	    code == NULL ||
	    consts == NULL || !PyTuple_Check(consts) ||
	    names == NULL || !PyTuple_Check(names) ||
	    varnames == NULL || !PyTuple_Check(varnames) ||
	    freevars == NULL || !PyTuple_Check(freevars) ||
	    cellvars == NULL || !PyTuple_Check(cellvars) ||
	    name == NULL || !PyString_Check(name) ||
	    filename == NULL || !PyString_Check(filename) ||
	    lnotab == NULL || !PyString_Check(lnotab) ||
	    !PyObject_CheckReadBuffer(code)) {
		PyErr_BadInternalCall();
		return NULL;
	}
	intern_strings(names);
	intern_strings(varnames);
	intern_strings(freevars);
	intern_strings(cellvars);
	/* Intern selected string constants */
	for (i = PyTuple_Size(consts); --i >= 0; ) {
		PyObject *v = PyTuple_GetItem(consts, i);
		if (!PyString_Check(v))
			continue;
		if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
			continue;
		PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
	}
	co = PyObject_NEW(PyCodeObject, &PyCode_Type);
	if (co != NULL) {
		co->co_argcount = argcount;
		co->co_nlocals = nlocals;
		co->co_stacksize = stacksize;
		co->co_flags = flags;
		Py_INCREF(code);
		co->co_code = code;
		Py_INCREF(consts);
		co->co_consts = consts;
		Py_INCREF(names);
		co->co_names = names;
		Py_INCREF(varnames);
		co->co_varnames = varnames;
		Py_INCREF(freevars);
		co->co_freevars = freevars;
		Py_INCREF(cellvars);
		co->co_cellvars = cellvars;
		Py_INCREF(filename);
		co->co_filename = filename;
		Py_INCREF(name);
		co->co_name = name;
		co->co_firstlineno = firstlineno;
		Py_INCREF(lnotab);
		co->co_lnotab = lnotab;
                co->co_zombieframe = NULL;
	}
	return co;
}
Example #27
0
static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
{
	struct tree_item *qsort_entries = NULL;
	int num_entries, n = 0, i;
	PyObject *ret, *key, *value, *py_mode, *py_sha;
	Py_ssize_t pos = 0;

	if (!PyDict_Check(entries)) {
		PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
		goto error;
	}

	num_entries = PyDict_Size(entries);
	if (PyErr_Occurred())
		goto error;
	qsort_entries = PyMem_New(struct tree_item, num_entries);
	if (!qsort_entries) {
		PyErr_NoMemory();
		goto error;
	}

	while (PyDict_Next(entries, &pos, &key, &value)) {
		if (!PyString_Check(key)) {
			PyErr_SetString(PyExc_TypeError, "Name is not a string");
			goto error;
		}

		if (PyTuple_Size(value) != 2) {
			PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
			goto error;
		}

		py_mode = PyTuple_GET_ITEM(value, 0);
		if (!PyInt_Check(py_mode)) {
			PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
			goto error;
		}

		py_sha = PyTuple_GET_ITEM(value, 1);
		if (!PyString_Check(py_sha)) {
			PyErr_SetString(PyExc_TypeError, "SHA is not a string");
			goto error;
		}
		qsort_entries[n].name = PyString_AS_STRING(key);
		qsort_entries[n].mode = PyInt_AS_LONG(py_mode);

		qsort_entries[n].tuple = PyObject_CallFunctionObjArgs(
				tree_entry_cls, key, py_mode, py_sha, NULL);
		if (qsort_entries[n].tuple == NULL)
			goto error;
		n++;
	}

	qsort(qsort_entries, num_entries, sizeof(struct tree_item), cmp_tree_item);

	ret = PyList_New(num_entries);
	if (ret == NULL) {
		PyErr_NoMemory();
		goto error;
	}

	for (i = 0; i < num_entries; i++) {
		PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
	}
	PyMem_Free(qsort_entries);
	return ret;

error:
	for (i = 0; i < n; i++) {
		Py_XDECREF(qsort_entries[i].tuple);
	}
	PyMem_Free(qsort_entries);
	return NULL;
}
/**
 *********************************************************************
 * This function will invoke aerospike_batch_get_bins to get filtered
 * bins from all the records in a batches.
 *
 * @param self                    AerospikeClient object
 * @param py_keys                 List of keys passed on by user
 * @param py_bins                 List of filter bins passed on by user
 * @param py_policy               User specified Policy dictionary
 *
 *********************************************************************
 **/
	static
PyObject * AerospikeClient_Select_Many_Invoke(
		AerospikeClient * self,
		PyObject * py_keys, PyObject * py_bins, PyObject * py_policy)
{
	// Python Return Value
	PyObject * py_recs = PyDict_New();

	// Aerospike Client Arguments
	as_error err;
	as_batch batch;
	as_policy_batch policy;
	as_policy_batch * batch_policy_p = NULL;
	Py_ssize_t bins_size = 0;
	char **filter_bins = NULL;

	// Unicode object's pool
	UnicodePyObjects u_objs;
	u_objs.size = 0;
	int i = 0;

	// Initialisation flags
	bool batch_initialised = false;

	// Initialize error
	as_error_init(&err);

	if (!self || !self->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	if (!self->is_conn_16) {
		as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
		goto CLEANUP;
	}

	// Convert python keys list to as_key ** and add it to as_batch.keys
	// keys can be specified in PyList or PyTuple
	if ( py_keys != NULL && PyList_Check(py_keys) ) {
		Py_ssize_t size = PyList_Size(py_keys);

		as_batch_init(&batch, size);
		// Batch object initialised
		batch_initialised = true;

		for ( i = 0; i < size; i++ ) {

			PyObject * py_key = PyList_GetItem(py_keys, i);

			if ( !PyTuple_Check(py_key) ){
				as_error_update(&err, AEROSPIKE_ERR_PARAM, "Key should be a tuple.");
				goto CLEANUP;
			}

			pyobject_to_key(&err, py_key, as_batch_keyat(&batch, i));

			if ( err.code != AEROSPIKE_OK ) {
				goto CLEANUP;
			}
		}
	}
	else if ( py_keys != NULL && PyTuple_Check(py_keys) ) {
		Py_ssize_t size = PyTuple_Size(py_keys);

		as_batch_init(&batch, size);
		// Batch object initialised.
		batch_initialised = true;

		for ( i = 0; i < size; i++ ) {
			PyObject * py_key = PyTuple_GetItem(py_keys, i);

			if ( !PyTuple_Check(py_key) ){
				as_error_update(&err, AEROSPIKE_ERR_PARAM, "Key should be a tuple.");
				goto CLEANUP;
			}

			pyobject_to_key(&err, py_key, as_batch_keyat(&batch, i));

			if ( err.code != AEROSPIKE_OK ) {
				goto CLEANUP;
			}
		}
	}
	else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple.");
		goto CLEANUP;
	}

	// Check the type of bins and get it's size
	// i.e. number of bins provided
	if (py_bins != NULL && PyList_Check(py_bins)){
		bins_size    = PyList_Size(py_bins);
	}
	else if (py_bins != NULL && PyTuple_Check(py_bins)){
		bins_size    = PyTuple_Size(py_bins);
	}
	else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter bins should be specified as a list or tuple.");
		goto CLEANUP;
	}

	filter_bins = (char **)malloc(sizeof(long int) * bins_size);

	for (i = 0; i < bins_size; i++){
		PyObject *py_bin = NULL;
		if(PyList_Check(py_bins)){
			py_bin = PyList_GetItem(py_bins, i);
		}
		if(PyTuple_Check(py_bins)){
			py_bin = PyTuple_GetItem(py_bins, i);
		}
		if (PyUnicode_Check(py_bin)){
			// Store the unicode object into a pool
			// It is DECREFed at later stages
			// So, no need of DECREF here.
			filter_bins[i] = PyString_AsString(
					store_unicode_bins(&u_objs, PyUnicode_AsUTF8String(py_bin)));
		}
		else if (PyString_Check(py_bin)){
			filter_bins[i]    = PyString_AsString(py_bin);
		}
		else{
			as_error_update(&err, AEROSPIKE_ERR_PARAM, "Bin name should be a string or unicode string.");
			goto CLEANUP;
		}
	}

	// Convert python policy object to as_policy_batch
	pyobject_to_policy_batch(&err, py_policy, &policy, &batch_policy_p,
			&self->as->config.policies.batch);
	if ( err.code != AEROSPIKE_OK ) {
		goto CLEANUP;
	}

	// Invoke C-client API
	aerospike_batch_get_bins(self->as, &err, batch_policy_p,
		&batch, (const char **) filter_bins, bins_size,
		(aerospike_batch_read_callback) batch_select_cb,
		py_recs);

CLEANUP:

	if (filter_bins != NULL){
		free(filter_bins);
	}

	// DECREFed all the unicode objects stored in Pool
	for ( i = 0; i< u_objs.size; i++){
		Py_DECREF(u_objs.ob[i]);
	}

	if (batch_initialised == true){
		// We should destroy batch object as we are using 'as_batch_init' for initialisation
		// Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction
		// is necessary.
		as_batch_destroy(&batch);
	}

	if ( err.code != AEROSPIKE_OK ) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		if(PyObject_HasAttrString(exception_type, "key")) {
			PyObject_SetAttrString(exception_type, "key", py_keys);
		} 
		if(PyObject_HasAttrString(exception_type, "bin")) {
			PyObject_SetAttrString(exception_type, "bin", Py_None);
		}
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		return NULL;
	}
	return py_recs;
}
Example #29
0
File: pydsdb.c Project: GSam/samba
/*
  normalise a ldb attribute list
 */
static PyObject *py_dsdb_normalise_attributes(PyObject *self, PyObject *args)
{
	PyObject *py_ldb, *el_list, *py_ret;
	struct ldb_context *ldb;
	char *ldap_display_name;
	const struct dsdb_attribute *a;
	struct dsdb_schema *schema;
	struct dsdb_syntax_ctx syntax_ctx;
	struct ldb_message_element *el, *new_el;
	struct drsuapi_DsReplicaAttribute *attr;
	PyLdbMessageElementObject *ret;
	TALLOC_CTX *tmp_ctx;
	WERROR werr;
	Py_ssize_t i;
	PyTypeObject *py_type = NULL;
	PyObject *module = NULL;

	if (!PyArg_ParseTuple(args, "OsO", &py_ldb, &ldap_display_name, &el_list)) {
		return NULL;
	}

	PyErr_LDB_OR_RAISE(py_ldb, ldb);

	schema = dsdb_get_schema(ldb, NULL);
	if (!schema) {
		PyErr_SetString(PyExc_RuntimeError, "Failed to find a schema from ldb");
		return NULL;
	}

	a = dsdb_attribute_by_lDAPDisplayName(schema, ldap_display_name);
	if (a == NULL) {
		PyErr_Format(PyExc_KeyError, "Failed to find attribute '%s'", ldap_display_name);
		return NULL;
	}

	dsdb_syntax_ctx_init(&syntax_ctx, ldb, schema);
	syntax_ctx.is_schema_nc = false;

	tmp_ctx = talloc_new(ldb);
	if (tmp_ctx == NULL) {
		PyErr_NoMemory();
		return NULL;
	}

	if (!PyList_Check(el_list)) {
		if (!py_check_dcerpc_type(el_list, "ldb", "MessageElement")) {
			PyErr_SetString(py_ldb_get_exception(),
					"list of strings or ldb MessageElement object required");
			return NULL;
		}
		/*
		 * NOTE:
		 * el may not be a valid talloc context, it
		 * could be part of an array
		 */
		el = pyldb_MessageElement_AsMessageElement(el_list);
	} else {
		el = talloc_zero(tmp_ctx, struct ldb_message_element);
		if (el == NULL) {
			PyErr_NoMemory();
			talloc_free(tmp_ctx);
			return NULL;
		}

		el->name = ldap_display_name;
		el->num_values = PyList_Size(el_list);

		el->values = talloc_array(el, struct ldb_val, el->num_values);
		if (el->values == NULL) {
			PyErr_NoMemory();
			talloc_free(tmp_ctx);
			return NULL;
		}

		for (i = 0; i < el->num_values; i++) {
			PyObject *item = PyList_GetItem(el_list, i);
			if (!PyString_Check(item)) {
				PyErr_Format(PyExc_TypeError, "ldif_elements should be strings");
				talloc_free(tmp_ctx);
				return NULL;
			}
			el->values[i].data = (uint8_t *)PyString_AsString(item);
			el->values[i].length = PyString_Size(item);
		}
	}

	new_el = talloc_zero(tmp_ctx, struct ldb_message_element);
	if (new_el == NULL) {
		PyErr_NoMemory();
		talloc_free(tmp_ctx);
		return NULL;
	}

	/* Normalise "objectClass" attribute if needed */
	if (ldb_attr_cmp(a->lDAPDisplayName, "objectClass") == 0) {
		int iret;
		iret = dsdb_sort_objectClass_attr(ldb, schema, el, new_el, new_el);
		if (iret != LDB_SUCCESS) {
			PyErr_SetString(PyExc_RuntimeError, ldb_errstring(ldb));
			talloc_free(tmp_ctx);
			return NULL;
		}
	}

	/* first run ldb_to_drsuapi, then convert back again. This has
	 * the effect of normalising the attributes
	 */

	attr = talloc_zero(tmp_ctx, struct drsuapi_DsReplicaAttribute);
	if (attr == NULL) {
		PyErr_NoMemory();
		talloc_free(tmp_ctx);
		return NULL;
	}

	werr = a->syntax->ldb_to_drsuapi(&syntax_ctx, a, el, attr, attr);
	PyErr_WERROR_NOT_OK_RAISE(werr);

	/* now convert back again */
	werr = a->syntax->drsuapi_to_ldb(&syntax_ctx, a, attr, new_el, new_el);
	PyErr_WERROR_NOT_OK_RAISE(werr);

	module = PyImport_ImportModule("ldb");
	if (module == NULL) {
		return NULL;
	}

	py_type = (PyTypeObject *)PyObject_GetAttrString(module, "MessageElement");
	if (py_type == NULL) {
		return NULL;
	}
	py_ret = py_type->tp_alloc(py_type, 0);
	ret = (PyLdbMessageElementObject *)py_ret;

	ret->mem_ctx = talloc_new(NULL);
	if (talloc_reference(ret->mem_ctx, new_el) == NULL) {
		PyErr_NoMemory();
		return NULL;
	}
	ret->el = new_el;

	talloc_free(tmp_ctx);

	return py_ret;
}
Example #30
0
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
    const char *s, *start;
    char *end;
    double x=0.0, y=0.0, z;
    int got_bracket=0;
#ifdef Py_USING_UNICODE
    char *s_buffer = NULL;
#endif
    Py_ssize_t len;

    if (PyString_Check(v)) {
        s = PyString_AS_STRING(v);
        len = PyString_GET_SIZE(v);
    }
#ifdef Py_USING_UNICODE
    else if (PyUnicode_Check(v)) {
        s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
        if (s_buffer == NULL)
            return PyErr_NoMemory();
        if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
                                    PyUnicode_GET_SIZE(v),
                                    s_buffer,
                                    NULL))
            goto error;
        s = s_buffer;
        len = strlen(s);
    }
#endif
    else if (PyObject_AsCharBuffer(v, &s, &len)) {
        PyErr_SetString(PyExc_TypeError,
                        "complex() arg is not a string");
        return NULL;
    }

    /* position on first nonblank */
    start = s;
    while (Py_ISSPACE(*s))
        s++;
    if (*s == '(') {
        /* Skip over possible bracket from repr(). */
        got_bracket = 1;
        s++;
        while (Py_ISSPACE(*s))
            s++;
    }

    /* a valid complex string usually takes one of the three forms:

         <float>                  - real part only
         <float>j                 - imaginary part only
         <float><signed-float>j   - real and imaginary parts

       where <float> represents any numeric string that's accepted by the
       float constructor (including 'nan', 'inf', 'infinity', etc.), and
       <signed-float> is any string of the form <float> whose first
       character is '+' or '-'.

       For backwards compatibility, the extra forms

         <float><sign>j
         <sign>j
         j

       are also accepted, though support for these forms may be removed from
       a future version of Python.
    */

    /* first look for forms starting with <float> */
    z = PyOS_string_to_double(s, &end, NULL);
    if (z == -1.0 && PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_ValueError))
            PyErr_Clear();
        else
            goto error;
    }
    if (end != s) {
        /* all 4 forms starting with <float> land here */
        s = end;
        if (*s == '+' || *s == '-') {
            /* <float><signed-float>j | <float><sign>j */
            x = z;
            y = PyOS_string_to_double(s, &end, NULL);
            if (y == -1.0 && PyErr_Occurred()) {
                if (PyErr_ExceptionMatches(PyExc_ValueError))
                    PyErr_Clear();
                else
                    goto error;
            }
            if (end != s)
                /* <float><signed-float>j */
                s = end;
            else {
                /* <float><sign>j */
                y = *s == '+' ? 1.0 : -1.0;
                s++;
            }
            if (!(*s == 'j' || *s == 'J'))
                goto parse_error;
            s++;
        }
        else if (*s == 'j' || *s == 'J') {
            /* <float>j */
            s++;
            y = z;
        }
        else
            /* <float> */
            x = z;
    }
    else {
        /* not starting with <float>; must be <sign>j or j */
        if (*s == '+' || *s == '-') {
            /* <sign>j */
            y = *s == '+' ? 1.0 : -1.0;
            s++;
        }
        else
            /* j */
            y = 1.0;
        if (!(*s == 'j' || *s == 'J'))
            goto parse_error;
        s++;
    }

    /* trailing whitespace and closing bracket */
    while (Py_ISSPACE(*s))
        s++;
    if (got_bracket) {
        /* if there was an opening parenthesis, then the corresponding
           closing parenthesis should be right here */
        if (*s != ')')
            goto parse_error;
        s++;
        while (Py_ISSPACE(*s))
            s++;
    }

    /* we should now be at the end of the string */
    if (s-start != len)
        goto parse_error;


#ifdef Py_USING_UNICODE
    if (s_buffer)
        PyMem_FREE(s_buffer);
#endif
    return complex_subtype_from_doubles(type, x, y);

parse_error:
    PyErr_SetString(PyExc_ValueError,
                    "complex() arg is a malformed string");
error:
#ifdef Py_USING_UNICODE
    if (s_buffer)
        PyMem_FREE(s_buffer);
#endif
    return NULL;
}