with self.assertRaises(TypeError):
increment_count(1, 2, 3)
+ def test_Py_CompileString(self):
+ # Check that Py_CompileString respects the coding cookie
+ _compile = _testcapi.Py_CompileString
+ code = b"# -*- coding: latin1 -*-\nprint('\xc2\xa4')\n"
+ result = _compile(code)
+ expected = compile(code, "<string>", "exec")
+ self.assertEqual(result.co_consts, expected.co_consts)
+
if __name__ == "__main__":
unittest.main()
PyType_GenericNew, /* tp_new */
};
+static PyObject*
+pycompilestring(PyObject* self, PyObject *obj) {
+ if (PyBytes_CheckExact(obj) == 0) {
+ PyErr_SetString(PyExc_ValueError, "Argument must be a bytes object");
+ return NULL;
+ }
+ const char *the_string = PyBytes_AsString(obj);
+ if (the_string == NULL) {
+ return NULL;
+ }
+ return Py_CompileString(the_string, "blech", Py_file_input);
+}
+
static PyObject*
test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
{
{"return_null_without_error", return_null_without_error, METH_NOARGS},
{"return_result_with_error", return_result_with_error, METH_NOARGS},
{"getitem_with_error", getitem_with_error, METH_VARARGS},
+ {"Py_CompileString", pycompilestring, METH_O},
{"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
{"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
int exec_input = start_rule == Py_file_input;
struct tok_state *tok;
- if (flags == NULL || flags->cf_flags & PyCF_IGNORE_COOKIE) {
+ if (flags != NULL && flags->cf_flags & PyCF_IGNORE_COOKIE) {
tok = _PyTokenizer_FromUTF8(str, exec_input);
} else {
tok = _PyTokenizer_FromString(str, exec_input);