Inline Helpers¶
Inline Helper functions are static inline
functions (written in C).
Those functions are usually small convenience functions that everyone could
write but in order to avoid duplicated effort, they are defined by HPy.
One category of inline helpers are functions that convert the commonly used
but not fixed width C types, such as int
, or long long
, to HPy API.
The HPy API always uses well-defined fixed width types like int32
or
unsigned int8
.
-
HPy
HPyErr_SetFromErrno
(HPyContext *ctx, HPy h_type)¶
[source] Same as
HPyErr_SetFromErrnoWithFilenameObjects()
but passesHPy_NULL
to the optional arguments.- Parameters
ctx – The execution context.
h_type – The exception type to raise.
- Returns
always returns
HPy_NULL
-
HPy
HPyErr_SetFromErrnoWithFilenameObject
(HPyContext *ctx, HPy h_type, HPy filename)¶
[source] Same as
HPyErr_SetFromErrnoWithFilenameObjects()
but passesHPy_NULL
to the last (optional) argument.- Parameters
ctx – The execution context.
h_type – The exception type to raise.
filename – a filename; may be
HPy_NULL
- Returns
always returns
HPy_NULL
-
HPy
HPyTuple_Pack
(HPyContext *ctx, HPy_ssize_t n, ...)¶
[source] Create a tuple from arguments.
A convenience function that will allocate a temporary array of
HPy
elements and useHPyTuple_FromArray()
to create a tuple.- Parameters
ctx – The execution context.
n – The number of elements to pack into a tuple.
... – Variable number of
HPy
arguments.
- Returns
A new tuple with
n
elements orHPy_NULL
in case of an error occurred.
-
int
HPy_DelAttr
(HPyContext *ctx, HPy obj, HPy name)¶
[source] Delete an attribute.
This is the equivalent of the Python statement
del o.attr_name
.- Parameters
ctx – The execution context.
obj – The object with the attribute.
name – The name (an unicode object) of the attribute.
- Returns
0
on success;-1
in case of an error.
-
int
HPy_DelAttr_s
(HPyContext *ctx, HPy obj, const char *utf8_name)¶
[source] Delete an attribute.
This is the equivalent of the Python statement
del o.attr_name
.- Parameters
ctx – The execution context.
obj – The object with the attribute.
utf8_name – The name (an UTF-8 encoded C string) of the attribute.
- Returns
0
on success;-1
in case of an error.
-
HPy
HPyLong_FromLong
(HPyContext *ctx, long l)¶
[source] Create a Python long object from a C
long
value.- Parameters
ctx – The execution context.
l – A C long value.
- Returns
A Python long object with the value of
l
orHPy_NULL
on failure.
-
HPy
HPyLong_FromUnsignedLong
(HPyContext *ctx, unsigned long l)¶
[source] Create a Python long object from a C
unsigned long
value.- Parameters
ctx – The execution context.
l – A C
unsigned long
value.
- Returns
A Python long object with the value of
l
orHPy_NULL
on failure.
-
HPy
HPyLong_FromLongLong
(HPyContext *ctx, long long l)¶
[source] Create a Python long object from a C
long long
value.- Parameters
ctx – The execution context.
l – A C
long long
value.
- Returns
A Python long object with the value of
l
orHPy_NULL
on failure.
-
HPy
HPyLong_FromUnsignedLongLong
(HPyContext *ctx, unsigned long long l)¶
[source] Create a Python long object from a C
unsigned long long
value.- Parameters
ctx – The execution context.
l – A C
unsigned long long
value.
- Returns
A Python long object with the value of
l
orHPy_NULL
on failure.
-
long
HPyLong_AsLong
(HPyContext *ctx, HPy h)¶
[source] Return a C
long
representation of the given Python long object. If the object is not an instance of Python long, the object’s__index__
method (if present) will be used to convert it to a Python long object.This function will raise an
OverflowError
if the value of the object is out of range for a Clong
.This function will raise a
TypeError
if:The object is neither an instance of Python long nor it provides an
__index__
method.If the
__index__
method does not return an instance of Python long.
- Parameters
ctx – The execution context.
h – Either an instance of Python long or an object that provides an
__index__
method (which returns a Python long).
- Returns
A C
long
value. Errors will be indicated with return value-1
. In this case, useHPyErr_Occurred()
to disambiguate.
-
unsigned long
HPyLong_AsUnsignedLong
(HPyContext *ctx, HPy h)¶
[source] Return a C
unsigned long
representation of the given Python long object.This function will raise a
TypeError
if the object is not an instance of Python long and it will raise anOverflowError
if the object’s value is negative or out of range for a Cunsigned long
.- Parameters
ctx – The execution context.
h – The object to convert to C
unsigned long
(must be an instance of Python long).
- Returns
A C
unsigned long
value. Errors will be indicated with return value(unsigned long)-1
. In this case, useHPyErr_Occurred()
to disambiguate.
-
unsigned long
HPyLong_AsUnsignedLongMask
(HPyContext *ctx, HPy h)¶
[source] Return a C
unsigned long
representation of the given Python long object. If the object is not an instance of Python long, the object’s__index__
method (if present) will be used to convert it to a Python long object.If the object’s value is out of range for an
unsigned long
, return the reduction of that value moduloULONG_MAX + 1
. Therefore, this function will NOT raise anOverflowError
if the value of the object is out of range for a Cunsigned long
.- Parameters
ctx – The execution context.
h – Either an instance of Python long or an object that provides an
__index__
method (which returns a Python long).
- Returns
A C
unsigned long
value. Errors will be indicated with return value(unsigned long)-1
. In this case, useHPyErr_Occurred()
to disambiguate.
-
long long
HPyLong_AsLongLong
(HPyContext *ctx, HPy h)¶
[source] Return a C
long long
representation of the given Python long object. If the object is not an instance of Python long, the object’s__index__
method (if present) will be used to convert it to a Python long object.This function will raise an
OverflowError
if the value of the object is out of range for a Clong long
.This function will raise a
TypeError
if:The object is neither an instance of Python long nor it provides an
__index__
method.If the
__index__
method does not return an instance of Python long.
- Parameters
ctx – The execution context.
h – Either an instance of Python long or an object that provides an
__index__
method (which returns a Python long).
- Returns
A C
long long
value. Errors will be indicated with return value-1
. In this case, useHPyErr_Occurred()
to disambiguate.
-
unsigned long long
HPyLong_AsUnsignedLongLong
(HPyContext *ctx, HPy h)¶
[source] Return a C
unsigned long long
representation of the given Python long object.This function will raise a
TypeError
if the object is not an instance of Python long and it will raise anOverflowError
if the object’s value is negative or out of range for a Cunsigned long
.- Parameters
ctx – The execution context.
h – The object to convert to C
unsigned long long
(must be an instance of Python long).
- Returns
A C
unsigned long long
value. Errors will be indicated with return value(unsigned long long)-1
. In this case, useHPyErr_Occurred()
to disambiguate.
-
unsigned long long
HPyLong_AsUnsignedLongLongMask
(HPyContext *ctx, HPy h)¶
[source] Return a C
unsigned long long
representation of the given Python long object. If the object is not an instance of Python long, the object’s__index__
method (if present) will be used to convert it to a Python long object.If the object’s value is out of range for an
unsigned long long
, return the reduction of that value moduloULLONG_MAX + 1
. Therefore, this function will NOT raise anOverflowError
if the value of the object is out of range for a Cunsigned long long
.- Parameters
ctx – The execution context.
h – Either an instance of Python long or an object that provides an
__index__
method (which returns a Python long).
- Returns
A C
unsigned long
value. Errors will be indicated with return value(unsigned long long)-1
. In this case, useHPyErr_Occurred()
to disambiguate.
-
HPy
HPyBool_FromLong
(HPyContext *ctx, long v)¶
[source] Returns Python
True
orFalse
depending on the truth value ofv
.- Parameters
ctx – The execution context.
v – A C
long
value.
- Returns
Python
True
ifv != 0
; PythonFalse
otherwise.
-
HPy_ssize_t
HPySlice_AdjustIndices
(HPyContext *_HPy_UNUSED_ARG(ctx), HPy_ssize_t length, HPy_ssize_t *start, HPy_ssize_t *stop, HPy_ssize_t step, )¶
[source] Adjust start/end slice indices assuming a sequence of the specified length.
Out of bounds indices are clipped in a manner consistent with the handling of normal slices. This function cannot fail and does not call interpreter routines.
- Parameters
ctx – The execution context.
length – The length of the sequence that should be assumed for adjusting the indices.
start – Pointer to the start value (must not be
NULL
).stop – Pointer to the stop value (must not be
NULL
).step – The step value of the slice (must not be
0
)
- Returns
Return the length of the slice. Always successful. Doesn’t call Python code.