Skip to content

Commit

Permalink
Merge pull request #139 from bluescarni/pr/py312_fixes
Browse files Browse the repository at this point in the history
Build fixes for Python 3.12
  • Loading branch information
bluescarni authored Oct 20, 2023
2 parents 84eda66 + 299e474 commit a8fbce7
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 29 deletions.
9 changes: 9 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ jobs:
command: bash ./tools/circleci_conda_heyoka_head_310.sh
- store_artifacts:
path: doc/_build/html
conda_heyoka_head_312:
docker:
- image: cimg/base:current
steps:
- checkout
- run:
name: Build and test
command: bash ./tools/circleci_conda_heyoka_head_312.sh
conda_heyoka_head_release_310:
docker:
- image: cimg/base:current
Expand All @@ -47,5 +55,6 @@ workflows:
jobs:
- conda_heyoka_head_38
- conda_heyoka_head_310
- conda_heyoka_head_312
- conda_heyoka_head_release_310
- ubuntu_arm64
9 changes: 9 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
Changelog
=========

3.0.1 (unreleased)
------------------

Fix
~~~

- Fix building against Python 3.12
(`#139 <https://github.com/bluescarni/heyoka.py/pull/139>`__).

3.0.0 (2023-10-07)
------------------

Expand Down
10 changes: 10 additions & 0 deletions heyoka/_test_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -1918,10 +1918,20 @@ def test_basic(self):
# Larger int, no precision.
x = real(-(1 << 50))
self.assertTrue("125899906842624" in str(x))
self.assertTrue(str(x).startswith("-"))

x = real(1 << 50)
self.assertTrue("125899906842624" in str(x))
self.assertFalse(str(x).startswith("-"))

# Int that does not fit in long/long long, no precision.
x = real(-(1 << 128))
self.assertTrue("40282366920938463463374607431768211456" in str(x))
self.assertTrue(str(x).startswith("-"))

x = real(1 << 128)
self.assertTrue("40282366920938463463374607431768211456" in str(x))
self.assertFalse(str(x).startswith("-"))

# Small int, explicit precision.
x = real(1, 12)
Expand Down
55 changes: 44 additions & 11 deletions heyoka/expose_real.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,44 @@ PyObject *py_real_new([[maybe_unused]] PyTypeObject *type, PyObject *, PyObject
return py_real_from_args();
}

// Small helper to fetch the number of digits and the sign
// of a Python integer. The integer must be nonzero.
auto py_int_size_sign(PyLongObject *nptr)
{

#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 11

// Fetch the signed size.
const auto ob_size = nptr->ob_base.ob_size;
assert(ob_size != 0);

// Is it negative?
const auto neg = ob_size < 0;

// Compute the unsigned size.
using size_type = std::make_unsigned_t<std::remove_const_t<decltype(ob_size)>>;
static_assert(std::is_same_v<size_type, decltype(static_cast<size_type>(0) + static_cast<size_type>(0))>);
auto abs_ob_size = neg ? -static_cast<size_type>(ob_size) : static_cast<size_type>(ob_size);

#else

// NOTE: these shifts and mask values come from here:
// https://github.com/python/cpython/blob/main/Include/internal/pycore_long.h
// Not sure if we can rely on this moving on, probably needs to be checked
// at every new Python release. Also, note that lv_tag is unsigned, so
// here we are always getting directly the absolute value of the size,
// unlike in Python<3.12 where we get out a signed size.
const auto abs_ob_size = nptr->long_value.lv_tag >> 3;
assert(abs_ob_size != 0u);

// Is it negative?
const auto neg = (nptr->long_value.lv_tag & 3) == 2;

#endif

return std::make_pair(abs_ob_size, neg);
}

// Helper to convert a Python integer to an mp++ real.
// The precision of the result is inferred from the
// bit size of the integer.
Expand Down Expand Up @@ -398,20 +436,15 @@ std::optional<mppp::real> py_int_to_real(PyObject *arg)
// Need to construct a multiprecision integer from the limb array.
auto *nptr = reinterpret_cast<PyLongObject *>(arg);

// Get the signed size of nptr.
const auto ob_size = nptr->ob_base.ob_size;
assert(ob_size != 0);
// Get the size and sign of nptr.
auto [abs_ob_size, neg] = py_int_size_sign(nptr);

// Get the limbs array.
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 11
const auto *ob_digit = nptr->ob_digit;

// Is it negative?
const auto neg = ob_size < 0;

// Compute the unsigned size.
using size_type = std::make_unsigned_t<std::remove_const_t<decltype(ob_size)>>;
static_assert(std::is_same_v<size_type, decltype(static_cast<size_type>(0) + static_cast<size_type>(0))>);
auto abs_ob_size = neg ? -static_cast<size_type>(ob_size) : static_cast<size_type>(ob_size);
#else
const auto *ob_digit = nptr->long_value.ob_digit;
#endif

// Init the retval with the first (most significant) limb. The Python integer is nonzero, so this is safe.
mppp::integer<1> retval_int = ob_digit[--abs_ob_size];
Expand Down
10 changes: 4 additions & 6 deletions tools/circleci_conda_heyoka_head_310.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ set -e
sudo apt-get install build-essential wget

# Install conda+deps.
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O miniforge.sh
export deps_dir=$HOME/local
export PATH="$HOME/miniconda/bin:$PATH"
bash miniconda.sh -b -p $HOME/miniconda
conda config --add channels conda-forge
conda config --set channel_priority strict
conda create -y -q -p $deps_dir python=3.10 git pybind11 numpy mpmath cmake llvmdev tbb-devel tbb astroquery boost-cpp mppp sleef fmt spdlog sphinx myst-nb matplotlib sympy scipy pykep cloudpickle sphinx-book-theme
export PATH="$HOME/miniforge/bin:$PATH"
bash miniforge.sh -b -p $HOME/miniforge
mamba create -y -q -p $deps_dir python=3.10 git pybind11 numpy mpmath cmake llvmdev tbb-devel tbb astroquery boost-cpp mppp sleef fmt spdlog sphinx myst-nb matplotlib sympy scipy pykep cloudpickle sphinx-book-theme
source activate $deps_dir

export HEYOKA_PY_PROJECT_DIR=`pwd`
Expand Down
43 changes: 43 additions & 0 deletions tools/circleci_conda_heyoka_head_312.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

# Echo each command
set -x

# Exit on error.
set -e

# Core deps.
sudo apt-get install build-essential wget

# Install conda+deps.
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O miniforge.sh
export deps_dir=$HOME/local
export PATH="$HOME/miniforge/bin:$PATH"
bash miniforge.sh -b -p $HOME/miniforge
mamba create -y -q -p $deps_dir python=3.12 git pybind11 numpy mpmath cmake llvmdev tbb-devel tbb boost-cpp mppp sleef fmt spdlog sympy cloudpickle
source activate $deps_dir

# Checkout, build and install heyoka's HEAD.
git clone https://github.com/bluescarni/heyoka.git heyoka_cpp
cd heyoka_cpp
mkdir build
cd build

cmake ../ -DCMAKE_INSTALL_PREFIX=$deps_dir -DCMAKE_PREFIX_PATH=$deps_dir -DCMAKE_BUILD_TYPE=Debug -DHEYOKA_WITH_MPPP=yes -DHEYOKA_WITH_SLEEF=yes -DBoost_NO_BOOST_CMAKE=ON
make -j2 VERBOSE=1 install

cd ../../

# Create the build dir and cd into it.
mkdir build
cd build

cmake ../ -DCMAKE_INSTALL_PREFIX=$deps_dir -DCMAKE_PREFIX_PATH=$deps_dir -DCMAKE_BUILD_TYPE=Debug -DHEYOKA_PY_ENABLE_IPO=yes -DBoost_NO_BOOST_CMAKE=ON
make -j2 VERBOSE=1 install

cd ../tools

python ci_test_runner.py

set +e
set +x
6 changes: 3 additions & 3 deletions tools/circleci_conda_heyoka_head_38.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ set -e
sudo apt-get install wget

# Install conda+deps.
wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh -O mambaforge.sh
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O miniforge.sh
export deps_dir=$HOME/local
export PATH="$HOME/mambaforge/bin:$PATH"
bash mambaforge.sh -b -p $HOME/mambaforge
export PATH="$HOME/miniforge/bin:$PATH"
bash miniforge.sh -b -p $HOME/miniforge
mamba create -y -q -p $deps_dir python=3.8 c-compiler cxx-compiler git pybind11 numpy mpmath cmake llvmdev tbb-devel tbb astroquery boost-cpp mppp sleef fmt spdlog sphinx myst-nb matplotlib sympy scipy pykep cloudpickle sphinx-book-theme
source activate $deps_dir

Expand Down
10 changes: 4 additions & 6 deletions tools/circleci_conda_heyoka_head_release_310.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ set -e
sudo apt-get install build-essential wget

# Install conda+deps.
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O miniforge.sh
export deps_dir=$HOME/local
export PATH="$HOME/miniconda/bin:$PATH"
bash miniconda.sh -b -p $HOME/miniconda
conda config --add channels conda-forge
conda config --set channel_priority strict
conda create -y -q -p $deps_dir python=3.10 git pybind11 numpy mpmath cmake llvmdev tbb-devel tbb astroquery boost-cpp mppp sleef fmt spdlog sphinx myst-nb matplotlib sympy scipy pykep cloudpickle sphinx-book-theme
export PATH="$HOME/miniforge/bin:$PATH"
bash miniforge.sh -b -p $HOME/miniforge
mamba create -y -q -p $deps_dir python=3.10 git pybind11 numpy mpmath cmake llvmdev tbb-devel tbb astroquery boost-cpp mppp sleef fmt spdlog sphinx myst-nb matplotlib sympy scipy pykep cloudpickle sphinx-book-theme
source activate $deps_dir

export HEYOKA_PY_PROJECT_DIR=`pwd`
Expand Down
6 changes: 3 additions & 3 deletions tools/circleci_ubuntu_arm64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ set -e
sudo apt-get install wget

# Install conda+deps.
wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-aarch64.sh -O mambaforge.sh
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh -O miniforge.sh
export deps_dir=$HOME/local
export PATH="$HOME/mambaforge/bin:$PATH"
bash mambaforge.sh -b -p $HOME/mambaforge
export PATH="$HOME/miniforge/bin:$PATH"
bash miniforge.sh -b -p $HOME/miniforge
mamba create -y -q -p $deps_dir cxx-compiler c-compiler cmake llvmdev tbb-devel tbb astroquery boost-cpp sleef fmt spdlog python=3.10 pybind11 numpy mpmath sympy scipy cloudpickle sphinx myst-nb matplotlib sphinx-book-theme mppp
source activate $deps_dir

Expand Down

0 comments on commit a8fbce7

Please sign in to comment.