Coverage for aiocoap/util/socknumbers.py: 57%

23 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-10 11:47 +0000

1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors 

2# 

3# SPDX-License-Identifier: MIT 

4 

5"""This module contains numeric constants that would be expected in the socket 

6module, but are not exposed there. 

7 

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). 

12 

13Where available, the CPython-private IN module is used to obtain some platform 

14specific constants. 

15 

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 

22As of 2024, most of these are not needed any more; this module will be removed 

23in favor of directly accessing `socket` constants once Python 3.13 support is 

24dropped (see [issue 352](https://github.com/chrysn/aiocoap/issues/352)). 

25""" 

26 

27import sys 

28 

29try: 

30 from socket import IPV6_PKTINFO, IPV6_RECVPKTINFO 

31except ImportError: 

32 if sys.platform == "linux": 

33 # Not sure if here are any Linux builds at all where this is 

34 # unavailable 

35 IPV6_PKTINFO = 50 

36 IPv6_RECVPKTINFO = 49 

37 elif sys.platform == "darwin": 

38 # when __APPLE_USE_RFC_3542 is defined / as would be when 

39 # https://bugs.python.org/issue35569 is fixed 

40 IPV6_PKTINFO = 46 

41 IPV6_RECVPKTINFO = 61 

42 # Not attempting to make any guesses for other platforms; the udp6 module 

43 # will fail to import where it needs the specifics 

44 

45try: 

46 from IN import IPV6_RECVERR, IP_RECVERR # type: ignore 

47except ImportError: 

48 if sys.platform == "linux": 

49 IPV6_RECVERR = 25 

50 IP_RECVERR = 11 

51 

52# for https://bitbucket.org/pypy/pypy/issues/2648/ 

53try: 

54 from socket import MSG_ERRQUEUE 

55except ImportError: 

56 if sys.platform == "linux": 

57 MSG_ERRQUEUE = 8192 # type: ignore 

58 

59HAS_RECVERR = "IP_RECVERR" in locals() and "MSG_ERRQUEUE" in locals() 

60"""Indicates whether the discovered constants indicate that the Linux 

61`setsockopt(IPV6, RECVERR)` / `recvmsg(..., MSG_ERRQUEUE)` mechanism is 

62available"""