Skip to content

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

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

Closed
jbogers opened this issue Feb 6, 2025 · 1 comment
Closed

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

jbogers opened this issue Feb 6, 2025 · 1 comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@jbogers
Copy link
Contributor

jbogers commented Feb 6, 2025

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

@jbogers jbogers added the type-bug An unexpected behavior, bug, or error label Feb 6, 2025
jbogers pushed a commit to jbogers/cpython that referenced this issue Feb 6, 2025
@jbogers
Copy link
Contributor Author

jbogers commented Feb 6, 2025

I've tried to make a PR with a fix (#129721). This is my first time contributing, so I hope I've correctly followed the required procedures.

@picnixz picnixz added the extension-modules C modules in the Modules dir label Feb 7, 2025
sobolevn pushed a commit that referenced this issue Apr 18, 2025
)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 18, 2025
…pythonGH-129721)

(cherry picked from commit ce31ae5)

Co-authored-by: Jeroen Bogers <11465689+jbogers@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
sobolevn pushed a commit that referenced this issue Apr 18, 2025
GH-129721) (#132702)

gh-129719: Restore missing `socket.CAN_RAW_ERR_FILTER` on Linux (GH-129721)
(cherry picked from commit ce31ae5)

Co-authored-by: Jeroen Bogers <11465689+jbogers@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants