GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix potential usage of None and Unbound variables issues by Avasam · Pull Request #248 · python-xlib/python-xlib

Conversation

@Avasam

@Avasam Avasam commented Nov 17, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Colormap.copy_colormap_and_free works again. Its parameter was named scr_cmap while the body used src_cmap, so every call raised NameError.
  • Resource.parse_value and ResourceObj.parse_value return the raw value when no display is given. The if not display check was commented out, so they raised AttributeError on None.get_resource_class.
  • String16.parse_binary_value with no length field (length=None) reads the whole buffer, like String8 and Binary already do. It used to fail on None arithmetic.
  • Struct.to_binary raises a RuntimeError naming the field when a variable size field has no pack_value, instead of calling None.
  • Display.close_internal resets the queues and buffers to empty lists and bytes instead of None, and returns the ConnectionClosedError it stores so callers raise it directly. send_request still raises that stored error before touching the queues.
  • ReplyRequest._data starts as an empty dict per instance, and reply() waits on not self._data. A reply always has fields, so this is equivalent to the old is None check. Calling reply() again after the response arrived is still a no-op, as its comment promises.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Avasam
Avasam commented Nov 17, 2022
View reviewed changes
Comment thread Xlib/protocol/rq.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Nov 17, 2022
py.typed and type comments #251
Draft
Missing imports in Xlib.XK #245
Closed
Avasam
Avasam commented Nov 18, 2022
View reviewed changes
Comment thread Xlib/protocol/rq.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@codecov-commenter

codecov-commenter commented Nov 19, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

Merging #248 (1fa7079) into master (4e8bbf8) will decrease coverage by 0.05%.
The diff coverage is 66.66%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #248      +/-   ##
==========================================
- Coverage   78.21%   78.16%   -0.06%     
==========================================
  Files          42       42              
  Lines        4705     4735      +30     
==========================================
+ Hits         3680     3701      +21     
- Misses       1025     1034       +9     
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Avasam

Avasam commented Jul 8, 2023

Copy link
Copy Markdown
Contributor Author

Could I get a review on this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Avasam

Avasam commented Aug 18, 2024

Copy link
Copy Markdown
Contributor Author

@python-xlib yearly bump

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Avasam
Fix potential usage of None and Unbound variables issues - python-xli…
46f8a33
…b#248

- `Colormap.copy_colormap_and_free` works again. Its parameter was named `scr_cmap` while the body used `src_cmap`, so every call raised `NameError`.
- `Resource.parse_value` and `ResourceObj.parse_value` return the raw value when no display is given. The `if not display` check was commented out, so they raised `AttributeError` on `None.get_resource_class`.
- `String16.parse_binary_value` with no length field (`length=None`) reads the whole buffer, like `String8` and `Binary` already do. It used to fail on `None` arithmetic.
- `Struct.to_binary` raises a `RuntimeError` naming the field when a variable size field has no `pack_value`, instead of calling `None`.
- `Display.close_internal` resets the queues and buffers to empty lists and bytes instead of `None`, and returns the `ConnectionClosedError` it stores so callers raise it directly. `send_request` still raises that stored error before touching the queues.
- `ReplyRequest._data` starts as an empty dict per instance, and `reply()` waits on `not self._data`. A reply always has fields, so this is equivalent to the old `is None` check. Calling `reply()` again after the response arrived is still a no-op, as its comment promises.
@Avasam
Avasam force-pushed the None-and-Unbound branch from 1fa7079 to 46f8a33 Compare October 31, 2026 23:31
@Avasam
Avasam marked this pull request as draft October 31, 2026 23:41
Avasam
Avasam commented Oct 31, 2026
View reviewed changes
Comment thread Xlib/ext/xinput.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Avasam
Avasam commented Oct 31, 2026
View reviewed changes
Comment thread Xlib/protocol/rq.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Avasam
Avasam commented Oct 31, 2026
View reviewed changes
Comment thread Xlib/protocol/rq.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Avasam
Avasam commented Oct 31, 2026
View reviewed changes
Comment thread Xlib/protocol/rq.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Avasam
Avasam commented Oct 31, 2026
View reviewed changes
Comment thread Xlib/protocol/rq.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Avasam
Avasam commented Oct 31, 2026
View reviewed changes
Comment thread Xlib/protocol/display.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Avasam
Apply batched suggestions from code review
682f7f7
Co-authored-by: Avasam <[email protected]>
@Avasam
Avasam marked this pull request as ready for review October 31, 2026 23:53
Avasam added 2 commits October 31, 2026 20:32
@Avasam
Check pack_value once per struct, harden PropertyData, add tests
2529bf3
- `Struct.__init__` collects the variable size fields that have no `pack_value`, without raising, since parse-only structs such as xinput's `DeviceEvent` must still build. `to_binary` raises one `RuntimeError` naming all of them before packing, instead of checking each field on every call.
- `PropertyData.parse_binary_value` returns `None` for format 0 before computing a length, so `length=None` no longer divides by zero, and raises `BadDataError` for a format other than 0, 8, 16 or 32. `ret` is now assigned on every path without a `None` default.
- Tests cover `Resource.parse_value` and `ResourceObj.parse_value` without a display, a struct with a parse-only field, `PropertyData` formats, and `Display.close_internal`, including each of its `raise self.close_internal(...)` call sites in `send_and_recv`.
@Avasam
Wait on ReplyRequest._data is None again
70da09e
`reply()` waited on `not self._data`, but `ListFontsWithInfo` stores `[]` as its reply when no font matches, so `list_fonts_with_info` with no match looped forever. The `None` check tells a missing reply apart from an empty one.
Avasam added a commit to Avasam/python-xlib that referenced this pull request Nov 1, 2026
@Avasam
Fix potential usage of None and Unbound variables (python-xlib#248)
f59cbbf
Applies 46f8a33, adapted to the stubs this package ships.

- `Resource.parse_value` and `ResourceObj.parse_value` return the raw value when no display is given. The `if not display` check was commented out, so they raised `AttributeError` on `None.get_resource_class`.
- `Struct.to_binary` raises a `RuntimeError` naming every variable size field that has no `pack_value`, instead of an `AttributeError`. `Struct.__init__` collects those fields once, without raising, since parse-only structs such as xinput's `DeviceEvent` must still build.
- `PropertyData.parse_binary_value` returns `None` for format 0 before computing a length, so `length=None` no longer divides by zero, and raises `BadDataError` for a format other than 0, 8, 16 or 32 instead of `UnboundLocalError`.
- `Display.close_internal` resets the queues and buffers to empty lists and bytes instead of `None`, and returns the `ConnectionClosedError` it stores so callers raise it directly. `send_request` still raises that stored error before touching the queues.
- For type checkers: `parse_response` initializes `rtype`, and the stubs declare `Request._request`, `ReplyRequest._request`, `Event._fields` and `ClassInfoClass.check_value`, and the return type of `close_internal`.

Left out from the original commit:
- `Colormap.copy_colormap_and_free` was already fixed, and `XK.pyi` already re-exports the keysyms.
- The `length is None` checks in `String16.parse_binary_value` and `ButtonState.parse_binary_value` were never reached, and the stubs already type `length` as not `None`.
- `ReplyRequest._data` keeps starting as `None`. Waiting on `not self._data` hung `list_fonts_with_info` when no font matched, since its reply data is then `[]`.
- Inline type comments moved to the stubs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Avasam @codecov-commenter
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.