# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DISTUTILS_EXT=1
DISTUTILS_USE_PEP517=setuptools
PYTHON_COMPAT=( pypy3 python3_{10..12} )
PYTHON_REQ_USE="threads(+)"

inherit distutils-r1

DESCRIPTION="Lightweight and super-fast messaging library built on top of the ZeroMQ library"
HOMEPAGE="
	https://zeromq.org/languages/python/
	https://pypi.org/project/pyzmq/
	https://github.com/zeromq/pyzmq/
"
SRC_URI="
	https://github.com/zeromq/pyzmq/archive/v${PV}.tar.gz
		-> ${P}.gh.tar.gz
"

LICENSE="LGPL-3"
SLOT="0"
KEYWORDS="~alpha amd64 arm arm64 hppa ~loong ~m68k ppc ppc64 ~riscv ~s390 sparc x86 ~amd64-linux ~x86-linux ~arm64-macos ~x64-macos"
IUSE="drafts"

# There are additional test failures if zeromq has the draft api enabled, but pyzmq has it disabled.
DEPEND="
	>=net-libs/zeromq-4.2.2-r2:=[drafts=]
"
# It uses cffi backend for pypy, cython backend for cpython
RDEPEND="
	${DEPEND}
"
BDEPEND="
	$(python_gen_cond_dep '
		dev-python/cython[${PYTHON_USEDEP}]
	' 'python*')
	test? (
		dev-python/pytest-asyncio[${PYTHON_USEDEP}]
		dev-python/pytest-rerunfailures[${PYTHON_USEDEP}]
		>=dev-python/tornado-5.0.2[${PYTHON_USEDEP}]
	)
"

PATCHES=(
	"${FILESDIR}"/${PN}-23.2.0-libdir.patch
)

distutils_enable_tests pytest
# TODO: Package enum_tools
# distutils_enable_sphinx docs/source \
# 	dev-python/numpydoc \
# 	dev-python/sphinx-rtd-theme \
# 	dev-python/myst-parser

python_prepare_all() {
	export ZMQ_DRAFT_API=$(usex drafts '1' '0')
	export ZMQ_PREFIX="${EPREFIX}/usr"
	distutils-r1_python_prepare_all
}

python_test() {
	local EPYTEST_DESELECT=(
		# TODO
		zmq/tests/test_auth.py
		zmq/tests/test_cython.py
		zmq/tests/test_zmqstream.py

		# swap thrashing on low-mem hosts, per Debian:
		# https://salsa.debian.org/python-team/packages/pyzmq/-/blob/master/debian/patches/skip_large_send
		zmq/tests/test_socket.py::TestSocket::test_large_send
	)
	local EPYTEST_IGNORE=(
		# Avoid dependency on mypy
		zmq/tests/test_mypy.py
	)

	case ${EPYTHON} in
		pypy3)
			EPYTEST_DESELECT+=(
				# not implemented in cffi variant?
				zmq/tests/test_draft.py::TestDraftSockets
			)
	esac

	local -x PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
	cd "${BUILD_DIR}/install$(python_get_sitedir)" || die
	epytest -p asyncio -p rerunfailures
}