Skip to content

Commit

Permalink
Merge pull request #405 from steve-s/ss/str-format
Browse files Browse the repository at this point in the history
Add HPyUnicode_FromFormat(v) and HPyErr_Format.
  • Loading branch information
mattip authored Feb 22, 2023
2 parents e1fa680 + 58d3608 commit aa49881
Show file tree
Hide file tree
Showing 11 changed files with 1,424 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/api-reference/formatting.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
String Formatting Helpers
=========================

.. autocmodule:: runtime/format.c
:no-members:
1 change: 1 addition & 0 deletions docs/api-reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ problems due to different compilers.

argument-parsing
build-value
formatting
helpers


Expand Down
1 change: 1 addition & 0 deletions hpy/devel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def get_extra_sources(self):
return list(map(str, [
self.src_dir.joinpath('argparse.c'),
self.src_dir.joinpath('buildvalue.c'),
self.src_dir.joinpath('format.c'),
self.src_dir.joinpath('helpers.c'),
]))

Expand Down
8 changes: 8 additions & 0 deletions hpy/devel/include/hpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,18 @@ typedef enum {
typedef Py_ssize_t HPy_ssize_t;
typedef Py_hash_t HPy_hash_t;
typedef Py_UCS4 HPy_UCS4;

# define HPY_SSIZE_T_MAX PY_SSIZE_T_MAX
# define HPY_SSIZE_T_MIN PY_SSIZE_T_MIN

#else
typedef intptr_t HPy_ssize_t;
typedef intptr_t HPy_hash_t;
typedef uint32_t HPy_UCS4;

# define HPY_SSIZE_T_MAX INTPTR_MAX
# define HPY_SSIZE_T_MIN (-HPY_SSIZE_T_MAX-1)

/* HPyCapsule field keys */
typedef enum {
HPyCapsule_key_Pointer = 0,
Expand All @@ -259,6 +266,7 @@ typedef enum {
#include "hpy/hpymodule.h"
#include "hpy/runtime/argparse.h"
#include "hpy/runtime/buildvalue.h"
#include "hpy/runtime/format.h"
#include "hpy/runtime/helpers.h"

#ifdef HPY_ABI_CPYTHON
Expand Down
18 changes: 18 additions & 0 deletions hpy/devel/include/hpy/runtime/format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* String formatting helpers. These functions are not part of HPy ABI. The implementation will be linked into HPy extensions.
*/
#ifndef HPY_COMMON_RUNTIME_FORMAT_H
#define HPY_COMMON_RUNTIME_FORMAT_H

#include "hpy.h"

HPyAPI_HELPER HPy
HPyUnicode_FromFormat(HPyContext *ctx, const char *fmt, ...);

HPyAPI_HELPER HPy
HPyUnicode_FromFormatV(HPyContext *ctx, const char *format, va_list vargs);

HPyAPI_HELPER HPy
HPyErr_Format(HPyContext *ctx, HPy h_type, const char *fmt, ...);

#endif /* HPY_COMMON_RUNTIME_FORMAT_H */
4 changes: 2 additions & 2 deletions hpy/devel/src/runtime/buildvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ static HPy_ssize_t count_items(HPyContext *ctx, const char *fmt, char end)
}
par_type = top_level_par;
}
snprintf(msg, MESSAGE_BUF_SIZE, "unmatched '%c' in the format string passed to HPy_BuildValue", par_type);
snprintf(msg, sizeof(msg), "unmatched '%c' in the format string passed to HPy_BuildValue", par_type);
HPyErr_SetString(ctx, ctx->h_SystemError, msg);
return -1;
}
Expand Down Expand Up @@ -260,7 +260,7 @@ static HPy build_single(HPyContext *ctx, const char **fmt, va_list *values, int

default: {
char message[MESSAGE_BUF_SIZE];
snprintf(message, MESSAGE_BUF_SIZE, "bad format char '%c' in the format string passed to HPy_BuildValue", format_char);
snprintf(message, sizeof(message), "bad format char '%c' in the format string passed to HPy_BuildValue", format_char);
HPyErr_SetString(ctx, ctx->h_SystemError, message);
return HPy_NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion hpy/devel/src/runtime/ctx_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ ctx_Type_FromSpec(HPyContext *ctx, HPyType_Spec *hpyspec,

#if HAVE_FROM_METACLASS
/* On Python 3.12 an newer, we can just use 'PyType_FromMetaclass'. */
PyObject *result = PyType_FromMetaclass(meta, NULL, spec, bases);
PyObject *result = PyType_FromMetaclass(metatype, NULL, spec, bases);
#else
/* On Python 3.11 an older, we need to use our own
'_PyType_FromMetaclass'. */
Expand Down
Loading

0 comments on commit aa49881

Please sign in to comment.