Esempio n. 1
0
    bool PythonScript::checkCompileError() {
        if (!PyErr_Occurred())
            return true;

        PyObject* errtype, *errvalue, *traceback;
        PyErr_Fetch(&errtype, &errvalue, &traceback);
        std::string log = "";
        char* msg = nullptr;
        PyObject* obj = nullptr;

        if (PyArg_ParseTuple(errvalue, "sO", &msg, &obj)) {
            int line, col;
            char* code = nullptr;
            char* mod = nullptr;

            if (PyArg_ParseTuple(obj, "siis", &mod, &line, &col, &code)) {
                log = "[" + toString(line) + ":" + toString(col) + "] " + toString(msg) + ": " + toString(code);
            }
        }

        // convert error to string, if it could not be parsed
        if (log.empty()) {
            LogWarn("Failed to parse exception, printing as string:");
            PyObject* s = PyObject_Str(errvalue);

            if (s && PyValueParser::is<std::string>(s)) {
                log = std::string(PyValueParser::parse<std::string>(s));
                Py_XDECREF(s);
            }
        }

        Py_XDECREF(errtype);
        Py_XDECREF(errvalue);
        Py_XDECREF(traceback);
        LogError(log);
        return false;
    }
Esempio n. 2
0
int report_python_error(const char *preamble, int code) {
    PyObject *exc, *val, *tb, *str;
    int ret, issysexit = 0; char *i, *buf; 

    if (!PyErr_Occurred()) return code;
    issysexit = PyErr_ExceptionMatches(PyExc_SystemExit);

    PyErr_Fetch(&exc, &val, &tb);

    if (exc != NULL) {
        PyErr_NormalizeException(&exc, &val, &tb);

        if (issysexit) {
            return (val) ? handle_sysexit(val) : 0;
        }
        if (val != NULL) {
            str = PyObject_Unicode(val);
            if (str == NULL) {
                PyErr_Clear();
                str = PyObject_Str(val);
            }
            i = PyString_AsString(str);
            if (i == NULL) OOM;
            buf = (char*)calloc(strlen(i)+strlen(preamble)+5, sizeof(char));
            if (buf == NULL) OOM;
            sprintf(buf, "%s::%s", preamble, i);
            ret = report_error(buf, code);
            if (buf) free(buf);
            if (tb != NULL) {
                PyErr_Restore(exc, val, tb);
                PyErr_Print();
            }
            return ret;
        }
    }
    return report_error(preamble, code);
}
Esempio n. 3
0
int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
    PyObject *writer, *value, *args, *result;
    _Py_IDENTIFIER(write);

    if (f == NULL) {
        PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
        return -1;
    }
    writer = _PyObject_GetAttrId(f, &PyId_write);
    if (writer == NULL)
        return -1;
    if (flags & Py_PRINT_RAW) {
        value = PyObject_Str(v);
    }
    else
        value = PyObject_Repr(v);
    if (value == NULL) {
        Py_DECREF(writer);
        return -1;
    }
    args = PyTuple_Pack(1, value);
    if (args == NULL) {
        Py_DECREF(value);
        Py_DECREF(writer);
        return -1;
    }
    result = PyEval_CallObject(writer, args);
    Py_DECREF(args);
    Py_DECREF(value);
    Py_DECREF(writer);
    if (result == NULL)
        return -1;
    Py_DECREF(result);
    return 0;
}
Esempio n. 4
0
/* read preview*/
PyObject *
Image_readPreview(PyObject *obj, PyObject *args, PyObject *kwargs)
{
    ImageObject *self = (ImageObject*) obj;

    if (self != NULL)
    {
        PyObject *input = NULL;
        int x = 0;
        int slice = CENTRAL_SLICE;

        if (PyArg_ParseTuple(args, "O|ii", &input, &x, &slice))
        {
            try
            {
              PyObject *pyStr;
              if ((pyStr = PyObject_Str(input)) != NULL)
              {
                  readImagePreview(self->image, PyString_AsString(pyStr), x, slice);
                  Py_RETURN_NONE;
              }
              else
              {
                  PyErr_SetString(PyExc_TypeError,
                                  "Image_readPreview: Expected string or FileName as first argument");
              }
            }
            catch (XmippError &xe)
            {
                PyErr_SetString(PyXmippError, xe.msg.c_str());
            }
        }
        else
          PyErr_SetString(PyXmippError,"Error with input arguments, expected filename and optional size");
    }
    return NULL;
}//function Image_readPreview
Esempio n. 5
0
static PyObject*
PyLorcon2_Context_send_bytes(PyLorcon2_Context *self, PyObject *args, PyObject *kwds)
{
    char *pckt_buffer;
    ssize_t pckt_size, sent;
    PyObject *pckt, *pckt_string;

    if (!PyArg_ParseTuple(args, "O", &pckt))
        return NULL;
    
    if (!self->monitored) {
        PyErr_SetString(PyExc_RuntimeError, "Context must be in monitor/injection-mode");
        return NULL;
    }

    pckt_string = PyObject_Str(pckt);
    if (!pckt_string) {
        PyErr_SetString(PyExc_ValueError, "Failed to get string-representation from object.");
        return NULL;
    }

    if (PyString_AsStringAndSize(pckt_string, &pckt_buffer, &pckt_size)) {
        Py_DECREF(pckt_string);
        return NULL;
    }

    sent = lorcon_send_bytes(self->context, pckt_size, (u_char*)pckt_buffer);
    if (sent < 0) {
        PyErr_SetString(Lorcon2Exception, lorcon_get_error(self->context));
        Py_DECREF(pckt_string);
        return NULL;
    }
    
    Py_DECREF(pckt_string);
    
    return PyInt_FromLong(sent);
}
Esempio n. 6
0
/*
 * Method for the setting items with the [int] operator on pyjlist.  For example,
 * o[i] = v
 */
static int pyjlist_setitem(PyObject *o, Py_ssize_t i, PyObject *v) {
    jmethodID         set      = NULL;
    PyJobject_Object *obj      = (PyJobject_Object*) o;
    JNIEnv           *env      = pyembed_get_env();
    jobject           value    = NULL;

    if(v == Py_None) {
        value = NULL;
    } else {
        value = pyembed_box_py(env, v);
        if(process_java_exception(env)) {
            return -1;
        } else if(!value) {
            /*
             * with the way pyembed_box_py is currently implemented, shouldn't
             * be able to get here
             */
            PyErr_Format(PyExc_TypeError,
                        "__setitem__ received an incompatible type: %s",
                        PyString_AsString(PyObject_Str((PyObject*) Py_TYPE(v))));
            return -1;
        }
    }

    set = (*env)->GetMethodID(env, obj->clazz, "set", "(ILjava/lang/Object;)Ljava/lang/Object;");
    if(process_java_exception(env) || !set) {
        return -1;
    }

    (*env)->CallObjectMethod(env, obj->object, set, (jint) i, value);
    if(process_java_exception(env)) {
        return -1;
    }

    // have to return 0 on success even though it's not documented
    return 0;
}
Esempio n. 7
0
CString ui_assoc_object::repr()
{
	CString csRet;
	static TCHAR *no_repr=_T("<None>");
	TCHAR *py_repr=NULL;
	BOOL bfree_repr=FALSE;

	if (virtualInst == NULL)
		py_repr=no_repr;
	else{
		PyObject *vi_repr=PyObject_Str(virtualInst);
		if (vi_repr==NULL || !PyWinObject_AsTCHAR(vi_repr, &py_repr, FALSE)){
			PyErr_Clear();
			py_repr=no_repr;
			}
		else
			bfree_repr=TRUE;
		Py_XDECREF(vi_repr);
		}
	csRet.Format(_T(" - assoc is %p, vi=%s"), assoc, py_repr);
	if (bfree_repr)
		PyWinObject_FreeTCHAR(py_repr);
	return ui_base_class::repr() + csRet;
}
Esempio n. 8
0
/* getImageSize */
PyObject *
xmipp_getImageSize(PyObject *obj, PyObject *args, PyObject *kwargs)
{
    PyObject *pyValue; //Only used to skip label and value

    if (PyArg_ParseTuple(args, "O", &pyValue))
    {
        try
        {

            PyObject * pyStr = PyObject_Str(pyValue);
            char * str = PyString_AsString(pyStr);
            size_t xdim, ydim, zdim, ndim;
            getImageSize(str, xdim, ydim, zdim, ndim);
            Py_DECREF(pyStr);
            return Py_BuildValue("kkkk", xdim, ydim, zdim, ndim);
        }
        catch (XmippError &xe)
        {
            PyErr_SetString(PyXmippError, xe.msg.c_str());
        }
    }
    return NULL;
}
Esempio n. 9
0
char *scriptFigureToPythonExpr(struct figureData *figure) {
	bool gil=!scriptIsGILAcquired();
	if (gil)
		scriptAcquireGIL();

	static PyObject *str=NULL;
	Py_XDECREF(str); str=NULL;

	PyObject *pyFigure=figureToPython(figure);
	if (pyFigure==Py_None) {
		if (gil)
			scriptReleaseGIL();
		return NULL;
	}

	str=PyObject_Str(pyFigure);
	Py_DECREF(pyFigure);
	char *ret=PyString_AsString(str);

	if (gil)
		scriptReleaseGIL();

	return ret;
}
Esempio n. 10
0
static int
lookup_attribute(attr_dir_object *self, PyObject *key, kdump_attr_ref_t *ref)
{
	PyObject *stringkey;
	char *keystr;
	int ret;

	if (!PyString_Check(key)) {
		stringkey = PyObject_Str(key);
		if (!stringkey)
			return -1;
	} else
		stringkey = key;

	ret = -1;

	keystr = PyString_AsString(stringkey);
	if (keystr) {
		kdump_ctx *ctx = self->kdumpfile->ctx;
		kdump_status status;

		status = kdump_sub_attr_ref(ctx, &self->baseref, keystr, ref);
		if (status == kdump_ok)
			ret = 1;
		else if (status == kdump_nokey)
			ret = 0;
		else
			PyErr_SetString(exception_map(status),
					kdump_err_str(ctx));
	}

	if (stringkey != key)
		Py_DECREF(stringkey);

	return ret;
}
static int
SWIG_Python_AddErrMesg(const char* mesg, int infront)
{
  if (PyErr_Occurred()) {
    PyObject *type = 0;
    PyObject *value = 0;
    PyObject *traceback = 0;
    PyErr_Fetch(&type, &value, &traceback);
    if (value) {
      PyObject *old_str = PyObject_Str(value);
      Py_XINCREF(type);
      PyErr_Clear();
      if (infront) {
	PyErr_Format(type, "%s %s", mesg, PyString_AsString(old_str));
      } else {
	PyErr_Format(type, "%s %s", PyString_AsString(old_str), mesg);
      }
      Py_DECREF(old_str);
    }
    return 1;
  } else {
    return 0;
  }
}
Esempio n. 12
0
    inline bool PyObjectToStringNoExcept(PyObject* obj, std::string& result)
    {
        if (!obj)
            return false;

#if PY_VERSION_HEX >= 0x03000000
        PyObjectHolder obj_str(PyObject_Str(obj));
#else
        PyObjectHolder obj_str(PyObject_Unicode(obj));
#endif
        if (!obj_str)
            return false;

        PyObjectHolder py_bytes(PyUnicode_AsUTF8String(obj_str));
        if (!py_bytes)
            return false;

        const char* content = PyBytes_AsString(py_bytes);
        if (!content)
            return false;

        result = content;
        return true;
    }
Esempio n. 13
0
NUITKA_MAY_BE_UNUSED static void dumpTraceback( PyTracebackObject *traceback )
{
    puts( "Dumping traceback:" );

    if ( traceback == NULL ) puts( "<NULL traceback?!>" );

    while( traceback )
    {
        puts( " Frame object chain:" );

        PyFrameObject *frame = traceback->tb_frame;

        while ( frame )
        {
            printf( "  Frame at %s\n", PyString_AsString( PyObject_Str( (PyObject *)frame->f_code )));

            frame = frame->f_back;
        }

        traceback = traceback->tb_next;
    }

    puts( "End of Dump." );
}
Esempio n. 14
0
void py_coulomb_potential(PyObject *self, void *p, void *nl, double *q,
			  double *phi, int *error)
{
  particles_t *py_p;
  neighbors_t *py_nl;
  PyObject *py_q, *py_phi, *r;

  int nat;
  npy_intp dims[2];
  
#ifdef DEBUG
  printf("[py_coulomb_potential] self = %s\n",
	 PyString_AsString(PyObject_Str(self)));
#endif

  f_particles_get_tag(p, (void**) &py_p);
  assert(py_p->f90obj == p);
  nat = data_get_len(py_p->f90data);

  f_neighbors_get_tag(nl, (void**) &py_nl);
  assert(py_nl->f90obj == nl);

  dims[0] = nat;
  dims[1] = 3;

  py_q = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, q);
  py_phi = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, phi);

  r = PyObject_CallMethod(self, "potential", "(OOOO)", py_p, py_nl, py_q,
			  py_phi);

  Py_DECREF(py_q);
  Py_DECREF(py_phi);
  PASS_PYTHON_ERROR(error, r);
  Py_DECREF(r);
}
Esempio n. 15
0
static PyObject* mod_connect(PyObject* self, PyObject* args, PyObject* kwargs)
{
    UNUSED(self);

    Object pConnectString;
    int fAutoCommit = 0;
    int fAnsi = 0;              // force ansi
    int fReadOnly = 0;
    long timeout = 0;
    Object encoding;

    Object attrs_before; // Optional connect attrs set before connecting

    Py_ssize_t size = args ? PyTuple_Size(args) : 0;

    if (size > 1)
    {
        PyErr_SetString(PyExc_TypeError, "function takes at most 1 non-keyword argument");
        return 0;
    }

    if (size == 1)
    {
        if (!PyString_Check(PyTuple_GET_ITEM(args, 0)) && !PyUnicode_Check(PyTuple_GET_ITEM(args, 0)))
            return PyErr_Format(PyExc_TypeError, "argument 1 must be a string or unicode object");

        pConnectString.Attach(PyUnicode_FromObject(PyTuple_GetItem(args, 0)));
        if (!pConnectString.IsValid())
            return 0;
    }

    if (kwargs && PyDict_Size(kwargs) > 0)
    {
        Object partsdict(PyDict_New());
        if (!partsdict.IsValid())
            return 0;

        Py_ssize_t pos = 0;
        PyObject* key = 0;
        PyObject* value = 0;

        Object okey; // in case we need to allocate a new key

        while (PyDict_Next(kwargs, &pos, &key, &value))
        {
            if (!Text_Check(key))
                return PyErr_Format(PyExc_TypeError, "Dictionary keys passed to connect must be strings");

            // // Note: key and value are *borrowed*.
            //
            // // Check for the two non-connection string keywords we accept.  (If we get many more of these, create something
            // // table driven.  Are we sure there isn't a Python function to parse keywords but leave those it doesn't know?)
            // const char* szKey = PyString_AsString(key);

            if (Text_EqualsI(key, "autocommit"))
            {
                fAutoCommit = PyObject_IsTrue(value);
                continue;
            }
            if (Text_EqualsI(key, "ansi"))
            {
                fAnsi = PyObject_IsTrue(value);
                continue;
            }
            if (Text_EqualsI(key, "timeout"))
            {
                timeout = PyInt_AsLong(value);
                if (PyErr_Occurred())
                    return 0;
                continue;
            }
            if (Text_EqualsI(key, "readonly"))
            {
                fReadOnly = PyObject_IsTrue(value);
                continue;
            }
            if (Text_EqualsI(key, "attrs_before"))
            {
                attrs_before = _CheckAttrsDict(value);
                if (PyErr_Occurred())
                    return 0;
                continue;
            }
            if (Text_EqualsI(key, "encoding"))
            {
#if PY_MAJOR_VERSION < 3
                if (!PyString_Check(value) || !PyUnicode_Check(value))
                    return PyErr_Format(PyExc_TypeError, "encoding must be a string or unicode object");
#else
                if (!PyUnicode_Check(value))
                    return PyErr_Format(PyExc_TypeError, "encoding must be a string");
#endif
                encoding = value;
                continue;
            }

            // Map DB API recommended names to ODBC names (e.g. user --> uid).

            for (size_t i = 0; i < _countof(keywordmaps); i++)
            {
                if (Text_EqualsI(key, keywordmaps[i].oldname))
                {
                    if (keywordmaps[i].newnameObject == 0)
                    {
                        keywordmaps[i].newnameObject = PyString_FromString(keywordmaps[i].newname);
                        if (keywordmaps[i].newnameObject == 0)
                            return 0;
                    }

                    key = keywordmaps[i].newnameObject;
                    break;
                }
            }

            PyObject* str = PyObject_Str(value); // convert if necessary
            if (!str)
                return 0;

            if (PyDict_SetItem(partsdict.Get(), key, str) == -1)
            {
                Py_XDECREF(str);
                return 0;
            }

            Py_XDECREF(str);
        }

        if (PyDict_Size(partsdict.Get()))
            pConnectString.Attach(MakeConnectionString(pConnectString.Get(), partsdict));
    }

    if (!pConnectString.IsValid())
        return PyErr_Format(PyExc_TypeError, "no connection information was passed");

    if (henv == SQL_NULL_HANDLE)
    {
        if (!AllocateEnv())
            return 0;
    }

    return (PyObject*)Connection_New(pConnectString.Get(), fAutoCommit != 0, fAnsi != 0, timeout,
                                     fReadOnly != 0, attrs_before, encoding);
}
Esempio n. 16
0
static
PyObject* downcast(PyObject* self, PyObject* args) {
    PyObject *obj, *cls;
    if (!PyArg_ParseTuple(args, "OO", &obj, &cls)) {
        return NULL;
    }

    if ((PyObject *) Py_TYPE(obj) == cls) {
        Py_INCREF(obj);
        return obj;
    }

    PyObject* apiModule = GetAPIModule();

    auto_pyobject fromTy = PyObject_GetAttrString(obj, "_llvm_type_");
    auto_pyobject toTy = PyObject_GetAttrString(cls, "_llvm_type_");

    std::ostringstream oss;

    auto_pyobject fromTyStr = PyObject_Str(*fromTy);
    auto_pyobject toTyStr = PyObject_Str(*toTy);

    const char * fromCS = PyString_AsString(*fromTyStr);
    const char * toCS = PyString_AsString(*toTyStr);

    oss << "downcast_";
    NormalizeString(oss, fromCS);
    oss << "_to_";
    NormalizeString(oss, toCS);
    std::string fname = oss.str();

    auto_pyobject caster = PyObject_GetAttrString(GetDowncastModule(),
                                                  fname.c_str());

    if (!caster) {
        std::ostringstream oss;
        oss << "Downcast from " << fromCS << " to " << toCS;
        std::string errmsg = oss.str();
        PyErr_SetString(PyExc_TypeError, errmsg.c_str());
        return NULL;
    }

    auto_pyobject oldObj = Unwrap(obj);
    auto_pyobject newObj = PyObject_CallFunctionObjArgs(*caster, *oldObj,
                                                        NULL);

    bool used_to_own = HasOwnership(*oldObj);

    PyObject *result = Wrap(*newObj, !used_to_own);

    int status = PyObject_Not(result);
    switch(status) {
    case 0:
        return result;
    case 1:
    default:
        PyErr_SetString(PyExc_ValueError, "Downcast failed");
        Py_XDECREF(result);
        return NULL;
    }
}
Esempio n. 17
0
static void *PyBigIntToSTR(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
  PyObject *obj = PyObject_Str((PyObject *) _obj);
  *_outLen = PyString_GET_SIZE(obj);
  return PyString_AS_STRING(obj);
}
Esempio n. 18
0
static int iosource_init(iosource *self, PyObject *args, PyObject *kwds) {
  char *keywords[] = { "opts", NULL};
  char *drivername;
  PyObject *opts = NULL;
  IOOptions options = NULL;
  PyObject *tmp;

  if(!PyArg_ParseTupleAndKeywords(args, kwds, "|O", keywords,
				  &opts)) {
    return -1;
  };

  if(!PyList_Check(opts)) {
    PyErr_Format(PyExc_TypeError, "Options must be a list of tuples");
    return -1;
  };

  if(!opts) {
    PyErr_Format(PyExc_Exception, "No options provided to driver");
    return -1;
  };

  options = CONSTRUCT(IOOptions, IOOptions, add, NULL, NULL, NULL, NULL);
  {
    int i=0;

    for(i=0;i<PyList_Size(opts);i++) {
      PyObject *temp,*key,*value;
      char *keyc, *valuec;

      temp = PyList_GetItem(opts,i); 
      if(!PyList_Check(temp)) {
	tmp = PyObject_Str(temp);
	PyErr_Format(PyExc_TypeError, "Element must be a list, not %s", PyString_AsString(tmp));
	Py_DECREF(tmp);
	return -1;
      };

      key = PyList_GetItem(temp,0);
      if(!key) return -1;

      value = PyList_GetItem(temp,1);
      if(!value) return -1;

      key = PyObject_Str(key);
      keyc = PyString_AsString(key);
      if(!keyc) {
	talloc_free(options);
	PyErr_Format(PyExc_Exception, "Not a string - driver options must be encoded as strings.");
    return -1;
      };

      value = PyObject_Str(value);
      valuec= PyString_AsString(value);
      if(!valuec) {
	talloc_free(options);
	PyErr_Format(PyExc_Exception, "Not a string - driver options must be encoded as strings.");
    return -1;
      };

      CONSTRUCT(IOOptions, IOOptions, add,options, options, keyc, valuec);
    };
  };

  drivername = CALL(options, get_value, "subsys");
  if(!drivername) {
    PyErr_Format(PyExc_TypeError, "No iodriver specified");
    return -1;
  };

  TRY {
    self->driver = iosubsys_Open(drivername, options);
  } EXCEPT(E_ANY) {
    talloc_free(options);
    PyErr_Format(map_exceptions_for_python(__EXCEPT__), "Unable to open iosource");
    return -1;
  };

  // We failed to instantiate this driver
  if(!self->driver) {
    talloc_free(options);
    PyErr_Format(map_errors_for_python(), "%s", _error_buff);
    return -1;
  };

  //Check that all the options have been consumed:
  if(!list_empty(&options->list)) {
    IOOptions first;
    list_next(first, &options->list, list);

    PyErr_Format(PyExc_RuntimeError, "Subsystem %s does not accept parameter %s", drivername,
		 first->name);
    talloc_free(options);
    return -1;
  };

  //Now ensure that the options are stolen to the iosource:
  talloc_steal(self->driver, options);
  self->size = self->driver->size;

  return 0;
};
Esempio n. 19
0
    tmp = PyUnicode_AsASCIIString(obj);
  }
  else {
    PyObject *tmp2;
    tmp2 = PyObject_Str(obj);
    if (tmp2) {
      tmp = PyUnicode_AsASCIIString(tmp2);
      Py_DECREF(tmp2);
    }
    else {
      tmp = NULL;
    }
  }
#else
  else {
    tmp = PyObject_Str(obj);
  }
#endif
  if (tmp == NULL) goto capi_fail;
  if (*len == -1)
    *len = PyString_GET_SIZE(tmp);
  STRINGMALLOC(*str,*len);
  STRINGCOPYN(*str,PyString_AS_STRING(tmp),*len+1);
  Py_DECREF(tmp);
  return 1;
capi_fail:
  Py_XDECREF(tmp);
  {
    PyObject* err = PyErr_Occurred();
    if (err==NULL) err = _lbfgsb_error;
    PyErr_SetString(err,errmess);
Esempio n. 20
0
/**
 * @brief
 *	runs python script in namespace.
 *
 * @param[in] interp_data - pointer to interpreter data
 * @param[in] py_script - pointer to python script info
 * @param[out] exit_code - exit code
 *
 * @return	int
 * @retval	-3 	script executed but got KeyboardInterrupt
 * @retval	-2 	script  compiled or executed with error
 * @retval	-1 	other failures
 * @retval	0 	success
 */
int
pbs_python_run_code_in_namespace(struct python_interpreter_data *interp_data,
	struct python_script *py_script,
	int *exit_code)
{
	static char func_id[] = "pbs_python_run_code_in_namespace";

#ifdef	PYTHON           /* -- BEGIN ONLY IF PYTHON IS CONFIGURED -- */

	PyObject *pdict;
	struct stat nbuf; /* new stat buf */
	struct stat obuf; /* old buf */
	int recompile = 1;
	PyObject *ptype;
	PyObject *pvalue;
	PyObject *ptraceback;
	PyObject *pobjStr;
	char      *pStr;
	int rc=0;

	if (!interp_data || !py_script) {
		log_err(-1, func_id, "Either interp_data or py_script is NULL");
		return -1;
	}

	/* ok, first time go straight to compile */
	do {
		if (!py_script->py_code_obj)
			break;
		(void) memcpy(&obuf, &(py_script->cur_sbuf), sizeof(obuf));
		if (py_script->check_for_recompile) {
			if ((stat(py_script->path, &nbuf) != -1) &&
				(nbuf.st_ino   == obuf.st_ino)    &&
				(nbuf.st_size  == obuf.st_size)   &&
				(nbuf.st_mtime == obuf.st_mtime)
				) {
				recompile = 0;
			} else {
				recompile = 1;
				(void) memcpy(&(py_script->cur_sbuf), &nbuf,
					sizeof(py_script->cur_sbuf));
				Py_CLEAR(py_script->py_code_obj); /* we are rebuilding */
			}
		}
	} while (0);

	if (recompile) {
		snprintf(log_buffer, LOG_BUF_SIZE-1,
			"Compiling script file: <%s>", py_script->path);
		log_buffer[LOG_BUF_SIZE-1] = '\0';
		if (IS_PBS_PYTHON_CMD(pbs_python_daemon_name))
			log_event(PBSEVENT_DEBUG3, PBS_EVENTCLASS_SERVER,
				LOG_INFO, interp_data->daemon_name, log_buffer);
		else
			log_event(PBSEVENT_SYSTEM|PBSEVENT_ADMIN |
				PBSEVENT_DEBUG, PBS_EVENTCLASS_SERVER,
				LOG_INFO, interp_data->daemon_name, log_buffer);

		if (!(py_script->py_code_obj =
			_pbs_python_compile_file(py_script->path,
			"<embedded code object>"))) {
			pbs_python_write_error_to_log("Failed to compile script");
			return -2;
		}
	}

	/* make new namespace dictionary, NOTE new reference */

	if (!(pdict = (PyObject *)pbs_python_ext_namespace_init(interp_data))) {
		log_err(-1, func_id, "while calling pbs_python_ext_namespace_init");
		return -1;
	}
	if ((pbs_python_setup_namespace_dict(pdict) == -1)) {
		Py_CLEAR(pdict);
		return -1;
	}

	/* clear previous global/local dictionary */
	if (py_script->global_dict) {
		PyDict_Clear((PyObject *)py_script->global_dict); /* clear k,v */
		Py_CLEAR(py_script->global_dict);
	}

	py_script->global_dict = pdict;

	PyErr_Clear(); /* clear any exceptions before starting code */
	/* precompile strings of code to bytecode objects */
	(void) PyEval_EvalCode((PyCodeObject *)py_script->py_code_obj,
		pdict, pdict);
	/* check for exception */
	if (PyErr_Occurred()) {
		if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
			pbs_python_write_error_to_log("Python script received a KeyboardInterrupt");
			return -3;
		}

		if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
			PyErr_Fetch(&ptype, &pvalue, &ptraceback);
			PyErr_Clear(); /* just in case, not clear from API doc */

			if (pvalue) {
				pobjStr = PyObject_Str(pvalue); /* new ref */
				pStr = PyString_AsString(pobjStr);
				rc = (int) atol(pStr);
				Py_XDECREF(pobjStr);
			}

			Py_XDECREF(ptype);
			Py_XDECREF(pvalue);

#if !defined(WIN32)
			Py_XDECREF(ptraceback);
#elif !defined(_DEBUG)
			/* for some reason this crashes on Windows Debug version */
			Py_XDECREF(ptraceback);
#endif

		} else {
			pbs_python_write_error_to_log("Error evaluating Python script");
			return -2;
		}
	}
	PyErr_Clear();

	if (exit_code)
		*exit_code=rc; /* set exit code if var is not null */

	return 0;
#else	/* !PYTHON */
	return -1;
#endif 	/* PYTHON */

}
Esempio n. 21
0
static PyObject *
proxy_str(proxyobject *pp)
{
	return PyObject_Str(pp->dict);
}
Esempio n. 22
0
static PyObject *
warn_explicit(PyObject *category, PyObject *message,
              PyObject *filename, int lineno,
              PyObject *module, PyObject *registry, PyObject *sourceline,
              PyObject *source)
{
    PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
    PyObject *item = NULL;
    PyObject *action;
    int rc;

    /* module can be None if a warning is emitted late during Python shutdown.
       In this case, the Python warnings module was probably unloaded, filters
       are no more available to choose as action. It is safer to ignore the
       warning and do nothing. */
    if (module == Py_None)
        Py_RETURN_NONE;

    if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
        PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
        return NULL;
    }

    /* Normalize module. */
    if (module == NULL) {
        module = normalize_module(filename);
        if (module == NULL)
            return NULL;
    }
    else
        Py_INCREF(module);

    /* Normalize message. */
    Py_INCREF(message);  /* DECREF'ed in cleanup. */
    rc = PyObject_IsInstance(message, PyExc_Warning);
    if (rc == -1) {
        goto cleanup;
    }
    if (rc == 1) {
        text = PyObject_Str(message);
        if (text == NULL)
            goto cleanup;
        category = (PyObject*)message->ob_type;
    }
    else {
        text = message;
        message = PyObject_CallFunction(category, "O", message);
        if (message == NULL)
            goto cleanup;
    }

    lineno_obj = PyLong_FromLong(lineno);
    if (lineno_obj == NULL)
        goto cleanup;

    /* Create key. */
    key = PyTuple_Pack(3, text, category, lineno_obj);
    if (key == NULL)
        goto cleanup;

    if ((registry != NULL) && (registry != Py_None)) {
        rc = already_warned(registry, key, 0);
        if (rc == -1)
            goto cleanup;
        else if (rc == 1)
            goto return_none;
        /* Else this warning hasn't been generated before. */
    }

    action = get_filter(category, text, lineno, module, &item);
    if (action == NULL)
        goto cleanup;

    if (PyUnicode_CompareWithASCIIString(action, "error") == 0) {
        PyErr_SetObject(category, message);
        goto cleanup;
    }

    /* Store in the registry that we've been here, *except* when the action
       is "always". */
    rc = 0;
    if (PyUnicode_CompareWithASCIIString(action, "always") != 0) {
        if (registry != NULL && registry != Py_None &&
                PyDict_SetItem(registry, key, Py_True) < 0)
            goto cleanup;
        else if (PyUnicode_CompareWithASCIIString(action, "ignore") == 0)
            goto return_none;
        else if (PyUnicode_CompareWithASCIIString(action, "once") == 0) {
            if (registry == NULL || registry == Py_None) {
                registry = get_once_registry();
                if (registry == NULL)
                    goto cleanup;
            }
            /* _once_registry[(text, category)] = 1 */
            rc = update_registry(registry, text, category, 0);
        }
        else if (PyUnicode_CompareWithASCIIString(action, "module") == 0) {
            /* registry[(text, category, 0)] = 1 */
            if (registry != NULL && registry != Py_None)
                rc = update_registry(registry, text, category, 0);
        }
        else if (PyUnicode_CompareWithASCIIString(action, "default") != 0) {
            PyErr_Format(PyExc_RuntimeError,
                        "Unrecognized action (%R) in warnings.filters:\n %R",
                        action, item);
            goto cleanup;
        }
    }

    if (rc == 1)  /* Already warned for this module. */
        goto return_none;
    if (rc == 0) {
        if (call_show_warning(category, text, message, filename, lineno,
                              lineno_obj, sourceline, source) < 0)
            goto cleanup;
    }
    else /* if (rc == -1) */
        goto cleanup;

 return_none:
    result = Py_None;
    Py_INCREF(result);

 cleanup:
    Py_XDECREF(item);
    Py_XDECREF(key);
    Py_XDECREF(text);
    Py_XDECREF(lineno_obj);
    Py_DECREF(module);
    Py_XDECREF(message);
    return result;  /* Py_None or NULL. */
}
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
{
    static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};

    PyObject* database;
    int detect_types = 0;
    PyObject* isolation_level = NULL;
    PyObject* factory = NULL;
    int check_same_thread = 1;
    int cached_statements = 100;
    double timeout = 5.0;
    int rc;
    PyObject* class_attr = NULL;
    PyObject* class_attr_str = NULL;
    int is_apsw_connection = 0;
    PyObject* database_utf8;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
                                     &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
    {
        return -1;
    }

    self->begin_statement = NULL;

    self->statement_cache = NULL;
    self->statements = NULL;

    Py_INCREF(Py_None);
    self->row_factory = Py_None;

    Py_INCREF(&PyUnicode_Type);
    self->text_factory = (PyObject*)&PyUnicode_Type;

    if (PyString_Check(database) || PyUnicode_Check(database)) {
        if (PyString_Check(database)) {
            database_utf8 = database;
            Py_INCREF(database_utf8);
        } else {
            database_utf8 = PyUnicode_AsUTF8String(database);
            if (!database_utf8) {
                return -1;
            }
        }

        Py_BEGIN_ALLOW_THREADS
        rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
        Py_END_ALLOW_THREADS

        Py_DECREF(database_utf8);

        if (rc != SQLITE_OK) {
            _pysqlite_seterror(self->db, NULL);
            return -1;
        }
    } else {
        /* Create a pysqlite connection from a APSW connection */
        class_attr = PyObject_GetAttrString(database, "__class__");
        if (class_attr) {
            class_attr_str = PyObject_Str(class_attr);
            if (class_attr_str) {
                if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
                    /* In the APSW Connection object, the first entry after
                     * PyObject_HEAD is the sqlite3* we want to get hold of.
                     * Luckily, this is the same layout as we have in our
                     * pysqlite_Connection */
                    self->db = ((pysqlite_Connection*)database)->db;

                    Py_INCREF(database);
                    self->apsw_connection = database;
                    is_apsw_connection = 1;
                }
            }
        }
        Py_XDECREF(class_attr_str);
        Py_XDECREF(class_attr);

        if (!is_apsw_connection) {
            PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
            return -1;
        }
    }

    if (!isolation_level) {
        isolation_level = PyString_FromString("");
        if (!isolation_level) {
            return -1;
        }
    } else {
        Py_INCREF(isolation_level);
    }
    self->isolation_level = NULL;
    pysqlite_connection_set_isolation_level(self, isolation_level);
    Py_DECREF(isolation_level);

    self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
    if (PyErr_Occurred()) {
        return -1;
    }

    self->statements = PyList_New(0);
    if (!self->statements) {
        return -1;
    }
    self->created_statements = 0;

    /* By default, the Cache class INCREFs the factory in its initializer, and
     * decrefs it in its deallocator method. Since this would create a circular
     * reference here, we're breaking it by decrementing self, and telling the
     * cache class to not decref the factory (self) in its deallocator.
     */
    self->statement_cache->decref_factory = 0;
    Py_DECREF(self);

    self->inTransaction = 0;
    self->detect_types = detect_types;
    self->timeout = timeout;
    (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
#ifdef WITH_THREAD
    self->thread_ident = PyThread_get_thread_ident();
#endif
    self->check_same_thread = check_same_thread;

    self->function_pinboard = PyDict_New();
    if (!self->function_pinboard) {
        return -1;
    }

    self->collations = PyDict_New();
    if (!self->collations) {
        return -1;
    }

    self->Warning               = pysqlite_Warning;
    self->Error                 = pysqlite_Error;
    self->InterfaceError        = pysqlite_InterfaceError;
    self->DatabaseError         = pysqlite_DatabaseError;
    self->DataError             = pysqlite_DataError;
    self->OperationalError      = pysqlite_OperationalError;
    self->IntegrityError        = pysqlite_IntegrityError;
    self->InternalError         = pysqlite_InternalError;
    self->ProgrammingError      = pysqlite_ProgrammingError;
    self->NotSupportedError     = pysqlite_NotSupportedError;

    return 0;
}
Esempio n. 24
0
void
PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
{
    PyObject *exc, *v, *tb, *tmp;
    _Py_IDENTIFIER(filename);
    _Py_IDENTIFIER(lineno);
    _Py_IDENTIFIER(msg);
    _Py_IDENTIFIER(offset);
    _Py_IDENTIFIER(print_file_and_line);
    _Py_IDENTIFIER(text);

    /* add attributes for the line number and filename for the error */
    PyErr_Fetch(&exc, &v, &tb);
    PyErr_NormalizeException(&exc, &v, &tb);
    /* XXX check that it is, indeed, a syntax error. It might not
     * be, though. */
    tmp = PyLong_FromLong(lineno);
    if (tmp == NULL)
        PyErr_Clear();
    else {
        if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
            PyErr_Clear();
        Py_DECREF(tmp);
    }
    if (col_offset >= 0) {
        tmp = PyLong_FromLong(col_offset);
        if (tmp == NULL)
            PyErr_Clear();
        else {
            if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
                PyErr_Clear();
            Py_DECREF(tmp);
        }
    }
    if (filename != NULL) {
        if (_PyObject_SetAttrId(v, &PyId_filename, filename))
            PyErr_Clear();

        tmp = PyErr_ProgramTextObject(filename, lineno);
        if (tmp) {
            if (_PyObject_SetAttrId(v, &PyId_text, tmp))
                PyErr_Clear();
            Py_DECREF(tmp);
        }
    }
    if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
        PyErr_Clear();
    }
    if (exc != PyExc_SyntaxError) {
        if (!_PyObject_HasAttrId(v, &PyId_msg)) {
            tmp = PyObject_Str(v);
            if (tmp) {
                if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
                    PyErr_Clear();
                Py_DECREF(tmp);
            } else {
                PyErr_Clear();
            }
        }
        if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
            if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
                                    Py_None))
                PyErr_Clear();
        }
    }
    PyErr_Restore(exc, v, tb);
}
Esempio n. 25
0
static PyObject *t_tzinfo__getTZID(t_tzinfo *self, void *data)
{
    return PyObject_Str((PyObject *) self);
}
Esempio n. 26
0
static PyObject *t_tzinfo_tzname(t_tzinfo *self, PyObject *dt)
{
    return PyObject_Str((PyObject *) self->tz);
}
Esempio n. 27
0
CString BZScriptPython::run(CBZScriptView* sview, const char * cmdstr)
{
	cbzsv = sview;

	// referring IDAPython PythonEvalOrExec..
	PyCompilerFlags cf = {0};
	PyObject *py_code = Py_CompileStringFlags(cmdstr, "<string>", Py_eval_input, &cf);
	if(py_code == NULL || PyErr_Occurred())
	{
		// Not an expression?
		PyErr_Clear();

		// Run as a string
		PyRun_SimpleString(cmdstr);
		return _T("");
	}

	PyObject *module = PyImport_AddModule("__main__");
	PyObject *py_globals = (module == NULL) ? NULL : PyModule_GetDict(module);
	//PYW_GIL_ENSURE;
#ifdef BZPYTHON2
	PyObject *py_result = PyEval_EvalCode((PyCodeObject*)py_code, py_globals, py_globals);
#endif
#ifdef BZPYTHON3
	PyObject *py_result = PyEval_EvalCode(py_code, py_globals, py_globals);
#endif
	//PYW_GIL_RELEASE;
	Py_DECREF(py_code);

	if(py_result == NULL || PyErr_Occurred())
	{
		PyErr_Print();
		return _T("");
	}
	if(py_result != Py_None)
	{
		PyObject *pystr = PyObject_Str(py_result);
		Py_DECREF(py_result);
#ifdef BZPYTHON2
		char *cstr = PyString_AsString(pystr);
		int cstrlen = strlen(cstr);
		CStringA csresult(cstr, cstrlen);
#endif
#ifdef BZPYTHON3
		// PyUnicode_AsUnicode->Py_UNICODE could be UCS4... only want wchar_t; use PyUnicode_AsWideChar(String).
		wchar_t *cstr = PyUnicode_AsWideCharString(pystr, NULL);
		int cstrlen = lstrlenW(cstr);
		CStringW csresult(cstr, cstrlen);
		PyMem_Free(cstr);
#endif
		Py_DECREF(pystr);
		CString csnative(csresult);
		CString ostr;

		csnative.Replace(_T("\n"), _T("\r\n"));
		ostr.Format(_T("%s\r\n"), csnative);

		return ostr;
	}

	Py_DECREF(py_result);

	return _T("");//ostr;
}
Esempio n. 28
0
File: jcc.cpp Progetto: ahua/java
static void JNICALL _PythonException_getErrorInfo(JNIEnv *vm_env, jobject self)
{
    PythonGIL gil(vm_env);

    if (!PyErr_Occurred())
        return;

    PyObject *type, *value, *tb, *errorName;
    jclass jcls = vm_env->GetObjectClass(self);

    PyErr_Fetch(&type, &value, &tb);

    errorName = PyObject_GetAttrString(type, "__name__");
    if (errorName != NULL)
    {
        jfieldID fid =
            vm_env->GetFieldID(jcls, "errorName", "Ljava/lang/String;");
        jstring str = env->fromPyString(errorName);

        vm_env->SetObjectField(self, fid, str);
        vm_env->DeleteLocalRef(str);
        Py_DECREF(errorName);
    }

    if (value != NULL)
    {
        PyObject *message = PyObject_Str(value);

        if (message != NULL)
        {
            jfieldID fid =
                vm_env->GetFieldID(jcls, "message", "Ljava/lang/String;");
            jstring str = env->fromPyString(message);

            vm_env->SetObjectField(self, fid, str);
            vm_env->DeleteLocalRef(str);
            Py_DECREF(message);
        }
    }

    PyObject *module = NULL, *cls = NULL, *stringIO = NULL, *result = NULL;
    PyObject *_stderr = PySys_GetObject("stderr");
    if (!_stderr)
        goto err;

    module = PyImport_ImportModule("cStringIO");
    if (!module)
        goto err;

    cls = PyObject_GetAttrString(module, "StringIO");
    Py_DECREF(module);
    if (!cls)
        goto err;

    stringIO = PyObject_CallObject(cls, NULL);
    Py_DECREF(cls);
    if (!stringIO)
        goto err;

    Py_INCREF(_stderr);
    PySys_SetObject("stderr", stringIO);

    PyErr_Restore(type, value, tb);
    PyErr_Print();

    result = PyObject_CallMethod(stringIO, "getvalue", NULL);
    Py_DECREF(stringIO);

    if (result != NULL)
    {
        jfieldID fid =
            vm_env->GetFieldID(jcls, "traceback", "Ljava/lang/String;");
        jstring str = env->fromPyString(result);

        vm_env->SetObjectField(self, fid, str);
        vm_env->DeleteLocalRef(str);
        Py_DECREF(result);
    }

    PySys_SetObject("stderr", _stderr);
    Py_DECREF(_stderr);

    return;

  err:
    PyErr_Restore(type, value, tb);
}
Esempio n. 29
0
static PyObject *t_tzinfo_str(t_tzinfo *self)
{
    return PyObject_Str((PyObject *) self->tz);
}
int LIBAGENT_PUBLIC_API agent_log_python_error(char *log_prefix)
{
    PyObject *ptype;
    PyObject *pvalue;
    PyObject *ptraceback;

    /* Acquire GIL */
    PyGILState_STATE gstate = PyGILState_Ensure();

    PyErr_Fetch(&ptype, &pvalue, &ptraceback);

    if (ptype == NULL)
    {
        agent_error("%s: No python error available", log_prefix);

        /* Just in case */
        Py_XDECREF(pvalue);
        Py_XDECREF(ptraceback);

        /* Release GIL */
        PyGILState_Release(gstate); 
        return -1;
    }

    if (ptraceback == NULL)
    {
        PyObject *obj;

        agent_error("%s: A python exception has occurred:", log_prefix);

        if (pvalue != NULL)
        {
            obj = PyObject_Str(pvalue);
        }
        else
        {
            obj = PyObject_Str(ptype);
        }

        agent_error("[EXC] %s", PyString_AsString(obj));

        Py_DECREF(obj);
        Py_DECREF(ptype);
        Py_XDECREF(pvalue);

        /* Release GIL */
        PyGILState_Release(gstate); 
        return 0;
    }

    PyObject *tb_mod = PyImport_AddModule("traceback");
    if (tb_mod == NULL)
    {
        PyErr_Clear();

        Py_DECREF(ptype);
        Py_XDECREF(pvalue);
        Py_XDECREF(ptraceback);

        agent_error("%s: [Couldn't find traceback module "
                "to print the error]", log_prefix);

        /* Release GIL */
        PyGILState_Release(gstate); 
        return -1;
    }

    /*
     * Call traceback.format_exception(ptype, pvalue, ptraceback)
     */

    PyObject *pobj_list = PyObject_CallMethod(tb_mod, "format_exception",
            "OOO", ptype, pvalue, ptraceback);
    if (pobj_list == NULL)
    {
        PyErr_Clear();

        Py_DECREF(ptype);
        Py_XDECREF(pvalue);
        Py_XDECREF(ptraceback);

        agent_error("%s: [Couldn't format traceback]", log_prefix);

        /* Release GIL */
        PyGILState_Release(gstate); 
        return -1;
    }

    Py_DECREF(ptype);
    Py_XDECREF(pvalue);
    Py_XDECREF(ptraceback);

    /*
     * Now we have a list of 'lines'.  Each 'line' might actually be
     * multiple lines, however ('line' might contain '\n's).  So, we
     * need to go through every list entry and log each real line
     * (looking for \n separator)
     */

    agent_error("%s: A python exception has occurred:", log_prefix);

    Py_ssize_t list_sz = PyList_Size(pobj_list);
    PyObject *pobj_str;

    Py_ssize_t i;

    for(i = 0;i < list_sz;i++)
    {
        pobj_str = PyList_GetItem(pobj_list, i);

        char *obj_str = strdup(PyString_AsString(pobj_str));

        Py_DECREF(pobj_str);
    
        if (obj_str == NULL)
        {
            agent_error("Out of memory");

            Py_DECREF(pobj_list);
    
            /* Release GIL */
            PyGILState_Release(gstate); 
            return 0;
        }
    
        char *ptr = strchr(obj_str, '\n');
        if (ptr == NULL)
        {
            /* No \n... just log this element and go to the next */
            agent_error("[EXC] %s", obj_str);
            free(obj_str);

            continue;
        }

        char *start = obj_str;
        *(ptr++) = '\0';

        agent_error("[EXC] %s", start);
    
        while((ptr != NULL) && (*ptr != '\0'))
        {
            start = ptr;
            ptr = strchr(start, '\n');
            if (ptr != NULL)
            {
                *ptr++ = '\0';
            }

            agent_error("[EXC] %s", start);
        }
    
        free(obj_str);
    }

    /* Release GIL */
    PyGILState_Release(gstate); 

    return 0;
}