Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jul 9, 2024
1 parent 14e8dae commit 472eff0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
// (mostly commonly, for bytearrays)
PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter) {
if (PyBytes_CheckExact(sep)) {
return _PyBytes_Join(sep, iter);
return PyObject_CallMethod(sep, "join", "O", iter);
} else {
_Py_IDENTIFIER(join);
return _PyObject_CallMethodIdOneArg(sep, &PyId_join, iter);
Expand Down
5 changes: 4 additions & 1 deletion mypyc/lib-rt/dict_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ PyObject *CPyDict_SetDefault(PyObject *dict, PyObject *key, PyObject *value) {
return ret;
}
_Py_IDENTIFIER(setdefault);
return _PyObject_CallMethodIdObjArgs(dict, &PyId_setdefault, key, value, NULL);
PyObject* name = _PyUnicode_FromId(&PyId_setdefault);
PyObject* res = PyObject_CallMethodObjArgs(dict, name, key, value, NULL);
Py_DECREF(name);
return res;
}

PyObject *CPyDict_SetDefaultWithNone(PyObject *dict, PyObject *key) {
Expand Down
65 changes: 58 additions & 7 deletions mypyc/lib-rt/pythonsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
#ifndef Py_BUILD_CORE
#define Py_BUILD_CORE
#endif
#include "internal/pycore_bytesobject.h" // _PyBytes_Join
#include "internal/pycore_call.h" // _PyObject_CallMethodIdNoArgs, _PyObject_CallMethodIdObjArgs, _PyObject_CallMethodIdOneArg
#include "internal/pycore_call.h" // _PyObject_CallMethodIdNoArgs, _PyObject_CallMethodIdOneArg
#include "internal/pycore_genobject.h" // _PyGen_FetchStopIterationValue
#include "internal/pycore_object.h" // _PyType_CalculateMetaclass
#include "internal/pycore_pyerrors.h" // _PyErr_FormatFromCause, _PyErr_SetKeyError
#include "internal/pycore_setobject.h" // _PySet_Update
#include "internal/pycore_unicodeobject.h" // _PyUnicode_EQ, _PyUnicode_FastCopyCharacters
#endif

#if CPY_3_12_FEATURES
#elif CPY_3_12_FEATURES
#include "internal/pycore_frame.h"
#endif

Expand Down Expand Up @@ -516,5 +511,61 @@ CPyCoro_GetAwaitableIter(PyObject *o)
return NULL;
}

#if CPY_3_13_FEATURES

Py_LOCAL_INLINE(int)
unicode_eq(PyObject *a, PyObject *b)
{
if (PyUnicode_GET_LENGTH(a) != PyUnicode_GET_LENGTH(b))
return 0;
if (PyUnicode_GET_LENGTH(a) == 0)
return 1;
if (PyUnicode_KIND(a) != PyUnicode_KIND(b))
return 0;
return memcmp(PyUnicode_1BYTE_DATA(a), PyUnicode_1BYTE_DATA(b),
PyUnicode_GET_LENGTH(a) * PyUnicode_KIND(a)) == 0;
}

int _PyUnicode_EQ(PyObject *aa, PyObject *bb)
{
return unicode_eq(aa, bb);
}

PyTypeObject *
_PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases)
{
Py_ssize_t i, nbases;
PyTypeObject *winner;
PyObject *tmp;
PyTypeObject *tmptype;

/* Determine the proper metatype to deal with this,
and check for metatype conflicts while we're at it.
Note that if some other metatype wins to contract,
it's possible that its instances are not types. */

nbases = PyTuple_GET_SIZE(bases);
winner = metatype;
for (i = 0; i < nbases; i++) {
tmp = PyTuple_GET_ITEM(bases, i);
tmptype = Py_TYPE(tmp);
if (PyType_IsSubtype(winner, tmptype))
continue;
if (PyType_IsSubtype(tmptype, winner)) {
winner = tmptype;
continue;
}
/* else: */
PyErr_SetString(PyExc_TypeError,
"metaclass conflict: "
"the metaclass of a derived class "
"must be a (non-strict) subclass "
"of the metaclasses of all its bases");
return NULL;
}
return winner;
}

#endif

#endif
4 changes: 2 additions & 2 deletions mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ PyObject *CPyStr_Build(Py_ssize_t len, ...) {
sz += add_sz;

// If these strings have different kind, we would call
// _PyUnicode_FastCopyCharacters() in the following part.
// PyUnicode_CopyCharacters() in the following part.
if (use_memcpy && last_obj != NULL) {
if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item))
use_memcpy = 0;
Expand Down Expand Up @@ -117,7 +117,7 @@ PyObject *CPyStr_Build(Py_ssize_t len, ...) {
PyObject *item = va_arg(args, PyObject *);
Py_ssize_t itemlen = PyUnicode_GET_LENGTH(item);
if (itemlen != 0) {
_PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen);
PyUnicode_CopyCharacters(res, res_offset, item, 0, itemlen);
res_offset += itemlen;
}
}
Expand Down

0 comments on commit 472eff0

Please sign in to comment.