Embeding Python & amp; Extending Python with FFPython

Source: Internet
Author: User
Tags builtin

Introduction

Ffpython is a C ++ lib, which is to simplify tasks that embed Python and extend Python. as the author, I am a developer for MMO server. mainly I use C ++ to implement part that needs to response user's requests in realtime, while other logic part that needs to be modified frequently is implemented by Python. python makes it possible to reload our part of the server when the server is running. python is so easy that even my colleague with no programing skills can implement some npc script. when I was first in charge of integrating Python runtime interpreter to our C ++ server, I used Boost. python. somehow Boost. python helped me solve most problems about Python API especially parts of Python reference. but Boost. python is not perfect.

  • When exception happens while invoking python function by boost. python, boost. python doesn' t provide interface to fetch traceback information.
  • Boost python is a big lib to some extent. Someone will find it much difficult who just wants to try to experience how embedding Python works.
  • Boost. pyhon supports badly to extend python by using C ++static Function and class in runtime. Boost. python recommends to use dynamic library to extending python.
  • Someone who wants to learn example code using Python API will find it difficult to understand well boost. python codes, in my opinion.
  • If you have the need to convert data between C ++ STL container and python object builtin, you have to implement these codes yourself, while Python reference is so annoying.
FFPython

Firstly, I implemented it just for converting data between C ++ STL container and python object. some of my colleagues have writes a lot of code using Python API directly. but there is some trap in some Python API even engineers who have a lot of experience using Python may have lost. pyDict_SetItem will auto increase reference key and value arguments, but other Python builtin structure API likePyTuple_SetItem Will not increase reference the argument. it will cause memory leak. so I wanted to wrap operations about converting data between C ++ STL container and Python builtin structure. finally, I found some elegant ways to wrap embedding Python and extending Python by C ++ template skills. so that is how FFPyhton was born.

Embedding Python

Sometimes I think it's easier to show codes to readers here. There _^. But it will be forbidden by administrator if I post lots of code. O (n_n) O ~. So see code files or Github.

When Embedding Python, such functions are most needed.

  • Fetch global variable of python script (or module). This happens when you use Python script as config files.
  • Call python function in script. That is the most useful interface. Two parts of it are important.
  • Fetch exception information when exception happens, especially traceback information. because of feature of dynamic type, exception regularly happens even in online server, let alone debugging time. ffpython will throw std exception when Python exception happens. so it is much easier to print or log traceback info by outputexception.what().
  • Ffpython support nine arguments.
  • Ffpython implemented by C ++ template to wrap Python API. It is easy to understand how it works if you see the code.
 printf(<>(,  a1 = ;  a2 = ;  a3 = <>(, <> a1;a1.push_back();a1.push_back(<> a2; a2.push_back();a2.push_back(<list<> ><>(, <, list<vector<> > >= ffpython.call<ret_t>(, );

 

Extending Python

Ffpython recommends to register C ++ function/class in runtime. It works to design and develop MMO game server. So that is common use for me. There are some key points when embedding Python.

  • Ffpython supports to register C ++staticFunction. C ++ builtin types and STL container can be as arguments.
  • C ++ class can be registered to Python.

Register C ++static Function, all base type supported. Arg num can be nine.

  print_val( a1,  a2,  & a3,  vector<>& ffpython.reg(&print_val, ); 

 

Register C ++ class, Python can use it just like builtin types.

 ~ get_value()  {  set_value( v_) { m_value = test_stl(map<, list<> >& dumy_t: ~ foo_t* obj_test(dumy_t* test_register_base_class(ffpython_t&<foo_t, PYCTOR()>(&foo_t::get_value, &foo_t::set_value, &foo_t::test_stl, &foo_t::m_value, 

 

Summary
  • Ffpython only one implements head file, it is easy to integrate to project.
  • Ffpython is simply wrap for Python API, so it is efficient.
  • Github: https://github.com/fanchy/ffpython
  • Python2.5 python2.6 python2.7, win/linux
  • Python3.x is being developed, but unfortunately, Python3.x API is so different to python2.x, even different between python3.2 and python3.3, Headache !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.