/
BirdLeon
/
ROBLOX2016
Обзор
Документация
Войти
/
BirdLeon
/
ROBLOX2016
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
Library/boost/libs/python/doc/html/reference/embedding.html
205 строк
14 KB
PatoFlamejanteTV
full source code
19 дек 2024, 19:11
19 дек 2024, 19:11
05db15d
Код
Авторство
О чём код?
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 6. Embedding</title> <link rel="stylesheet" href="../boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> <link rel="home" href="index.html" title="Boost.Python Reference Manual"> <link rel="up" href="index.html" title="Boost.Python Reference Manual"> <link rel="prev" href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html" title="boost/python/register_ptr_to_python.hpp"> <link rel="next" href="embedding/boost_python_import_hpp.html" title="boost/python/import.hpp"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="embedding/boost_python_import_hpp.html"><img src="../images/next.png" alt="Next"></a> </div> <div class="chapter"> <div class="titlepage"><div><div><h1 class="title"> <a name="embedding"></a>Chapter 6. Embedding</h1></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <dl class="toc"> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp">boost/python/exec.hpp</a></span></dt> <dd><dl> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.introduction">Introduction</a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_eval">Function <code class="computeroutput"><span class="identifier">eval</span></code></a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec">Function <code class="computeroutput"><span class="identifier">exec</span></code></a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec_file">Function <code class="computeroutput"><span class="identifier">exec_file</span></code></a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.examples">Examples</a></span></dt> </dl></dd> <dt><span class="section"><a href="embedding/boost_python_import_hpp.html">boost/python/import.hpp</a></span></dt> <dd><dl> <dt><span class="section"><a href="embedding/boost_python_import_hpp.html#embedding.boost_python_import_hpp.introduction">Introduction</a></span></dt> <dt><span class="section"><a href="embedding/boost_python_import_hpp.html#embedding.boost_python_import_hpp.function_import">Function <code class="computeroutput"><span class="identifier">import</span></code></a></span></dt> <dt><span class="section"><a href="embedding/boost_python_import_hpp.html#embedding.boost_python_import_hpp.examples">Examples</a></span></dt> </dl></dd> </dl> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="embedding.boost_python_exec_hpp"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp" title="boost/python/exec.hpp">boost/python/exec.hpp</a> </h2></div></div></div> <div class="toc"><dl class="toc"> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.introduction">Introduction</a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_eval">Function <code class="computeroutput"><span class="identifier">eval</span></code></a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec">Function <code class="computeroutput"><span class="identifier">exec</span></code></a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.function_exec_file">Function <code class="computeroutput"><span class="identifier">exec_file</span></code></a></span></dt> <dt><span class="section"><a href="embedding.html#embedding.boost_python_exec_hpp.examples">Examples</a></span></dt> </dl></div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="embedding.boost_python_exec_hpp.introduction"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.introduction" title="Introduction">Introduction</a> </h3></div></div></div> <p> Exposes a mechanism for embedding the python interpreter into C++ code. </p> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="embedding.boost_python_exec_hpp.function_eval"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.function_eval" title="Function eval">Function <code class="computeroutput"><span class="identifier">eval</span></code></a> </h3></div></div></div> <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">eval</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">expression</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span> <span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">());</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl class="variablelist"> <dt><span class="term">Effects</span></dt> <dd><p> Evaluate Python expression from expression in the context specified by the dictionaries globals and locals. </p></dd> <dt><span class="term">Returns</span></dt> <dd><p> An instance of object which holds the value of the expression. </p></dd> </dl> </div> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="embedding.boost_python_exec_hpp.function_exec"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.function_exec" title="Function exec">Function <code class="computeroutput"><span class="identifier">exec</span></code></a> </h3></div></div></div> <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">exec</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">code</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span> <span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">());</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl class="variablelist"> <dt><span class="term">Effects</span></dt> <dd><p> Execute Python source code from code in the context specified by the dictionaries globals and locals. </p></dd> <dt><span class="term">Returns</span></dt> <dd><p> An instance of object which holds the result of executing the code. </p></dd> </dl> </div> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="embedding.boost_python_exec_hpp.function_exec_file"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.function_exec_file" title="Function exec_file">Function <code class="computeroutput"><span class="identifier">exec_file</span></code></a> </h3></div></div></div> <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">exec_file</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">filename</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span> <span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">());</span> </pre> <div class="variablelist"> <p class="title"><b></b></p> <dl class="variablelist"> <dt><span class="term">Effects</span></dt> <dd><p> Execute Python source code from the file named by filename in the context specified by the dictionaries globals and locals. </p></dd> <dt><span class="term">Returns</span></dt> <dd><p> An instance of object which holds the result of executing the code. </p></dd> </dl> </div> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="embedding.boost_python_exec_hpp.examples"></a><a class="link" href="embedding.html#embedding.boost_python_exec_hpp.examples" title="Examples">Examples</a> </h3></div></div></div> <p> The following example demonstrates the use of import and exec to define a function in python, and later call it from within C++. </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> <span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">string</span><span class="special">></span> <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span> <span class="keyword">void</span> <span class="identifier">greet</span><span class="special">()</span> <span class="special">{</span> <span class="comment">// Retrieve the main module.</span> <span class="identifier">object</span> <span class="identifier">main</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"__main__"</span><span class="special">);</span> <span class="comment">// Retrieve the main module's namespace</span> <span class="identifier">object</span> <span class="identifier">global</span><span class="special">(</span><span class="identifier">main</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span> <span class="comment">// Define greet function in Python.</span> <span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">exec</span><span class="special">(</span> <span class="string">"def greet(): \n"</span> <span class="string">" return 'Hello from Python!' \n"</span><span class="special">,</span> <span class="identifier">global</span><span class="special">,</span> <span class="identifier">global</span><span class="special">);</span> <span class="comment">// Create a reference to it.</span> <span class="identifier">object</span> <span class="identifier">greet</span> <span class="special">=</span> <span class="identifier">global</span><span class="special">[</span><span class="string">"greet"</span><span class="special">];</span> <span class="comment">// Call it.</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">message</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>(</span><span class="identifier">greet</span><span class="special">());</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">message</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="special">}</span> </pre> <p> Instead of embedding the python script into a string, we could also store it in an a file... </p> <pre class="programlisting"><span class="identifier">def</span> <span class="identifier">greet</span><span class="special">():</span> <span class="keyword">return</span> <span class="char">'Hello from Python!'</span> </pre> <p> ... and execute that instead. </p> <pre class="programlisting"> <span class="comment">// ...</span> <span class="comment">// Load the greet function from a file.</span> <span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">exec_file</span><span class="special">(</span><span class="identifier">script</span><span class="special">,</span> <span class="identifier">global</span><span class="special">,</span> <span class="identifier">global</span><span class="special">);</span> <span class="comment">// ...</span> <span class="special">}</span> </pre> </div> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a> </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="embedding/boost_python_import_hpp.html"><img src="../images/next.png" alt="Next"></a> </div> </body> </html>