Skip to content

bpo-43973: Use Py_TPFLAGS_IMMUTABLETYPE to check for class assignments #25714

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

Merged
merged 1 commit into from
Apr 30, 2021

Conversation

erlend-aasland
Copy link
Contributor

@erlend-aasland erlend-aasland commented Apr 29, 2021

@erlend-aasland erlend-aasland changed the title Use Py_TPFLAGS_IMMUTABLETYPE to check for class assignments bpo-43973: Use Py_TPFLAGS_IMMUTABLETYPE to check for class assignments Apr 29, 2021
@erlend-aasland
Copy link
Contributor Author

cc. @vstinner

See msg392291: Can we get rid of the huge comment preceding the if statement now? Or at least reduce it considerably.

@erlend-aasland
Copy link
Contributor Author

AFAICS, this does not require a news item, but I might be wrong.

@erlend-aasland erlend-aasland marked this pull request as ready for review April 29, 2021 09:32
@shreyanavigyan
Copy link
Contributor

Doesn't this also require a change?

cpython/Objects/typeobject.c

Lines 4679 to 4690 in e047239

if (compatible_for_assignment(oldto, newto, "__class__")) {
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(newto);
}
Py_SET_TYPE(self, newto);
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
Py_DECREF(oldto);
return 0;
}
else {
return -1;
}

Suggested :-

 if (compatible_for_assignment(oldto, newto, "__class__")) { 
     if (!(newto->tp_flags & Py_TPFLAGS_IMMUTABLETYPE)) { 
         Py_INCREF(newto); 
     } 
     Py_SET_TYPE(self, newto); 
     if (!(oldto->tp_flags & Py_TPFLAGS_IMMUTABLETYPE))
         Py_DECREF(oldto); 
     return 0; 
 } 
 else { 
     return -1; 
 } 

@erlend-aasland
Copy link
Contributor Author

erlend-aasland commented Apr 29, 2021

Doesn't this also require a change?

AFAICT, no. They adjust the ref. count if heap types are used:

If the old class was a heap type, decrement its ref. count. If the new class is a heap type, acquire a strong reference to it (increment the ref. count).

@vstinner vstinner merged commit b73b5fb into python:master Apr 30, 2021
@erlend-aasland erlend-aasland deleted the class_assignments branch April 30, 2021 10:07
@erlend-aasland
Copy link
Contributor Author

Thanks for reviewing, @vstinner !

@vstinner
Copy link
Member

Merged, thanks for the fix.

@pitrou
Copy link
Member

pitrou commented Apr 30, 2021

The comment above the check should have been fixed to reflect the new semantics, no?

Also, is the explicit check for PyModule_Type still needed?

@erlend-aasland
Copy link
Contributor Author

erlend-aasland commented Apr 30, 2021

The comment above the check should have been fixed to reflect the new semantics, no?

Yes, and I believe it can be considerably reduced.

Also, is the explicit check for PyModule_Type still needed?

I don't know. See bpo-24912.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants