-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small documentation improvements & quickstart guide #399
Changes from all commits
3ed0a22
e0e60c5
889d347
2a1c057
b63aa8f
78e5586
70a8a0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// quickstart.c | ||
|
||
// This header file is the entrypoint to the HPy API: | ||
#include "hpy.h" | ||
|
||
// HPy method: the HPyDef_METH macro generates some boilerplate code, | ||
// the same code can be also written manually if desired | ||
HPyDef_METH(say_hello, "say_hello", HPyFunc_NOARGS) | ||
static HPy say_hello_impl(HPyContext *ctx, HPy self) | ||
{ | ||
// Methods take HPyContext, which must be passed as the first argument to | ||
// all HPy API functions. Other than that HPyUnicode_FromString does the | ||
// same thing as PyUnicode_FromString. | ||
// | ||
// HPy type represents a "handle" to a Python object, but may not be | ||
// a pointer to the object itself. It should be fully "opaque" to the | ||
// users. Try uncommenting the following two lines to see the difference | ||
// from PyObject*: | ||
// | ||
// if (self == self) | ||
// HPyUnicode_FromString(ctx, "Surprise? Try HPy_Is(ctx, self, self)"); | ||
|
||
return HPyUnicode_FromString(ctx, "Hello world"); | ||
} | ||
|
||
static HPyDef *QuickstartMethods[] = { | ||
&say_hello, // 'say_hello' generated for us by the HPyDef_METH macro | ||
NULL, | ||
}; | ||
|
||
static HPyModuleDef quickstart_def = { | ||
.doc = "HPy Quickstart Example", | ||
.defines = QuickstartMethods, | ||
}; | ||
|
||
// The Python interpreter will create the module for us from the | ||
// HPyModuleDef specification. Additional initialization can be | ||
// done in the HPy_mod_execute slot | ||
HPy_MODINIT(quickstart, quickstart_def) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# setup.py | ||
|
||
from setuptools import setup, Extension | ||
from os import path | ||
|
||
DIR = path.dirname(__file__) | ||
setup( | ||
name="hpy-quickstart", | ||
hpy_ext_modules=[ | ||
Extension('quickstart', sources=[path.join(DIR, 'quickstart.c')]), | ||
], | ||
setup_requires=['hpy'], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,36 +8,58 @@ HPy: a better API for Python | |
|
||
HPy provides a new API for extending Python in C. | ||
|
||
There are several advantages to writing C extensions in HPy: | ||
|
||
- **Speed**: it runs much faster on PyPy, GraalPy, and at native speed on CPython | ||
|
||
- **Deployment**: it is possible to compile a single binary which runs unmodified on all | ||
supported Python implementations and versions -- think "stable ABI" on steroids | ||
|
||
- **Simplicity**: it is simpler and more manageable than the ``Python.h`` API, both for | ||
the users and the Pythons implementing it | ||
|
||
- **Debugging**: it provides an improved debugging experience. Debug mode can be turned | ||
on at runtime without the need to recompile the extension or the Python running it. | ||
HPy design is more suitable for automated checks. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I shuffled this at the top, shortened the points, and added |
||
|
||
The official `Python/C API <https://docs.python.org/3/c-api/index.html>`_, | ||
also informally known as ``#include <Python.h>``, is | ||
specific to the current implementation of CPython: it exposes a lot of | ||
internal details which makes it hard: | ||
internal details which makes it hard to: | ||
|
||
- implement it for other Python implementations (e.g. PyPy, GraalPy, | ||
Jython, ...) | ||
|
||
- experiment with new approaches inside CPython itself, for example: | ||
|
||
- to implement it for other Python implementations (e.g. PyPy, GraalPython, | ||
Jython, IronPython, etc.) | ||
- use a tracing garbage collection instead of reference counting | ||
- remove the global interpreter lock (GIL) to take full advantage of multicore architectures | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this true? Does using HPy make it any easier to be sure code does not have threading problems? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, good point. This bullet point was here before, I just shuffled it and added "... to take full advantage of multicore architectures".
Depends on the point of view. My line of thinking is that it would make it easier to do the internal changes in CPython that are needed to remove the GIL. Still extensions would have to make sure that they operate correctly without GIL. I think it still falls into the category of "what is wrong with current C API", although better HPy would not solve the problem as a whole, only a part of it. Explaining all this in detail would make this bullet point too long/complex. I like that "removing GIL" is something that many would clearly understand brings benefits and there is bit of a hype around it now :-) I think this simplification could be fine for such a pitch as the beginning of the documentation? |
||
- use tagged pointers to reduce memory footprint | ||
|
||
- to experiment with new things inside CPython itself: e.g. using a GC | ||
instead of refcounting, or to remove the GIL. | ||
Where to go next: | ||
----------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could pitch some parts of the documentation depending on what you're looking for: either code samples or some more explanations/details |
||
|
||
There are several advantages to write your C extension in HPy: | ||
- Show me the code: | ||
|
||
- it runs much faster on PyPy, GraalPython, and at native speed on CPython | ||
- :doc:`Quickstart<quickstart>` | ||
- :ref:`Simple documented HPy extension example<simple example>` | ||
- :doc:`Tutorial: porting Python/C API extension to HPy<porting-example/index>` | ||
|
||
- it is possible to compile a single binary which runs unmodified on all | ||
supported Python implementations and versions | ||
- Details: | ||
|
||
- it is simpler and more manageable than the ``Python.h`` API | ||
- :doc:`HPy overview: motivation, goals, current status<overview>` | ||
- :doc:`HPy API concepts introduction<api>` | ||
- :doc:`Python/C API to HPy Porting guide<porting-guide>` | ||
- :doc:`HPy API reference<api-reference/index>` | ||
|
||
- it provides an improved debugging experience: in "debug mode", HPy | ||
actively checks for many common mistakes such as reference leaks and | ||
invalid usage of objects after they have been deleted. It is possible to | ||
turn the "debug mode" on at startup time, without needing to recompile | ||
Python or the extension itself | ||
|
||
Full table of contents: | ||
----------------------- | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
quickstart | ||
overview | ||
api | ||
porting-guide | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ Misc notes | |
========== | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
:maxdepth: 1 | ||
|
||
str-builder-api | ||
embedding | ||
protocols | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two were not a real documentation, but design notes. I don't think it fits in the docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw this comment only after my review. Maybe we could move the string stuff to the wiki? |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this paragraph is just shuffled at the end of this file