Skip to content

Commit d21bf2b

Browse files
committed
PYTHON-3717 Speed up _type_marker check in BSON
1 parent bda9e3a commit d21bf2b

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

bson/_cbsonmodule.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,13 @@ static int _load_python_objects(PyObject* module) {
428428
*
429429
* Return the type marker, 0 if there is no marker, or -1 on failure.
430430
*/
431+
static PyObject *TYPEMARKERSTR;
431432
static long _type_marker(PyObject* object) {
432433
PyObject* type_marker = NULL;
433434
long type = 0;
434435

435-
if (PyObject_HasAttrString(object, "_type_marker")) {
436-
type_marker = PyObject_GetAttrString(object, "_type_marker");
436+
if (PyObject_HasAttr(object, TYPEMARKERSTR)) {
437+
type_marker = PyObject_GetAttr(object, TYPEMARKERSTR);
437438
if (type_marker == NULL) {
438439
return -1;
439440
}
@@ -450,13 +451,6 @@ static long _type_marker(PyObject* object) {
450451
if (type_marker && PyLong_CheckExact(type_marker)) {
451452
type = PyLong_AsLong(type_marker);
452453
Py_DECREF(type_marker);
453-
/*
454-
* Py(Long|Int)_AsLong returns -1 for error but -1 is a valid value
455-
* so we call PyErr_Occurred to differentiate.
456-
*/
457-
if (type == -1 && PyErr_Occurred()) {
458-
return -1;
459-
}
460454
} else {
461455
Py_XDECREF(type_marker);
462456
}
@@ -3031,5 +3025,7 @@ PyInit__cbson(void)
30313025
INITERROR;
30323026
}
30333027

3028+
TYPEMARKERSTR = PyUnicode_FromString("_type_marker");
3029+
30343030
return m;
30353031
}

0 commit comments

Comments
 (0)