Skip to content

Commit

Permalink
Fix 'Creating types in HPy' docs
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-s committed Jan 31, 2023
1 parent 4d9ca6b commit 8cf1ed6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
6 changes: 5 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,11 @@ the previously defined array ``Point_defines``.

The type specification for the simple type ``simple_type.Point`` represented in
C by structure ``PointObject`` is now complete. All that remains is to create
the type object and add it to the module which is easily achieved by using
the type object and add it to the module.

We will define a module execute slot, which is executed by the runtime right
after the module is created. The purpose of the execute slot is to initialize
the newly created module object. We can then add the type by using
:c:func:`HPyHelpers_AddType`:

.. literalinclude:: examples/hpytype-example/simple_type.c
Expand Down
35 changes: 15 additions & 20 deletions docs/examples/hpytype-example/builtin_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,26 @@ static void make_Language(HPyContext *ctx, HPy module)
// END: add_Language
}

static HPyModuleDef moduledef = {
.name = "builtin_type",
.size = -1,
};

HPy_MODINIT(builtin_type)
static HPy init_builtin_type_impl(HPyContext *ctx)
{
HPy m = HPyModule_Create(ctx, &moduledef);
if (HPy_IsNull(m))
goto MODINIT_ERROR;

HPyDef_SLOT(simple_exec, HPy_mod_exec)
int simple_exec_impl(HPyContext *ctx, HPy m) {
make_Dummy(ctx, m);
if (HPyErr_Occurred(ctx))
goto MODINIT_ERROR;
return -1;

make_Language(ctx, m);
if (HPyErr_Occurred(ctx))
goto MODINIT_ERROR;
return -1;

return m;
return 0; // success
}

MODINIT_ERROR:
static HPyDef *mod_defines[] = {
&simple_exec, // 'simple_exec' is generated by the HPyDef_SLOT macro
NULL,
};

if (!HPy_IsNull(m))
HPy_Close(ctx, m);
return HPy_NULL;
}
static HPyModuleDef moduledef = {
.defines = mod_defines
};

HPy_MODINIT(builtin_type, moduledef)
41 changes: 18 additions & 23 deletions docs/examples/hpytype-example/simple_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,27 @@ static HPyType_Spec Point_spec = {
};
// END: spec

static HPyModuleDef moduledef = {
.name = "simple_type",
.doc = "A simple HPy type",
.size = -1,
.legacy_methods = NULL,
};

HPy_MODINIT(simple_type)
static HPy init_simple_type_impl(HPyContext *ctx)
{
HPy m = HPy_NULL;
m = HPyModule_Create(ctx, &moduledef);
if (HPy_IsNull(m))
goto MODINIT_ERROR;

// BEGIN: add_type
HPyDef_SLOT(simple_exec, HPy_mod_exec)
int simple_exec_impl(HPyContext *ctx, HPy m) {
if (!HPyHelpers_AddType(ctx, m, "Point", &Point_spec, NULL)) {
goto MODINIT_ERROR;
return -1;
}
// END: add_type
return 0; // success
}

return m;
static HPyDef *mod_defines[] = {
&simple_exec, // 'simple_exec' is generated by the HPyDef_SLOT macro
NULL,
};

MODINIT_ERROR:
static HPyModuleDef moduledef = {
.defines = mod_defines,
// ...
// END: add_type
.doc = "A simple HPy type",
.size = -1,
.legacy_methods = NULL,
};

if (!HPy_IsNull(m))
HPy_Close(ctx, m);
return HPy_NULL;
}
HPy_MODINIT(simple_type, moduledef)

0 comments on commit 8cf1ed6

Please sign in to comment.