GithubHelp home page GithubHelp logo

arkmohba / call_python_from_c Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 58 KB

CからPythonを呼ぶ方法の調査(逆も多分調査する)

License: Apache License 2.0

Dockerfile 7.35% Shell 10.89% C++ 64.21% Python 5.15% Java 12.41%

call_python_from_c's Introduction

call_python_from_c

CからPythonを呼ぶ方法の調査(逆も多分調査する)

CからPythonを呼び出す

参考: 他のアプリケーションへの Python の埋め込み

  • 初期化
  • インタプリタを呼び出す方法は以下の通り
    • PyRun_SimpleString()
      • Pythonの文字列を直接渡す
    • PyRun_SimpleFile()
      • Pythonのスクリプトファイルのパスを渡す
    • 低水準の操作を呼び出してPythonオブジェクトを構築したり使用することもできる。
      • PyObjectなど?(詳細は未調査)
  • CとPythonの間のやり取りが無い、超高水準の埋め込みは以下の手順でできる。
    • Py_SetProgramName(program)
    • Py_Initialize()
    • PyRun_SimpleString("...") or PyRun_SimpleFile("xx.py")
    • Py_FinalizeEx()
    • PyMem_RawFree(program)
  • CとPythonの間のやり取りをするためには低水準のインターフェース呼び出しを利用する必要がある。
  • CからPythonを呼び出すためには以下を行う必要がある。(データ変換については別途勉強が必要)
    • データ値を C から Python に変換する。
    • 変換された値を使って Python インターフェースルーチンの関数呼び出しを行い
    • 呼び出しで得られたデータ値 Python から C に変換する。
  • 低レイヤー
    • Py_Initialize()
    • PyObject* pModule = PyImport_Import(pName);
      • Pythonファイルをモジュールとして呼び込む
    • PyObject* pFunc = PyObject_GetAttrString(pModule, "function name");
      • モジュールのアトリビュートとして関数名から関数オブジェクトを取り出す
    • PyObject* pArgs = PyTuple_New(N)
      • N個の関数引数のデータとしてタプルを用意
    • PyObject* pValue = PyLong_FromLong(i);
    • PyObject* PyTuple_SetItem(pArgs, i, pValue);
      • Cの整数をPythonの整数に変換し、関数引数タプルにセット
    • pValue = PyObject_CallObject(pFunc, pArgs);
      • 関数をコールする
    • int a = PyLong_AsLong(pValue)
      • PythonからCに型を変換する
    • ※Py_DECREFなどの参照削除は省略

call_python_from_c's People

Contributors

arkmohba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.