Skip to content

Commit fe5f0c9

Browse files
authored
better cython debugging (#53821)
1 parent 43332c2 commit fe5f0c9

File tree

7 files changed

+105
-5
lines changed

7 files changed

+105
-5
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
.mesonpy-native-file.ini
4040
MANIFEST
4141
compile_commands.json
42-
debug
42+
.debug
4343

4444
# Python files #
4545
################

doc/source/development/debugging_extensions.rst

+28-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ For Python developers with limited or no C/C++ experience this can seem a daunti
1414
2. `Fundamental Python Debugging Part 2 - Python Extensions <https://willayd.com/fundamental-python-debugging-part-2-python-extensions.html>`_
1515
3. `Fundamental Python Debugging Part 3 - Cython Extensions <https://willayd.com/fundamental-python-debugging-part-3-cython-extensions.html>`_
1616

17-
Generating debug builds
18-
-----------------------
17+
Debugging locally
18+
-----------------
1919

2020
By default building pandas from source will generate a release build. To generate a development build you can type::
2121

@@ -27,6 +27,32 @@ By default building pandas from source will generate a release build. To generat
2727

2828
By specifying ``builddir="debug"`` all of the targets will be built and placed in the debug directory relative to the project root. This helps to keep your debug and release artifacts separate; you are of course able to choose a different directory name or omit altogether if you do not care to separate build types.
2929

30+
Using Docker
31+
------------
32+
33+
To simplify the debugging process, pandas has created a Docker image with a debug build of Python and the gdb/Cython debuggers pre-installed. You may either ``docker pull pandas/pandas-debug`` to get access to this image or build it from the ``tooling/debug`` folder locallly.
34+
35+
You can then mount your pandas repository into this image via:
36+
37+
.. code-block:: sh
38+
39+
docker run --rm -it -w /data -v ${PWD}:/data pandas/pandas-debug
40+
41+
Inside the image, you can use meson to build/install pandas and place the build artifacts into a ``debug`` folder using a command as follows:
42+
43+
.. code-block:: sh
44+
45+
python -m pip install -ve . --no-build-isolation --config-settings=builddir="debug" --config-settings=setup-args="-Dbuildtype=debug"
46+
47+
If planning to use cygdb, the files required by that application are placed within the build folder. So you have to first ``cd`` to the build folder, then start that application.
48+
49+
.. code-block:: sh
50+
51+
cd debug
52+
cygdb
53+
54+
Within the debugger you can use `cygdb commands <https://docs.cython.org/en/latest/src/userguide/debugging.html#using-the-debugger>`_ to navigate cython extensions.
55+
3056
Editor support
3157
--------------
3258

pandas/_libs/meson.build

+9-1
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ libs_sources = {
101101
'writers': {'sources': ['writers.pyx']}
102102
}
103103

104+
cython_args = [
105+
'--include-dir',
106+
meson.current_build_dir(),
107+
'-X always_allow_keywords=true'
108+
]
109+
if get_option('buildtype') == 'debug'
110+
cython_args += ['--gdb']
111+
endif
104112

105113
foreach ext_name, ext_dict : libs_sources
106114
py.extension_module(
107115
ext_name,
108116
ext_dict.get('sources'),
109-
cython_args: ['--include-dir', meson.current_build_dir(), '-X always_allow_keywords=true'],
117+
cython_args: cython_args,
110118
include_directories: [inc_np, inc_pd],
111119
dependencies: ext_dict.get('deps', ''),
112120
subdir: 'pandas/_libs',

pandas/_libs/tslibs/meson.build

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ tslibs_sources = {
1919
'vectorized': {'sources': ['vectorized.pyx']},
2020
}
2121

22+
cython_args = [
23+
'--include-dir',
24+
meson.current_build_dir(),
25+
'-X always_allow_keywords=true'
26+
]
27+
if get_option('buildtype') == 'debug'
28+
cython_args += ['--gdb']
29+
endif
30+
2231
foreach ext_name, ext_dict : tslibs_sources
2332
py.extension_module(
2433
ext_name,
2534
ext_dict.get('sources'),
26-
cython_args: ['--include-dir', meson.current_build_dir(), '-X always_allow_keywords=true'],
35+
cython_args: cython_args,
2736
include_directories: [inc_np, inc_pd],
2837
dependencies: ext_dict.get('deps', ''),
2938
subdir: 'pandas/_libs/tslibs',

setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ def maybe_cythonize(extensions, *args, **kwargs):
418418

419419
kwargs["nthreads"] = parsed.parallel
420420
build_ext.render_templates(_pxifiles)
421+
if debugging_symbols_requested:
422+
kwargs["gdb_debug"] = True
423+
421424
return cythonize(extensions, *args, **kwargs)
422425

423426

tooling/debug/Dockerfile.pandas-debug

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM ubuntu:latest
2+
3+
RUN apt-get update && apt-get upgrade -y
4+
RUN apt-get install -y build-essential git valgrind
5+
6+
# cpython dev install
7+
RUN git clone -b 3.10 --depth 1 https://github.com/python/cpython.git /clones/cpython
8+
RUN apt-get install -y libbz2-dev libffi-dev libssl-dev zlib1g-dev liblzma-dev libsqlite3-dev libreadline-dev
9+
RUN cd /clones/cpython && ./configure --with-pydebug && CFLAGS="-g3" make -s -j$(nproc) && make install
10+
11+
# gdb installation
12+
RUN apt-get install -y wget libgmp-dev
13+
RUN cd /tmp && wget http://mirrors.kernel.org/sourceware/gdb/releases/gdb-12.1.tar.gz && tar -zxf gdb-12.1.tar.gz
14+
RUN cd /tmp/gdb-12.1 && ./configure --with-python=python3 && make -j$(nproc) && make install
15+
RUN rm -r /tmp/gdb-12.1
16+
17+
# pandas dependencies
18+
RUN python3 -m pip install \
19+
cython \
20+
hypothesis \
21+
ninja \
22+
numpy \
23+
meson \
24+
meson-python \
25+
pytest \
26+
pytest-asyncio \
27+
python-dateutil \
28+
pytz \
29+
versioneer[toml]
30+
31+
# At the time this docker image was built, there was a bug/limitation
32+
# with meson where only having a python3 executable and not python
33+
# would cause the build to fail. This symlink could be removed if
34+
# users stick to always calling python3 within the container
35+
RUN ln -s /usr/local/bin/python3 /usr/local/bin/python

tooling/debug/README

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The Docker image here helps to set up an isolated environment containing a debug version of Python and a gdb installation which the Cython debugger can work with.
2+
3+
If you have internet access, you can pull a pre-built image via
4+
5+
```sh
6+
docker pull pandas/pandas-debug
7+
```
8+
9+
To build the image locally, you can do
10+
11+
```sh
12+
docker build . -t pandas/pandas-debug -f Dockerfile.pandas-debug
13+
```
14+
15+
For pandas developers, you can push a new copy of the image to dockerhub via
16+
17+
```sh
18+
docker push pandas/pandas-debug
19+
```

0 commit comments

Comments
 (0)