Skip to content

Instantly share code, notes, and snippets.

@qunaibit
Last active June 9, 2022 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qunaibit/4da64fac538150ded4f319c25b23e2ee to your computer and use it in GitHub Desktop.
Save qunaibit/4da64fac538150ded4f319c25b23e2ee to your computer and use it in GitHub Desktop.
// src/ft2font_wrapper.cpp
typedef struct
{
-   PyObject_HEAD
    FT2Font *x;
+   HPyField fname;
-   PyObject *fname;
+   HPyField py_file;
-   PyObject *py_file;
    FT_StreamRec stream;
    HPy_ssize_t shape[2];
    HPy_ssize_t strides[2];
    HPy_ssize_t suboffsets[2];
} PyFT2Font;

+ HPyType_HELPERS(PyFT2Font)

static HPy PyFT2Font_new(HPyContext *ctx, HPy type, HPy* args, HPy_ssize_t nargs, HPy kwds)
{
    PyFT2Font *self;
+   HPy h_self = HPy_New(ctx, type, &self);
-   self = (PyFT2Font *)type->tp_alloc(type, 0);
    self->x = NULL;
+   HPyField_Store(ctx, h_self, &self->fname, HPy_NULL);
-   self->fname = NULL;
+   HPyField_Store(ctx, h_self, &self->py_file, HPy_NULL);
-   self->py_file = NULL;
    memset(&self->stream, 0, sizeof(FT_StreamRec));
    return h_self;
}

...
static HPy PyFT2Font_get_num_glyphs(HPyContext *ctx, HPy h_self)
{
+   PyFT2Font* self = PyFT2Font_AsStruct(ctx, h_self);
+   return HPyLong_FromLong(ctx, self->x->get_num_glyphs());
-   return PyLong_FromLong(self->x->get_num_glyphs());
}
...
static HPy PyFT2Font_fname(HPyContext *ctx, HPy h_self, void *closure)
{
+   PyFT2Font* self = PyFT2Font_AsStruct(ctx, h_self);
+   HPy fname = HPyField_Load(ctx, h_self, self->fname);
+   if (!HPy_IsNull(fname)) {
-   if (self->fname) {
+      return fname;
-      return self->fname;
    }

    return HPy_Dup(ctx, ctx->h_None);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment