Ticket #970: doc.patch
File doc.patch, 13.2 KB (added by , 15 years ago) |
---|
-
tutorial/doc/tutorial.qbk
? doc.patch ? tutorial/doc/html/HTML.manifest RCS file: /cvsroot/boost/boost/libs/python/doc/tutorial/doc/tutorial.qbk,v retrieving revision 1.16 diff -u -r1.16 tutorial.qbk
1365 1365 1366 1366 [def Py_Initialize [@http://www.python.org/doc/current/api/initialization.html#l2h-652 Py_Initialize]] 1367 1367 [def Py_Finalize [@http://www.python.org/doc/current/api/initialization.html#l2h-656 Py_Finalize]] 1368 [def PyRun_String [@http://www.python.org/doc/current/api/veryhigh.html#l2h-55 PyRun_String]]1369 [def PyRun_File [@http://www.python.org/doc/current/api/veryhigh.html#l2h-56 PyRun_File]]1370 [def Py_eval_input [@http://www.python.org/doc/current/api/veryhigh.html#l2h-58 Py_eval_input]]1371 [def Py_file_input [@http://www.python.org/doc/current/api/veryhigh.html#l2h-59 Py_file_input]]1372 [def Py_single_input [@http://www.python.org/doc/current/api/veryhigh.html#l2h-60 Py_single_input]]1373 1368 [def Py_XINCREF [@http://www.python.org/doc/current/api/countingRefs.html#l2h-65 Py_XINCREF]] 1374 1369 [def Py_XDECREF [@http://www.python.org/doc/current/api/countingRefs.html#l2h-67 Py_XDECREF]] 1375 1370 [def PyImport_AppendInittab [@http://www.python.org/doc/current/api/importing.html#l2h-137 PyImport_AppendInittab]] … … 1396 1391 1397 1392 [h2 Building embedded programs] 1398 1393 1399 To be able to use embedding in your programs, they have to be linkedto1400 both Boost.Python's a nd Python's static linklibrary.1394 To be able to embed python into your programs, you have to link to 1395 both Boost.Python's as well as Python's own runtime library. 1401 1396 1402 Boost.Python's static linklibrary comes in two variants. Both are located1397 Boost.Python's library comes in two variants. Both are located 1403 1398 in Boost's [^/libs/python/build/bin-stage] subdirectory. On Windows, the 1404 1399 variants are called [^boost_python.lib] (for release builds) and 1405 1400 [^boost_python_debug.lib] (for debugging). If you can't find the libraries, 1406 1401 you probably haven't built Boost.Python yet. See 1407 1402 [@../../../building.html Building and Testing] on how to do this. 1408 1403 1409 Python's static linklibrary can be found in the [^/libs] subdirectory of1404 Python's library can be found in the [^/libs] subdirectory of 1410 1405 your Python directory. On Windows it is called pythonXY.lib where X.Y is 1411 1406 your major Python version number. 1412 1407 … … 1444 1439 1445 1440 # Call other Python C API routines to use the interpreter.\n\n 1446 1441 1447 # Call Py_Finalize() to stop the interpreter and release its resources. 1442 [/ # Call Py_Finalize() to stop the interpreter and release its resources.] 1443 1444 [blurb __note__ [*Note that at this time you must not call Py_Finalize() to stop the 1445 interpreter. This may be fixed in a future version of boost.python.] 1446 ] 1448 1447 1449 1448 (Of course, there can be other C++ code between all of these steps.) 1450 1449 … … 1461 1460 Fortunately Boost.Python provides the [@../../../v2/handle.html handle] and 1462 1461 [@../../../v2/object.html object] class templates to automate the process. 1463 1462 1464 [h2 Reference-counting handles and objects]1465 1466 There are two ways in which a function in the Python/C API can return a1467 [^PyObject*]: as a ['borrowed reference] or as a ['new reference]. Which of1468 these a function uses, is listed in that function's documentation. The two1469 require slightely different approaches to reference-counting but both can1470 be 'handled' by Boost.Python.1471 1472 For a function returning a ['borrowed reference] we'll have to tell the1473 [^handle] that the [^PyObject*] is borrowed with the aptly named1474 [@../../../v2/handle.html#borrowed-spec borrowed] function. Two functions1475 returning borrowed references are PyImport_AddModule and PyModule_GetDict.1476 The former returns a reference to an already imported module, the latter1477 retrieves a module's namespace dictionary. Let's use them to retrieve the1478 namespace of the [^__main__] module:1479 1480 object main_module((1481 handle<>(borrowed(PyImport_AddModule("__main__")))));1482 1483 object main_namespace = main_module.attr("__dict__");1484 1485 For a function returning a ['new reference] we can just create a [^handle]1486 out of the raw [^PyObject*] without wrapping it in a call to borrowed. One1487 such function that returns a new reference is PyRun_String which we'll1488 discuss in the next section.1489 1490 [blurb __note__ [*Handle is a class ['template], so why haven't we been using any template parameters?]\n1491 \n1492 [^handle] has a single template parameter specifying the type of the managed object. This type is [^PyObject] 99% of the time, so the parameter was defaulted to [^PyObject] for convenience. Therefore we can use the shorthand [^handle<>] instead of the longer, but equivalent, [^handle<PyObject>].1493 ]1494 1495 1463 [h2 Running Python code] 1496 1464 1497 To run Python code from C++ there is a family of functions in the API 1498 starting with the PyRun prefix. You can find the full list of these 1499 functions [@http://www.python.org/doc/current/api/veryhigh.html here]. They 1500 all work similarly so we will look at only one of them, namely: 1501 1502 PyObject* PyRun_String(char *str, int start, PyObject *globals, PyObject *locals) 1503 1504 PyRun_String takes the code to execute as a null-terminated (C-style) 1505 string in its [^str] parameter. The function returns a new reference to a 1506 Python object. Which object is returned depends on the [^start] paramater. 1465 Boost.python provides three related functions to run Python code from C++. 1507 1466 1508 The [^start] parameter is the start symbol from the Python grammar to use 1509 for interpreting the code. The possible values are: 1510 1511 [table Start symbols 1512 1513 [[Py_eval_input] [for interpreting isolated expressions]] 1514 [[Py_file_input] [for interpreting sequences of statements]] 1515 [[Py_single_input] [for interpreting a single statement]] 1516 ] 1467 object eval(str expression, object globals = object(), object locals = object()) 1468 object exec(str code, object globals = object(), object locals = object()) 1469 object exec_file(str filename, object globals = object(), object locals = object()) 1517 1470 1518 When using Py_eval_input, the input string must contain a single expression 1519 and its result is returned. When using Py_file_input, the string can 1520 contain an abitrary number of statements and None is returned. 1521 Py_single_input works in the same way as Py_file_input but only accepts a 1522 single statement. 1471 eval evaluates the given expression and returns the resulting value. 1472 exec executes the given code (typically a set of statements) returning the result, 1473 and exec_file executes the code contained in the given file. 1523 1474 1524 Lastly, the [^globals] and [^locals] parameters are Python dictionaries1475 The [^globals] and [^locals] parameters are Python dictionaries 1525 1476 containing the globals and locals of the context in which to run the code. 1526 1477 For most intents and purposes you can use the namespace dictionary of the 1527 1478 [^__main__] module for both parameters. 1528 1479 1529 We have already seen how to get the [^__main__] module's namespace so let's 1530 run some Python code in it: 1480 Boost.python provides a function to import a module: 1531 1481 1532 object main_module(( 1533 handle<>(borrowed(PyImport_AddModule("__main__"))))); 1482 object import(str name) 1534 1483 1535 object main_namespace = main_module.attr("__dict__"); 1484 import imports a python module (potentially loading it into the running process 1485 first), and returns it. 1536 1486 1537 handle<> ignored((PyRun_String( 1487 Let's import the [^__main__] module and run some Python code in its namespace: 1538 1488 1539 "hello = file('hello.txt', 'w')\n" 1540 "hello.write('Hello world!')\n" 1541 "hello.close()" 1542 1543 , Py_file_input 1544 , main_namespace.ptr() 1545 , main_namespace.ptr()) 1546 )); 1489 object main_module = import("__main__"); 1490 object main_namespace = main_module.attr("__dict__"); 1547 1491 1548 Because the Python/C API doesn't know anything about [^object]s, we used 1549 the object's [^ptr] member function to retrieve the [^PyObject*]. 1492 object ignored = exec("hello = file('hello.txt', 'w')\n" 1493 "hello.write('Hello world!')\n" 1494 "hello.close()", 1495 main_namespace); 1550 1496 1551 1497 This should create a file called 'hello.txt' in the current directory 1552 1498 containing a phrase that is well-known in programming circles. 1553 1499 1554 [blurb 1555 __note__ [*Note] that we wrap the return value of PyRun_String in a 1556 (nameless) [^handle] even though we are not interested in it. If we didn't 1557 do this, the the returned object would be kept alive unnecessarily. Unless 1558 you want to be a Dr. Frankenstein, always wrap [^PyObject*]s in [^handle]s. 1559 ] 1560 1561 [h2 Beyond handles] 1500 [h2 Manipulating Python objects] 1562 1501 1563 It's nice that [^handle] manages the reference counting details for us, but 1564 other than that it doesn't do much. Often we'd like to have a more useful 1565 class to manipulate Python objects. But we have already seen such a class 1566 above, and in the [@python/object.html previous section]: the aptly 1567 named [^object] class and it's derivatives. We've already seen that they 1568 can be constructed from a [^handle]. The following examples should further 1569 illustrate this fact: 1570 1571 object main_module(( 1572 handle<>(borrowed(PyImport_AddModule("__main__"))))); 1502 Often we'd like to have a class to manipulate Python objects. 1503 But we have already seen such a class above, and in the 1504 [@python/object.html previous section]: the aptly named [^object] class 1505 and its derivatives. We've already seen that they can be constructed from 1506 a [^handle]. The following examples should further illustrate this fact: 1573 1507 1508 object main_module = import("__main__"); 1574 1509 object main_namespace = main_module.attr("__dict__"); 1575 1576 handle<> ignored((PyRun_String( 1577 1578 "result = 5 ** 2" 1579 1580 , Py_file_input 1581 , main_namespace.ptr() 1582 , main_namespace.ptr()) 1583 )); 1584 1510 object ignored = exec("result = 5 ** 2", main_namespace); 1585 1511 int five_squared = extract<int>(main_namespace["result"]); 1586 1512 1587 1513 Here we create a dictionary object for the [^__main__] module's namespace. 1588 1514 Then we assign 5 squared to the result variable and read this variable from 1589 the dictionary. Another way to achieve the same result is to let 1590 PyRun_String return the result directly with Py_eval_input: 1591 1592 object result((handle<>( 1593 PyRun_String("5 ** 2" 1594 , Py_eval_input 1595 , main_namespace.ptr() 1596 , main_namespace.ptr())) 1597 )); 1515 the dictionary. Another way to achieve the same result is to use eval instead, 1516 which returns the result directly: 1598 1517 1518 object result = eval("5 ** 2"); 1599 1519 int five_squared = extract<int>(result); 1600 1520 1601 [blurb1602 __note__ [*Note] that [^object]'s member function to return the wrapped1603 [^PyObject*] is called [^ptr] instead of [^get]. This makes sense if you1604 take into account the different functions that [^object] and [^handle]1605 perform.1606 ]1607 1608 1521 [h2 Exception handling] 1609 1522 1610 1523 If an exception occurs in the execution of some Python code, the PyRun_String … … 1615 1528 1616 1529 try 1617 1530 { 1618 object result((handle<>(PyRun_String( 1619 "5/0" 1620 , Py_eval_input 1621 , main_namespace.ptr() 1622 , main_namespace.ptr())) 1623 )); 1624 1531 object result = eval("5/0"); 1625 1532 // execution will never get here: 1626 1533 int five_divided_by_zero = extract<int>(result); 1627 1534 } -
v2/exec.html
RCS file: /cvsroot/boost/boost/libs/python/doc/v2/exec.html,v retrieving revision 1.3 diff -u -r1.3 exec.html
38 38 39 39 <dd> 40 40 <dl class="page-index"> 41 <dt><a href="#eval-spec"><code>eval</code></a></dt> 41 42 <dt><a href="#exec-spec"><code>exec</code></a></dt> 42 43 <dt><a href="#exec_file-spec"><code>exec_file</code></a></dt> 43 44 </dl> … … 52 53 53 54 <h2><a name="functions"></a>Functions</h2> 54 55 56 <h3><a name="eval-spec"></a><code>eval</code></h3> 57 <pre> 58 object eval(str expression, 59 object globals = object(), 60 object locals = object()); 61 </pre> 62 <dl class="function-semantics"> 63 <dt><b>Effects:</b> 64 Evaluate Python expression from <code>expression</code> in the context 65 specified by the dictionaries <code>globals</code> and <code>locals</code>. 66 </dt> 67 <dt><b>Returns:</b> 68 An instance of <a href="object.html#object-spec">object</a> 69 which holds the value of the expression. 70 </dt> 71 </dl> 72 55 73 <h3><a name="exec-spec"></a><code>exec</code></h3> 56 74 <pre> 57 75 object exec(str code, -
v2/reference.html
RCS file: /cvsroot/boost/boost/libs/python/doc/v2/reference.html,v retrieving revision 1.40 diff -u -r1.40 reference.html
977 977 978 978 <dd> 979 979 <dl class="index"> 980 <dt><a href="exec.html#eval-spec">eval</a></dt> 980 981 <dt><a href="exec.html#exec-spec">exec</a></dt> 981 982 <dt><a href="exec.html#exec_file-spec">exec_file</a></dt> 982 983 </dl>