[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版
Running Simple Code Strings Perhaps the simplest way to run Python code from C is by calling the PyRun_Simple String API functio ...
library file that is generated when Python is compiled, not the Python source code standard library. Today, everything in Python ...
embed-bytecode.exe all: $(BASICS) embed%.exe: embed%.o gcc embed$*.o -L$(PYLIB) -lpython3.1 -g -o $@ embed%.o: embed%.c gcc embe ...
Most of this output is produced by Python print statements sent from C to the linked- in Python library. It’s as if C has become ...
to a Python variable name. As a result, it’s probably more similar to the Python import built-in function. The PyRun_String call ...
running a string expression and assigning a global variable (X) within the module’s namespace to serve as input for a Python pri ...
Py_BuildValue Converts C values to Python objects We used both of the data conversion functions earlier in this chapter in exten ...
function object directly and printing its return value that is sent back to C. Input to the transform function is a function arg ...
Example 20-28. PP4E\Integrate\Embed\Basics\embed-dict.c / make a new dictionary for code string namespace / include <Python.h ...
There is one subtlety in this embedding mode: dictionaries that serve as namespaces for running code are generally required to h ...
/ make new namespace dictionary / pdict = PyDict_New(); if (pdict == NULL) return −1; PyDict_SetItemString(pdict, "builtins", Py ...
Registering Callback Handler Objects In the embedding examples thus far, C has been running and calling Python code from a stand ...
extending and embedding. Its extending interface (widget objects) is used to register Python callback handlers, which are later ...
if (pres != NULL) { / use and decref handler result / PyArg_Parse(pres, "s", &cres); printf("%s\n", cres); Py_DECREF(pres); ...
Ultimately, this C file is an extension module for Python, not a standalone C program that embeds Python (though C could just as ...
####################################### Python calls a C extension module to register handlers, trigger events ################# ...
having multiple Python interpreter levels active at the same time. Each level runs dif- ferent code and operates independently. ...
This is fairly easy stuff in Python. You can do all of these operations in C, too, but it takes a bit more code. The C file in E ...
reference count management every step of the way. In return for all the work, C gets to use the techniques shown in this file to ...
} pres = PyEval_CallObject(pmeth, pargs); / call method(x,y) / Py_DECREF(pmeth); Py_DECREF(pargs); if (pres == NULL) error("Erro ...
«
73
74
75
76
77
78
79
80
81
82
»
Free download pdf