Skip to content
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

Incorrect warning when assign an unsigned integer member #114388

Closed
serhiy-storchaka opened this issue Jan 21, 2024 · 1 comment
Closed

Incorrect warning when assign an unsigned integer member #114388

serhiy-storchaka opened this issue Jan 21, 2024 · 1 comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Jan 21, 2024

Assigning a negative value to an unsigned integer member can emit a RuntimeWarning.

$ ./python -Wa
>>> import _testcapi
>>> ts = _testcapi._test_structmembersType_NewAPI()
>>> ts.T_UBYTE = -42
<stdin>:1: RuntimeWarning: Truncation of value to unsigned char
>>> ts.T_USHORT = -42
<stdin>:1: RuntimeWarning: Truncation of value to unsigned short
>>> ts.T_UINT = -42
<stdin>:1: RuntimeWarning: Writing negative value into unsigned field
<stdin>:1: RuntimeWarning: Truncation of value to unsigned int
>>> ts.T_ULONG = -42
<stdin>:1: RuntimeWarning: Writing negative value into unsigned field

(And for T_ULONGLONG it raises an OverflowError, but this is perhaps another issue).

The first issue is that it emits two warnings for T_UINT. It is also weird that warnings are different.

The second issue is that if the value is not an int subclass, it emits a warning also for positive values for T_UINT and T_ULONG:

>>> class Index:
...     def __init__(self, value):
...         self.value = value
...     def __index__(self):
...         return self.value
... 
>>> ts.T_UBYTE = Index(42)
>>> ts.T_USHORT = Index(42)
>>> ts.T_UINT = Index(42)
<stdin>:1: RuntimeWarning: Writing negative value into unsigned field
>>> ts.T_ULONG = Index(42)
<stdin>:1: RuntimeWarning: Writing negative value into unsigned field

(No warning is emitted for assigning a negative index to T_ULONGLONG, but this is perhaps another issue).

Linked PRs

@serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Jan 21, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jan 21, 2024
* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
serhiy-storchaka added a commit that referenced this issue Feb 4, 2024
…4391)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 4, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 4, 2024
…member (pythonGH-114391)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
(cherry picked from commit 3ddc515)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 4, 2024
…member (pythonGH-114391)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
(cherry picked from commit 3ddc515)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Feb 4, 2024
…GH-114391) (GH-115001)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
(cherry picked from commit 3ddc515)
serhiy-storchaka added a commit that referenced this issue Feb 4, 2024
…GH-114391) (GH-115002)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
(cherry picked from commit 3ddc515)
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…ythonGH-114391)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
…ythonGH-114391)

* Fix a RuntimeWarning emitted when assign an integer-like value that
  is not an instance of int to an attribute that corresponds to a C
  struct member of type T_UINT and T_ULONG.
* Fix a double RuntimeWarning emitted when assign a negative integer value
  to an attribute that corresponds to a C struct member of type T_UINT.
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
@encukou
Copy link
Member

encukou commented Mar 28, 2024

With this change, there's a new warning emitted for “Writing negative value into unsigned field”.
@zooba, you might be interested in this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

2 participants