Skip to content

Commit f5264ce

Browse files
committed
Add note on thread safety.
1 parent 0b5768c commit f5264ce

File tree

8 files changed

+1416
-1149
lines changed

8 files changed

+1416
-1149
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
build/
22
*.so
3-
PythonExtensionPatterns/
43
doc/sphinx/build/
54
__pycache__/

PythonExtensionPatterns.bbprojectd/paulross.bbprojectsettings

+1,140-1,089
Large diffs are not rendered by default.

PythonExtensionPatterns.bbprojectd/project.bbprojectdata

+34-34
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,43 @@
3030
<dict>
3131
<key>BookmarkData</key>
3232
<data>
33-
Ym9va0gEAAAAAAQQMAAAAAAAAAAAAAAAAAAAAAAAAAAA
33+
Ym9va1QEAAAAAAQQMAAAAAAAAAAAAAAAAAAAAAAAAAAA
3434
AAAAAAAAAAAAAAAAAAAAOAMAAAUAAAABAQAAVXNlcnMA
35-
AAAIAAAAAQEAAHBhdWxyb3NzCQAAAAEBAABEb2N1bWVu
36-
dHMAAAAJAAAAAQEAAHdvcmtzcGFjZQAAABcAAAABAQAA
37-
UHl0aG9uRXh0ZW5zaW9uUGF0dGVybnMAAwAAAAEBAABk
38-
b2MABgAAAAEBAABzcGhpbngAAAYAAAABAQAAc291cmNl
39-
AAAgAAAAAQYAAAQAAAAUAAAAJAAAADgAAABMAAAAbAAA
40-
AHgAAACIAAAACAAAAAQDAACOZAIAAAAAAAgAAAAEAwAA
41-
OfoIAAAAAAAIAAAABAMAADr6CAAAAAAACAAAAAQDAAD9
42-
mAsAAAAAAAgAAAAEAwAACYM5AAAAAAAIAAAABAMAAKHM
43-
OQAAAAAACAAAAAQDAADkHjoAAAAAAAgAAAAEAwAA9R46
44-
AAAAAAAgAAAAAQYAAMAAAADQAAAA4AAAAPAAAAAAAQAA
45-
EAEAACABAAAwAQAACAAAAAAEAABBuR/7+QAAABgAAAAB
46-
AgAAAgAAAAAAAAAPAAAAAAAAAAAAAAAAAAAAAAAA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EgAA
60+
AAQQAACAAAAAAAAAAAUQAAAoAQAAAAAAABAQAABgAQAA
61+
AAAAAEAQAABQAQAAAAAAAFUQAACIAQAAAAAAAFYQAACA
62+
AQAAAAAAAAIgAABIAgAAAAAAAAUgAACsAQAAAAAAABAg
63+
AADIAQAAAAAAABEgAAD8AQAAAAAAABIgAADcAQAAAAAA
64+
ABMgAADsAQAAAAAAACAgAAAoAgAAAAAAADAgAACAAQAA
65+
AAAAAAHAAACUAQAAAAAAABHAAAAUAAAAAAAAABLAAACg
66+
AQAAAAAAAIDwAABUAgAAAAAAAA==
6767
</data>
6868
<key>FileURL</key>
69-
<string>file://localhost/Users/paulross/Documents/workspace/PythonExtensionPatterns/doc/sphinx/source/</string>
69+
<string>file://localhost/Users/paulross/GitHub/paulross/PythonExtensionPatterns/doc/sphinx/source/</string>
7070
<key>RelativePath</key>
7171
<string>./doc/sphinx/source</string>
7272
<key>TypeID</key>

doc/sphinx/source/debugging/gcov.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ Configuring Xcode
3838
Running and Analysing Code Coverage
3939
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

41-
In Xcode select Product->Run then once that has completed note the GCov_build directory in Xcode by selecting Products-><project> in the left hand pane. In the right hand pane under 'Identity and Type' you should see 'Full Path' of something like::
41+
In Xcode select Product->Run then once that has completed note the GCov_build directory in Xcode by selecting Products-><project> in the left hand pane. In the right hand pane under 'Identity and Type' you should see 'Full Path' of something like:
42+
43+
.. code-block:: console
4244
4345
/Users/$USER/Library/Developer/Xcode/DerivedData/<project name and hash>/Build/Products/GCov_Build/<project>
4446

doc/sphinx/source/debugging/leak_newrefs_vg.rst

+12-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ In the second shell pidmon.py shows the sudden jump in memory usage:
124124

125125
.. code-block:: bash
126126
127-
python3 pidmon.py 14488
127+
$ python3 pidmon.py 14488
128128
pextmem(rss=7659520, vms=2475937792, pfaults=8380416, pageins=2617344)
129129
7.660 [Mb]
130130
7.660 [Mb] +0.000 [Mb]
@@ -146,7 +146,9 @@ Observing the Total Reference Counts
146146

147147
If you have a debug build of Python with ``Py_REF_DEBUG`` defined you might notice a very high level of total reference counts by either invoking Python with ``-X showrefcount`` or calling ``sys.gettotalrefcount()``.
148148

149-
For example::
149+
For example:
150+
151+
.. code-block:: python
150152
151153
>>> import sys
152154
>>> import cPyRefs
@@ -179,7 +181,9 @@ And those references are not collectable::
179181
Observing the Reference Counts for a Particular Type
180182
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
181183

182-
If you have a debug build with ``COUNT_ALLOCS`` [See: :ref:`debug-version-of-python-COUNT_ALLOCS-label`] defined you can see the references counts for each type. This build will have a new function ``sys.getcounts()`` which returns a list of tuples ``(tp_name, tp_allocs, tp_frees, tp_maxalloc)`` where ``tp_maxalloc`` is the maximum ever seen value of the reference ``tp_allocs - tp_frees``. The list is ordered by time of first object allocation::
184+
If you have a debug build with ``COUNT_ALLOCS`` [See: :ref:`debug-version-of-python-COUNT_ALLOCS-label`] defined you can see the references counts for each type. This build will have a new function ``sys.getcounts()`` which returns a list of tuples ``(tp_name, tp_allocs, tp_frees, tp_maxalloc)`` where ``tp_maxalloc`` is the maximum ever seen value of the reference ``tp_allocs - tp_frees``. The list is ordered by time of first object allocation:
185+
186+
.. code-block:: python
183187
184188
>>> import pprint
185189
>>> import sys
@@ -193,7 +197,9 @@ If you have a debug build with ``COUNT_ALLOCS`` [See: :ref:`debug-version-of-pyt
193197
('dict', 1421, 714, 714),
194198
('tuple', 13379, 9633, 3746)]
195199
196-
We can try our leaky code::
200+
We can try our leaky code:
201+
202+
.. code-block:: python
197203
198204
>>> import cPyRefs
199205
>>> cPyRefs.leakNewRefs(1000, 1000000)
@@ -211,6 +217,8 @@ There is a big jump in ``tp_maxalloc`` for ints that is worth investigating.
211217

212218
When the Python process finishes you get a dump of this list as the interpreter is broken down::
213219

220+
.. code-block:: console
221+
214222
memoryview alloc'd: 210, freed: 210, max in use: 1
215223
managedbuffer alloc'd: 210, freed: 210, max in use: 1
216224
PrettyPrinter alloc'd: 2, freed: 2, max in use: 1

doc/sphinx/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This describes reliable patterns of coding Python Extensions in C. It covers the
1919
super_call
2020
compiler_flags
2121
debugging/debug
22+
thread_safety
2223

2324

2425
Indices and tables

doc/sphinx/source/refcount.rst

+18-20
Original file line numberDiff line numberDiff line change
@@ -290,32 +290,30 @@ If we try a different string::
290290

291291
At least this will get your attention!
292292

293-
.. note::
293+
Incidentially from Python 3.3 onwards there is a module `faulthandler <https://docs.python.org/3.3/library/faulthandler.html#module-faulthandler>`_ that can give useful debugging information (file ``FaultHandlerExample.py``):
294294

295-
Incidentially from Python 3.3 onwards there is a module `faulthandler <https://docs.python.org/3.3/library/faulthandler.html#module-faulthandler>`_ that can give useful debugging information (file ``FaultHandlerExample.py``):
296-
297-
.. code-block:: python
298-
:linenos:
299-
:emphasize-lines: 7
300-
301-
import faulthandler
302-
faulthandler.enable()
295+
.. code-block:: python
296+
:linenos:
297+
:emphasize-lines: 5
303298
304-
import cPyRefs
299+
import faulthandler
300+
faulthandler.enable()
301+
import cPyRefs
302+
l = ['abc' * 200]
303+
cPyRefs.popBAD(l)
305304
306-
l = ['abc' * 200]
307-
cPyRefs.popBAD(l)
305+
And this is what you get:
308306

309-
And this is what you get::
307+
.. code-block:: console
310308
311-
$ python3 FaultHandlerExample.py
312-
Ref count was: 1
313-
Ref count now: 2305843009213693952
314-
Fatal Python error: Segmentation fault
309+
$ python3 FaultHandlerExample.py
310+
Ref count was: 1
311+
Ref count now: 2305843009213693952
312+
Fatal Python error: Segmentation fault
315313
316-
Current thread 0x00007fff73c88310:
317-
File "FaultHandlerExample.py", line 7 in <module>
318-
Segmentation fault: 11
314+
Current thread 0x00007fff73c88310:
315+
File "FaultHandlerExample.py", line 7 in <module>
316+
Segmentation fault: 11
319317
320318
There is a more subtle issue; suppose that in your Python code there is a reference to the last item in the list, then the problem suddenly "goes away"::
321319

0 commit comments

Comments
 (0)