Skip to content

Reformat threading macros in _tkinter #25026

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
wants to merge 2 commits into from
Closed

Conversation

encukou
Copy link
Member

@encukou encukou commented Mar 26, 2021

I got a static analyzer report about misleading indentation in _tkinter. The indentation really is misleading, the worst example being ENTER_PYTHON:

 #define ENTER_PYTHON \
    { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
        if(tcl_lock) \
          PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); }

That last line combines:

  • a conditional statement whose if is one line above,
  • a non-conditional statement, and
  • a closing bracket that does NOT belong to the if.

I'm well aware that style-only changes are generally frowned upon in CPython. I propose making an exception here, since the code might actually be interpreted differently by humans than by machines.

Since I'm touching this code, I went ahead and reformatted the macros more thoroughly. If that's not wanted, I can drop the second commit.
(Deliberately breaking a PEP7 rule, I kept single-statement if blocks on one line in macros to limit noisy \ lines. The bodies are bracketed to make the code flow clear.)

encukou added 2 commits March 26, 2021 15:33
Combining conditional and non-conditional statements in one line
is quite confusing, and is being flagged as misleading indentation by
static analyzers.

Style-only changes are generally froned upon in CPython. But here, the
code might be interpreted differently by humans than by machines,
so it warrants an exception.
The thread-related macros in _tkinter are slit to have one
statement per line and indented to show structure.

(Deliberately breaking a PEP7 rule, single-statement `if` blocks are
kept on one line in the macros to limit noisy "\" lines. The bodies
are bracketed to make the code flow clear.)
@stratakis
Copy link
Contributor

Your changes definitely make the code look cleaner. +1.

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 26, 2021
@orsenthil
Copy link
Member

The commit 6ec259c is certainly better.

  • I still feel like this is a stylistic commit.
  • The code in the context was written many years ago. Most recently saw PEP7 related changes.

Given that it is defining a macro, and we are able to follow (even if it is takes slight extra effort, cases like this exist in many places), I think, we should do style changes along with functional changes so that history will be easier to follow.

-1 to this change. If no other core-dev comes in support and merges this PR, we could close this PR.

Thank you!

@serhiy-storchaka
Copy link
Member

Yes, it is a stylistic commit, and we usually do not make such changes.
But the current formatting looks random, so I don't mind improving it.
The last time stylistic changes in this file were made in d0ad0b3, and seems it was incomplete. @asvetlov

{ \
PyThreadState *tstate = PyThreadState_Get(); \
Py_BEGIN_ALLOW_THREADS \
if(tcl_lock) { PyThread_acquire_lock(tcl_lock, 1); } \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space after if. And I am not sure that braces add readability.

PEP7 requires adding braces and writing it in three lines

        if (tcl_lock) {
            PyThread_acquire_lock(tcl_lock, 1);
        }

but I think that for macros we can make exception and write it in one line:

       if (tcl_lock) PyThread_acquire_lock(tcl_lock, 1);

If you strongly prefer writing it in more verbose way, I suggest to align backslashes:

#define ENTER_TCL                                       \
    {                                                   \
        PyThreadState *tstate = PyThreadState_Get();    \
        Py_BEGIN_ALLOW_THREADS                          \
        if (tcl_lock) {                                 \
            PyThread_acquire_lock(tcl_lock, 1);         \
        }                                               \
        tcl_tstate = tstate;

@orsenthil
Copy link
Member

The last time stylistic changes in this file were made in d0ad0b3, and seems it was incomplete.

I noticed this too, but I didn't go into details about the inconsistency.

Thanks for picking. I am fine with the changes you are proposing.

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
I don't recall details but this module's formatting was always not very consistent. Let's fix this.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Apr 27, 2021
@github-actions
Copy link

github-actions bot commented Jun 3, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jun 3, 2021
@ambv
Copy link
Contributor

ambv commented Aug 10, 2021

Superseded by GH-26244.

@ambv ambv closed this Aug 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge skip issue skip news stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants