Skip to content

Commit

Permalink
Merge pull request hpyproject#411 from hpyproject/fa/unicode_fromecod…
Browse files Browse the repository at this point in the history
…ed_substring

Introduce HPyUnicode_FromEncodedObjec, HPyUnicode_Substring, HPyDict_Keys, and HPyDict_Copy.
  • Loading branch information
fangerer authored Mar 3, 2023
2 parents 4f14ae0 + 1c126cd commit cdf9e3e
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/api-reference/function-index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ HPy Core API Function Index
* :c:func:`HPyContextVar_New`
* :c:func:`HPyContextVar_Set`
* :c:func:`HPyDict_Check`
* :c:func:`HPyDict_Copy`
* :c:func:`HPyDict_Keys`
* :c:func:`HPyDict_New`
* :c:func:`HPyErr_Clear`
* :c:func:`HPyErr_ExceptionMatches`
Expand Down Expand Up @@ -94,9 +96,11 @@ HPy Core API Function Index
* :c:func:`HPyUnicode_DecodeFSDefaultAndSize`
* :c:func:`HPyUnicode_DecodeLatin1`
* :c:func:`HPyUnicode_EncodeFSDefault`
* :c:func:`HPyUnicode_FromEncodedObject`
* :c:func:`HPyUnicode_FromString`
* :c:func:`HPyUnicode_FromWideChar`
* :c:func:`HPyUnicode_ReadChar`
* :c:func:`HPyUnicode_Substring`
* :c:func:`HPy_ASCII`
* :c:func:`HPy_Absolute`
* :c:func:`HPy_Add`
Expand Down
4 changes: 4 additions & 0 deletions docs/porting-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ with the code for the :term:`CPython ABI` mode, so it is guaranteed to be correc
`PyContextVar_New <https://docs.python.org/3/c-api/contextvars.html#c.PyContextVar_New>`_ :c:func:`HPyContextVar_New`
`PyContextVar_Set <https://docs.python.org/3/c-api/contextvars.html#c.PyContextVar_Set>`_ :c:func:`HPyContextVar_Set`
`PyDict_Check <https://docs.python.org/3/c-api/dict.html#c.PyDict_Check>`_ :c:func:`HPyDict_Check`
`PyDict_Copy <https://docs.python.org/3/c-api/dict.html#c.PyDict_Copy>`_ :c:func:`HPyDict_Copy`
`PyDict_Keys <https://docs.python.org/3/c-api/dict.html#c.PyDict_Keys>`_ :c:func:`HPyDict_Keys`
`PyDict_New <https://docs.python.org/3/c-api/dict.html#c.PyDict_New>`_ :c:func:`HPyDict_New`
`PyErr_Clear <https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Clear>`_ :c:func:`HPyErr_Clear`
`PyErr_ExceptionMatches <https://docs.python.org/3/c-api/exceptions.html#c.PyErr_ExceptionMatches>`_ :c:func:`HPyErr_ExceptionMatches`
Expand Down Expand Up @@ -269,9 +271,11 @@ with the code for the :term:`CPython ABI` mode, so it is guaranteed to be correc
`PyUnicode_DecodeFSDefaultAndSize <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeFSDefaultAndSize>`_ :c:func:`HPyUnicode_DecodeFSDefaultAndSize`
`PyUnicode_DecodeLatin1 <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeLatin1>`_ :c:func:`HPyUnicode_DecodeLatin1`
`PyUnicode_EncodeFSDefault <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_EncodeFSDefault>`_ :c:func:`HPyUnicode_EncodeFSDefault`
`PyUnicode_FromEncodedObject <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FromEncodedObject>`_ :c:func:`HPyUnicode_FromEncodedObject`
`PyUnicode_FromString <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FromString>`_ :c:func:`HPyUnicode_FromString`
`PyUnicode_FromWideChar <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FromWideChar>`_ :c:func:`HPyUnicode_FromWideChar`
`PyUnicode_ReadChar <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_ReadChar>`_ :c:func:`HPyUnicode_ReadChar`
`PyUnicode_Substring <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_Substring>`_ :c:func:`HPyUnicode_Substring`
================================================================================================================================== ================================================


Expand Down
8 changes: 8 additions & 0 deletions hpy/debug/src/autogen_debug_ctx_init.h

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

36 changes: 36 additions & 0 deletions hpy/debug/src/autogen_debug_wrappers.c

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

21 changes: 21 additions & 0 deletions hpy/debug/src/debug_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,24 @@ int debug_ctx_Type_IsSubtype(HPyContext *dctx, DHPy sub, DHPy type)
ctx_info->is_valid = true;
return res;
}

DHPy debug_ctx_Unicode_Substring(HPyContext *dctx, DHPy str, HPy_ssize_t start, HPy_ssize_t end)
{
HPyDebugCtxInfo *ctx_info;
HPyContext *uctx;

ctx_info = get_ctx_info(dctx);
if (!ctx_info->is_valid) {
report_invalid_debug_context();
}

HPy uh_str = DHPy_unwrap(dctx, str);
uctx = ctx_info->info->uctx;
if (!HPy_TypeCheck(uctx, uh_str, uctx->h_UnicodeType)) {
HPy_FatalError(uctx, "HPyUnicode_Substring arg 1 must be a Unicode object");
}
ctx_info->is_valid = false;
HPy universal_result = HPyUnicode_Substring(uctx, uh_str, start, end);
ctx_info->is_valid = true;
return DHPy_open(dctx, universal_result);
}
20 changes: 20 additions & 0 deletions hpy/devel/include/hpy/cpython/autogen_api_impl.h

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

4 changes: 4 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_ctx.h

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

16 changes: 16 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_trampolines.h

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

1 change: 1 addition & 0 deletions hpy/tools/autogen/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class autogen_debug_wrappers(AutoGenFile):
'HPyContextVar_Get',
'HPyType_GetName',
'HPyType_IsSubtype',
'HPyUnicode_Substring',
}

def generate(self):
Expand Down
93 changes: 92 additions & 1 deletion hpy/tools/autogen/public_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ int HPy_TypeCheck(HPyContext *ctx, HPy obj, HPy type);
* Return the type's name.
*
* Equivalent to getting the type's ``__name__`` attribute. If you want to
* retrieve the type's name as Python unicode object, then just use
* retrieve the type's name as a handle that refers to a ``str``, then just use
* ``HPy_GetAttr_s(ctx, type, "__name__")``.
*
* :param ctx:
Expand Down Expand Up @@ -536,6 +536,62 @@ HPy HPyUnicode_DecodeASCII(HPyContext *ctx, const char *ascii, HPy_ssize_t size,
HPy_ID(197)
HPy HPyUnicode_DecodeLatin1(HPyContext *ctx, const char *latin1, HPy_ssize_t size, const char *errors);

/**
* Decode a bytes-like object to a Unicode object.
*
* The bytes of the bytes-like object are decoded according to the given
* encoding and using the error handling defined by ``errors``.
*
* :param ctx:
* The execution context.
* :param obj:
* A bytes-like object. This can be, for example, Python *bytes*,
* *bytearray*, *memoryview*, *array.array* and objects that support the
* Buffer protocol. If this argument is `HPy_NULL``, a ``SystemError`` will
* be raised. If the argument is not a bytes-like object, a ``TypeError``
* will be raised.
* :param encoding:
* The name (UTF-8 encoded C string) of the encoding to use. If the encoding
* does not exist, a ``LookupError`` will be raised. If this argument is
* ``NULL``, the default encoding ``UTF-8`` will be used.
* :param errors:
* The error handling (UTF-8 encoded C string) to use when decoding. The
* possible values depend on the used encoding. This argument may be
* ``NULL`` in which case it will default to ``"strict"``.
*
* :returns:
* A handle to a ``str`` object created from the decoded bytes or
* ``HPy_NULL`` in case of errors.
*/
HPy_ID(255)
HPy HPyUnicode_FromEncodedObject(HPyContext *ctx, HPy obj, const char *encoding, const char *errors);

/**
* Return a substring of ``str``, from character index ``start`` (included) to
* character index ``end`` (excluded).
*
* Indices ``start`` and ``end`` must not be negative, otherwise an
* ``IndexError`` will be raised. If ``start >= len(str)`` or if
* ``end < start``, an empty string will be returned. If ``end > len(str)`` then
* ``end == len(str)`` will be assumed.
*
* :param ctx:
* The execution context.
* :param str:
* A Python Unicode object (must not be ``HPy_NULL``). Otherwise, the
* behavior is undefined (verification of the argument is only done in
* debug mode).
* :param start:
* The non-negative start index (inclusive).
* :param end:
* The non-negative end index (exclusive).
*
* :returns:
* The requested substring or ``HPy_NULL`` in case of an error.
*/
HPy_ID(256)
HPy HPyUnicode_Substring(HPyContext *ctx, HPy str, HPy_ssize_t start, HPy_ssize_t end);

/* listobject.h */
HPy_ID(198)
int HPyList_Check(HPyContext *ctx, HPy h);
Expand All @@ -550,6 +606,41 @@ int HPyDict_Check(HPyContext *ctx, HPy h);
HPy_ID(202)
HPy HPyDict_New(HPyContext *ctx);

/**
* Returns a list of all keys from the dictionary.
*
* Note: This function will directly access the storage of the dict object and
* therefore ignores if method ``keys`` was overwritten.
*
* :param ctx:
* The execution context.
* :param h:
* A Python dict object. If this argument is ``HPy_NULL`` or not an
* instance of a Python dict, a ``SystemError`` will be raised.
*
* :returns:
* A Python list object containing all keys of the given dictionary or
* ``HPy_NULL`` in case of an error.
*/
HPy_ID(257)
HPy HPyDict_Keys(HPyContext *ctx, HPy h);

/**
* Creates a copy of the provided Python dict object.
*
* :param ctx:
* The execution context.
* :param h:
* A Python dict object. If this argument is ``HPy_NULL`` or not an
* instance of a Python dict, a ``SystemError`` will be raised.
*
* :returns:
* Return a new dictionary that contains the same key-value pairs as ``h``
* or ``HPy_NULL`` in case of an error.
*/
HPy_ID(258)
HPy HPyDict_Copy(HPyContext *ctx, HPy h);

/* tupleobject.h */
HPy_ID(203)
int HPyTuple_Check(HPyContext *ctx, HPy h);
Expand Down
12 changes: 10 additions & 2 deletions hpy/trace/src/autogen_trace_ctx_init.h

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

Loading

0 comments on commit cdf9e3e

Please sign in to comment.