如何从指向字符串的PyObject获取char * [英] How to get a char* from a PyObject which points to a string

查看:563
本文介绍了如何从指向字符串的PyObject获取char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从指向字符串的 PyObject 中获取 char * .例如,这是python脚本

How can I get a char* from a PyObject which points to a string. For example, this is the python script,

Test.Connect("272.22.20.65", 1234)

这是C ++代码,

static PyObject* Connect(PyObject *self, PyObject *args)
{
    PyObject* pIP;
    PyObject* pPort;

    if (!PyArg_UnpackTuple(args, "Connect", 2, 2, &pIP, &pPort)) 
    {
        return NULL;
    }

    const char* zIP = GetAsString(pIP);
    long iPort      = PyLong_AsLong(pPort);

我想获取该IP地址为char *(GetAsString是伪函数:D).请注意,我正在使用Python 3.1.

I want to get that IP address as a char* (GetAsString is a dummy function :D ). Please note that I'm using Python 3.1.

P.S. 我认为问题没有找到正确的答案, 因为在Python 3中没有 PyStringObject PyString_AsString ,不是吗?

P.S. I don't think this question got the correct answer , since there is no PyStringObject or PyString_AsString in Python 3. Isn't it ?

推荐答案

首先,您获取它 .不要忘了临时引用.

First you encode it, then you retrieve it. Don't forget to decref the temporary.

这篇关于如何从指向字符串的PyObject获取char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆