Coverage for aiocoap/util/socknumbers.py: 58%
24 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-16 16:09 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-16 16:09 +0000
1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
2#
3# SPDX-License-Identifier: MIT
5"""This module contains numeric constants that would be expected in the socket
6module, but are not exposed there.
8This gathers both socket numbers that can be present in the socket module (eg.
9the PKTINFO constants) but are not in some versions (eg. on macOS before
10<https://bugs.python.org/issue35569> is fixed) and platform dependent constants
11that are not generally available at all (the ERR constants).
13Where available, the CPython-private IN module is used to obtain some platform
14specific constants.
16Any hints on where to get them from in a more reliable way are appreciated;
17possible options are parsing C header files (at build time?) or interacting
18with shared libraries for obtaining the symbols. The right way would probably
19be including them in Python in a "other constants defined on this platform for
20sockets" module or dictionary.
21"""
23import sys
25try:
26 from socket import IPV6_PKTINFO, IPV6_RECVPKTINFO
27except ImportError:
28 if sys.platform == 'linux':
29 # Not sure if here are any Linux builds at all where this is
30 # unavailable
31 IPV6_PKTINFO = 50
32 IPv6_RECVPKTINFO = 49
33 elif sys.platform == 'darwin':
34 # when __APPLE_USE_RFC_3542 is defined / as would be when
35 # https://bugs.python.org/issue35569 is fixed
36 IPV6_PKTINFO = 46
37 IPV6_RECVPKTINFO = 61
38 # Not attempting to make any guesses for other platforms; the udp6 module
39 # will fail to import where it needs the specifics
41try:
42 from IN import IPV6_RECVERR, IP_RECVERR
43except ImportError:
44 if sys.platform == 'linux':
45 IPV6_RECVERR = 25
46 IP_RECVERR = 11
48# for https://bitbucket.org/pypy/pypy/issues/2648/
49try:
50 from socket import MSG_ERRQUEUE
51except ImportError:
52 if sys.platform == 'linux':
53 MSG_ERRQUEUE = 8192
55HAS_RECVERR = 'IP_RECVERR' in locals() and 'MSG_ERRQUEUE' in locals()
56"""Indicates whether the discovered constants indicate that the Linux
57`setsockopt(IPV6, RECVERR)` / `recvmsg(..., MSG_ERRQUEUE)` mechanism is
58available"""