Skip to content

Commit 588d6c6

Browse files
committed
Sync mypy: base64 is non-experimental now
1 parent 80dcab7 commit 588d6c6

41 files changed

Lines changed: 5999 additions & 694 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib-rt/CPy.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op);
149149
CPyTagged CPyTagged_Rshift_(CPyTagged left, CPyTagged right);
150150
CPyTagged CPyTagged_Lshift_(CPyTagged left, CPyTagged right);
151151
CPyTagged CPyTagged_BitLength(CPyTagged self);
152+
PyObject *CPyTagged_ToBytes(CPyTagged self, Py_ssize_t length, PyObject *byteorder, int signed_flag);
153+
PyObject *CPyTagged_ToBigEndianBytes(CPyTagged self, Py_ssize_t length, int signed_flag);
154+
PyObject *CPyTagged_ToLittleEndianBytes(CPyTagged self, Py_ssize_t length, int signed_flag);
152155

153156
PyObject *CPyTagged_Str(CPyTagged n);
154157
CPyTagged CPyTagged_FromFloat(double f);
@@ -605,8 +608,8 @@ static void CPy_DecRef(PyObject *p) {
605608
}
606609

607610
CPy_NOINLINE
608-
static void CPy_XDecRef(PyObject *p) {
609-
CPy_XDECREF(p);
611+
static void CPy_XDecRef(void *p) {
612+
CPy_XDECREF((PyObject *)p);
610613
}
611614

612615
static inline CPyTagged CPyObject_Size(PyObject *obj) {
@@ -790,7 +793,7 @@ PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter);
790793
CPyTagged CPyBytes_Ord(PyObject *obj);
791794
PyObject *CPyBytes_Multiply(PyObject *bytes, CPyTagged count);
792795
int CPyBytes_Startswith(PyObject *self, PyObject *subobj);
793-
796+
int CPyBytes_Endswith(PyObject *self, PyObject *subobj);
794797
int CPyBytes_Compare(PyObject *left, PyObject *right);
795798

796799

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "libbase64.h"
66
#include "pythoncapi_compat.h"
77

8-
#ifdef MYPYC_EXPERIMENTAL
9-
108
static PyObject *
119
b64decode_handle_invalid_input(
1210
PyObject *out_bytes, char *outbuf, size_t max_out, const char *src, size_t srclen, bool freesrc);
@@ -327,20 +325,14 @@ urlsafe_b64decode(PyObject *self, PyObject *const *args, size_t nargs) {
327325
return b64decode_internal(args[0], true);
328326
}
329327

330-
#endif
331-
332328
static PyMethodDef librt_base64_module_methods[] = {
333-
#ifdef MYPYC_EXPERIMENTAL
334329
{"b64encode", (PyCFunction)b64encode, METH_FASTCALL, PyDoc_STR("Encode bytes object using Base64.")},
335330
{"b64decode", (PyCFunction)b64decode, METH_FASTCALL, PyDoc_STR("Decode a Base64 encoded bytes object or ASCII string.")},
336331
{"urlsafe_b64encode", (PyCFunction)urlsafe_b64encode, METH_FASTCALL, PyDoc_STR("Encode bytes object using URL and file system safe Base64 alphabet.")},
337332
{"urlsafe_b64decode", (PyCFunction)urlsafe_b64decode, METH_FASTCALL, PyDoc_STR("Decode bytes or ASCII string using URL and file system safe Base64 alphabet.")},
338-
#endif
339333
{NULL, NULL, 0, NULL}
340334
};
341335

342-
#ifdef MYPYC_EXPERIMENTAL
343-
344336
static int
345337
base64_abi_version(void) {
346338
return LIBRT_BASE64_ABI_VERSION;
@@ -351,12 +343,9 @@ base64_api_version(void) {
351343
return LIBRT_BASE64_API_VERSION;
352344
}
353345

354-
#endif
355-
356346
static int
357347
librt_base64_module_exec(PyObject *m)
358348
{
359-
#ifdef MYPYC_EXPERIMENTAL
360349
// Export mypy internal C API, be careful with the order!
361350
static void *base64_api[LIBRT_BASE64_API_LEN] = {
362351
(void *)base64_abi_version,
@@ -368,7 +357,6 @@ librt_base64_module_exec(PyObject *m)
368357
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
369358
return -1;
370359
}
371-
#endif
372360
return 0;
373361
}
374362

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
#ifndef LIBRT_BASE64_H
22
#define LIBRT_BASE64_H
33

4-
#ifndef MYPYC_EXPERIMENTAL
5-
6-
static int
7-
import_librt_base64(void)
8-
{
9-
// All librt.base64 features are experimental for now, so don't set up the API here
10-
return 0;
11-
}
12-
13-
#else // MYPYC_EXPERIMENTAL
14-
154
#include <Python.h>
165

176
#define LIBRT_BASE64_ABI_VERSION 1
@@ -58,6 +47,4 @@ import_librt_base64(void)
5847
return 0;
5948
}
6049

61-
#endif // MYPYC_EXPERIMENTAL
62-
6350
#endif // LIBRT_BASE64_H

lib-rt/bytes_ops.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,36 @@ int CPyBytes_Startswith(PyObject *self, PyObject *subobj) {
183183
}
184184
return ret;
185185
}
186+
187+
int CPyBytes_Endswith(PyObject *self, PyObject *subobj) {
188+
if (PyBytes_CheckExact(self) && PyBytes_CheckExact(subobj)) {
189+
if (self == subobj) {
190+
return 1;
191+
}
192+
193+
Py_ssize_t subobj_len = PyBytes_GET_SIZE(subobj);
194+
if (subobj_len == 0) {
195+
return 1;
196+
}
197+
198+
Py_ssize_t self_len = PyBytes_GET_SIZE(self);
199+
if (subobj_len > self_len) {
200+
return 0;
201+
}
202+
203+
const char *self_buf = PyBytes_AS_STRING(self);
204+
const char *subobj_buf = PyBytes_AS_STRING(subobj);
205+
206+
return memcmp(self_buf + (self_len - subobj_len), subobj_buf, (size_t)subobj_len) == 0 ? 1 : 0;
207+
}
208+
PyObject *result = PyObject_CallMethodOneArg(self, mypyc_interned_str.endswith, subobj);
209+
if (result == NULL) {
210+
return 2;
211+
}
212+
int ret = PyObject_IsTrue(result);
213+
Py_DECREF(result);
214+
if (ret < 0) {
215+
return 2;
216+
}
217+
return ret;
218+
}

lib-rt/byteswriter_extra_ops.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ char CPyBytesWriter_Write(PyObject *obj, PyObject *value) {
3232
return CPY_NONE;
3333
}
3434

35+
void CPyBytes_ReadError(int64_t index, Py_ssize_t size) {
36+
if (index < 0) {
37+
PyErr_SetString(PyExc_ValueError, "index must be non-negative");
38+
} else {
39+
PyErr_Format(PyExc_IndexError,
40+
"index %lld out of range for bytes of length %zd",
41+
(long long)index, size);
42+
}
43+
}
44+
3545
#endif // MYPYC_EXPERIMENTAL

0 commit comments

Comments
 (0)