Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Sep 20, 2023
1 parent 3c9cb41 commit eab142e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
58 changes: 46 additions & 12 deletions Include/object.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#ifndef Py_OBJECT_H
#define Py_OBJECT_H

#if defined(Py_ABI_VERSION_4) && Py_LIMITED_API+0 < 0x030d0000 && defined(MS_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -223,20 +230,52 @@ PyAPI_FUNC(int) Py_IsImmortal(PyObject *o);
#endif

#if defined(Py_ABI_VERSION_4) && Py_LIMITED_API+0 < 0x030d0000
PyAPI_FUNC(PyTypeObject *) Py_Type(PyObject *o) Py_ATTRIBUTE_WEAK;
// PyAPI_FUNC(PyTypeObject *) Py_Type(PyObject *o) Py_ATTRIBUTE_WEAK;
PyAPI_FUNC(void) Py_SetType(PyObject *o, PyTypeObject *type) Py_ATTRIBUTE_WEAK;
PyAPI_FUNC(Py_ssize_t) Py_Refcnt(PyObject *o) Py_ATTRIBUTE_WEAK;
PyAPI_FUNC(void) Py_SetRefcnt(PyObject *o, Py_ssize_t refcnt) Py_ATTRIBUTE_WEAK;
PyAPI_FUNC(Py_ssize_t) PyVarObject_Size(PyObject *o) Py_ATTRIBUTE_WEAK;
PyAPI_FUNC(void) PyVarObject_SetSize(PyObject *o, Py_ssize_t size) Py_ATTRIBUTE_WEAK;
#ifdef MS_WINDOWS
static inline PyTypeObject *
legacy_Py_Type(PyObject *ob)
{
return ((struct _object_abi3 *)ob)->ob_type;
}
static inline PyTypeObject *dispatch_Py_Type(PyObject *ob);

static PyTypeObject* (*_Py_type)(PyObject *) = &dispatch_Py_Type;

static inline PyTypeObject *
dispatch_Py_Type(PyObject *ob)
{
typedef PyTypeObject *(*_Py_type_func)(PyObject *);
HANDLE dll = GetModuleHandleW(NULL);
_Py_type_func proc = (_Py_type_func)GetProcAddress(dll, "Py_Type");
if (proc != NULL) {
printf("using found Py_TYPE\n");
_Py_type = proc;
return _Py_type(ob);
}
dll = GetModuleHandle("python3.dll");
if (dll) {
proc = (_Py_type_func)GetProcAddress(dll, "Py_Type");
if (proc) {
printf("using found Py_TYPE (python3.dll)\n");
_Py_type = proc;
return _Py_type(ob);
}
}
printf("using legacy Py_TYPE\n");
_Py_type = &legacy_Py_Type;
return _Py_type(ob);
}
#endif
#endif

static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
#if defined(Py_ABI_VERSION_4) && Py_LIMITED_API+0 < 0x030d0000
if (Py_Refcnt) {
return Py_Refcnt(ob);
}
return ((struct _object_abi3 *)ob)->ob_refcnt;
return Py_Refcnt(ob);
#elif !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000
return ob->ob_refcnt;
#else
Expand All @@ -251,16 +290,11 @@ static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
static inline PyTypeObject* Py_TYPE(PyObject *ob) {
#if defined(Py_ABI_VERSION_4) && Py_LIMITED_API+0 < 0x030d0000
if (Py_Type) {
return Py_Type(ob);
}
else {
return ((struct _object_abi3 *)ob)->ob_type;
}
return _Py_type(ob);
#elif !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000
return ob->ob_type;
#else
return Py_Type(ob);
return ob->ob_type;
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
Expand Down
2 changes: 2 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ _Py_DecRef(PyObject *o)
Py_DECREF(o);
}

PyAPI_FUNC(PyTypeObject *) Py_Type(PyObject *o);

PyTypeObject *
Py_Type(PyObject *o)
{
Expand Down
8 changes: 8 additions & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eab142e

Please sign in to comment.