Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
A few more
  • Loading branch information
cdce8p committed Dec 22, 2025
commit 0bc3066d4d642709f76dfbab9302ad7b433bbf0c
3 changes: 1 addition & 2 deletions mypyc/codegen/emitwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,8 @@ def generate_bin_op_reverse_dunder_call(fn: FuncIR, emitter: Emitter, rmethod: s
if fn.name in ("__pow__", "__rpow__"):
# Ternary pow() will never call the reverse dunder.
emitter.emit_line("if (obj_mod == Py_None) {")
emitter.emit_line(f"_Py_IDENTIFIER({rmethod});")
emitter.emit_line(
'return CPy_CallReverseOpMethod(obj_left, obj_right, "{}", &PyId_{});'.format(
'return CPy_CallReverseOpMethod(obj_left, obj_right, "{}", mypyc_interned_str.{});'.format(
op_methods_to_symbols[fn.name], rmethod
)
)
Expand Down
2 changes: 1 addition & 1 deletion mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ int CPyStatics_Initialize(PyObject **statics,
const int *frozensets);
PyObject *CPy_Super(PyObject *builtins, PyObject *self);
PyObject *CPy_CallReverseOpMethod(PyObject *left, PyObject *right, const char *op,
_Py_Identifier *method);
PyObject *method);

bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *globals,
PyObject *tb_path, PyObject *tb_function, Py_ssize_t *tb_lines);
Expand Down
6 changes: 2 additions & 4 deletions mypyc/lib-rt/dict_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ int CPyDict_Update(PyObject *dict, PyObject *stuff) {
int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff) {
if (PyDict_CheckExact(dict)) {
// Argh this sucks
_Py_IDENTIFIER(keys);
if (PyDict_Check(stuff) || _CPyObject_HasAttrId(stuff, &PyId_keys)) {
if (PyDict_Check(stuff) || _CPyObject_HasAttr(stuff, mypyc_interned_str.keys)) {
return PyDict_Update(dict, stuff);
} else {
return PyDict_MergeFromSeq2(dict, stuff, 1);
Expand All @@ -179,8 +178,7 @@ PyObject *CPyDict_FromAny(PyObject *obj) {
if (!dict) {
return NULL;
}
_Py_IDENTIFIER(keys);
if (_CPyObject_HasAttrId(obj, &PyId_keys)) {
if (_CPyObject_HasAttr(obj, mypyc_interned_str.keys)) {
res = PyDict_Update(dict, obj);
} else {
res = PyDict_MergeFromSeq2(dict, obj, 1);
Expand Down
10 changes: 4 additions & 6 deletions mypyc/lib-rt/misc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ PyObject *CPyIter_Send(PyObject *iter, PyObject *val)
// Signals an error (2) if the an exception should be propagated.
int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp)
{
_Py_IDENTIFIER(close);
_Py_IDENTIFIER(throw);
PyObject *exc_type = (PyObject *)Py_TYPE(CPy_ExcState()->exc_value);
PyObject *type, *value, *traceback;
PyObject *_m;
PyObject *res;
*outp = NULL;

if (PyErr_GivenExceptionMatches(exc_type, PyExc_GeneratorExit)) {
_m = _PyObject_GetAttrId(iter, &PyId_close);
_m = PyObject_GetAttr(iter, mypyc_interned_str.close_);
if (_m) {
res = PyObject_CallNoArgs(_m);
Py_DECREF(_m);
Expand All @@ -67,7 +65,7 @@ int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp)
return 2;
}
} else {
_m = _PyObject_GetAttrId(iter, &PyId_throw);
_m = PyObject_GetAttr(iter, mypyc_interned_str.throw_);
if (_m) {
_CPy_GetExcInfo(&type, &value, &traceback);
res = PyObject_CallFunctionObjArgs(_m, type, value, traceback, NULL);
Expand Down Expand Up @@ -872,9 +870,9 @@ PyObject *
CPy_CallReverseOpMethod(PyObject *left,
PyObject *right,
const char *op,
_Py_Identifier *method) {
PyObject *method) {
// Look up reverse method
PyObject *m = _PyObject_GetAttrId(right, method);
PyObject *m = PyObject_GetAttr(right, method);
if (m == NULL) {
// If reverse method not defined, generate TypeError instead AttributeError
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Expand Down
14 changes: 4 additions & 10 deletions mypyc/lib-rt/pythonsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ extern "C" {

/////////////////////////////////////////
// Adapted from bltinmodule.c in Python 3.7.0
_Py_IDENTIFIER(__mro_entries__);
static PyObject*
update_bases(PyObject *bases)
{
Expand All @@ -58,7 +57,7 @@ update_bases(PyObject *bases)
}
continue;
}
if (PyObject_GetOptionalAttrString(base, PyId___mro_entries__.string, &meth) < 0) {
if (PyObject_GetOptionalAttrString(base, "__mro_entries__", &meth) < 0) {

@cdce8p cdce8p Dec 22, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We can't use mypyc_interned_str.__mro_entries__ here since it isn't initialized yet when the header file is loaded. Similarly for __init_subclass__ below.

I believe the best solution might be to just move these functions to pythonsupport.c but that can be done at a later point.

goto error;
}
if (!meth) {
Expand Down Expand Up @@ -111,7 +110,6 @@ update_bases(PyObject *bases)
}

// From Python 3.7's typeobject.c
_Py_IDENTIFIER(__init_subclass__);
static int
init_subclass(PyTypeObject *type, PyObject *kwds)
{
Expand All @@ -123,7 +121,7 @@ init_subclass(PyTypeObject *type, PyObject *kwds)
return -1;
}

func = _PyObject_GetAttrId(super, &PyId___init_subclass__);
func = PyObject_GetAttrString(super, "__init_subclass__");
Py_DECREF(super);
if (func == NULL) {
return -1;
Expand Down Expand Up @@ -378,19 +376,15 @@ _CPyDictView_New(PyObject *dict, PyTypeObject *type)
}
#endif

#if PY_VERSION_HEX >= 0x030A0000 // 3.10

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Mypy recently dropped support for Python 3.9, so the version guard is no longer necessary.

static int
_CPyObject_HasAttrId(PyObject *v, _Py_Identifier *name) {
_CPyObject_HasAttr(PyObject *v, PyObject *name) {
PyObject *tmp = NULL;
int result = PyObject_GetOptionalAttrString(v, name->string, &tmp);
int result = PyObject_GetOptionalAttr(v, name, &tmp);
if (tmp) {
Py_DECREF(tmp);
}
return result;
}
Comment thread
cdce8p marked this conversation as resolved.
#else
#define _CPyObject_HasAttrId _PyObject_HasAttrId
#endif

#if CPY_3_12_FEATURES

Expand Down