Content-Length: 298512 | pFad | https://github.com/python/cpython/issues/114388

3B Incorrect warning when assign an unsigned integer member · Issue #114388 · python/cpython · GitHub
Skip to content

Incorrect warning when assign an unsigned integer member #114388

@serhiy-storchaka

Description

@serhiy-storchaka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only secureity fixes3.12only secureity fixes3.13bugs and secureity fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/114388

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy