Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eradicate wait_on_error #51

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
schedule:
- cron: '17 3 * * 0'

concurrency:
group: ${{ github.head_ref || github.ref_name }}
cancel-in-progress: true

jobs:
ruff:
name: Ruff
Expand All @@ -33,12 +37,28 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- uses: MarkusJx/install-boost@v2
with:
boost_version: 1.87.0
boost_install_dir: /home/runner/boost
id: install-boost
- name: "Main Script"
env:
BOOST_ROOT: ${{ steps.install-boost.outputs.BOOST_ROOT }}
run: |
curl -L -O https://tiker.net/ci-support-v0
. ./ci-support-v0

PY_VER=$(python3 -c 'import sys; print(f"{sys.version_info[0]}{sys.version_info[1]}")')
cat > $HOME/.aksetup-defaults.py <<EOF
BOOST_INC_DIR = ["$BOOST_ROOT/include"]
BOOST_LIB_DIR = ["$BOOST_ROOT/lib"]
BOOST_PYTHON_LIBNAME = ["boost_python$PY_VER-mt-x64"]
EOF

build_py_project_in_venv

export LD_LIBRARY_PATH="$BOOST_ROOT/lib:$LD_LIBRARY_PATH"
test_py_project

mypy:
Expand Down
12 changes: 12 additions & 0 deletions codepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
"""
Errors
^^^^^^

.. autoexception:: CompileError

.. automodule:: codepy.jit
.. automodule:: codepy.toolchain
.. automodule:: codepy.bpl
"""


class CompileError(Exception):
pass
9 changes: 8 additions & 1 deletion codepy/bpl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
"""Convenience interface for using CodePy with Boost.Python."""
"""
:mod:`codepy.bpl` -- Support for Boost.Python
---------------------------------------------

.. autoclass:: BoostPythonModule
:members:
:undoc-members:
"""

from collections.abc import Callable, Iterable
from dataclasses import replace
Expand Down
2 changes: 1 addition & 1 deletion codepy/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def compile(self,
object=True, **local_host_kwargs)
device_checksum, _device_mod_name, device_object, device_compiled = \
compile_from_string(
nvcc_toolchain, "gpu", device_code, "gpu.cu",
nvcc_toolchain, "gpu", device_code, source_name="gpu.cu",
object=True, **local_nvcc_kwargs)
# The name of the shared lib depends on the hex checksums of both
# host and device code to prevent accidentally returned a cached
Expand Down
37 changes: 16 additions & 21 deletions codepy/jit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"""Just-in-time Python extension compilation."""
"""
:mod:`codepy.jit` -- Compilation and Linking of C Source Code
-------------------------------------------------------------

.. autofunction:: extension_file_from_string
.. autofunction:: extension_from_string
"""


__copyright__ = """
Expand Down Expand Up @@ -32,7 +38,6 @@
from types import ModuleType
from typing import Any, NamedTuple

from codepy import CompileError
from codepy.toolchain import GCCLikeToolchain, Toolchain


Expand All @@ -57,7 +62,7 @@ def extension_file_from_string(
debug: bool = False) -> None:
"""Using *toolchain*, build the extension file named *ext_file*
from the source code in *source_string*, which is saved to a
temporary file named *source_name*. Raise :exc:`CompileError` in
temporary file named *source_name*. Raise :exc:`~codepy.CompileError` in
case of error.

If *debug* is True, show commands involved in the build.
Expand Down Expand Up @@ -195,12 +200,11 @@ def extension_from_string(
source_name: str | list[str] = "module.cpp",
cache_dir: str | None = None,
debug: bool = False,
wait_on_error: bool | None = None,
debug_recompile: bool = True,
sleep_delay: int = 1) -> ModuleType:
"""Return a reference to the extension module *name*, which can be built
from the source code in *source_string* if necessary. Raise
:exc:`CompileError` in case of error.
:exc:`codepy.CompileError` in case of error.

Compiled code is cached in *cache_dir* and available immediately if it has
been compiled at some point in the past. Compiler and Python API versions
Expand All @@ -221,8 +225,9 @@ def extension_from_string(
recompilation is taking place.
"""
_checksum, mod_name, ext_file, _recompiled = (
compile_from_string(toolchain, name, source_string, source_name,
cache_dir, debug, wait_on_error, debug_recompile,
compile_from_string(toolchain, name, source_string, source_name=source_name,
cache_dir=cache_dir, debug=debug,
debug_recompile=debug_recompile,
object=False, sleep_delay=sleep_delay))

# try loading it
Expand Down Expand Up @@ -250,10 +255,10 @@ def compile_from_string(
toolchain: Toolchain,
name: str,
source_string: str | bytes | list[str] | list[bytes],
*,
source_name: str | list[str] | None = None,
cache_dir: str | None = None,
debug: bool = False,
wait_on_error: bool | None = None,
debug_recompile: bool = True,
object: bool = False,
source_is_binary: bool = False,
Expand All @@ -265,7 +270,7 @@ def compile_from_string(
*recompiled* is *True* if the object had to be recompiled, *False* if the cache
is hit.

Raises :exc:`CompileError` in case of error. The *mod_name* and *file_name*
Raises :exc:`~codepy.CompileError` in case of error. The *mod_name* and *file_name*
are designed to be used with ``load_dynamic`` to load a Python module from
this object, if desired.

Expand Down Expand Up @@ -306,11 +311,6 @@ def compile_from_string(
if isinstance(source_name, str):
source_name = [source_name]

if wait_on_error is not None:
from warnings import warn
warn("Passing 'wait_on_error' is deprecated and has no effect. ",
DeprecationWarning, stacklevel=2)

import os

if cache_dir is None:
Expand Down Expand Up @@ -487,7 +487,7 @@ def link_extension(
mod_name: str,
cache_dir: str | None = None,
debug: bool = False,
wait_on_error: bool = True) -> ModuleType:
) -> ModuleType:
if not isinstance(toolchain, GCCLikeToolchain):
raise TypeError(f"Unsupported toolchain type: {type(toolchain)}")

Expand All @@ -501,12 +501,7 @@ def link_extension(
destination_base,
mod_name + toolchain.so_ext)

try:
toolchain.link_extension(destination, objects, debug=debug)
except CompileError:
if wait_on_error:
input(f"Link error, examine {objects}, then press [Enter]")
raise
toolchain.link_extension(destination, objects, debug=debug)

# try loading it
from codepy.tools import load_dynamic
Expand Down
9 changes: 7 additions & 2 deletions codepy/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ def update_config(fname: str) -> None:

def get_boost_libname(basename: str, aksetup: Config) -> list[str]:
varname = f"BOOST_{basename.upper()}_LIBNAME"
libs = getlist(aksetup, varname, [f"boost_{basename}"])
default = f"boost_{basename}"
if basename == "python":
import sys
version = sys.version_info[:2]
default = "boost_python{}{}".format(*version)
libs = getlist(aksetup, varname, [default])

return libs

Expand All @@ -133,7 +138,7 @@ def add_boost_python(toolchain: Toolchain) -> None:
getlist(aksetup, "BOOST_INC_DIR", []),
getlist(aksetup, "BOOST_LIB_DIR", []),
[
*get_boost_libname("python{}{}".format(*version), aksetup),
*get_boost_libname("python", aksetup),
"python{}.{}{}".format(*version, sys.abiflags),
])

Expand Down
22 changes: 20 additions & 2 deletions codepy/toolchain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
"""Toolchains for Just-in-time Python extension compilation."""
"""
:mod:`codepy.toolchain` -- Tool support code
--------------------------------------------

.. autoexception:: ToolchainGuessError

.. autoclass:: Toolchain
:members: copy, get_version, abi_id, add_library, build_extension
:undoc-members:

.. autoclass:: GCCLikeToolchain
:show-inheritance:

.. autoclass:: GCCToolchain
:show-inheritance:

.. autofunction:: guess_toolchain

"""

__copyright__ = """
"Copyright (C) 2008,9 Andreas Kloeckner, Bryan Catanzaro
Expand Down Expand Up @@ -108,7 +126,7 @@ def build_extension(self,
source_files: list[str],
debug: bool = False) -> None:
"""Create the extension file *ext_file* from *source_files*
by invoking the toolchain. Raise :exc:`~codepy.jit.CompileError` in
by invoking the toolchain. Raise :exc:`~codepy.CompileError` in
case of error.

If *debug* is True, print the commands executed.
Expand Down
2 changes: 0 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Welcome to CodePy's documentation!
==================================

.. module:: codepy

CodePy is a C metaprogramming toolkit for Python. It handles two aspects of
metaprogramming:

Expand Down
41 changes: 1 addition & 40 deletions doc/jit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,4 @@ to determine a few configuration values, notably:
For lack of better installation documentation at this moment, please see `this
wiki page <http://wiki.tiker.net/Hedge/HowTo/InstallingFromGit>`_.

:mod:`codepy.jit` -- Compilation and Linking of C Source Code
-------------------------------------------------------------

.. module:: codepy.jit

.. autofunction:: extension_file_from_string
.. autofunction:: extension_from_string

Errors
^^^^^^

.. autoexception:: CompileError

:mod:`codepy.toolchain` -- Tool support code
--------------------------------------------

.. module:: codepy.toolchain

.. autoexception:: ToolchainGuessError

.. autoclass:: Toolchain
:members: copy, get_version, abi_id, add_library, build_extension
:undoc-members:

.. autoclass:: GCCLikeToolchain
:show-inheritance:

.. autoclass:: GCCToolchain
:show-inheritance:

.. autofunction:: guess_toolchain

:mod:`codepy.bpl` -- Support for Boost.Python
---------------------------------------------

.. automodule:: codepy.bpl

.. autoclass:: BoostPythonModule
:members:
:undoc-members:
.. automodule:: codepy
7 changes: 1 addition & 6 deletions test/test_identical_symbols.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from types import ModuleType

import pytest


def make_greet_mod(greeting: str) -> ModuleType:
from cgen import (
Expand All @@ -25,12 +23,9 @@ def make_greet_mod(greeting: str) -> ModuleType:
))

from codepy.toolchain import guess_toolchain
return mod.compile(guess_toolchain(), wait_on_error=True)
return mod.compile(guess_toolchain())


@pytest.mark.xfail(reason="You probably don't have "
"Boost.Python installed where I am looking for it, "
"and that's OK.")
def test_identical_symbols() -> None:
us = make_greet_mod("Hi there")
aussie = make_greet_mod("G'day")
Expand Down
Loading