Skip to content

Commit 89782cc

Browse files
authored
[mypyc] Add primitive for librt.base64.b64decode (#20272)
Forgot to add this in #20263.
1 parent f24678f commit 89782cc

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

mypyc/lib-rt/librt_base64.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,20 @@ static PyMethodDef librt_base64_module_methods[] = {
259259
{NULL, NULL, 0, NULL}
260260
};
261261

262+
#ifdef MYPYC_EXPERIMENTAL
263+
262264
static int
263265
base64_abi_version(void) {
264-
return 0;
266+
return LIBRT_BASE64_ABI_VERSION;
265267
}
266268

267269
static int
268270
base64_api_version(void) {
269-
return 0;
271+
return LIBRT_BASE64_API_VERSION;
270272
}
271273

274+
#endif
275+
272276
static int
273277
librt_base64_module_exec(PyObject *m)
274278
{
@@ -278,6 +282,7 @@ librt_base64_module_exec(PyObject *m)
278282
(void *)base64_abi_version,
279283
(void *)base64_api_version,
280284
(void *)b64encode_internal,
285+
(void *)b64decode_internal,
281286
};
282287
PyObject *c_api_object = PyCapsule_New((void *)base64_api, "librt.base64._C_API", NULL);
283288
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {

mypyc/lib-rt/librt_base64.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import_librt_base64(void)
1313
#else // MYPYC_EXPERIMENTAL
1414

1515
#define LIBRT_BASE64_ABI_VERSION 0
16-
#define LIBRT_BASE64_API_VERSION 0
17-
#define LIBRT_BASE64_API_LEN 3
16+
#define LIBRT_BASE64_API_VERSION 1
17+
#define LIBRT_BASE64_API_LEN 4
1818

1919
static void *LibRTBase64_API[LIBRT_BASE64_API_LEN];
2020

2121
#define LibRTBase64_ABIVersion (*(int (*)(void)) LibRTBase64_API[0])
2222
#define LibRTBase64_APIVersion (*(int (*)(void)) LibRTBase64_API[1])
2323
#define LibRTBase64_b64encode_internal (*(PyObject* (*)(PyObject *source)) LibRTBase64_API[2])
24+
#define LibRTBase64_b64decode_internal (*(PyObject* (*)(PyObject *source)) LibRTBase64_API[3])
2425

2526
static int
2627
import_librt_base64(void)

mypyc/primitives/misc_ops.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from mypyc.ir.ops import ERR_FALSE, ERR_MAGIC, ERR_MAGIC_OVERLAPPING, ERR_NEVER
66
from mypyc.ir.rtypes import (
77
KNOWN_NATIVE_TYPES,
8+
RUnion,
89
bit_rprimitive,
910
bool_rprimitive,
1011
bytes_rprimitive,
@@ -475,3 +476,13 @@
475476
experimental=True,
476477
capsule="librt.base64",
477478
)
479+
480+
function_op(
481+
name="librt.base64.b64decode",
482+
arg_types=[RUnion([bytes_rprimitive, str_rprimitive])],
483+
return_type=bytes_rprimitive,
484+
c_function_name="LibRTBase64_b64decode_internal",
485+
error_kind=ERR_MAGIC,
486+
experimental=True,
487+
capsule="librt.base64",
488+
)

mypyc/test-data/irbuild-base64.test

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
[case testBase64_experimental]
2-
from librt.base64 import b64encode
2+
from librt.base64 import b64encode, b64decode
33

44
def enc(b: bytes) -> bytes:
55
return b64encode(b)
6+
7+
def dec_bytes(b: bytes) -> bytes:
8+
return b64decode(b)
9+
10+
def dec_str(b: str) -> bytes:
11+
return b64decode(b)
612
[out]
713
def enc(b):
814
b, r0 :: bytes
915
L0:
1016
r0 = LibRTBase64_b64encode_internal(b)
1117
return r0
18+
def dec_bytes(b):
19+
b, r0 :: bytes
20+
L0:
21+
r0 = LibRTBase64_b64decode_internal(b)
22+
return r0
23+
def dec_str(b):
24+
b :: str
25+
r0 :: bytes
26+
L0:
27+
r0 = LibRTBase64_b64decode_internal(b)
28+
return r0
1229

1330
[case testBase64ExperimentalDisabled]
1431
from librt.base64 import b64encode

0 commit comments

Comments
 (0)