Make fixes, improvements, and chibicc python bindings

- python now mixes audio 10x faster
- python octal notation is restored
- chibicc now builds code 3x faster
- chibicc now has help documentation
- chibicc can now generate basic python bindings
- linenoise now supports some paredit-like features

See #141
This commit is contained in:
Justine Tunney 2021-10-08 08:11:51 -07:00
parent 28997f3acb
commit 7061c79c22
121 changed files with 5272 additions and 1928 deletions

View file

@ -2,7 +2,6 @@
#define Py_CEVAL_H
#include "libc/bits/likely.h"
#include "libc/dce.h"
#include "libc/log/libfatal.internal.h"
#include "third_party/python/Include/object.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pystate.h"
@ -102,31 +101,34 @@ int Py_GetRecursionLimit(void);
#define _Py_MakeEndRecCheck(x) \
(--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
int _Py_CheckRecursiveCall(const char *);
int Py_EnterRecursiveCall(const char *);
void Py_LeaveRecursiveCall(void);
#ifndef Py_LIMITED_API
extern int _Py_CheckRecursionLimit;
forceinline int Py_EnterRecursiveCall(const char *where) {
const char *rsp, *bot;
if (!IsTiny()) {
if (IsModeDbg()) {
PyThreadState_GET()->recursion_depth++;
return _Py_CheckRecursiveCall(where);
} else {
rsp = __builtin_frame_address(0);
asm(".weak\tape_stack_vaddr\n\t"
"movabs\t$ape_stack_vaddr+32768,%0" : "=r"(bot));
if (UNLIKELY(rsp < bot)) {
PyErr_Format(PyExc_MemoryError, "Stack overflow%s", where);
return -1;
}
}
}
return 0;
}
forceinline void Py_LeaveRecursiveCall(void) {
PyThreadState_GET()->recursion_depth--;
}
int _Py_CheckRecursiveCall(const char *);
#define Py_LeaveRecursiveCall() PyThreadState_GET()->recursion_depth--
#define Py_EnterRecursiveCall(where) \
({ \
int rc = 0; \
const char *rsp, *bot; \
if (!IsTiny()) { \
if (IsModeDbg()) { \
PyThreadState_GET()->recursion_depth++; \
rc = _Py_CheckRecursiveCall(where); \
} else { \
rsp = __builtin_frame_address(0); \
asm(".weak\tape_stack_vaddr\n\t" \
"movabs\t$ape_stack_vaddr+32768,%0" : "=r"(bot)); \
if (UNLIKELY(rsp < bot)) { \
PyErr_Format(PyExc_MemoryError, "Stack overflow%s", where); \
rc = -1; \
} \
} \
} \
rc; \
})
#endif
#define Py_ALLOW_RECURSION \
do { \

View file

@ -6,36 +6,50 @@
#define Py_TOLOWER(c) kToLower[255 & (c)]
#define Py_TOUPPER(c) kToUpper[255 & (c)]
forceinline bool Py_ISDIGIT(unsigned char c) {
return '0' <= c && c <= '9';
}
#define Py_ISDIGIT(C) \
({ \
unsigned char c_ = (C); \
'0' <= c_&& c_ <= '9'; \
})
forceinline bool Py_ISLOWER(unsigned char c) {
return 'a' <= c && c <= 'z';
}
#define Py_ISLOWER(C) \
({ \
unsigned char c_ = (C); \
'a' <= c_&& c_ <= 'z'; \
})
forceinline bool Py_ISUPPER(unsigned char c) {
return 'A' <= c && c <= 'Z';
}
#define Py_ISUPPER(C) \
({ \
unsigned char c_ = (C); \
'A' <= c_&& c_ <= 'Z'; \
})
forceinline bool Py_ISALPHA(unsigned char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
#define Py_ISALPHA(C) \
({ \
unsigned char c_ = (C); \
('A' <= c_ && c_ <= 'Z') || ('a' <= c_ && c_ <= 'z'); \
})
forceinline bool Py_ISALNUM(unsigned char c) {
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') ||
('a' <= c && c <= 'z');
}
#define Py_ISALNUM(C) \
({ \
unsigned char c_ = (C); \
(('0' <= c_ && c_ <= '9') || ('A' <= c_ && c_ <= 'Z') || \
('a' <= c_ && c_ <= 'z')); \
})
forceinline bool Py_ISSPACE(unsigned char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' ||
c == '\v';
}
#define Py_ISSPACE(C) \
({ \
unsigned char c_ = (C); \
(c_ == ' ' || c_ == '\t' || c_ == '\r' || c_ == '\n' || c_ == '\f' || \
c_ == '\v'); \
})
forceinline bool Py_ISXDIGIT(unsigned char c) {
return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') ||
('a' <= c && c <= 'f');
}
#define Py_ISXDIGIT(C) \
({ \
unsigned char c_ = (C); \
(('0' <= c_ && c_ <= '9') || ('A' <= c_ && c_ <= 'F') || \
('a' <= c_ && c_ <= 'f')); \
})
#endif /* !PYCTYPE_H */
#endif /* !Py_LIMITED_API */

View file

@ -490,6 +490,7 @@ class ClassTests(unittest.TestCase):
self.assertRaises(TypeError, hash, C2())
@unittest.skipIf(cosmo.MODE == 'tiny', "no stack awareness in tiny mode")
def testSFBug532646(self):
# Test for SF bug 532646

View file

@ -1331,7 +1331,7 @@ class EscapeDecodeTest(unittest.TestCase):
check(br"[\x410]", b"[A0]")
for i in range(97, 123):
b = bytes([i])
if b not in b'abfnrtvx':
if b not in b'abfnrtvxe': # [jart] support \e
with self.assertWarns(DeprecationWarning):
check(b"\\" + b, b"\\" + b)
with self.assertWarns(DeprecationWarning):
@ -2603,7 +2603,7 @@ class UnicodeEscapeTest(unittest.TestCase):
check(br"\U0001d120", "\U0001d120")
for i in range(97, 123):
b = bytes([i])
if b not in b'abfnrtuvx':
if b not in b'abfnrtuvxe': # [jart] support \e
with self.assertWarns(DeprecationWarning):
check(b"\\" + b, "\\" + chr(i))
if b.upper() not in b'UN':

View file

@ -163,8 +163,12 @@ if 1:
for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
"080000000000000", "000000000000009", "000000000000008",
"0b42", "0BADCAFE", "0o123456789", "0b1.1", "0o4.2",
"0b101j2", "0o153j2", "0b100e1", "0o777e1", "0777",
"000777", "000000000000007"]:
"0b101j2", "0o153j2", "0b100e1", "0o777e1",
# [jart] restore octal
# "0777",
# "000777",
# "000000000000007",
]:
self.assertRaises(SyntaxError, eval, arg)
self.assertEqual(eval("0xff"), 255)

View file

@ -177,7 +177,7 @@ class AutoFileTests:
finally:
os.close(fd)
# @unittest.skipUnless(cosmo.MODE == "dbg", "disabled recursion checking")
@unittest.skipIf(cosmo.MODE == 'tiny', "no stack awareness in tiny mode")
def testRecursiveRepr(self):
# Issue #25455
with swap_attr(self.f, 'name', self.f):

View file

@ -814,6 +814,7 @@ class TestBinaryPlistlib(unittest.TestCase):
b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY))
self.assertIs(b['x'], b)
@unittest.skipIf(cosmo.MODE == 'tiny', "no stack awareness in tiny mode")
def test_deep_nesting(self):
for N in [300, 100000]:
chunks = [b'\xa1' + (i + 1).to_bytes(4, 'big') for i in range(N)]

View file

@ -8,10 +8,10 @@ exit1 = cosmo.exit1
class BooTest(unittest.TestCase):
def test_boo(self):
pass
# cosmo.ftrace()
# chr(33)
# eval('0')
# exit1()
pass
if __name__ == '__main__':
unittest.main()

View file

@ -28,7 +28,6 @@
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "libc/fmt/fmt.h"
#include "libc/log/libfatal.internal.h"
#include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/boolobject.h"
#include "third_party/python/Include/complexobject.h"

View file

@ -1,6 +1,5 @@
#ifndef UMODARITH_H
#define UMODARITH_H
#include "libc/log/libfatal.internal.h"
#include "third_party/python/Modules/_decimal/libmpdec/constants.h"
#include "third_party/python/Modules/_decimal/libmpdec/mpdecimal.h"
#include "third_party/python/Modules/_decimal/libmpdec/typearith.h"

View file

@ -18,7 +18,6 @@
#define PY_SSIZE_T_CLEAN
#include "libc/calls/calls.h"
#include "libc/log/backtrace.internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
@ -27,7 +26,6 @@
#include "third_party/mbedtls/md.h"
#include "third_party/mbedtls/pkcs5.h"
#include "third_party/python/Include/Python.h"
#include "third_party/python/Include/ezprint.h"
#include "third_party/python/Include/import.h"
#include "third_party/python/Include/object.h"
#include "third_party/python/Include/pyerrors.h"

View file

@ -1629,7 +1629,7 @@ getargs_u(PyObject *self, PyObject *args)
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "u", &str))
return NULL;
size = Py_UNICODE_strlen(str);
size = wcslen(str);
return PyUnicode_FromUnicode(str, size);
}
@ -1651,7 +1651,7 @@ getargs_Z(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "Z", &str))
return NULL;
if (str != NULL) {
size = Py_UNICODE_strlen(str);
size = wcslen(str);
return PyUnicode_FromUnicode(str, size);
} else
Py_RETURN_NONE;

View file

@ -849,7 +849,34 @@ audioop_add_impl(PyObject *module, Py_buffer *fragment1,
if (rv == NULL)
return NULL;
ncp = (signed char *)PyBytes_AsString(rv);
for (i = 0; i < fragment1->len; i += width) {
i = 0;
#if defined(__GNUC__) && defined(__SSE2__)
/* [jart] make audio mixing 20x faster */
if (width == 2) {
for (; i + 16 <= fragment1->len; i += 16) {
asm("movups\t%1,%%xmm0\n\t"
"movups\t%2,%%xmm1\n\t"
"paddsw\t%%xmm1,%%xmm0\n\t"
"movups\t%%xmm0,%0"
: "=m"(*(char(*)[16])(ncp + i))
: "m"(*(char(*)[16])((char *)fragment1->buf + i)),
"m"(*(char(*)[16])((char *)fragment2->buf + i))
: "xmm0", "xmm1");
}
} else if (width == 1) {
for (; i + 16 <= fragment1->len; i += 16) {
asm("movups\t%1,%%xmm0\n\t"
"movups\t%2,%%xmm1\n\t"
"paddsb\t%%xmm1,%%xmm0\n\t"
"movups\t%%xmm0,%0"
: "=m"(*(char(*)[16])(ncp + i))
: "m"(*(char(*)[16])((char *)fragment1->buf + i)),
"m"(*(char(*)[16])((char *)fragment2->buf + i))
: "xmm0", "xmm1");
}
}
#endif
for (; i < fragment1->len; i += width) {
int val1 = GETRAWSAMPLE(width, fragment1->buf, i);
int val2 = GETRAWSAMPLE(width, fragment2->buf, i);
if (width < 4) {

View file

@ -19,7 +19,6 @@
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/log/libfatal.internal.h"
#include "libc/macros.internal.h"
#include "libc/runtime/gc.internal.h"
#include "libc/str/str.h"

View file

@ -1193,6 +1193,7 @@ PyObject *_PyBytes_DecodeEscape(const char *s,
case 'n': *p++ = '\n'; break;
case 'r': *p++ = '\r'; break;
case 'v': *p++ = '\013'; break; /* VT */
case 'e': *p++ = '\033'; break; /* [jart] ansi escape */
case 'a': *p++ = '\007'; break; /* BEL, not classic C */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':

View file

@ -0,0 +1,431 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi
Python 3
https://docs.python.org/3/license.html │
*/
#define PY_SSIZE_T_CLEAN
#include "libc/assert.h"
#include "third_party/python/Include/codecs.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymem.h"
#include "third_party/python/Include/unicodeobject.h"
#include "third_party/python/Include/warnings.h"
/* clang-format off */
#define _PyUnicode_STATE(op) \
(((PyASCIIObject *)(op))->state)
int ensure_unicode(PyObject *);
PyObject *unicode_result(PyObject *);
int unicode_check_modifiable(PyObject *);
PyObject *unicode_encode_ucs1(PyObject *, const char *, const Py_UCS4);
PyObject *_PyUnicode_TranslateCharmap(PyObject *, PyObject *, const char *);
/* The max unicode value is always 0x10FFFF while using the PEP-393 API.
This function is kept for backward compatibility with the old API. */
Py_UNICODE
PyUnicode_GetMax(void)
{
#ifdef Py_UNICODE_WIDE
return 0x10FFFF;
#else
/* This is actually an illegal character, so it should
not be passed to unichr. */
return 0xFFFF;
#endif
}
PyObject *
PyUnicode_AsDecodedObject(PyObject *unicode,
const char *encoding,
const char *errors)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsDecodedObject() is deprecated; "
"use PyCodec_Decode() to decode from str", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Decode via the codec registry */
return PyCodec_Decode(unicode, encoding, errors);
}
PyObject *
PyUnicode_AsDecodedUnicode(PyObject *unicode,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsDecodedUnicode() is deprecated; "
"use PyCodec_Decode() to decode from str to str", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Decode via the codec registry */
v = PyCodec_Decode(unicode, encoding, errors);
if (v == NULL)
goto onError;
if (!PyUnicode_Check(v)) {
PyErr_Format(PyExc_TypeError,
"'%.400s' decoder returned '%.400s' instead of 'str'; "
"use codecs.decode() to decode to arbitrary types",
encoding,
Py_TYPE(unicode)->tp_name);
Py_DECREF(v);
goto onError;
}
return unicode_result(v);
onError:
return NULL;
}
PyObject *
PyUnicode_AsEncodedObject(PyObject *unicode,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsEncodedObject() is deprecated; "
"use PyUnicode_AsEncodedString() to encode from str to bytes "
"or PyCodec_Encode() for generic encoding", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Encode via the codec registry */
v = PyCodec_Encode(unicode, encoding, errors);
if (v == NULL)
goto onError;
return v;
onError:
return NULL;
}
PyObject *
PyUnicode_AsEncodedUnicode(PyObject *unicode,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsEncodedUnicode() is deprecated; "
"use PyCodec_Encode() to encode from str to str", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Encode via the codec registry */
v = PyCodec_Encode(unicode, encoding, errors);
if (v == NULL)
goto onError;
if (!PyUnicode_Check(v)) {
PyErr_Format(PyExc_TypeError,
"'%.400s' encoder returned '%.400s' instead of 'str'; "
"use codecs.encode() to encode to arbitrary types",
encoding,
Py_TYPE(v)->tp_name);
Py_DECREF(v);
goto onError;
}
return v;
onError:
return NULL;
}
wchar_t *
_PyUnicode_AsWideCharString(PyObject *unicode)
{
const wchar_t *wstr;
wchar_t *buffer;
Py_ssize_t buflen;
if (unicode == NULL) {
PyErr_BadInternalCall();
return NULL;
}
wstr = PyUnicode_AsUnicodeAndSize(unicode, &buflen);
if (wstr == NULL) {
return NULL;
}
if (wcslen(wstr) != (size_t)buflen) {
PyErr_SetString(PyExc_ValueError,
"embedded null character");
return NULL;
}
buffer = PyMem_NEW(wchar_t, buflen + 1);
if (buffer == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(buffer, wstr, (buflen + 1) * sizeof(wchar_t));
return buffer;
}
const Py_UNICODE *
_PyUnicode_AsUnicode(PyObject *unicode)
{
Py_ssize_t size;
const Py_UNICODE *wstr;
wstr = PyUnicode_AsUnicodeAndSize(unicode, &size);
if (wstr && wcslen(wstr) != (size_t)size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return NULL;
}
return wstr;
}
Py_ssize_t
PyUnicode_GetSize(PyObject *unicode)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
return PyUnicode_GET_SIZE(unicode);
onError:
return -1;
}
int
PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
{
if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
PyErr_BadArgument();
return -1;
}
assert(PyUnicode_IS_READY(unicode));
if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
PyErr_SetString(PyExc_IndexError, "string index out of range");
return -1;
}
if (unicode_check_modifiable(unicode))
return -1;
if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) {
PyErr_SetString(PyExc_ValueError, "character out of range");
return -1;
}
PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
index, ch);
return 0;
}
/* Deprecated */
PyObject *
PyUnicode_EncodeLatin1(const Py_UNICODE *p,
Py_ssize_t size,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (unicode == NULL)
return NULL;
result = unicode_encode_ucs1(unicode, errors, 256);
Py_DECREF(unicode);
return result;
}
/* Deprecated */
PyObject *
PyUnicode_EncodeASCII(const Py_UNICODE *p,
Py_ssize_t size,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (unicode == NULL)
return NULL;
result = unicode_encode_ucs1(unicode, errors, 128);
Py_DECREF(unicode);
return result;
}
PyObject *
PyUnicode_Encode(const Py_UNICODE *s,
Py_ssize_t size,
const char *encoding,
const char *errors)
{
PyObject *v, *unicode;
unicode = PyUnicode_FromUnicode(s, size);
if (unicode == NULL)
return NULL;
v = PyUnicode_AsEncodedString(unicode, encoding, errors);
Py_DECREF(unicode);
return v;
}
/* Deprecated */
PyObject *
PyUnicode_EncodeCharmap(const Py_UNICODE *p,
Py_ssize_t size,
PyObject *mapping,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (unicode == NULL)
return NULL;
result = _PyUnicode_EncodeCharmap(unicode, mapping, errors);
Py_DECREF(unicode);
return result;
}
/* Deprecated. Use PyUnicode_Translate instead. */
PyObject *
PyUnicode_TranslateCharmap(const Py_UNICODE *p,
Py_ssize_t size,
PyObject *mapping,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (!unicode)
return NULL;
result = _PyUnicode_TranslateCharmap(unicode, mapping, errors);
Py_DECREF(unicode);
return result;
}
void
PyUnicode_InternImmortal(PyObject **p)
{
PyUnicode_InternInPlace(p);
if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
_PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL;
Py_INCREF(*p);
}
}
Py_UNICODE*
Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2)
{
Py_UNICODE *u = s1;
while ((*u++ = *s2++));
return s1;
}
Py_UNICODE*
Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
{
Py_UNICODE *u = s1;
while ((*u++ = *s2++))
if (n-- == 0)
break;
return s1;
}
Py_UNICODE*
Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2)
{
Py_UNICODE *u1 = s1;
u1 += Py_UNICODE_strlen(u1);
Py_UNICODE_strcpy(u1, s2);
return s1;
}
int
Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2)
{
while (*s1 && *s2 && *s1 == *s2)
s1++, s2++;
if (*s1 && *s2)
return (*s1 < *s2) ? -1 : +1;
if (*s1)
return 1;
if (*s2)
return -1;
return 0;
}
int
Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
{
Py_UNICODE u1, u2;
for (; n != 0; n--) {
u1 = *s1;
u2 = *s2;
if (u1 != u2)
return (u1 < u2) ? -1 : +1;
if (u1 == '\0')
return 0;
s1++;
s2++;
}
return 0;
}
Py_UNICODE*
Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c)
{
const Py_UNICODE *p;
for (p = s; *p; p++)
if (*p == c)
return (Py_UNICODE*)p;
return NULL;
}
Py_UNICODE*
Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c)
{
const Py_UNICODE *p;
p = s + Py_UNICODE_strlen(s);
while (p != s) {
p--;
if (*p == c)
return (Py_UNICODE*)p;
}
return NULL;
}
size_t
Py_UNICODE_strlen(const Py_UNICODE *u)
{
int res = 0;
while(*u++)
res++;
return res;
}
Py_UNICODE*
PyUnicode_AsUnicodeCopy(PyObject *unicode)
{
Py_UNICODE *u, *copy;
Py_ssize_t len, size;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
u = PyUnicode_AsUnicodeAndSize(unicode, &len);
if (u == NULL)
return NULL;
/* Ensure we won't overflow the size. */
if (len > ((PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(Py_UNICODE)) - 1)) {
PyErr_NoMemory();
return NULL;
}
size = len + 1; /* copy the null character */
size *= sizeof(Py_UNICODE);
copy = PyMem_Malloc(size);
if (copy == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(copy, u, size);
return copy;
}

View file

@ -447,20 +447,6 @@ get_error_handler(const char *errors)
return _Py_ERROR_OTHER;
}
/* The max unicode value is always 0x10FFFF while using the PEP-393 API.
This function is kept for backward compatibility with the old API. */
Py_UNICODE
PyUnicode_GetMax(void)
{
#ifdef Py_UNICODE_WIDE
return 0x10FFFF;
#else
/* This is actually an illegal character, so it should
not be passed to unichr. */
return 0xFFFF;
#endif
}
#ifdef Py_DEBUG
int
_PyUnicode_CheckConsistency(PyObject *op, int check_content)
@ -616,7 +602,6 @@ static PyObject*
unicode_result_ready(PyObject *unicode)
{
Py_ssize_t length;
length = PyUnicode_GET_LENGTH(unicode);
if (length == 0) {
if (unicode != unicode_empty) {
@ -625,7 +610,6 @@ unicode_result_ready(PyObject *unicode)
}
return unicode_empty;
}
if (length == 1) {
void *data = PyUnicode_DATA(unicode);
int kind = PyUnicode_KIND(unicode);
@ -647,12 +631,11 @@ unicode_result_ready(PyObject *unicode)
}
}
}
assert(_PyUnicode_CheckConsistency(unicode, 1));
return unicode;
}
static PyObject*
PyObject*
unicode_result(PyObject *unicode)
{
assert(_PyUnicode_CHECK(unicode));
@ -1471,7 +1454,7 @@ unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end,
}
#endif
static int
int
unicode_check_modifiable(PyObject *unicode)
{
if (!unicode_modifiable(unicode)) {
@ -3176,37 +3159,6 @@ PyUnicode_AsWideCharString(PyObject *unicode,
return buffer;
}
wchar_t*
_PyUnicode_AsWideCharString(PyObject *unicode)
{
const wchar_t *wstr;
wchar_t *buffer;
Py_ssize_t buflen;
if (unicode == NULL) {
PyErr_BadInternalCall();
return NULL;
}
wstr = PyUnicode_AsUnicodeAndSize(unicode, &buflen);
if (wstr == NULL) {
return NULL;
}
if (wcslen(wstr) != (size_t)buflen) {
PyErr_SetString(PyExc_ValueError,
"embedded null character");
return NULL;
}
buffer = PyMem_NEW(wchar_t, buflen + 1);
if (buffer == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(buffer, wstr, (buflen + 1) * sizeof(wchar_t));
return buffer;
}
PyObject *
PyUnicode_FromOrdinal(int ordinal)
{
@ -3409,113 +3361,6 @@ PyUnicode_Decode(const char *s,
return NULL;
}
PyObject *
PyUnicode_AsDecodedObject(PyObject *unicode,
const char *encoding,
const char *errors)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsDecodedObject() is deprecated; "
"use PyCodec_Decode() to decode from str", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Decode via the codec registry */
return PyCodec_Decode(unicode, encoding, errors);
}
PyObject *
PyUnicode_AsDecodedUnicode(PyObject *unicode,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsDecodedUnicode() is deprecated; "
"use PyCodec_Decode() to decode from str to str", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Decode via the codec registry */
v = PyCodec_Decode(unicode, encoding, errors);
if (v == NULL)
goto onError;
if (!PyUnicode_Check(v)) {
PyErr_Format(PyExc_TypeError,
"'%.400s' decoder returned '%.400s' instead of 'str'; "
"use codecs.decode() to decode to arbitrary types",
encoding,
Py_TYPE(unicode)->tp_name);
Py_DECREF(v);
goto onError;
}
return unicode_result(v);
onError:
return NULL;
}
PyObject *
PyUnicode_Encode(const Py_UNICODE *s,
Py_ssize_t size,
const char *encoding,
const char *errors)
{
PyObject *v, *unicode;
unicode = PyUnicode_FromUnicode(s, size);
if (unicode == NULL)
return NULL;
v = PyUnicode_AsEncodedString(unicode, encoding, errors);
Py_DECREF(unicode);
return v;
}
PyObject *
PyUnicode_AsEncodedObject(PyObject *unicode,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsEncodedObject() is deprecated; "
"use PyUnicode_AsEncodedString() to encode from str to bytes "
"or PyCodec_Encode() for generic encoding", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Encode via the codec registry */
v = PyCodec_Encode(unicode, encoding, errors);
if (v == NULL)
goto onError;
return v;
onError:
return NULL;
}
static size_t
wcstombs_errorpos(const wchar_t *wstr)
{
@ -3824,45 +3669,6 @@ PyUnicode_AsEncodedString(PyObject *unicode,
return NULL;
}
PyObject *
PyUnicode_AsEncodedUnicode(PyObject *unicode,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
goto onError;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PyUnicode_AsEncodedUnicode() is deprecated; "
"use PyCodec_Encode() to encode from str to str", 1) < 0)
return NULL;
if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
/* Encode via the codec registry */
v = PyCodec_Encode(unicode, encoding, errors);
if (v == NULL)
goto onError;
if (!PyUnicode_Check(v)) {
PyErr_Format(PyExc_TypeError,
"'%.400s' encoder returned '%.400s' instead of 'str'; "
"use codecs.encode() to encode to arbitrary types",
encoding,
Py_TYPE(v)->tp_name);
Py_DECREF(v);
goto onError;
}
return v;
onError:
return NULL;
}
static size_t
mbstowcs_errorpos(const char *str, size_t len)
{
@ -4335,21 +4141,6 @@ PyUnicode_AsUnicode(PyObject *unicode)
return PyUnicode_AsUnicodeAndSize(unicode, NULL);
}
const Py_UNICODE *
_PyUnicode_AsUnicode(PyObject *unicode)
{
Py_ssize_t size;
const Py_UNICODE *wstr;
wstr = PyUnicode_AsUnicodeAndSize(unicode, &size);
if (wstr && wcslen(wstr) != (size_t)size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return NULL;
}
return wstr;
}
Py_ssize_t
PyUnicode_GetSize(PyObject *unicode)
{
@ -4358,7 +4149,6 @@ PyUnicode_GetSize(PyObject *unicode)
goto onError;
}
return PyUnicode_GET_SIZE(unicode);
onError:
return -1;
}
@ -4397,29 +4187,6 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
return PyUnicode_READ(kind, data, index);
}
int
PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
{
if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
PyErr_BadArgument();
return -1;
}
assert(PyUnicode_IS_READY(unicode));
if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
PyErr_SetString(PyExc_IndexError, "string index out of range");
return -1;
}
if (unicode_check_modifiable(unicode))
return -1;
if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) {
PyErr_SetString(PyExc_ValueError, "character out of range");
return -1;
}
PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
index, ch);
return 0;
}
const char *
PyUnicode_GetDefaultEncoding(void)
{
@ -5063,6 +4830,7 @@ encode_char:
return NULL;
return v;
}
PyObject *
PyUnicode_EncodeUTF7(const Py_UNICODE *s,
Py_ssize_t size,
@ -6169,7 +5937,7 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
assert(writer.pos < writer.size);
switch (c) {
/* \x escapes */
/* \x escapes */
case '\n': continue;
case '\\': WRITE_ASCII_CHAR('\\'); continue;
case '\'': WRITE_ASCII_CHAR('\''); continue;
@ -6184,8 +5952,10 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
case 'v': WRITE_ASCII_CHAR('\013'); continue;
/* BEL, not classic C */
case 'a': WRITE_ASCII_CHAR('\007'); continue;
/* [jart] ansi escape */
case 'e': WRITE_ASCII_CHAR('\033'); continue;
/* \OOO (octal) escapes */
/* \OOO (octal) escapes */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
ch = c - '0';
@ -6464,7 +6234,6 @@ PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,
if (tmp == NULL) {
return NULL;
}
result = PyUnicode_AsUnicodeEscapeString(tmp);
Py_DECREF(tmp);
return result;
@ -6899,7 +6668,7 @@ unicode_encode_call_errorhandler(const char *errors,
return resunicode;
}
static PyObject *
PyObject *
unicode_encode_ucs1(PyObject *unicode,
const char *errors,
const Py_UCS4 limit)
@ -7084,21 +6853,6 @@ unicode_encode_ucs1(PyObject *unicode,
return NULL;
}
/* Deprecated */
PyObject *
PyUnicode_EncodeLatin1(const Py_UNICODE *p,
Py_ssize_t size,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (unicode == NULL)
return NULL;
result = unicode_encode_ucs1(unicode, errors, 256);
Py_DECREF(unicode);
return result;
}
PyObject *
_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors)
{
@ -7225,21 +6979,6 @@ PyUnicode_DecodeASCII(const char *s,
return NULL;
}
/* Deprecated */
PyObject *
PyUnicode_EncodeASCII(const Py_UNICODE *p,
Py_ssize_t size,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (unicode == NULL)
return NULL;
result = unicode_encode_ucs1(unicode, errors, 128);
Py_DECREF(unicode);
return result;
}
PyObject *
_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
{
@ -8075,22 +7814,6 @@ _PyUnicode_EncodeCharmap(PyObject *unicode,
return NULL;
}
/* Deprecated */
PyObject *
PyUnicode_EncodeCharmap(const Py_UNICODE *p,
Py_ssize_t size,
PyObject *mapping,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (unicode == NULL)
return NULL;
result = _PyUnicode_EncodeCharmap(unicode, mapping, errors);
Py_DECREF(unicode);
return result;
}
PyObject *
PyUnicode_AsCharmapString(PyObject *unicode,
PyObject *mapping)
@ -8395,7 +8118,7 @@ exit:
return res;
}
static PyObject *
PyObject *
_PyUnicode_TranslateCharmap(PyObject *input,
PyObject *mapping,
const char *errors)
@ -8412,29 +8135,23 @@ _PyUnicode_TranslateCharmap(PyObject *input,
PyObject *exc = NULL;
int ignore;
int res;
if (mapping == NULL) {
PyErr_BadArgument();
return NULL;
}
if (PyUnicode_READY(input) == -1)
return NULL;
data = (char*)PyUnicode_DATA(input);
kind = PyUnicode_KIND(input);
size = PyUnicode_GET_LENGTH(input);
if (size == 0)
return PyUnicode_FromObject(input);
/* allocate enough for a simple 1:1 translation without
replacements, if we need more, we'll resize */
_PyUnicodeWriter_Init(&writer);
if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1)
goto onError;
ignore = (errors != NULL && strcmp(errors, "ignore") == 0);
if (PyUnicode_READY(input) == -1)
return NULL;
if (PyUnicode_IS_ASCII(input)) {
@ -8449,7 +8166,6 @@ _PyUnicode_TranslateCharmap(PyObject *input,
else {
i = 0;
}
while (i<size) {
/* try to encode it */
int translate;
@ -8459,22 +8175,18 @@ _PyUnicode_TranslateCharmap(PyObject *input,
Py_ssize_t collstart;
Py_ssize_t collend;
Py_UCS4 ch;
ch = PyUnicode_READ(kind, data, i);
translate = charmaptranslate_output(ch, mapping, &writer);
if (translate < 0)
goto onError;
if (translate != 0) {
/* it worked => adjust input pointer */
++i;
continue;
}
/* untranslatable character */
collstart = i;
collend = i+1;
/* find all untranslatable characters */
while (collend < size) {
PyObject *x;
@ -8486,7 +8198,6 @@ _PyUnicode_TranslateCharmap(PyObject *input,
break;
++collend;
}
if (ignore) {
i = collend;
}
@ -8507,7 +8218,6 @@ _PyUnicode_TranslateCharmap(PyObject *input,
Py_XDECREF(exc);
Py_XDECREF(errorHandler);
return _PyUnicodeWriter_Finish(&writer);
onError:
_PyUnicodeWriter_Dealloc(&writer);
Py_XDECREF(exc);
@ -8515,22 +8225,6 @@ _PyUnicode_TranslateCharmap(PyObject *input,
return NULL;
}
/* Deprecated. Use PyUnicode_Translate instead. */
PyObject *
PyUnicode_TranslateCharmap(const Py_UNICODE *p,
Py_ssize_t size,
PyObject *mapping,
const char *errors)
{
PyObject *result;
PyObject *unicode = PyUnicode_FromUnicode(p, size);
if (!unicode)
return NULL;
result = _PyUnicode_TranslateCharmap(unicode, mapping, errors);
Py_DECREF(unicode);
return result;
}
PyObject *
PyUnicode_Translate(PyObject *str,
PyObject *mapping,
@ -8946,7 +8640,6 @@ _PyUnicode_InsertThousandsGrouping(
return count;
}
Py_ssize_t
PyUnicode_Count(PyObject *str,
PyObject *substr,
@ -8957,21 +8650,17 @@ PyUnicode_Count(PyObject *str,
int kind1, kind2;
void *buf1 = NULL, *buf2 = NULL;
Py_ssize_t len1, len2;
if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
return -1;
kind1 = PyUnicode_KIND(str);
kind2 = PyUnicode_KIND(substr);
if (kind1 < kind2)
return 0;
len1 = PyUnicode_GET_LENGTH(str);
len2 = PyUnicode_GET_LENGTH(substr);
ADJUST_INDICES(start, end, len1);
if (end - start < len2)
return 0;
buf1 = PyUnicode_DATA(str);
buf2 = PyUnicode_DATA(substr);
if (kind2 != kind1) {
@ -8979,7 +8668,6 @@ PyUnicode_Count(PyObject *str,
if (!buf2)
goto onError;
}
switch (kind1) {
case PyUnicode_1BYTE_KIND:
if (PyUnicode_IS_ASCII(str) && PyUnicode_IS_ASCII(substr))
@ -9008,10 +8696,8 @@ PyUnicode_Count(PyObject *str,
default:
assert(0); result = 0;
}
if (kind2 != kind1)
PyMem_Free(buf2);
return result;
onError:
if (kind2 != kind1 && buf2)
@ -14751,16 +14437,6 @@ PyUnicode_InternInPlace(PyObject **p)
_PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL;
}
void
PyUnicode_InternImmortal(PyObject **p)
{
PyUnicode_InternInPlace(p);
if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
_PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL;
Py_INCREF(*p);
}
}
PyObject *
PyUnicode_InternFromString(const char *cp)
{
@ -14771,14 +14447,13 @@ PyUnicode_InternFromString(const char *cp)
return s;
}
void
relegated void
_Py_ReleaseInternedUnicodeStrings(void)
{
PyObject *keys;
PyObject *s;
Py_ssize_t i, n;
Py_ssize_t immortal_size = 0, mortal_size = 0;
if (interned == NULL || !PyDict_Check(interned))
return;
keys = PyDict_Keys(interned);
@ -14786,12 +14461,10 @@ _Py_ReleaseInternedUnicodeStrings(void)
PyErr_Clear();
return;
}
/* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak
detector, interned unicode strings are not forcibly deallocated;
rather, we give them their stolen references back, and then clear
and DECREF the interned dict. */
n = PyList_GET_SIZE(keys);
fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n",
n);
@ -14826,7 +14499,6 @@ _Py_ReleaseInternedUnicodeStrings(void)
Py_CLEAR(interned);
}
/********************* Unicode Iterator **************************/
typedef struct {
@ -14985,126 +14657,6 @@ unicode_iter(PyObject *seq)
return (PyObject *)it;
}
size_t
Py_UNICODE_strlen(const Py_UNICODE *u)
{
int res = 0;
while(*u++)
res++;
return res;
}
Py_UNICODE*
Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2)
{
Py_UNICODE *u = s1;
while ((*u++ = *s2++));
return s1;
}
Py_UNICODE*
Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
{
Py_UNICODE *u = s1;
while ((*u++ = *s2++))
if (n-- == 0)
break;
return s1;
}
Py_UNICODE*
Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2)
{
Py_UNICODE *u1 = s1;
u1 += Py_UNICODE_strlen(u1);
Py_UNICODE_strcpy(u1, s2);
return s1;
}
int
Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2)
{
while (*s1 && *s2 && *s1 == *s2)
s1++, s2++;
if (*s1 && *s2)
return (*s1 < *s2) ? -1 : +1;
if (*s1)
return 1;
if (*s2)
return -1;
return 0;
}
int
Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
{
Py_UNICODE u1, u2;
for (; n != 0; n--) {
u1 = *s1;
u2 = *s2;
if (u1 != u2)
return (u1 < u2) ? -1 : +1;
if (u1 == '\0')
return 0;
s1++;
s2++;
}
return 0;
}
Py_UNICODE*
Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c)
{
const Py_UNICODE *p;
for (p = s; *p; p++)
if (*p == c)
return (Py_UNICODE*)p;
return NULL;
}
Py_UNICODE*
Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c)
{
const Py_UNICODE *p;
p = s + Py_UNICODE_strlen(s);
while (p != s) {
p--;
if (*p == c)
return (Py_UNICODE*)p;
}
return NULL;
}
Py_UNICODE*
PyUnicode_AsUnicodeCopy(PyObject *unicode)
{
Py_UNICODE *u, *copy;
Py_ssize_t len, size;
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
return NULL;
}
u = PyUnicode_AsUnicodeAndSize(unicode, &len);
if (u == NULL)
return NULL;
/* Ensure we won't overflow the size. */
if (len > ((PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(Py_UNICODE)) - 1)) {
PyErr_NoMemory();
return NULL;
}
size = len + 1; /* copy the null character */
size *= sizeof(Py_UNICODE);
copy = PyMem_Malloc(size);
if (copy == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(copy, u, size);
return copy;
}
/* A _string module, to export formatter_parser and formatter_field_name_split
to the string.Formatter class implemented in Python. */

View file

@ -1702,7 +1702,7 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
} while (c == '_');
}
else {
int nonzero = 0;
int nonoctal = 0;
/* maybe old-style octal; c is first char of it */
/* in any case, allow '0' as a literal */
while (1) {
@ -1719,8 +1719,25 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
}
c = tok_nextc(tok);
}
/* [jart] restore octal */
if ('1' <= c && c <= '7') {
while (1) {
if (c == '_') {
c = tok_nextc(tok);
if (!('0' <= c && c <= '7')) {
tok->done = E_TOKEN;
tok_backup(tok, c);
return ERRORTOKEN;
}
}
if (!('0' <= c && c <= '7')) {
break;
}
c = tok_nextc(tok);
}
}
if (isdigit(c)) {
nonzero = 1;
nonoctal = 1;
c = tok_decimal_tail(tok);
if (c == 0) {
return ERRORTOKEN;
@ -1736,8 +1753,7 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
else if (c == 'j' || c == 'J') {
goto imaginary;
}
else if (nonzero) {
/* Old-style octal: now disallowed. */
else if (nonoctal) {
tok->done = E_TOKEN;
tok_backup(tok, c);
return ERRORTOKEN;

View file

@ -35,6 +35,7 @@
#include "third_party/python/Include/moduleobject.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pyport.h"
#include "third_party/python/Include/yoink.h"
#include "third_party/xed/x86.h"
/* clang-format off */
@ -129,7 +130,7 @@ static PyObject *
cosmo_ftrace(PyObject *self, PyObject *noargs)
{
ftrace_install();
return Py_None;
Py_RETURN_NONE;
}
PyDoc_STRVAR(crc32c_doc,

View file

@ -968,7 +968,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
*p = PyUnicode_AsUnicodeAndSize(arg, &len);
if (*p == NULL)
RETURN_ERR_OCCURRED;
if (Py_UNICODE_strlen(*p) != (size_t)len) {
if (wcslen(*p) != (size_t)len) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
RETURN_ERR_OCCURRED;
}

View file

@ -301,7 +301,7 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
}
else {
if (n < 0)
n = Py_UNICODE_strlen(u);
n = wcslen(u);
v = PyUnicode_FromUnicode(u, n);
}
return v;

View file

@ -137,11 +137,15 @@ PyOS_strtoul(const char *str, char **ptr, int base)
/* skip all zeroes... */
while (*str == '0')
++str;
while (Py_ISSPACE(Py_CHARMASK(*str)))
++str;
if (ptr)
*ptr = (char *)str;
return 0;
if ('0' <= *str && *str <= '7') {
base = 8; /* [jart] restore octal */
} else {
while (Py_ISSPACE(Py_CHARMASK(*str)))
++str;
if (ptr)
*ptr = (char *)str;
return 0;
}
}
}
else

32
third_party/python/Python/recursive.c vendored Normal file
View file

@ -0,0 +1,32 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "third_party/python/Include/ceval.h"
/* clang-format off */
int
(Py_EnterRecursiveCall)(const char *where)
{
return Py_EnterRecursiveCall(where);
}
void
(Py_LeaveRecursiveCall)(void)
{
Py_LeaveRecursiveCall();
}

View file

@ -11,6 +11,8 @@ LICENSE
LOCAL CHANGES
- Restore octal notation
- Support \e → 033 escapes
- Undiamond #include lines
- Support zipos file loading
- Make Python binaries work on six operating systems

3
third_party/python/chibicc.inc vendored Normal file
View file

@ -0,0 +1,3 @@
#define Py_LIMITED_API
#define PY_SSIZE_T_CLEAN
#include "third_party/python/Include/Python.h"

View file

@ -24,7 +24,6 @@
#include "libc/elf/def.h"
#include "libc/fmt/conv.h"
#include "libc/log/check.h"
#include "libc/log/libfatal.internal.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"

View file

@ -204,6 +204,7 @@ THIRD_PARTY_PYTHON_HDRS = \
third_party/python/pyconfig.h
THIRD_PARTY_PYTHON_INCS = \
third_party/python/chibicc.inc \
third_party/python/Objects/stringlib/localeutil.inc \
third_party/python/Objects/stringlib/unicodedefs.inc \
third_party/python/Objects/stringlib/replace.inc \
@ -509,6 +510,7 @@ THIRD_PARTY_PYTHON_STAGE2_A_SRCS = \
third_party/python/repl.c \
third_party/python/launch.c \
third_party/python/Objects/fromfd.c \
third_party/python/Objects/unicodeobject-deadcode.c \
third_party/python/Modules/_bisectmodule.c \
third_party/python/Modules/_bz2module.c \
third_party/python/Modules/_codecsmodule.c \
@ -695,6 +697,7 @@ THIRD_PARTY_PYTHON_STAGE2_A_SRCS = \
third_party/python/Parser/metagrammar.c \
third_party/python/Parser/pgen.c \
third_party/python/Python/dynamic_annotations.c \
third_party/python/Python/recursive.c \
third_party/python/Python/frozen.c \
third_party/python/Python/frozenmain.c \
third_party/python/Python/getopt.c \
@ -4214,6 +4217,103 @@ o/$(MODE)/third_party/python/freeze.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
.PRECIOUS: o/$(MODE)/third_party/python/chibicc.inc
o/$(MODE)/third_party/python/chibicc.inc: \
third_party/python/chibicc.inc \
libc/assert.h \
libc/bits/likely.h \
libc/calls/struct/stat.h \
libc/calls/struct/timespec.h \
libc/dce.h \
libc/errno.h \
libc/fmt/fmts.h \
libc/fmt/pflink.h \
libc/integral/c.inc \
libc/integral/lp64arg.inc \
libc/integral/normalize.inc \
libc/limits.h \
libc/math.h \
libc/mem/mem.h \
libc/nexgen32e/kcpuids.h \
libc/runtime/runtime.h \
libc/runtime/symbolic.h \
libc/runtime/valist.h \
libc/stdio/stdio.h \
libc/str/str.h \
libc/unicode/unicode.h \
third_party/python/Include/Python.h \
third_party/python/Include/abstract.h \
third_party/python/Include/bltinmodule.h \
third_party/python/Include/boolobject.h \
third_party/python/Include/bytearrayobject.h \
third_party/python/Include/bytesobject.h \
third_party/python/Include/cellobject.h \
third_party/python/Include/ceval.h \
third_party/python/Include/classobject.h \
third_party/python/Include/code.h \
third_party/python/Include/codecs.h \
third_party/python/Include/compile.h \
third_party/python/Include/complexobject.h \
third_party/python/Include/descrobject.h \
third_party/python/Include/dictobject.h \
third_party/python/Include/dtoa.h \
third_party/python/Include/dynamic_annotations.h \
third_party/python/Include/enumobject.h \
third_party/python/Include/eval.h \
third_party/python/Include/fileobject.h \
third_party/python/Include/fileutils.h \
third_party/python/Include/floatobject.h \
third_party/python/Include/funcobject.h \
third_party/python/Include/genobject.h \
third_party/python/Include/import.h \
third_party/python/Include/intrcheck.h \
third_party/python/Include/iterobject.h \
third_party/python/Include/listobject.h \
third_party/python/Include/longintrepr.h \
third_party/python/Include/longobject.h \
third_party/python/Include/memoryobject.h \
third_party/python/Include/methodobject.h \
third_party/python/Include/modsupport.h \
third_party/python/Include/moduleobject.h \
third_party/python/Include/namespaceobject.h \
third_party/python/Include/object.h \
third_party/python/Include/objimpl.h \
third_party/python/Include/odictobject.h \
third_party/python/Include/op.h \
third_party/python/Include/osmodule.h \
third_party/python/Include/patchlevel.h \
third_party/python/Include/pyarena.h \
third_party/python/Include/pyatomic.h \
third_party/python/Include/pycapsule.h \
third_party/python/Include/pyctype.h \
third_party/python/Include/pydebug.h \
third_party/python/Include/pyerrors.h \
third_party/python/Include/pyfpe.h \
third_party/python/Include/pyhash.h \
third_party/python/Include/pylifecycle.h \
third_party/python/Include/pymacro.h \
third_party/python/Include/pymath.h \
third_party/python/Include/pymem.h \
third_party/python/Include/pyport.h \
third_party/python/Include/pystate.h \
third_party/python/Include/pystrcmp.h \
third_party/python/Include/pystrtod.h \
third_party/python/Include/pythonrun.h \
third_party/python/Include/pytime.h \
third_party/python/Include/rangeobject.h \
third_party/python/Include/setobject.h \
third_party/python/Include/sliceobject.h \
third_party/python/Include/structseq.h \
third_party/python/Include/sysmodule.h \
third_party/python/Include/traceback.h \
third_party/python/Include/tupleobject.h \
third_party/python/Include/typeslots.h \
third_party/python/Include/unicodeobject.h \
third_party/python/Include/warnings.h \
third_party/python/Include/weakrefobject.h \
third_party/python/pyconfig.h
@$(COMPILE) -ACHECK.h $(COMPILE.c) -xc -E -P -fdirectives-only -dD -D__chibicc__ -o $@ $<
################################################################################
# HELLO.COM