python3.5 EMBED in C

首先吐槽下,python2 跟 3的差别太大了,网上找的embed python的教程大多数都是2.x的

在3.x版本大多不能用,很多函数都不存在了。找了几天的资料终于完成了c ember python基本的调用。

环境准备说明:

1、vc6 + python3.5.2 , 默认安装不会生成python_xx_d.lib ,

         PYTHON安装包安装时需要选择高级模式,全部勾选。然后_d 调试文件都会生成

2、python3.5是vs2015编译的,所以还需要下载 vc14运行环境,调试版的还需要下载vcruntime140d.dll,ucrtbased.dll

========================================================================================

先上python脚本

#coding: utf-8
class mymath:
    def __init__(self,name):
        self.name=name
        print(name)
        
    def add(self,num1,num2):
        print(num1+num2)
        return num1 + num2

    def retTuple(self):
        print (self.name)
        return ("中国人民解放军",231,222.55)

    def retSingleStr(self,egg):
        print (egg+"法拉利")
        
        return "中国人民解放军"
    def retSingleInt(self):
        print (self.name)
        return 231
    def retSingleFloat(self,num1):
        print (self.name)
        return num1 + 231.58

============================================================================================

// pyEmbed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <assert.h>
#include <Python.h>
#include <windows.h>
#include "pyhelper.hpp"
int main()
{
    CPyInstance hInstance;
    PyObject *pModule, *pClass,*pStuObj;
    
    //载入模块pyemb3
    try{
        pModule = PyImport_ImportModule("pyemb3");
        assert(pModule!=NULL);
        //从模块中得到类mymath
        pClass = PyObject_GetAttrString (pModule, "mymath");
        assert(pClass!=NULL);
        PyObject *temp=Py_BuildValue("(s)","JACK!");
        assert(temp!=NULL);
        pStuObj = PyObject_CallObject(pClass, temp);
        返回元组
        PyObject *ast = PyObject_CallMethod(pStuObj, "retTuple", "()");
        char *command;
        int amt;
        float nums;
        char szData[]="我不想上幼儿园";
        if (!PyArg_ParseTuple(ast, "sif", &command,&amt,&nums))
        {
            printf("发生错误!");
        }
        Convert(command,command,CP_UTF8,CP_ACP);
        printf("返回元组的函数:%s_%i_%.2f\n",command,amt,nums);
        调用返回单个字符串的类函数
        Convert(szData,szData,CP_ACP,CP_UTF8);
        ast = PyObject_CallMethod(pStuObj, "retSingleStr", "(s)",szData);
        char *test = PyUnicode_AsUTF8(ast);
        Convert(test,test,CP_UTF8,CP_ACP);
        printf("返回单个字符串函数%s,value: %s\n ",Py_TYPE(ast)->tp_name,test);
        返回int 型
        PyObject *result = PyObject_CallMethod(pStuObj, "add", "(ii)", 1, 2);    
        printf("返回INT:1 + 2 = %d\n", PyLong_AsLong(result));
        返回FLOAT型
        result = PyObject_CallMethod(pStuObj, "retSingleFloat", "(f)", 3.25);    
        printf("返回float:%.2f\n", PyFloat_AsDouble(result));
    }
    catch(...)
    {
        printf("发生错误!");
    }
    
    return 0;
}


最后我有个疑问

************不知道为什么 我没有调用 Py_XDECREF, Py_XDECREF 维护计数器 也没有出现内存泄漏

维护计数器太烦人了,每个变量都这样自行维护真是吐血啊。。。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值