Exemple #1
0
PyObject *
PyNumber_Long(PyObject *o)
{
	PyNumberMethods *m;
	const char *buffer;
	int buffer_len;

	if (o == NULL)
		return null_error();
	if (PyLong_Check(o)) {
		Py_INCREF(o);
		return o;
	}
	if (PyString_Check(o))
		/* need to do extra error checking that PyLong_FromString() 
		 * doesn't do.  In particular long('9.5') must raise an
		 * exception, not truncate the float.
		 */
		return long_from_string(PyString_AS_STRING(o),
					PyString_GET_SIZE(o));
	if (PyUnicode_Check(o))
		/* The above check is done in PyLong_FromUnicode(). */
		return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
					  PyUnicode_GET_SIZE(o),
					  10);
	m = o->ob_type->tp_as_number;
	if (m && m->nb_long)
		return m->nb_long(o);
	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
		return long_from_string(buffer, buffer_len);

	return type_error("object can't be converted to long");
}
bool embedding_simple_script(int argc, char* argv[])
#endif
{
	if (argc < 3)
	{
		std::cerr << "Usage: call pythonfile funcname [args]" << std::endl;
		std::cerr << "\te.g.) embedding.exe arithmetic add 3 2" << std::endl;
		return false;
	}

	Py_Initialize();
#if defined(_UNICODE) || defined(UNICODE)
	PyObject *pName = PyUnicode_FromUnicode(argv[1], std::wcslen(argv[1]));
#else
	//PyObject *pName = PyString_FromString(argv[1]);
	PyObject *pName = PyUnicode_FromString(argv[1]);
#endif
	// Error checking of pName left out

	PyObject *pModule = PyImport_Import(pName);
	Py_DECREF(pName);

	if (pModule != NULL)
	{
#if defined(_UNICODE) || defined(UNICODE)
		PyObject *pTmp = PyUnicode_FromWideChar(argv[2], std::wcslen(argv[2]));
		PyObject *pFunc = PyObject_GetAttr(pModule, pTmp);
		Py_DECREF(pTmp);
#else
		PyObject *pFunc = PyObject_GetAttrString(pModule, argv[2]);
#endif
		// pFunc is a new reference

		if (pFunc && PyCallable_Check(pFunc))
		{
			PyObject *pValue = NULL;

			PyObject *pArgs = PyTuple_New(argc - 3);
			for (int i = 0; i < argc - 3; ++i)
			{
				// TODO [check] >> is it correct?
#if defined(_UNICODE) || defined(UNICODE)
				pValue = PyLong_FromUnicode(argv[i + 3], std::wcslen(argv[i + 3]), 0);
#else
				pValue = PyLong_FromLong(atoi(argv[i + 3]));
				//pValue = PyLong_FromString(argv[i + 3], NULL, 0);
#endif
				if (!pValue)
				{
					Py_DECREF(pArgs);
					Py_DECREF(pModule);
					std::cerr << "Cannot convert argument" << std::endl;
					return false;
				}

				// pValue reference stolen here
				PyTuple_SetItem(pArgs, i, pValue);
			}

			pValue = PyObject_CallObject(pFunc, pArgs);
			Py_DECREF(pArgs);

			if (pValue != NULL)
			{
				std::cout << "Result of call: " << PyLong_AsLong(pValue) << std::endl;
				Py_DECREF(pValue);
			}
			else
			{
				Py_DECREF(pFunc);
				Py_DECREF(pModule);

				PyErr_Print();
				std::cerr << "Call failed" << std::endl;
				return false;
			}
		}
		else
		{
			if (PyErr_Occurred())
				PyErr_Print();
			std::cerr << "Cannot find function \"" << argv[2] << "\"" << std::endl;
		}

		Py_XDECREF(pFunc);
		Py_DECREF(pModule);
	}
	else
	{
		PyErr_Print();
		std::cerr << "Failed to load \"" << argv[1] << "\"" << std::endl;
		return false;
	}

	Py_Finalize();
	return true;
}
static PyObject *next(PyObject *self)
{
	ligolw_Tokenizer *tokenizer = (ligolw_Tokenizer *) self;
	PyObject *type;
	PyObject *token;
	Py_UNICODE *start, *end;

	/*
	 * Identify the start and end of the next token.
	 */

	do {
		type = next_token(tokenizer, &start, &end);
		if(!type)
			return NULL;
	} while(type == Py_None);

	/*
	 * Extract token as desired type.
	 */

	if(start == NULL) {
		/*
		 * unquoted zero-length string == None
		 */

		Py_INCREF(Py_None);
		token = Py_None;
	} else if(type == (PyObject *) &PyFloat_Type) {
		char ascii_buffer[end - start + 1];
		char *ascii_end;
		if(PyUnicode_EncodeDecimal(start, end - start, ascii_buffer, NULL))
			return NULL;
		token = PyFloat_FromDouble(strtod(ascii_buffer, &ascii_end));
		if(ascii_end == ascii_buffer || *ascii_end != 0) {
			/*
			 * strtod() couldn't convert the token, emulate
			 * float()'s error message
			 */

			Py_XDECREF(token);
			PyErr_Format(PyExc_ValueError, "invalid literal for float(): '%s'", ascii_buffer);
			token = NULL;
		}
	} else if(type == (PyObject *) &PyUnicode_Type) {
		token = PyUnicode_FromUnicode(start, end - start);
	} else if(type == (PyObject *) &PyString_Type) {
		token = PyUnicode_Encode(start, end - start, NULL, NULL);
	} else if(type == (PyObject *) &PyInt_Type) {
		token = PyInt_FromUnicode(start, end - start, 0);
	} else if(type == (PyObject *) &PyLong_Type) {
		token = PyLong_FromUnicode(start, end - start, 0);
	} else {
		token = PyObject_CallFunction(type, "u#", start, end - start);
	}

	/*
	 * Done.
	 */

	return token;
}