FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use private _Py_ID · python/mypy@dbbc992 · GitHub

Repository navigation

Commit dbbc992

Browse files
committed
Use private _Py_ID
1 parent f287f7d commit dbbc992

8 files changed

Lines changed: 28 additions & 109 deletions

File tree

‎mypyc/codegen/emitwrapper.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def generate_bin_op_reverse_dunder_call(fn: FuncIR, emitter: Emitter, rmethod: s
453453
# Ternary pow() will never call the reverse dunder.
454454
emitter.emit_line("if (obj_mod == Py_None) {")
455455
emitter.emit_line(
456-
'return CPy_CallReverseOpMethod(obj_left, obj_right, "{}", "{}");'.format(
456+
'return CPy_CallReverseOpMethod(obj_left, obj_right, "{}", &_Py_ID({}));'.format(
457457
op_methods_to_symbols[fn.name], rmethod
458458
)
459459
)

‎mypyc/lib-rt/CPy.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ int CPyStatics_Initialize(PyObject **statics,
938938
const int *frozensets);
939939
PyObject *CPy_Super(PyObject *builtins, PyObject *self);
940940
PyObject *CPy_CallReverseOpMethod(PyObject *left, PyObject *right, const char *op,
941-
const char *method);
941+
PyObject *method);
942942

943943
bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *globals,
944944
PyObject *tb_path, PyObject *tb_function, Py_ssize_t *tb_lines);

‎mypyc/lib-rt/bytes_extra_ops.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,5 @@ PyObject *CPyBytes_Translate(PyObject *bytes, PyObject *table) {
4141
}
4242

4343
// Fallback to Python method call for non-exact types or non-standard tables
44-
PyObject *name = PyUnicode_FromString("translate");
45-
if (name == NULL) {
46-
return NULL;
47-
}
48-
PyObject *res = PyObject_CallMethodOneArg(bytes, name, table);
49-
Py_DECREF(name);
50-
return res;
44+
return PyObject_CallMethodOneArg(bytes, &_Py_ID(translate), table);
5145
}

‎mypyc/lib-rt/bytes_ops.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,7 @@ PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter) {
101101
if (PyBytes_CheckExact(sep)) {
102102
return PyBytes_Join(sep, iter);
103103
} else {
104-
PyObject *name = PyUnicode_FromString("join");
105-
if (name == NULL) {
106-
return NULL;
107-
}
108-
PyObject *res = PyObject_CallMethodOneArg(sep, name, iter);
109-
Py_DECREF(name);
110-
return res;
104+
return PyObject_CallMethodOneArg(sep, &_Py_ID(join), iter);
111105
}
112106
}
113107

‎mypyc/lib-rt/dict_ops.c‎

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int CPyDict_Update(PyObject *dict, PyObject *stuff) {
170170
int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff) {
171171
if (PyDict_CheckExact(dict)) {
172172
// Argh this sucks
173-
if (PyDict_Check(stuff) || _CPyObject_HasAttrId(stuff, "keys")) {
173+
if (PyDict_Check(stuff) || _CPyObject_HasAttrId(stuff, &_Py_ID(keys))) {
174174
return PyDict_Update(dict, stuff);
175175
} else {
176176
return PyDict_MergeFromSeq2(dict, stuff, 1);
@@ -189,7 +189,7 @@ PyObject *CPyDict_FromAny(PyObject *obj) {
189189
if (!dict) {
190190
return NULL;
191191
}
192-
if (_CPyObject_HasAttrId(obj, "keys")) {
192+
if (_CPyObject_HasAttrId(obj, &_Py_ID(keys))) {
193193
res = PyDict_Update(dict, obj);
194194
} else {
195195
res = PyDict_MergeFromSeq2(dict, obj, 1);
@@ -206,39 +206,21 @@ PyObject *CPyDict_KeysView(PyObject *dict) {
206206
if (PyDict_CheckExact(dict)){
207207
return _CPyDictView_New(dict, &PyDictKeys_Type);
208208
}
209-
PyObject *name = PyUnicode_FromString("keys");
210-
if (name == NULL) {
211-
return NULL;
212-
}
213-
PyObject *res = PyObject_CallMethodNoArgs(dict, name);
214-
Py_DECREF(name);
215-
return res;
209+
return PyObject_CallMethodNoArgs(dict, &_Py_ID(keys));
216210
}
217211

218212
PyObject *CPyDict_ValuesView(PyObject *dict) {
219213
if (PyDict_CheckExact(dict)){
220214
return _CPyDictView_New(dict, &PyDictValues_Type);
221215
}
222-
PyObject *name = PyUnicode_FromString("values");
223-
if (name == NULL) {
224-
return NULL;
225-
}
226-
PyObject *res = PyObject_CallMethodNoArgs(dict, name);
227-
Py_DECREF(name);
228-
return res;
216+
return PyObject_CallMethodNoArgs(dict, &_Py_ID(values));
229217
}
230218

231219
PyObject *CPyDict_ItemsView(PyObject *dict) {
232220
if (PyDict_CheckExact(dict)){
233221
return _CPyDictView_New(dict, &PyDictItems_Type);
234222
}
235-
PyObject *name = PyUnicode_FromString("items");
236-
if (name == NULL) {
237-
return NULL;
238-
}
239-
PyObject *res = PyObject_CallMethodNoArgs(dict, name);
240-
Py_DECREF(name);
241-
return res;
223+
return PyObject_CallMethodNoArgs(dict, &_Py_ID(items));
242224
}
243225

244226
PyObject *CPyDict_Keys(PyObject *dict) {
@@ -247,12 +229,7 @@ PyObject *CPyDict_Keys(PyObject *dict) {
247229
}
248230
// Inline generic fallback logic to also return a list.
249231
PyObject *list = PyList_New(0);
250-
PyObject *name = PyUnicode_FromString("keys");
251-
if (name == NULL) {
252-
return NULL;
253-
}
254-
PyObject *view = PyObject_CallMethodNoArgs(dict, name);
255-
Py_DECREF(name);
232+
PyObject *view = PyObject_CallMethodNoArgs(dict, &_Py_ID(keys));
256233
if (view == NULL) {
257234
return NULL;
258235
}
@@ -270,12 +247,7 @@ PyObject *CPyDict_Values(PyObject *dict) {
270247
}
271248
// Inline generic fallback logic to also return a list.
272249
PyObject *list = PyList_New(0);
273-
PyObject *name = PyUnicode_FromString("values");
274-
if (name == NULL) {
275-
return NULL;
276-
}
277-
PyObject *view = PyObject_CallMethodNoArgs(dict, name);
278-
Py_DECREF(name);
250+
PyObject *view = PyObject_CallMethodNoArgs(dict, &_Py_ID(values));
279251
if (view == NULL) {
280252
return NULL;
281253
}
@@ -293,12 +265,7 @@ PyObject *CPyDict_Items(PyObject *dict) {
293265
}
294266
// Inline generic fallback logic to also return a list.
295267
PyObject *list = PyList_New(0);
296-
PyObject *name = PyUnicode_FromString("items");
297-
if (name == NULL) {
298-
return NULL;
299-
}
300-
PyObject *view = PyObject_CallMethodNoArgs(dict, name);
301-
Py_DECREF(name);
268+
PyObject *view = PyObject_CallMethodNoArgs(dict, &_Py_ID(items));
302269
if (view == NULL) {
303270
return NULL;
304271
}
@@ -314,12 +281,7 @@ char CPyDict_Clear(PyObject *dict) {
314281
if (PyDict_CheckExact(dict)) {
315282
PyDict_Clear(dict);
316283
} else {
317-
PyObject *name = PyUnicode_FromString("clear");
318-
if (name == NULL) {
319-
return 0;
320-
}
321-
PyObject *res = PyObject_CallMethodNoArgs(dict, name);
322-
Py_DECREF(name);
284+
PyObject *res = PyObject_CallMethodNoArgs(dict, &_Py_ID(clear));
323285
if (res == NULL) {
324286
return 0;
325287
}
@@ -331,12 +293,7 @@ PyObject *CPyDict_Copy(PyObject *dict) {
331293
if (PyDict_CheckExact(dict)) {
332294
return PyDict_Copy(dict);
333295
}
334-
PyObject *name = PyUnicode_FromString("copy");
335-
if (name == NULL) {
336-
return NULL;
337-
}
338-
PyObject *res = PyObject_CallMethodNoArgs(dict, name);
339-
Py_DECREF(name);
296+
PyObject *res = PyObject_CallMethodNoArgs(dict, &_Py_ID(copy));
340297
return res;
341298
}
342299

@@ -355,12 +312,7 @@ PyObject *CPyDict_GetItemsIter(PyObject *dict) {
355312
Py_INCREF(dict);
356313
return dict;
357314
}
358-
PyObject *name = PyUnicode_FromString("items");
359-
if (name == NULL) {
360-
return NULL;
361-
}
362-
PyObject *view = PyObject_CallMethodNoArgs(dict, name);
363-
Py_DECREF(name);
315+
PyObject *view = PyObject_CallMethodNoArgs(dict, &_Py_ID(items));
364316
if (view == NULL) {
365317
return NULL;
366318
}
@@ -375,12 +327,7 @@ PyObject *CPyDict_GetValuesIter(PyObject *dict) {
375327
Py_INCREF(dict);
376328
return dict;
377329
}
378-
PyObject *name = PyUnicode_FromString("values");
379-
if (name == NULL) {
380-
return NULL;
381-
}
382-
PyObject *view = PyObject_CallMethodNoArgs(dict, name);
383-
Py_DECREF(name);
330+
PyObject *view = PyObject_CallMethodNoArgs(dict, &_Py_ID(values));
384331
if (view == NULL) {
385332
return NULL;
386333
}

‎mypyc/lib-rt/list_ops.c‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ char CPyList_Clear(PyObject *list) {
3333
if (PyList_CheckExact(list)) {
3434
PyList_Clear(list);
3535
} else {
36-
PyObject *name = PyUnicode_FromString("clear");
37-
if (name == NULL) {
38-
return 0;
39-
}
40-
PyObject *res = PyObject_CallMethodNoArgs(list, name);
41-
Py_DECREF(name);
36+
PyObject *res = PyObject_CallMethodNoArgs(list, &_Py_ID(clear));
4237
if (res == NULL) {
4338
return 0;
4439
}
@@ -51,13 +46,7 @@ PyObject *CPyList_Copy(PyObject *list) {
5146
return PyList_GetSlice(list, 0, PyList_GET_SIZE(list));
5247
}
5348

54-
PyObject *name = PyUnicode_FromString("copy");
55-
if (name == NULL) {
56-
return NULL;
57-
}
58-
PyObject *res = PyObject_CallMethodNoArgs(list, name);
59-
Py_DECREF(res);
60-
return res;
49+
return PyObject_CallMethodNoArgs(list, &_Py_ID(copy));
6150
}
6251

6352
PyObject *CPyList_GetItemShort(PyObject *list, CPyTagged index) {

‎mypyc/lib-rt/misc_ops.c‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ PyObject *CPyIter_Send(PyObject *iter, PyObject *val)
2929
if (Py_IsNone(val)) {
3030
return CPyIter_Next(iter);
3131
} else {
32-
PyObject *name = PyUnicode_FromString("send");
33-
if (name == NULL) {
34-
return NULL;
35-
}
36-
PyObject *res = PyObject_CallMethodOneArg(iter, name, val);
37-
Py_DECREF(name);
38-
return res;
32+
return PyObject_CallMethodOneArg(iter, &_Py_ID(send), val);
3933
}
4034
}
4135

@@ -876,9 +870,9 @@ PyObject *
876870
CPy_CallReverseOpMethod(PyObject *left,
877871
PyObject *right,
878872
const char *op,
879-
const char *method) {
873+
PyObject *method) {
880874
// Look up reverse method
881-
PyObject *m = PyObject_GetAttrString(right, method);
875+
PyObject *m = PyObject_GetAttr(right, method);
882876
if (m == NULL) {
883877
// If reverse method not defined, generate TypeError instead AttributeError
884878
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
@@ -1064,10 +1058,7 @@ PyObject *CPy_GetName(PyObject *obj) {
10641058
if (PyType_Check(obj)) {
10651059
return PyType_GetName((PyTypeObject *)obj);
10661060
}
1067-
PyObject *name = PyUnicode_FromString("__name__");
1068-
PyObject *res = PyObject_GetAttr(obj, name);
1069-
Py_DECREF(name);
1070-
return res;
1061+
return PyObject_GetAttr(obj, &_Py_ID(__name__));
10711062
}
10721063

10731064
#endif

‎mypyc/lib-rt/pythonsupport.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
#include "internal/pycore_genobject.h" // _PyGen_FetchStopIterationValue
2121
#include "internal/pycore_pyerrors.h" // _PyErr_FormatFromCause, _PyErr_SetKeyError
2222
#include "internal/pycore_setobject.h" // _PySet_Update
23+
#include "internal/pycore_runtime.h"
24+
#include "internal/pycore_global_strings.h"
2325
#endif
2426

2527
#if CPY_3_12_FEATURES
2628
#include "internal/pycore_frame.h"
29+
#include "internal/pycore_runtime.h"
30+
#include "internal/pycore_global_strings.h"
2731
#endif
2832

2933
#ifdef __cplusplus
@@ -377,9 +381,9 @@ _CPyDictView_New(PyObject *dict, PyTypeObject *type)
377381

378382
#if PY_VERSION_HEX >= 0x030A0000 // 3.10
379383
static int
380-
_CPyObject_HasAttrId(PyObject *v, const char *name) {
384+
_CPyObject_HasAttrId(PyObject *v, PyObject *name) {
381385
PyObject *tmp = NULL;
382-
int result = PyObject_GetOptionalAttrString(v, name, &tmp);
386+
int result = PyObject_GetOptionalAttr(v, name, &tmp);
383387
if (tmp) {
384388
Py_DECREF(tmp);
385389
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL