@@ -601,20 +601,37 @@ builtin_chr_impl(PyModuleDef *module, int i)
601601
602602
603603static const char *
604- source_as_string (PyObject * cmd , const char * funcname , const char * what , PyCompilerFlags * cf , Py_buffer * view )
604+ source_as_string (PyObject * cmd , const char * funcname , const char * what , PyCompilerFlags * cf , PyObject * * cmd_copy )
605605{
606606 const char * str ;
607607 Py_ssize_t size ;
608+ Py_buffer view ;
608609
610+ * cmd_copy = NULL ;
609611 if (PyUnicode_Check (cmd )) {
610612 cf -> cf_flags |= PyCF_IGNORE_COOKIE ;
611613 str = PyUnicode_AsUTF8AndSize (cmd , & size );
612614 if (str == NULL )
613615 return NULL ;
614616 }
615- else if (PyObject_GetBuffer (cmd , view , PyBUF_SIMPLE ) == 0 ) {
616- str = (const char * )view -> buf ;
617- size = view -> len ;
617+ else if (PyBytes_Check (cmd )) {
618+ str = PyBytes_AS_STRING (cmd );
619+ size = PyBytes_GET_SIZE (cmd );
620+ }
621+ else if (PyByteArray_Check (cmd )) {
622+ str = PyByteArray_AS_STRING (cmd );
623+ size = PyByteArray_GET_SIZE (cmd );
624+ }
625+ else if (PyObject_GetBuffer (cmd , & view , PyBUF_SIMPLE ) == 0 ) {
626+ /* Copy to NUL-terminated buffer. */
627+ * cmd_copy = PyBytes_FromStringAndSize (
628+ (const char * )view .buf , view .len );
629+ PyBuffer_Release (& view );
630+ if (* cmd_copy == NULL ) {
631+ return NULL ;
632+ }
633+ str = PyBytes_AS_STRING (* cmd_copy );
634+ size = PyBytes_GET_SIZE (* cmd_copy );
618635 }
619636 else {
620637 PyErr_Format (PyExc_TypeError ,
@@ -626,7 +643,7 @@ source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompil
626643 if (strlen (str ) != (size_t )size ) {
627644 PyErr_SetString (PyExc_ValueError ,
628645 "source code string cannot contain null bytes" );
629- PyBuffer_Release ( view );
646+ Py_CLEAR ( * cmd_copy );
630647 return NULL ;
631648 }
632649 return str ;
@@ -662,7 +679,7 @@ builtin_compile_impl(PyModuleDef *module, PyObject *source,
662679 int dont_inherit , int optimize )
663680/*[clinic end generated code: output=31881762c1bb90c4 input=9d53e8cfb3c86414]*/
664681{
665- Py_buffer view = { NULL , NULL } ;
682+ PyObject * source_copy ;
666683 const char * str ;
667684 int compile_mode = -1 ;
668685 int is_ast ;
@@ -734,12 +751,12 @@ builtin_compile_impl(PyModuleDef *module, PyObject *source,
734751 goto finally ;
735752 }
736753
737- str = source_as_string (source , "compile" , "string, bytes or AST" , & cf , & view );
754+ str = source_as_string (source , "compile" , "string, bytes or AST" , & cf , & source_copy );
738755 if (str == NULL )
739756 goto error ;
740757
741758 result = Py_CompileStringObject (str , filename , start [compile_mode ], & cf , optimize );
742- PyBuffer_Release ( & view );
759+ Py_XDECREF ( source_copy );
743760 goto finally ;
744761
745762error :
@@ -816,8 +833,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
816833/*[clinic end generated code: output=7284501fb7b4d666 input=89d323839395e49d]*/
817834{
818835 STACKLESS_GETARG ();
819- PyObject * result , * tmp = NULL ;
820- Py_buffer view = {NULL , NULL };
836+ PyObject * result , * source_copy ;
821837 const char * str ;
822838 PyCompilerFlags cf ;
823839
@@ -866,7 +882,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
866882 }
867883
868884 cf .cf_flags = PyCF_SOURCE_IS_UTF8 ;
869- str = source_as_string (source , "eval" , "string, bytes or code" , & cf , & view );
885+ str = source_as_string (source , "eval" , "string, bytes or code" , & cf , & source_copy );
870886 if (str == NULL )
871887 return NULL ;
872888
@@ -877,8 +893,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
877893 STACKLESS_PROMOTE_ALL ();
878894 result = PyRun_StringFlags (str , Py_eval_input , globals , locals , & cf );
879895 STACKLESS_ASSERT ();
880- PyBuffer_Release (& view );
881- Py_XDECREF (tmp );
896+ Py_XDECREF (source_copy );
882897 return result ;
883898}
884899
@@ -953,12 +968,13 @@ builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
953968 STACKLESS_ASSERT ();
954969 }
955970 else {
956- Py_buffer view = { NULL , NULL } ;
971+ PyObject * source_copy ;
957972 const char * str ;
958973 PyCompilerFlags cf ;
959974 cf .cf_flags = PyCF_SOURCE_IS_UTF8 ;
960975 str = source_as_string (source , "exec" ,
961- "string, bytes or code" , & cf , & view );
976+ "string, bytes or code" , & cf ,
977+ & source_copy );
962978 if (str == NULL )
963979 return NULL ;
964980 STACKLESS_PROMOTE_ALL ();
@@ -968,7 +984,7 @@ builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
968984 else
969985 v = PyRun_String (str , Py_file_input , globals , locals );
970986 STACKLESS_ASSERT ();
971- PyBuffer_Release ( & view );
987+ Py_XDECREF ( source_copy );
972988 }
973989 if (v == NULL )
974990 return NULL ;
0 commit comments