Skip to content

Commit becc033

Browse files
committed
First Commit
Fairly mature and reviewed. Lets release!
0 parents  commit becc033

22 files changed

+3249
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
doc/sphinx/build/*
2+
src/build/*
3+
*.so
4+
PythonExtensionPatterns/*

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>PythonExtensionPatterns</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

.pydevproject

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/${PROJECT_DIR_NAME}/src</path>
5+
</pydev_pathproperty>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
7+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">python3.3</pydev_property>
8+
</pydev_project>

PythonExtensionPatterns.bbprojectd/Scratchpad.txt

Whitespace-only changes.

PythonExtensionPatterns.bbprojectd/Unix Worksheet.worksheet

Whitespace-only changes.

doc/sphinx/Makefile

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line.
5+
SPHINXOPTS =
6+
SPHINXBUILD = sphinx-build
7+
PAPER =
8+
BUILDDIR = build
9+
10+
# User-friendly check for sphinx-build
11+
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
12+
$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/)
13+
endif
14+
15+
# Internal variables.
16+
PAPEROPT_a4 = -D latex_paper_size=a4
17+
PAPEROPT_letter = -D latex_paper_size=letter
18+
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
19+
# the i18n builder cannot share the environment and doctrees with the others
20+
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
21+
22+
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
23+
24+
help:
25+
@echo "Please use \`make <target>' where <target> is one of"
26+
@echo " html to make standalone HTML files"
27+
@echo " dirhtml to make HTML files named index.html in directories"
28+
@echo " singlehtml to make a single large HTML file"
29+
@echo " pickle to make pickle files"
30+
@echo " json to make JSON files"
31+
@echo " htmlhelp to make HTML files and a HTML help project"
32+
@echo " qthelp to make HTML files and a qthelp project"
33+
@echo " devhelp to make HTML files and a Devhelp project"
34+
@echo " epub to make an epub"
35+
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
36+
@echo " latexpdf to make LaTeX files and run them through pdflatex"
37+
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
38+
@echo " text to make text files"
39+
@echo " man to make manual pages"
40+
@echo " texinfo to make Texinfo files"
41+
@echo " info to make Texinfo files and run them through makeinfo"
42+
@echo " gettext to make PO message catalogs"
43+
@echo " changes to make an overview of all changed/added/deprecated items"
44+
@echo " xml to make Docutils-native XML files"
45+
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
46+
@echo " linkcheck to check all external links for integrity"
47+
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
48+
49+
clean:
50+
rm -rf $(BUILDDIR)/*
51+
52+
html:
53+
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
54+
@echo
55+
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
56+
57+
dirhtml:
58+
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
59+
@echo
60+
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
61+
62+
singlehtml:
63+
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
64+
@echo
65+
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
66+
67+
pickle:
68+
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
69+
@echo
70+
@echo "Build finished; now you can process the pickle files."
71+
72+
json:
73+
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
74+
@echo
75+
@echo "Build finished; now you can process the JSON files."
76+
77+
htmlhelp:
78+
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
79+
@echo
80+
@echo "Build finished; now you can run HTML Help Workshop with the" \
81+
".hhp project file in $(BUILDDIR)/htmlhelp."
82+
83+
qthelp:
84+
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
85+
@echo
86+
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
87+
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
88+
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/PythonExtensionPatterns.qhcp"
89+
@echo "To view the help file:"
90+
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/PythonExtensionPatterns.qhc"
91+
92+
devhelp:
93+
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
94+
@echo
95+
@echo "Build finished."
96+
@echo "To view the help file:"
97+
@echo "# mkdir -p $$HOME/.local/share/devhelp/PythonExtensionPatterns"
98+
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/PythonExtensionPatterns"
99+
@echo "# devhelp"
100+
101+
epub:
102+
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
103+
@echo
104+
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
105+
106+
latex:
107+
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
108+
@echo
109+
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
110+
@echo "Run \`make' in that directory to run these through (pdf)latex" \
111+
"(use \`make latexpdf' here to do that automatically)."
112+
113+
latexpdf:
114+
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
115+
@echo "Running LaTeX files through pdflatex..."
116+
$(MAKE) -C $(BUILDDIR)/latex all-pdf
117+
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
118+
119+
latexpdfja:
120+
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
121+
@echo "Running LaTeX files through platex and dvipdfmx..."
122+
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
123+
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
124+
125+
text:
126+
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
127+
@echo
128+
@echo "Build finished. The text files are in $(BUILDDIR)/text."
129+
130+
man:
131+
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
132+
@echo
133+
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
134+
135+
texinfo:
136+
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
137+
@echo
138+
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
139+
@echo "Run \`make' in that directory to run these through makeinfo" \
140+
"(use \`make info' here to do that automatically)."
141+
142+
info:
143+
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
144+
@echo "Running Texinfo files through makeinfo..."
145+
make -C $(BUILDDIR)/texinfo info
146+
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
147+
148+
gettext:
149+
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
150+
@echo
151+
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
152+
153+
changes:
154+
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
155+
@echo
156+
@echo "The overview file is in $(BUILDDIR)/changes."
157+
158+
linkcheck:
159+
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
160+
@echo
161+
@echo "Link check complete; look for any errors in the above output " \
162+
"or in $(BUILDDIR)/linkcheck/output.txt."
163+
164+
doctest:
165+
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
166+
@echo "Testing of doctests in the sources finished, look at the " \
167+
"results in $(BUILDDIR)/doctest/output.txt."
168+
169+
xml:
170+
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
171+
@echo
172+
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
173+
174+
pseudoxml:
175+
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
176+
@echo
177+
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
.. toctree::
2+
:maxdepth: 3
3+
4+
5+
===========================================
6+
A Pythonic Coding Pattern for C Functions
7+
===========================================
8+
9+
To avoid all the errors we have seen it is useful to have a C coding pattern for handling ``PyObjects`` that does the following:
10+
11+
* No early returns and a single place for clean up code.
12+
* Borrowed references incref'd and decref'd correctly.
13+
* No stale Exception from previous execution path.
14+
* Exceptions set and tested.
15+
* NULL is returned when an exception is set.
16+
* Non-NULL is returned when no exception is set.
17+
18+
The basic pattern in C is similar to Python's try/except/finally pattern:
19+
20+
.. code-block:: c
21+
22+
try:
23+
/* Do fabulous stuff here. */
24+
except:
25+
/* Handle every abnormal condition and clean up. */
26+
finally:
27+
/* Clean up under normal conditions and return an appropriate value. */
28+
29+
30+
Firstly we set any local ``PyObject`` (s) and the return value to ``NULL``:
31+
32+
.. code-block:: c
33+
34+
static PyObject *function(PyObject *arg_1) {
35+
PyObject *obj_a = NULL;
36+
PyObject *ret = NULL;
37+
38+
Then we have a little bit of Pythonic C - this can be omitted:
39+
40+
.. code-block:: c
41+
42+
goto try; /* Pythonic 'C' ;-) */
43+
try:
44+
45+
Check that there are no lingering Exceptions:
46+
47+
.. code-block:: c
48+
49+
assert(! PyErr_Occurred());
50+
51+
An alternative check for no lingering Exceptions:
52+
53+
.. code-block:: c
54+
55+
if(PyErr_Occurred()) {
56+
goto except;
57+
}
58+
59+
Now we assume that any argument is a "Borrowed" reference so we increment it (we need a matching ``Py_DECREF`` before function exit, see below). The first pattern assumes a non-NULL argument.
60+
61+
.. code-block:: c
62+
63+
assert(arg_1);
64+
Py_INCREF(arg_1);
65+
66+
If you are willing to accept NULL arguments then this pattern would be more suitable:
67+
68+
.. code-block:: c
69+
70+
if (arg_1) {
71+
Py_INCREF(arg_1);
72+
}
73+
74+
Of course the same test must be used when calling ``Py_DECFREF``, or just use ``Py_XDECREF``.
75+
76+
Now we create any local objects, if they are "Borrowed" references we need to incref them. With any abnormal behaviour we do a local jump straight to the cleanup code.
77+
78+
.. code-block:: c
79+
80+
/* Local object creation. */
81+
/* obj_a = ...; */
82+
if (! obj_a) {
83+
PyErr_SetString(PyExc_ValueError, "Ooops.");
84+
goto except;
85+
}
86+
/* If obj_a is a borrowed reference rather than a new reference. */
87+
Py_INCREF(obj_a);
88+
89+
Create the return value and deal with abnormal behaviour in the same way:
90+
91+
.. code-block:: c
92+
93+
/* More of your code to do stuff with arg_1 and obj_a. */
94+
/* Return object creation, ret should be a new reference otherwise you are in trouble. */
95+
/* ret = ...; */
96+
if (! ret) {
97+
PyErr_SetString(PyExc_ValueError, "Ooops again.");
98+
goto except;
99+
}
100+
101+
You might want to check the contents of the return value here. On error jump to ``except:`` otherwise jump to ``finally:``.
102+
103+
.. code-block:: c
104+
105+
/* Any return value checking here. */
106+
107+
/* If success then check exception is clear,
108+
* then goto finally; with non-NULL return value. */
109+
assert(! PyErr_Occurred());
110+
assert(ret);
111+
goto finally;
112+
113+
This is the except block where we cleanup any local objects and set the return value to NULL.
114+
115+
.. code-block:: c
116+
117+
except:
118+
/* Failure so Py_XDECREF the return value here. */
119+
Py_XDECREF(ret);
120+
/* Check a Python error is set somewhere above. */
121+
assert(PyErr_Occurred());
122+
/* Signal failure. */
123+
ret = NULL;
124+
125+
Notice the ``except:`` block falls through to the ``finally:`` block.
126+
127+
.. code-block:: c
128+
129+
finally:
130+
/* All _local_ PyObjects declared at the entry point are Py_XDECREF'd here.
131+
* For new references this will free them. For borrowed references this
132+
* will return them to their previous refcount.
133+
*/
134+
Py_XDECREF(obj_a);
135+
/* Decrement the ref count of externally supplied the arguments here.
136+
* If you allow arg_1 == NULL then Py_XDECREF(arg_1). */
137+
Py_DECREF(arg_1);
138+
/* And return...*/
139+
return ret;
140+
}
141+
142+
143+
Here is the complete code with minimal comments:
144+
145+
.. code-block:: c
146+
147+
static PyObject *function(PyObject *arg_1) {
148+
PyObject *obj_a = NULL;
149+
PyObject *ret = NULL;
150+
151+
goto try;
152+
try:
153+
assert(! PyErr_Occurred());
154+
assert(arg_1);
155+
Py_INCREF(arg_1);
156+
157+
/* obj_a = ...; */
158+
if (! obj_a) {
159+
PyErr_SetString(PyExc_ValueError, "Ooops.");
160+
goto except;
161+
}
162+
/* Only do this if obj_a is a borrowed reference. */
163+
Py_INCREF(obj_a);
164+
165+
/* More of your code to do stuff with obj_a. */
166+
167+
/* Return object creation, ret must be a new reference. */
168+
/* ret = ...; */
169+
if (! ret) {
170+
PyErr_SetString(PyExc_ValueError, "Ooops again.");
171+
goto except;
172+
}
173+
assert(! PyErr_Occurred());
174+
assert(ret);
175+
goto finally;
176+
except:
177+
Py_XDECREF(ret);
178+
assert(PyErr_Occurred());
179+
ret = NULL;
180+
finally:
181+
/* Only do this if obj_a is a borrowed reference. */
182+
Py_XDECREF(obj_a);
183+
Py_DECREF(arg_1);
184+
return ret;
185+
}

0 commit comments

Comments
 (0)