Skip to content

Commit a65d9e7

Browse files
committed
Update HPy inlined files: 8fe1487
1 parent b3a8464 commit a65d9e7

File tree

11 files changed

+155
-7
lines changed

11 files changed

+155
-7
lines changed

graalpython/com.oracle.graal.python.cext/include/hpy/cpython/autogen_api_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,21 @@ HPyAPI_FUNC HPy HPyUnicode_AsUTF8String(HPyContext *ctx, HPy h)
440440
return _py2h(PyUnicode_AsUTF8String(_h2py(h)));
441441
}
442442

443+
HPyAPI_FUNC const char *HPyUnicode_AsUTF8AndSize(HPyContext *ctx, HPy h, HPy_ssize_t *size)
444+
{
445+
return PyUnicode_AsUTF8AndSize(_h2py(h), size);
446+
}
447+
443448
HPyAPI_FUNC HPy HPyUnicode_FromWideChar(HPyContext *ctx, const wchar_t *w, HPy_ssize_t size)
444449
{
445450
return _py2h(PyUnicode_FromWideChar(w, size));
446451
}
447452

453+
HPyAPI_FUNC HPy HPyUnicode_DecodeFSDefault(HPyContext *ctx, const char *v)
454+
{
455+
return _py2h(PyUnicode_DecodeFSDefault(v));
456+
}
457+
448458
HPyAPI_FUNC int HPyList_Check(HPyContext *ctx, HPy h)
449459
{
450460
return PyList_Check(_h2py(h));

graalpython/com.oracle.graal.python.cext/include/hpy/runtime/ctx_funcs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// This file contains the prototypes for all the functions defined in
55
// hpy/devel/src/runtime/ctx_*.c
66

7-
#include <Python.h>
87
#include "hpy.h"
98

109
// ctx_bytes.c

graalpython/com.oracle.graal.python.cext/include/hpy/universal/autogen_ctx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ struct _HPyContext_s {
193193
HPy (*ctx_Unicode_FromString)(HPyContext *ctx, const char *utf8);
194194
int (*ctx_Unicode_Check)(HPyContext *ctx, HPy h);
195195
HPy (*ctx_Unicode_AsUTF8String)(HPyContext *ctx, HPy h);
196+
const char *(*ctx_Unicode_AsUTF8AndSize)(HPyContext *ctx, HPy h, HPy_ssize_t *size);
196197
HPy (*ctx_Unicode_FromWideChar)(HPyContext *ctx, const wchar_t *w, HPy_ssize_t size);
198+
HPy (*ctx_Unicode_DecodeFSDefault)(HPyContext *ctx, const char *v);
197199
int (*ctx_List_Check)(HPyContext *ctx, HPy h);
198200
HPy (*ctx_List_New)(HPyContext *ctx, HPy_ssize_t len);
199201
int (*ctx_List_Append)(HPyContext *ctx, HPy h_list, HPy h_item);

graalpython/com.oracle.graal.python.cext/include/hpy/universal/autogen_trampolines.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,18 @@ HPyAPI_FUNC HPy HPyUnicode_AsUTF8String(HPyContext *ctx, HPy h) {
418418
return ctx->ctx_Unicode_AsUTF8String ( ctx, h );
419419
}
420420

421+
HPyAPI_FUNC const char *HPyUnicode_AsUTF8AndSize(HPyContext *ctx, HPy h, HPy_ssize_t *size) {
422+
return ctx->ctx_Unicode_AsUTF8AndSize ( ctx, h, size );
423+
}
424+
421425
HPyAPI_FUNC HPy HPyUnicode_FromWideChar(HPyContext *ctx, const wchar_t *w, HPy_ssize_t size) {
422426
return ctx->ctx_Unicode_FromWideChar ( ctx, w, size );
423427
}
424428

429+
HPyAPI_FUNC HPy HPyUnicode_DecodeFSDefault(HPyContext *ctx, const char *v) {
430+
return ctx->ctx_Unicode_DecodeFSDefault ( ctx, v );
431+
}
432+
425433
HPyAPI_FUNC int HPyList_Check(HPyContext *ctx, HPy h) {
426434
return ctx->ctx_List_Check ( ctx, h );
427435
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
// automatically generated by setup.py:get_scm_config()
3-
#define HPY_VERSION "0.0.3.dev308+ng7d457b4"
4-
#define HPY_GIT_REVISION "7d457b4"
3+
#define HPY_VERSION "0.0.3.dev317+ng8fe1487"
4+
#define HPY_GIT_REVISION "8fe1487"

graalpython/lib-graalpython/modules/hpy/devel/src/runtime/argparse.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@
5454
* ``d (float) [double]``
5555
* Convert a Python floating point number to a C double.
5656
*
57+
* Strings and buffers
58+
* ~~~~~~~~~~~~~~~~~~~~~~~~
59+
*
60+
* These formats allow accessing an object as a contiguous chunk of memory.
61+
* You don't have to provide raw storage for the returned unicode or bytes
62+
* area.
63+
*
64+
* In general, when a format sets a pointer to a buffer, the pointer is valid
65+
* only until the corresponding HPy handle is closed.
66+
*
67+
* ``s (unicode) [const char*]``
68+
*
69+
* Convert a Unicode object to a C pointer to a character string.
70+
* A pointer to an existing string is stored in the character pointer
71+
* variable whose address you pass. The C string is NUL-terminated.
72+
* The Python string must not contain embedded null code points; if it does,
73+
* a `ValueError` exception is raised. Unicode objects are converted
74+
* to C strings using 'utf-8' encoding. If this conversion fails,
75+
* a `UnicodeError` is raised.
76+
*
77+
* Note: This format does not accept bytes-like objects and is therefore
78+
* not suitable for filesystem paths.
79+
*
5780
* Handles (Python Objects)
5881
* ~~~~~~~~~~~~~~~~~~~~~~~~
5982
*
@@ -337,11 +360,41 @@ parse_item(HPyContext *ctx, HPyTracker *ht, HPy current_arg, int current_arg_tmp
337360
break;
338361
}
339362

340-
default:
363+
case 's': {
364+
const char **output = va_arg(*vl, const char **);
365+
if (!HPyUnicode_Check(ctx, current_arg)) {
366+
set_error(ctx, ctx->h_TypeError, err_fmt, "a str is required");
367+
return 0;
368+
}
369+
HPy_ssize_t size;
370+
const char *data = HPyUnicode_AsUTF8AndSize(ctx, current_arg, &size);
371+
if (data == NULL) {
372+
set_error(ctx, ctx->h_SystemError, err_fmt, "unicode conversion error");
373+
return 0;
374+
}
375+
// loop bounded by size is more robust/paranoid than strlen
376+
HPy_ssize_t i;
377+
for (i = 0; i < size; ++i) {
378+
if (data[i] == '\0') {
379+
set_error(ctx, ctx->h_ValueError, err_fmt, "embedded null character");
380+
return 0;
381+
}
382+
}
383+
if (data[i] != '\0') {
384+
set_error(ctx, ctx->h_SystemError, err_fmt, "missing terminating null character");
385+
return 0;
386+
}
387+
*output = data;
388+
break;
389+
}
390+
391+
default: {
341392
set_error(ctx, ctx->h_SystemError, err_fmt, "unknown arg format code");
342393
return 0;
343394
}
344395

396+
} // switch
397+
345398
return 1;
346399
}
347400

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
# automatically generated by setup.py:get_scm_config()
3-
__version__ = "0.0.3.dev308+ng7d457b4"
4-
__git_revision__ = "7d457b4"
3+
__version__ = "0.0.3.dev317+ng8fe1487"
4+
__git_revision__ = "8fe1487"

graalpython/lib-graalpython/modules/hpy/test/support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def make_module(self, ExtensionTemplate, main_src, name, extra_sources):
225225
We don't want to unnecessarily modify the global state inside tests:
226226
if you are writing a test which needs a proper import, you should not
227227
use make_module but explicitly use compile_module and import it
228-
manually as requied by your test.
228+
manually as required by your test.
229229
"""
230230
so_filename = self.compile_module(
231231
ExtensionTemplate, main_src, name, extra_sources)

graalpython/lib-graalpython/modules/hpy/test/test_argparse.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ def test_b(self):
5656
"function unsigned byte integer is less than minimum"
5757
)
5858

59+
def test_s(self):
60+
import pytest
61+
mod = self.make_parse_item("s", "const char*", "HPyUnicode_FromString")
62+
assert mod.f("hello HPy") == "hello HPy"
63+
with pytest.raises(ValueError) as err:
64+
mod.f(b"hello\0HPy".decode('utf-8'))
65+
assert str(err.value) == (
66+
"function embedded null character"
67+
)
68+
with pytest.raises(TypeError) as err:
69+
mod.f(b"hello HPy")
70+
assert str(err.value) == (
71+
"function a str is required"
72+
)
73+
5974
def test_B(self):
6075
mod = self.make_parse_item("B", "char", "char_to_hpybytes")
6176
assert mod.f(0) == b"\x00"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .support import HPyTest
2+
3+
class TestModule(HPyTest):
4+
def test_HPyModule_Create(self):
5+
mod = self.make_module("""
6+
HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS)
7+
static HPy f_impl(HPyContext *ctx, HPy self)
8+
{
9+
HPyModuleDef def = {
10+
.m_name = "foo",
11+
.m_doc = "Some doc",
12+
.m_size = -1,
13+
};
14+
return HPyModule_Create(ctx, &def);
15+
}
16+
@EXPORT(f)
17+
@INIT
18+
""")
19+
m = mod.f()
20+
assert m.__name__ == "foo"
21+
assert m.__doc__ == "Some doc"
22+
assert m.__package__ is None
23+
assert m.__loader__ is None
24+
assert m.__spec__ is None
25+
assert set(vars(m).keys()) == {
26+
'__name__', '__doc__', '__package__', '__loader__', '__spec__'}

0 commit comments

Comments
 (0)