Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_type_comments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
import sys
import unittest
from test.support import import_helper


funcdef = """\
Expand Down Expand Up @@ -391,6 +392,13 @@ def check_both_ways(source):
check_both_ways("pass # type: ignorewhatever\n")
check_both_ways("pass # type: ignoreé\n")

def test_non_utf8_type_comment_with_ignore_cookie(self):
_testcapi = import_helper.import_module('_testcapi')
flags = 0x0800 | 0x1000 # PyCF_IGNORE_COOKIE | PyCF_TYPE_COMMENTS
with self.assertRaises(UnicodeDecodeError):
_testcapi.Py_CompileStringExFlags(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need _testcapi? Was the error not reproducible with a direct call to e.g. compile()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't pass PyCF_IGNORE_COOKIE to compile() (or any other Python API).

>>> compile(b'a=1 # type: \x80', '<test>', 'single', flags=0x0800|0x1000)                                                                       
ValueError: compile(): unrecognised flags

Since this is the function used in the fuzzer, (I hope) it may be useful for future issues found by it.

b"a=1 # type: \x80", "<test>", 256, flags)

def test_func_type_input(self):

def parse_func_type_input(source):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an unlikely crash in the parser when certain errors were erroneously not
propagated. Found by OSS Fuzz in :oss-fuzz:`491369109`.
13 changes: 13 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ pycompilestring(PyObject* self, PyObject *obj) {
return Py_CompileString(the_string, "<string>", Py_file_input);
}

static PyObject*
pycompilestringexflags(PyObject *self, PyObject *args) {
const char *the_string, *filename;
int start, flags;
if (!PyArg_ParseTuple(args, "ysii", &the_string, &filename, &start, &flags)) {
return NULL;
}
PyCompilerFlags cf = _PyCompilerFlags_INIT;
cf.cf_flags = flags;
return Py_CompileStringExFlags(the_string, filename, start, &cf, -1);
}

static PyObject*
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -2659,6 +2671,7 @@ static PyMethodDef TestMethods[] = {
{"return_result_with_error", return_result_with_error, METH_NOARGS},
{"getitem_with_error", getitem_with_error, METH_VARARGS},
{"Py_CompileString", pycompilestring, METH_O},
{"Py_CompileStringExFlags", pycompilestringexflags, METH_VARARGS},
{"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
{"stack_pointer", stack_pointer, METH_NOARGS},
#ifdef W_STOPCODE
Expand Down
Loading
Loading