Skip to content

Commit 1261e88

Browse files
committed
Improves C++ support.
1 parent 857473b commit 1261e88

File tree

9 files changed

+208
-133
lines changed

9 files changed

+208
-133
lines changed

PythonExtensionPatterns/PythonExtensionPatterns.xcodeproj/project.pbxproj

+28-24
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
/* Begin PBXBuildFile section */
1010
C376B9971AD07F4A0040BB9C /* scratch.c in Sources */ = {isa = PBXBuildFile; fileRef = C376B9961AD07F4A0040BB9C /* scratch.c */; };
1111
C38238D119253638002B27B7 /* cObjmodule.c in Sources */ = {isa = PBXBuildFile; fileRef = C38238D019253638002B27B7 /* cObjmodule.c */; };
12-
C39468E919264D6D004B5C26 /* noddy.c in Sources */ = {isa = PBXBuildFile; fileRef = C39468E319264D6D004B5C26 /* noddy.c */; };
13-
C39468EA19264D6D004B5C26 /* noddy2.c in Sources */ = {isa = PBXBuildFile; fileRef = C39468E419264D6D004B5C26 /* noddy2.c */; };
14-
C39468EB19264D6D004B5C26 /* noddy3.c in Sources */ = {isa = PBXBuildFile; fileRef = C39468E519264D6D004B5C26 /* noddy3.c */; };
15-
C39468EC19264D6D004B5C26 /* noddy4.c in Sources */ = {isa = PBXBuildFile; fileRef = C39468E619264D6D004B5C26 /* noddy4.c */; };
16-
C39468EE19264D6D004B5C26 /* shoddy.c in Sources */ = {isa = PBXBuildFile; fileRef = C39468E819264D6D004B5C26 /* shoddy.c */; };
12+
C3A41230209A39B200A6C2C1 /* noddy.c in Sources */ = {isa = PBXBuildFile; fileRef = C3A4122A209A39B200A6C2C1 /* noddy.c */; };
13+
C3A41231209A39B200A6C2C1 /* noddy2.c in Sources */ = {isa = PBXBuildFile; fileRef = C3A4122B209A39B200A6C2C1 /* noddy2.c */; };
14+
C3A41232209A39B200A6C2C1 /* noddy3.c in Sources */ = {isa = PBXBuildFile; fileRef = C3A4122C209A39B200A6C2C1 /* noddy3.c */; };
15+
C3A41233209A39B200A6C2C1 /* noddy4.c in Sources */ = {isa = PBXBuildFile; fileRef = C3A4122D209A39B200A6C2C1 /* noddy4.c */; };
16+
C3A41234209A39B200A6C2C1 /* shoddy.c in Sources */ = {isa = PBXBuildFile; fileRef = C3A4122E209A39B200A6C2C1 /* shoddy.c */; };
17+
C3A41236209A39D800A6C2C1 /* cExceptions.c in Sources */ = {isa = PBXBuildFile; fileRef = C3A41235209A39D800A6C2C1 /* cExceptions.c */; };
1718
C3BEEEC6191AB448007D7F35 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEEC5191AB448007D7F35 /* main.c */; };
1819
C3BEEEC8191AB448007D7F35 /* PythonExtensionPatterns.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C3BEEEC7191AB448007D7F35 /* PythonExtensionPatterns.1 */; };
1920
C3BEEECF191AB46E007D7F35 /* Python in Frameworks */ = {isa = PBXBuildFile; fileRef = C3BEEECE191AB46E007D7F35 /* Python */; };
2021
C3BEEED1191AB49D007D7F35 /* PythonExtensionPatterns.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEED0191AB49D007D7F35 /* PythonExtensionPatterns.c */; };
2122
C3BEEED7191B737E007D7F35 /* cPyRefs.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEED6191B737E007D7F35 /* cPyRefs.c */; };
2223
C3BEEED9191BD3D3007D7F35 /* cParseArgs.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEED8191BD3D3007D7F35 /* cParseArgs.c */; };
23-
C3BEEEDB191BFC1D007D7F35 /* cExcep.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEEDA191BFC1D007D7F35 /* cExcep.c */; };
2424
C3BEEEDD191CBB9D007D7F35 /* cCanonical.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEEDC191CBB9D007D7F35 /* cCanonical.c */; };
2525
C3BEEEDF191CFA9B007D7F35 /* cModuleGlobals.c in Sources */ = {isa = PBXBuildFile; fileRef = C3BEEEDE191CFA9B007D7F35 /* cModuleGlobals.c */; };
2626
/* End PBXBuildFile section */
@@ -42,11 +42,14 @@
4242
C376B9961AD07F4A0040BB9C /* scratch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = scratch.c; path = ../../src/scratch.c; sourceTree = "<group>"; };
4343
C382388B1925321C002B27B7 /* floatobject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = floatobject.h; path = ../../../../../../../Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m/floatobject.h; sourceTree = "<group>"; };
4444
C38238D019253638002B27B7 /* cObjmodule.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cObjmodule.c; path = ../../src/cObjmodule.c; sourceTree = "<group>"; };
45-
C39468E319264D6D004B5C26 /* noddy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy.c; path = "../../../../../Downloads/Python-3.3.5/Doc/includes/noddy.c"; sourceTree = "<group>"; };
46-
C39468E419264D6D004B5C26 /* noddy2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy2.c; path = "../../../../../Downloads/Python-3.3.5/Doc/includes/noddy2.c"; sourceTree = "<group>"; };
47-
C39468E519264D6D004B5C26 /* noddy3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy3.c; path = "../../../../../Downloads/Python-3.3.5/Doc/includes/noddy3.c"; sourceTree = "<group>"; };
48-
C39468E619264D6D004B5C26 /* noddy4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy4.c; path = "../../../../../Downloads/Python-3.3.5/Doc/includes/noddy4.c"; sourceTree = "<group>"; };
49-
C39468E819264D6D004B5C26 /* shoddy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = shoddy.c; path = "../../../../../Downloads/Python-3.3.5/Doc/includes/shoddy.c"; sourceTree = "<group>"; };
45+
C3A41229209A39B200A6C2C1 /* capsulethunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = capsulethunk.h; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/capsulethunk.h"; sourceTree = "<group>"; };
46+
C3A4122A209A39B200A6C2C1 /* noddy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy.c; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/noddy.c"; sourceTree = "<group>"; };
47+
C3A4122B209A39B200A6C2C1 /* noddy2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy2.c; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/noddy2.c"; sourceTree = "<group>"; };
48+
C3A4122C209A39B200A6C2C1 /* noddy3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy3.c; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/noddy3.c"; sourceTree = "<group>"; };
49+
C3A4122D209A39B200A6C2C1 /* noddy4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = noddy4.c; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/noddy4.c"; sourceTree = "<group>"; };
50+
C3A4122E209A39B200A6C2C1 /* shoddy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = shoddy.c; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/shoddy.c"; sourceTree = "<group>"; };
51+
C3A4122F209A39B200A6C2C1 /* typestruct.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = typestruct.h; path = "../../../../../dev/Python/Python-3.6.2/Doc/includes/typestruct.h"; sourceTree = "<group>"; };
52+
C3A41235209A39D800A6C2C1 /* cExceptions.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cExceptions.c; path = ../../src/cExceptions.c; sourceTree = "<group>"; };
5053
C3B868121BA964670082493C /* setup.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; name = setup.py; path = ../../src/setup.py; sourceTree = "<group>"; };
5154
C3BEEEC2191AB448007D7F35 /* PythonExtensionPatterns */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = PythonExtensionPatterns; sourceTree = BUILT_PRODUCTS_DIR; };
5255
C3BEEEC5191AB448007D7F35 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
@@ -55,7 +58,6 @@
5558
C3BEEED0191AB49D007D7F35 /* PythonExtensionPatterns.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = PythonExtensionPatterns.c; sourceTree = "<group>"; };
5659
C3BEEED6191B737E007D7F35 /* cPyRefs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cPyRefs.c; path = ../../src/cPyRefs.c; sourceTree = "<group>"; };
5760
C3BEEED8191BD3D3007D7F35 /* cParseArgs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cParseArgs.c; path = ../../src/cParseArgs.c; sourceTree = "<group>"; };
58-
C3BEEEDA191BFC1D007D7F35 /* cExcep.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cExcep.c; path = ../../src/cExcep.c; sourceTree = "<group>"; };
5961
C3BEEEDC191CBB9D007D7F35 /* cCanonical.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cCanonical.c; path = ../../src/cCanonical.c; sourceTree = "<group>"; };
6062
C3BEEEDE191CFA9B007D7F35 /* cModuleGlobals.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cModuleGlobals.c; path = ../../src/cModuleGlobals.c; sourceTree = "<group>"; };
6163
/* End PBXFileReference section */
@@ -91,15 +93,17 @@
9193
C3BEEEC4191AB448007D7F35 /* PythonExtensionPatterns */ = {
9294
isa = PBXGroup;
9395
children = (
96+
C3A41235209A39D800A6C2C1 /* cExceptions.c */,
97+
C3A41229209A39B200A6C2C1 /* capsulethunk.h */,
98+
C3A4122A209A39B200A6C2C1 /* noddy.c */,
99+
C3A4122B209A39B200A6C2C1 /* noddy2.c */,
100+
C3A4122C209A39B200A6C2C1 /* noddy3.c */,
101+
C3A4122D209A39B200A6C2C1 /* noddy4.c */,
102+
C3A4122E209A39B200A6C2C1 /* shoddy.c */,
103+
C3A4122F209A39B200A6C2C1 /* typestruct.h */,
94104
C3B868121BA964670082493C /* setup.py */,
95-
C39468E319264D6D004B5C26 /* noddy.c */,
96105
C376B9961AD07F4A0040BB9C /* scratch.c */,
97-
C39468E419264D6D004B5C26 /* noddy2.c */,
98-
C39468E519264D6D004B5C26 /* noddy3.c */,
99-
C39468E619264D6D004B5C26 /* noddy4.c */,
100-
C39468E819264D6D004B5C26 /* shoddy.c */,
101106
C3BEEEDC191CBB9D007D7F35 /* cCanonical.c */,
102-
C3BEEEDA191BFC1D007D7F35 /* cExcep.c */,
103107
C3BEEEDE191CFA9B007D7F35 /* cModuleGlobals.c */,
104108
C38238D019253638002B27B7 /* cObjmodule.c */,
105109
C3BEEED8191BD3D3007D7F35 /* cParseArgs.c */,
@@ -165,19 +169,19 @@
165169
buildActionMask = 2147483647;
166170
files = (
167171
C376B9971AD07F4A0040BB9C /* scratch.c in Sources */,
168-
C39468EB19264D6D004B5C26 /* noddy3.c in Sources */,
172+
C3A41236209A39D800A6C2C1 /* cExceptions.c in Sources */,
169173
C3BEEED9191BD3D3007D7F35 /* cParseArgs.c in Sources */,
170-
C39468EA19264D6D004B5C26 /* noddy2.c in Sources */,
174+
C3A41230209A39B200A6C2C1 /* noddy.c in Sources */,
171175
C3BEEED1191AB49D007D7F35 /* PythonExtensionPatterns.c in Sources */,
172176
C3BEEEDD191CBB9D007D7F35 /* cCanonical.c in Sources */,
173177
C38238D119253638002B27B7 /* cObjmodule.c in Sources */,
174-
C39468EE19264D6D004B5C26 /* shoddy.c in Sources */,
175-
C3BEEEDB191BFC1D007D7F35 /* cExcep.c in Sources */,
178+
C3A41234209A39B200A6C2C1 /* shoddy.c in Sources */,
179+
C3A41231209A39B200A6C2C1 /* noddy2.c in Sources */,
180+
C3A41233209A39B200A6C2C1 /* noddy4.c in Sources */,
176181
C3BEEED7191B737E007D7F35 /* cPyRefs.c in Sources */,
177-
C39468E919264D6D004B5C26 /* noddy.c in Sources */,
178182
C3BEEEC6191AB448007D7F35 /* main.c in Sources */,
179183
C3BEEEDF191CFA9B007D7F35 /* cModuleGlobals.c in Sources */,
180-
C39468EC19264D6D004B5C26 /* noddy4.c in Sources */,
184+
C3A41232209A39B200A6C2C1 /* noddy3.c in Sources */,
181185
);
182186
runOnlyForDeploymentPostprocessing = 0;
183187
};

doc/sphinx/source/code_layout.rst

+24-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ Testing CPython Utility Code
3737

3838
When making Python C API calls from a C/C++ environment it is important to initialise the Python interpreter. For example, this small program segfaults:
3939

40-
.. code-block:: sh
40+
.. code-block:: c
41+
:linenos:
42+
:emphasize-lines: 4, 5, 6
4143
4244
#include <Python.h>
4345
4446
int main(int /* argc */, const char *[] /* argv[] */) {
47+
/* Forgot this:
48+
Py_Initialize();
49+
*/
4550
PyErr_Format(PyExc_TypeError, "Stuff",);
4651
return 0;
4752
}
@@ -50,3 +55,21 @@ The reason is that ``PyErr_Format`` calls ``PyThreadState *thread_state = PyThre
5055

5156
So you need to call ``Py_Initialize()`` to set up statically allocated interpreter data. Alternativley put ``if (! Py_IsInitialized()) Py_Initialize();`` in every test. See: `https://docs.python.org/3/c-api/init.html <https://docs.python.org/3/c-api/init.html>`_
5257

58+
Here are a couple of useful C++ functions that assert all is well that can be used at the begining of any function:
59+
60+
.. code-block:: cpp
61+
62+
/* Returns non zero if Python is initialised and there is no Python error set.
63+
* The second version also checks that the given pointer is non-NULL
64+
* Use this thus, it will do nothing if NDEBUG is defined:
65+
*
66+
* assert(cpython_asserts());
67+
* assert(cpython_asserts(p));
68+
*/
69+
int cpython_asserts() {
70+
return Py_IsInitialized() && PyErr_Occurred() == NULL;
71+
}
72+
73+
int cpython_asserts(PyObject *pobj) {
74+
return cpython_asserts() && pobj != NULL;
75+
}

doc/sphinx/source/cpp.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
.. _cpp_and_cpython:
3+
4+
********************************************
5+
Using C++ With CPython Code
6+
********************************************
7+
8+
Using C++ can take a lot of the pain out of interfacing CPython code, here are some examples.
9+
10+
.. include:: cpp_and_cpython.rst
11+
.. include:: cpp_and_unicode.rst

0 commit comments

Comments
 (0)