Beispiel #1
0
static PyObject*
Snmp_repr(SnmpObject *self)
{
	PyObject *peer = NULL, *community = NULL, *rpeer = NULL,
	    *rcommunity = NULL, *result;
	if ((peer = PyString_FromString(self->ss->peername)) == NULL)
		return NULL;
	if ((community = PyString_FromString((char*)self->ss->community)) == NULL)
		goto reprerror;
	if ((rpeer = PyObject_Repr(peer)) == NULL)
		goto reprerror;
	if ((rcommunity = PyObject_Repr(community)) == NULL)
		goto reprerror;
	result = PyString_FromFormat("%s(host=%s, community=%s, version=%d)",
	    self->ob_type->tp_name,
	    PyString_AsString(rpeer),
	    PyString_AsString(rcommunity),
	    (self->ss->version == SNMP_VERSION_1)?1:2);
	Py_DECREF(rpeer);
	Py_DECREF(peer);
	Py_DECREF(rcommunity);
	Py_DECREF(community);
	return result;
reprerror:
	Py_XDECREF(rpeer);
	Py_XDECREF(peer);
	Py_XDECREF(rcommunity);
	Py_XDECREF(community);
	return NULL;
}
Beispiel #2
0
/** \ingroup python_interface_edge
 * \brief Formats an \c igraph.Edge object as a string
 * 
 * \return the formatted textual representation as a \c PyObject
 */
PyObject* igraphmodule_Edge_repr(igraphmodule_EdgeObject *self) {
  PyObject *s;
  PyObject *attrs;
#ifndef IGRAPH_PYTHON3
  PyObject *grepr, *drepr;
#endif

  attrs = igraphmodule_Edge_attributes(self);
  if (attrs == 0)
    return NULL;

#ifdef IGRAPH_PYTHON3
  s = PyUnicode_FromFormat("igraph.Edge(%R, %ld, %R)",
      (PyObject*)self->gref, (long int)self->idx, attrs);
  Py_DECREF(attrs);
#else
  grepr=PyObject_Repr((PyObject*)self->gref);
  drepr=PyObject_Repr(attrs);
  Py_DECREF(attrs);
  if (!grepr || !drepr) {
    Py_XDECREF(grepr);
    Py_XDECREF(drepr);
    return NULL;
  }
  s=PyString_FromFormat("igraph.Edge(%s, %ld, %s)", PyString_AsString(grepr),
    (long int)self->idx, PyString_AsString(drepr));
  Py_DECREF(grepr);
  Py_DECREF(drepr);
#endif
  return s;
}
Beispiel #3
0
static PyObject *
html_State_repr(html_State *self) {
    PyObject *bold = NULL, *italic = NULL, *lang = NULL, *ans = NULL;
    bold = PyObject_Repr(self->is_bold); italic = PyObject_Repr(self->is_italic); lang = PyObject_Repr(self->current_lang);
    if (bold && italic && lang)
        ans = PyString_FromFormat("State(bold=%s, italic=%s, lang=%s)", PyString_AS_STRING(bold), PyString_AS_STRING(italic), PyString_AS_STRING(lang));
    Py_XDECREF(bold); Py_XDECREF(italic); Py_XDECREF(lang);
    return ans;
}
Beispiel #4
0
static PyObject *
html_Tag_repr(html_Tag *self) {
    PyObject *name = NULL, *bold = NULL, *italic = NULL, *lang = NULL, *ans = NULL;
    name = PyObject_Repr(self->name); bold = PyObject_Repr(self->bold); italic = PyObject_Repr(self->italic); lang = PyObject_Repr(self->lang);
    if (name && bold && italic && lang)
        ans = PyString_FromFormat("Tag(%s, bold=%s, italic=%s, lang=%s)", PyString_AS_STRING(name), PyString_AS_STRING(bold), PyString_AS_STRING(italic), PyString_AS_STRING(lang));
    Py_XDECREF(name); Py_XDECREF(bold); Py_XDECREF(italic); Py_XDECREF(lang);
    return ans;
}
Beispiel #5
0
static PyObject *pair_repr(PyObject *self) {
  PyObject *tmp = NULL;
  PyObject *col = PyList_New(0);
  PyObject *found = PyDict_New();
  size_t index = 0;

  PyObject *rest = self;
  PyObject *rest_id;

  PyList_Append(col, _str_cons_paren);

  while (rest->ob_type == &SibPairType) {
    rest_id = PyLong_FromVoidPtr(rest);

    if (PyDict_Contains(found, rest_id)) {
      /* recursive pair detected */
      PyList_Append(col, _str_recursive_true);

      if (rest != self) {
	tmp = PyDict_GetItem(found, rest_id);
	PyList_Insert(col, PyLong_AsSize_t(tmp) - 1, _str_cons_paren);
	PyList_Append(col, _str_close_paren);
      }
      Py_DECREF(rest_id);
      rest = NULL;
      break;

    } else {
      index += 2;

      tmp = PyLong_FromSize_t(index);
      PyDict_SetItem(found, rest_id, tmp);
      Py_DECREF(tmp);

      tmp = PyObject_Repr(SibPair_CAR(rest));
      PyList_Append(col, tmp);
      PyList_Append(col, _str_comma_space);
      Py_DECREF(tmp);

      rest = SibPair_CDR(rest);
      Py_DECREF(rest_id);
    }
  }

  if (rest) {
    PyList_Append(col, PyObject_Repr(rest));
  }

  PyList_Append(col, _str_close_paren);

  tmp = PyUnicode_Join(_str_empty, col);
  Py_DECREF(col);
  Py_DECREF(found);

  return tmp;
}
Beispiel #6
0
static PyObject *
contextvar_tp_repr(PyContextVar *self)
{
    _PyUnicodeWriter writer;

    _PyUnicodeWriter_Init(&writer);

    if (_PyUnicodeWriter_WriteASCIIString(
            &writer, "<ContextVar name=", 17) < 0)
    {
        goto error;
    }

    PyObject *name = PyObject_Repr(self->var_name);
    if (name == NULL) {
        goto error;
    }
    if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
        Py_DECREF(name);
        goto error;
    }
    Py_DECREF(name);

    if (self->var_default != NULL) {
        if (_PyUnicodeWriter_WriteASCIIString(&writer, " default=", 9) < 0) {
            goto error;
        }

        PyObject *def = PyObject_Repr(self->var_default);
        if (def == NULL) {
            goto error;
        }
        if (_PyUnicodeWriter_WriteStr(&writer, def) < 0) {
            Py_DECREF(def);
            goto error;
        }
        Py_DECREF(def);
    }

    PyObject *addr = PyUnicode_FromFormat(" at %p>", self);
    if (addr == NULL) {
        goto error;
    }
    if (_PyUnicodeWriter_WriteStr(&writer, addr) < 0) {
        Py_DECREF(addr);
        goto error;
    }
    Py_DECREF(addr);

    return _PyUnicodeWriter_Finish(&writer);

error:
    _PyUnicodeWriter_Dealloc(&writer);
    return NULL;
}
/*
 * Convert Python object to C string in server encoding.
 */
char *
PLyObject_AsString(PyObject *plrv)
{
	PyObject   *plrv_bo;
	char	   *plrv_sc;
	size_t		plen;
	size_t		slen;

	if (PyUnicode_Check(plrv))
		plrv_bo = PLyUnicode_Bytes(plrv);
	else if (PyFloat_Check(plrv))
	{
		/* use repr() for floats, str() is lossy */
#if PY_MAJOR_VERSION >= 3
		PyObject   *s = PyObject_Repr(plrv);

		plrv_bo = PLyUnicode_Bytes(s);
		Py_XDECREF(s);
#else
		plrv_bo = PyObject_Repr(plrv);
#endif
	}
	else
	{
#if PY_MAJOR_VERSION >= 3
		PyObject   *s = PyObject_Str(plrv);

		plrv_bo = PLyUnicode_Bytes(s);
		Py_XDECREF(s);
#else
		plrv_bo = PyObject_Str(plrv);
#endif
	}
	if (!plrv_bo)
		PLy_elog(ERROR, "could not create string representation of Python object");

	plrv_sc = pstrdup(PyBytes_AsString(plrv_bo));
	plen = PyBytes_Size(plrv_bo);
	slen = strlen(plrv_sc);

	Py_XDECREF(plrv_bo);

	if (slen < plen)
		ereport(ERROR,
				(errcode(ERRCODE_DATATYPE_MISMATCH),
				 errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes")));
	else if (slen > plen)
		elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length");
	pg_verifymbstr(plrv_sc, slen, false);

	return plrv_sc;
}
Beispiel #8
0
void
pystring_concat_repr(PyObject **pystr, PyObject *obj)
{
#if PY_MAJOR_VERSION >= 3
    PyObject *repr = PyObject_Repr(obj);
    PyObject *newstr = PyUnicode_Concat(*pystr, repr);
    Py_DECREF(repr);
    Py_DECREF(*pystr);
    *pystr = newstr;
#else
    PyString_ConcatAndDel(pystr, PyObject_Repr(obj));
#endif
}
Beispiel #9
0
void print_dict(PyObject *dict)
{
	PyObject *key, *value;
	Py_ssize_t pos = 0;
	while (PyDict_Next(dict, &pos, &key, &value)) {
		PyObject* k_str_exc_type = PyObject_Repr(key);
		PyObject* k_pyStr = PyUnicode_AsEncodedString(k_str_exc_type, "utf-8", "Error ~");
		printf("key:%s ---> ", (const char *)PyBytes_AS_STRING(k_pyStr));

		PyObject* v_str_exc_type = PyObject_Repr(value);
		PyObject* v_pyStr = PyUnicode_AsEncodedString(v_str_exc_type, "utf-8", "Error ~");
		printf("value:%s\n", PyBytes_AsString(v_pyStr));
	}
}
Beispiel #10
0
static PyObject *slot_QSizeF___repr__(PyObject *sipSelf)
{
    QSizeF *sipCpp = reinterpret_cast<QSizeF *>(sipGetCppPtr((sipSimpleWrapper *)sipSelf,sipType_QSizeF));

    if (!sipCpp)
        return 0;


    {
        {
            PyObject * sipRes = 0;

#line 116 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\sip/QtCore/qsize.sip"
        if (sipCpp->isNull())
        {
        #if PY_MAJOR_VERSION >= 3
            sipRes = PyUnicode_FromString("PyQt5.QtCore.QSizeF()");
        #else
            sipRes = PyString_FromString("PyQt5.QtCore.QSizeF()");
        #endif
        }
        else
        {
            PyObject *w = PyFloat_FromDouble(sipCpp->width());
            PyObject *h = PyFloat_FromDouble(sipCpp->height());
        
            if (w && h)
            {
        #if PY_MAJOR_VERSION >= 3
                sipRes = PyUnicode_FromFormat("PyQt5.QtCore.QSizeF(%R, %R)", w, h);
        #else
                sipRes = PyString_FromString("PyQt5.QtCore.QSizeF(");
                PyString_ConcatAndDel(&sipRes, PyObject_Repr(w));
                PyString_ConcatAndDel(&sipRes, PyString_FromString(", "));
                PyString_ConcatAndDel(&sipRes, PyObject_Repr(h));
                PyString_ConcatAndDel(&sipRes, PyString_FromString(")"));
        #endif
            }
        
            Py_XDECREF(w);
            Py_XDECREF(h);
        }
#line 872 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\QtCore/sipQtCoreQSizeF.cpp"

            return sipRes;
        }
    }

    return 0;
}
Beispiel #11
0
/*
Returns the name of the Python object which this instance wraps.

If it cannot determine a reasonable name it just gives up.
*/
static
VALUE rpObjectectGetName(VALUE self)
{
	//It only makes sense to query a python object if the interpreter is running.
	if(Py_IsInitialized())
	{
		PyObject *pObject,*pName,*pRepr;
		VALUE rName;
		
		pObject = rpObjectGetPyObject(self);
		
		
		pName = PyObject_GetAttrString(pObject,"__name__");
		
		if(!pName)
		{
			PyErr_Clear();
			
			pName = PyObject_GetAttrString(pObject,"__class__");
	 		pRepr = PyObject_Repr(pName);
			rName = ptorString(pRepr);
			Py_XDECREF(pRepr);
			
			return rb_str_concat(rb_str_new2("An instance of "), rName);
			if(!pName)
			{
				PyErr_Clear();
				
				pName = PyObject_Repr(pObject);
				
				if(!pName)
				{
					PyErr_Clear();
					return rb_str_new2("__Unnameable__");
				}
			}
		}
		
		rName = ptorString(pName);
		
		Py_XDECREF(pName);
		
		return rName;
	}
	
	return rb_str_new2("__FREED__");

}
static PyObject *slot_QQuaternion___repr__(PyObject *sipSelf)
{
    QQuaternion *sipCpp = reinterpret_cast<QQuaternion *>(sipGetCppPtr((sipSimpleWrapper *)sipSelf,sipType_QQuaternion));

    if (!sipCpp)
        return 0;


    {
        {
            PyObject * sipRes = 0;

#line 45 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\sip/QtGui/qquaternion.sip"
        PyObject *scalar = PyFloat_FromDouble(sipCpp->scalar());
        PyObject *x = PyFloat_FromDouble(sipCpp->x());
        PyObject *y = PyFloat_FromDouble(sipCpp->y());
        PyObject *z = PyFloat_FromDouble(sipCpp->z());
        
        if (scalar && x && y && z)
        {
        #if PY_MAJOR_VERSION >= 3
            sipRes = PyUnicode_FromFormat("PyQt5.QtGui.QQuaternion(%R, %R, %R, %R)",
                    scalar, x, y, z);
        #else
            sipRes = PyString_FromString("PyQt5.QtGui.QQuaternion(");
            PyString_ConcatAndDel(&sipRes, PyObject_Repr(scalar));
            PyString_ConcatAndDel(&sipRes, PyString_FromString(", "));
            PyString_ConcatAndDel(&sipRes, PyObject_Repr(x));
            PyString_ConcatAndDel(&sipRes, PyString_FromString(", "));
            PyString_ConcatAndDel(&sipRes, PyObject_Repr(y));
            PyString_ConcatAndDel(&sipRes, PyString_FromString(", "));
            PyString_ConcatAndDel(&sipRes, PyObject_Repr(z));
            PyString_ConcatAndDel(&sipRes, PyString_FromString(")"));
        #endif
        }
        
        Py_XDECREF(scalar);
        Py_XDECREF(x);
        Py_XDECREF(y);
        Py_XDECREF(z);
#line 1111 "C:\\Users\\marcus\\Downloads\\PyQt-gpl-5.4\\PyQt-gpl-5.4\\QtGui/sipQtGuiQQuaternion.cpp"

            return sipRes;
        }
    }

    return 0;
}
Beispiel #13
0
int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
    PyObject *writer, *value, *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;
    }
    result = PyObject_CallFunctionObjArgs(writer, value, NULL);
    Py_DECREF(value);
    Py_DECREF(writer);
    if (result == NULL)
        return -1;
    Py_DECREF(result);
    return 0;
}
Beispiel #14
0
SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
{
	// for safety, todo: only allow for registered actuators (pointertable)
	// we don't want to crash gameengine/blender by python scripts
	std::vector<SCA_IActuator*> lacts =  m_sCurrentController->GetLinkedActuators();
	std::vector<SCA_IActuator*>::iterator it;
	
	if (PyUnicode_Check(value)) {
		/* get the actuator from the name */
		char *name= _PyUnicode_AsString(value);
		for(it = lacts.begin(); it!= lacts.end(); ++it) {
			if( name == (*it)->GetName() ) {
				return *it;
			}
		}
	}
	else if (PyObject_TypeCheck(value, &SCA_IActuator::Type)) {
		PyObjectPlus *value_plus= BGE_PROXY_REF(value);
		for(it = lacts.begin(); it!= lacts.end(); ++it) {
			if( static_cast<SCA_IActuator*>(value_plus) == (*it) ) {
				return *it;
			}
		}
	}
	
	/* set the exception */
	PyObject *value_str = PyObject_Repr(value); /* new ref */
	PyErr_Format(PyExc_ValueError, "'%s' not in this python controllers actuator list", _PyUnicode_AsString(value_str));
	Py_DECREF(value_str);
	
	return NULL;
}
Beispiel #15
0
static PyObject *
normalizeUserObj(PyObject *obj)
{
	PyCFunctionObject *fn;
	if (!PyCFunction_Check(obj)) {
		Py_INCREF(obj);
		return obj;
	}
	/* Replace built-in function objects with a descriptive string
	   because of built-in methods -- keeping a reference to
	   __self__ is probably not a good idea. */
	fn = (PyCFunctionObject *)obj;

	if (fn->m_self == NULL) {
		/* built-in function: look up the module name */
		PyObject *mod = fn->m_module;
		const char *modname;
		if (mod && PyUnicode_Check(mod)) {
			modname = _PyUnicode_AsString(mod);
		}
		else if (mod && PyModule_Check(mod)) {
			modname = PyModule_GetName(mod);
			if (modname == NULL) {
				PyErr_Clear();
				modname = "builtins";
			}
		}
		else {
			modname = "builtins";
		}
		if (strcmp(modname, "builtins") != 0)
			return PyUnicode_FromFormat("<%s.%s>",
						    modname,
						    fn->m_ml->ml_name);
		else
			return PyUnicode_FromFormat("<%s>",
						    fn->m_ml->ml_name);
	}
	else {
		/* built-in method: try to return
			repr(getattr(type(__self__), __name__))
		*/
		PyObject *self = fn->m_self;
		PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name);
		if (name != NULL) {
			PyObject *mo = _PyType_Lookup(Py_TYPE(self), name);
			Py_XINCREF(mo);
			Py_DECREF(name);
			if (mo != NULL) {
				PyObject *res = PyObject_Repr(mo);
				Py_DECREF(mo);
				if (res != NULL)
					return res;
			}
		}
		PyErr_Clear();
		return PyUnicode_FromFormat("<built-in method %s>",
					    fn->m_ml->ml_name);
	}
}
static PyObject *
ax25_address_repr(ax25_address_object* self)
{
    PyObject *b, *r, *f;
    
    b = PyBytes_FromStringAndSize(self->field.ax25_call, 7);
    if (b == NULL)
        return NULL;

    r = PyObject_Repr(b);
    if (r == NULL) {
        Py_DECREF(b);
        return NULL;
    }

    f = PyUnicode_FromFormat("<ax25_address %U>", r);
    if (f == NULL) {
        Py_DECREF(r);
        Py_DECREF(b);
        return NULL;
    }

    Py_DECREF(r);
    Py_DECREF(b);
    return f;
}
Beispiel #17
0
void OutputHook::call(std::string name, boost::python::object obj)
{
    Hooks::checkName(name);

    auto repr_ = PyObject_Repr(obj.ptr());
    if (PyErr_Occurred())
    {
        PyErr_Clear();
        throw Hooks::Exception("Failed to get __repr__ of argument");
    }
    auto repr = std::string(PyUnicode_AsUTF8(repr_));
    Py_DECREF(repr_);

    PyObject* g = Py_BuildValue(
            "{sO}", "__builtins__", PyEval_GetBuiltins());
    node->parent->loadDatumHooks(g);
    auto out = PyRun_String(repr.c_str(), Py_eval_input, g, g);
    Py_DECREF(g);
    Py_XDECREF(out);
    if (PyErr_Occurred())
    {
        PyErr_Clear();
        throw Hooks::Exception("Could not evaluate __repr__ of output");
    }

    const bool result = node->makeDatum(
            name, obj.ptr()->ob_type, Datum::SIGIL_OUTPUT + repr, true);

    if (!result)
        throw Hooks::Exception("Datum was already defined in this script.");
}
configParserStruct::pythonParser::containerForVariables configParserStruct::pythonParser::listOfVariables( void *Object )
{
  containerForVariables Result;
  
  if ( Object == NULL )
    return Result;

  PyObject *Dict = castToPyObject(Object);

  if ( ! PyDict_Check( Dict ) )
    return Result;

  PyObject *KeysList = PyDict_Keys( Dict );
  Py_ssize_t KeysListSize = PyList_Size( KeysList );

  for ( ptrdiff_t Index = 0; Index < static_cast<ptrdiff_t>(KeysListSize); Index++ )
  {
    PyObject *Key = PyList_GetItem( KeysList, Index );
    PyObject *KeyRepr = PyObject_Repr( Key );
    std::string KeyString = dequoteString( PyString_AsString( KeyRepr ), "'" );
    PyObject *Item = PyDict_GetItem( Dict, Key );
    if ( PyDict_Check(Item) )
    {
      containerForVariables ListOfItemsinSubDict = listOfVariables( Item );
      for ( containerForVariables::const_iterator i = ListOfItemsinSubDict.begin(); i != ListOfItemsinSubDict.end(); ++i )
        Result.insert( KeyString + structSeparator() + *i );
    } else {
      Result.insert( KeyString );
    } 
  }

  return Result;
}
Beispiel #19
0
static PyObject *
pyg_remove_emission_hook(PyGObject *self, PyObject *args)
{
    PyObject *pygtype;
    char *name;
    guint signal_id;
    gulong hook_id;
    GType gtype;
    
    if (!PyArg_ParseTuple(args, "Osk:gobject.remove_emission_hook",
			  &pygtype, &name, &hook_id))
	return NULL;
    
    if ((gtype = pyg_type_from_object(pygtype)) == 0) {
	return NULL;
    }
    
    if (!g_signal_parse_name(name, gtype, &signal_id, NULL, TRUE)) {
	PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
		     PyString_AsString(PyObject_Repr((PyObject*)self)),
		     name);
	return NULL;
    }

    g_signal_remove_emission_hook(signal_id, hook_id);
    
    Py_INCREF(Py_None);
    return Py_None;
}
Beispiel #20
0
//-----------------------------------------------------------------------------
// Variable_Repr()
//   Return a string representation of the variable.
//-----------------------------------------------------------------------------
static PyObject *Variable_Repr(
    udt_Variable *self)                 // variable to return the string for
{
    PyObject *valueRepr, *value, *module, *name, *result, *format, *formatArgs;

    value = Variable_GetValue(self, 0);
    if (!value)
        return NULL;
    valueRepr = PyObject_Repr(value);
    Py_DECREF(value);
    if (!valueRepr)
        return NULL;
    format = ceString_FromAscii("<%s.%s with value %s>");
    if (!format) {
        Py_DECREF(valueRepr);
        return NULL;
    }
    if (GetModuleAndName(Py_TYPE(self), &module, &name) < 0) {
        Py_DECREF(valueRepr);
        Py_DECREF(format);
        return NULL;
    }
    formatArgs = PyTuple_Pack(3, module, name, valueRepr);
    Py_DECREF(module);
    Py_DECREF(name);
    Py_DECREF(valueRepr);
    if (!formatArgs) {
        Py_DECREF(format);
        return NULL;
    }
    result = ceString_Format(format, formatArgs);
    Py_DECREF(format);
    Py_DECREF(formatArgs);
    return result;
}
Beispiel #21
0
PyObject *
PyObject_Unicode(PyObject *v)
{
	PyObject *res;
	PyObject *func;
	PyObject *str;
	static PyObject *unicodestr;

	if (v == NULL) {
		res = PyString_FromString("<NULL>");
		if (res == NULL)
			return NULL;
		str = PyUnicode_FromEncodedObject(res, NULL, "strict");
		Py_DECREF(res);
		return str;
	} else if (PyUnicode_CheckExact(v)) {
		Py_INCREF(v);
		return v;
	}
	/* XXX As soon as we have a tp_unicode slot, we should
	   check this before trying the __unicode__
	   method. */
	if (unicodestr == NULL) {
		unicodestr= PyString_InternFromString("__unicode__");
		if (unicodestr == NULL)
			return NULL;
	}
	func = PyObject_GetAttr(v, unicodestr);
	if (func != NULL) {
		res = PyEval_CallObject(func, (PyObject *)NULL);
		Py_DECREF(func);
	}
	else {
		PyErr_Clear();
		if (PyUnicode_Check(v)) {
			/* For a Unicode subtype that's didn't overwrite __unicode__,
			   return a true Unicode object with the same data. */
			return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
			                             PyUnicode_GET_SIZE(v));
		}
		if (PyString_CheckExact(v)) {
			Py_INCREF(v);
			res = v;
		}
		else {
			if (v->ob_type->tp_str != NULL)
				res = (*v->ob_type->tp_str)(v);
			else
				res = PyObject_Repr(v);
		}
	}
	if (res == NULL)
		return NULL;
	if (!PyUnicode_Check(res)) {
		str = PyUnicode_FromEncodedObject(res, NULL, "strict");
		Py_DECREF(res);
		res = str;
	}
	return res;
}
Beispiel #22
0
static PyObject *
Array_tp_repr(DBusPyArray *self)
{
    PyObject *parent_repr = (PyList_Type.tp_repr)((PyObject *)self);
    PyObject *sig_repr = PyObject_Repr(self->signature);
    PyObject *my_repr = NULL;
    long variant_level = self->variant_level;

    if (!parent_repr) goto finally;
    if (!sig_repr) goto finally;
    if (variant_level > 0) {
        my_repr = PyUnicode_FromFormat("%s(%V, signature=%V, "
                                       "variant_level=%ld)",
                                       Py_TYPE(&self->super)->tp_name,
                                       REPRV(parent_repr),
                                       REPRV(sig_repr),
                                       variant_level);
    }
    else {
        my_repr = PyUnicode_FromFormat("%s(%V, signature=%V)",
                                       Py_TYPE(&self->super)->tp_name,
                                       REPRV(parent_repr),
                                       REPRV(sig_repr));
    }
finally:
    Py_CLEAR(parent_repr);
    Py_CLEAR(sig_repr);
    return my_repr;
}
Beispiel #23
0
static PyObject* sllistnode_repr(SLListNodeObject* self)
{
    PyObject* str = NULL;
    PyObject* tmp_str;

    str = Py23String_FromString("<sllistnode(");
    if (str == NULL)
        goto str_alloc_error;

    tmp_str = PyObject_Repr(self->value);
    if (tmp_str == NULL)
        goto str_alloc_error;
    Py23String_ConcatAndDel(&str, tmp_str);

    tmp_str = Py23String_FromString(")>");
    if (tmp_str == NULL)
        goto str_alloc_error;
    Py23String_ConcatAndDel(&str, tmp_str);

    return str;

str_alloc_error:
    Py_XDECREF(str);
    PyErr_SetString(PyExc_RuntimeError, "Failed to create string");
    return NULL;
}
Beispiel #24
0
static PyObject *
slice_repr(PySliceObject *r)
{
	PyObject *s, *comma;

	s = PyString_FromString("slice(");
	comma = PyString_FromString(", ");
	PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
	PyString_Concat(&s, comma);
	PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
	PyString_Concat(&s, comma);
	PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
	PyString_ConcatAndDel(&s, PyString_FromString(")"));
	Py_DECREF(comma);
	return s;
}
Beispiel #25
0
void IDP_spit(IDProperty *prop)
{
    if (prop) {
        PyGILState_STATE gilstate;
        int use_gil = TRUE; /* !PyC_IsInterpreterActive(); */
        PyObject *ret_dict;
        PyObject *ret_str;

        if (use_gil) {
            gilstate = PyGILState_Ensure();
        }

        /* to_dict() */
        ret_dict = BPy_IDGroup_MapDataToPy(prop);
        ret_str = PyObject_Repr(ret_dict);
        Py_DECREF(ret_dict);

        printf("IDProperty(%p): %s\n", prop, _PyUnicode_AsString(ret_str));

        Py_DECREF(ret_str);

        if (use_gil) {
            PyGILState_Release(gilstate);
        }
    }
    else {
        printf("IDProperty: <NIL>\n");
    }
}
Beispiel #26
0
int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
	PyObject *writer, *value, *args, *result;
	if (f == NULL) {
		PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
		return -1;
	}
	writer = PyObject_GetAttrString(f, "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;
}
Beispiel #27
0
static bool
sp_search_type_converter(PyObject *o, void *address) {
    sp_search_type *type = (sp_search_type *)address;

    if (o == NULL || o == Py_None)
        return 1;

    if (!PyString_Check(o))
        goto error;

    char *tmp = PyString_AsString(o);
    if (strcmp(tmp, "standard") == 0)
        *type = SP_SEARCH_STANDARD;
    else if (strcmp(tmp, "suggest") == 0)
        *type = SP_SEARCH_SUGGEST;
    else
        goto error;

    return 1;

error:
    PyErr_Format(PyExc_ValueError, "Unknown search type: %s",
                 PyString_AsString(PyObject_Repr(o)));
    return 0;
}
Beispiel #28
0
void
_PyGC_Fini(void)
{
    if (!(debug & DEBUG_SAVEALL)
        && garbage != NULL && PyList_GET_SIZE(garbage) > 0) {
        char *message;
        if (debug & DEBUG_UNCOLLECTABLE)
            message = "gc: %zd uncollectable objects at " \
                "shutdown";
        else
            message = "gc: %zd uncollectable objects at " \
                "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
        if (PyErr_WarnFormat(PyExc_ResourceWarning, 0, message,
                             PyList_GET_SIZE(garbage)) < 0)
            PyErr_WriteUnraisable(NULL);
        if (debug & DEBUG_UNCOLLECTABLE) {
            PyObject *repr = NULL, *bytes = NULL;
            repr = PyObject_Repr(garbage);
            if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
                PyErr_WriteUnraisable(garbage);
            else {
                PySys_WriteStderr(
                    "    %s\n",
                    PyBytes_AS_STRING(bytes)
                    );
            }
            Py_XDECREF(repr);
            Py_XDECREF(bytes);
        }
    }
}
Beispiel #29
0
/*
 * repr function
 * convert to a list and get its string value
 */
static PyObject *KX_PythonSeq_repr( KX_PythonSeq * self )
{
	PyObject *list = PySequence_List((PyObject *)self);
	PyObject *repr = PyObject_Repr(list);
	Py_DECREF(list);
	return repr;
}
Beispiel #30
0
static PyObject *xns_repr(PyXPathNamespaceObject *self)
{
  char buf[256];

  PyObject *name = PyObject_Repr(self->nodeName);
  PyObject *value = PyObject_Repr(self->nodeValue);

  sprintf(buf, "<cXPathNamespace at %p: name %.50s, value %.100s>", self,
          name == NULL ? "(null)" : PyString_AS_STRING(name),
          value == NULL ? "(null)" : PyString_AS_STRING(value));

  Py_XDECREF(name);
  Py_XDECREF(value);

  return PyString_FromString(buf);
}