Skip to content

Commit e6d7666

Browse files
author
Kraig Brockschmidt
committed
1 parent 01efd34 commit e6d7666

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

docs/python/cpp-and-python.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Working with C++ and Python in Visual Studio | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: 09/28/2017
4+
ms.date: 1/2/20178
55
ms.reviewer: ""
66
ms.suite: ""
77
ms.technology:
@@ -123,14 +123,14 @@ For more information, see [Installing Python Support for Visual Studio](installa
123123
> Don't set the **C/C++ > Code Generation > Runtime Library** option to "Multi-threaded Debug DLL (/MDd)" even for a Debug configuration. Select the "Multi-threaded DLL (/MD)" runtime because that's what the non-debug Python binaries are built with. If you happen to set the /MDd option, you see error *C1189: Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG* when building a Debug configuration of your DLL. Furthermore, if you remove `Py_LIMITED_API` to avoid the build error, Python crashes when attempting to import the module. (The crash happens within the DLL's call to `PyModule_Create` as described later, with the output message of *Fatal Python error: PyThreadState_Get: no current thread*.)
124124
>
125125
> Note that the /MDd option is what's used to build the Python debug binaries (such as python_d.exe), but selecting it for an extension DLL still causes the build error with `Py_LIMITED_API`.
126-
126+
127127
1. Right-click the C++ project and select **Build** to test your configurations (both Debug and Release). The `.pyd` files are located in the *solution* folder under **Debug** and **Release**, not the C++ project folder itself.
128128

129129
1. Add the following code to the C++ project's main `.cpp` file:
130130

131131
```cpp
132132
#include <Windows.h>
133-
#include <cmath>
133+
#include <cmath>
134134

135135
const double e = 2.7182818284590452353602874713527;
136136

@@ -149,7 +149,6 @@ For more information, see [Installing Python Support for Visual Studio](installa
149149

150150
1. Build the C++ project again to confirm that your code is correct.
151151

152-
153152
## Convert the C++ project to an extension for Python
154153

155154
To make the C++ DLL into an extension for Python, you need to modify the exported methods to interact with Python types. Then you need to add a function that exports the module, along with definitions of the module's methods. For background on what's shown here, refer to the [Python/C API Reference Manual](https://docs.python.org/3/c-api/index.html) and especially [Module Objects](https://docs.python.org/3/c-api/module.html) on python.org. (Remember to select your version of Python from the drop-down control on the upper right.)
@@ -170,34 +169,35 @@ To make the C++ DLL into an extension for Python, you need to modify the exporte
170169
}
171170
```
172171

173-
1. Add a structure that defines how the C++ `tanh` function is presented to Python:
172+
1. Add a structure that defines how the C++ `tanh_impl` function is presented to Python:
174173

175174
```cpp
176175
static PyMethodDef superfastcode_methods[] = {
177-
// The first property is the name exposed to python, the second is the C++ function name
176+
// The first property is the name exposed to Python, fast_tanh, the second is the C++
177+
// function name that contains the implementation.
178178
{ "fast_tanh", (PyCFunction)tanh_impl, METH_O, nullptr },
179179

180180
// Terminate the array with an object containing nulls.
181181
{ nullptr, nullptr, 0, nullptr }
182182
};
183183
```
184184

185-
1. Add a structure that defines the module as you see it through Python code. (Filenames internal to the C++ project, like module.cpp, are inconsequential.)
185+
1. Add a structure that defines the module as you want to refer to it in your Python code, specifically when using the `from...import` statement. In the following example, the "superfastcode" module name means you can use `from superfastcode import fast_tanh` in Python, because `fast_tanh` is defined within `superfastcode_methods`. (Filenames internal to the C++ project, like module.cpp, are inconsequential.)
186186

187187
```cpp
188188
static PyModuleDef superfastcode_module = {
189189
PyModuleDef_HEAD_INIT,
190-
"superfastcode", // Module name as Python sees it
190+
"superfastcode", // Module name to use with Python import statements
191191
"Provides some functions, but faster", // Module description
192192
0,
193-
superfastcode_methods // Structure that defines the methods
193+
superfastcode_methods // Structure that defines the methods of the module
194194
};
195195
```
196196

197197
1. Add a method that Python calls when it loads the module, which must be named `PyInit_<module-name>`, where *&lt;module_name&gt;* exactly matches the C++ Project's **General > Target Name** property (that is, it matches the filename of the `.pyd` built by the project).
198198

199199
```cpp
200-
PyMODINIT_FUNC PyInit_superfastcode() {
200+
PyMODINIT_FUNC PyInit_superfastcode() {
201201
return PyModule_Create(&superfastcode_module);
202202
}
203203
```
@@ -248,7 +248,7 @@ After you've completed either of the methods above, you can now call the `fast_t
248248
1. Add the following lines in your `.py` file to call the `fast_tanh` method exported from the DLL and display its output. If you type the `from s` statement manually, you'll see `superfastcode` come up in the completion list, and after typing `import` the `fast_tanh` method appears.
249249

250250
```python
251-
from superfastcode import fast_tanh
251+
from superfastcode import fast_tanh
252252
test(lambda d: [fast_tanh(x) for x in d], '[fast_tanh(x) for x in d]')
253253
```
254254

0 commit comments

Comments
 (0)