Skip to content

socket.CAN_RAW_ERR_FILTER is not defined in Python 3.11 and later #129719

@jbogers

Description

@jbogers

Bug report

Bug description:

When trying to use socket.CAN_RAW_ERR_FILTER on a Debian bookworm system, running Python 3.11, an AttributeError is raised. This is because the value is no longer defined in socket.py/_socket.

I have also tested in Python 3.13.1, and the issue is still present there.

Typical usage of the value:

import socket
sock = socket.socket(socket.AF_CAN, socket.SOCK_RAW | socket.SOCK_NONBLOCK, socket.CAN_RAW)
sock.bind((interface_name,))
sock.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_ERR_FILTER, socket.CAN_ERR_MASK)

Minimized reproduction:

import socket
print(socket.CAN_RAW_ERR_FILTER)

Actual output:

Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    print(socket.CAN_RAW_ERR_FILTER)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'socket' has no attribute 'CAN_RAW_ERR_FILTER'. Did you mean: 'CAN_RAW_FILTER'?

Expected output (taken from Python 3.10.12):

2

Investigating the issue, it seems to have been introduced in Python 3.11 by the following PR: #30066
In this PR, adding the CAN_RAW_ERR_FILTER value to socket was made conditional on defining CAN_RAW_ERR_FILTER during compilation. However, no change was made to actually define the value, thus it is never compiled for compatible systems.

Note that the CAN_RAW_ERR_FILTER value is coming from linux/can/raw.h where is it defined as an enum value. Thus it will not be used by the C/C++ preprocessor to evaluate the #ifdef CAN_RAW_ERR_FILTER in Modules/socketmodule.c.

Excerpt of relevant raw.h file from Debian Bookworm:

/* for socket options affecting the socket (not the global system) */

enum {
	CAN_RAW_FILTER = 1,	/* set 0 .. n can_filter(s)          */
	CAN_RAW_ERR_FILTER,	/* set filter for error frames       */
	CAN_RAW_LOOPBACK,	/* local loopback (default:on)       */
	CAN_RAW_RECV_OWN_MSGS,	/* receive my own msgs (default:off) */
	CAN_RAW_FD_FRAMES,	/* allow CAN FD frames (default:off) */
	CAN_RAW_JOIN_FILTERS,	/* all filters must match to trigger */
	CAN_RAW_XL_FRAMES,	/* allow CAN XL frames (default:off) */
};

CPython versions tested on:

3.11, 3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions