@@ -599,20 +599,37 @@ builtin_chr_impl(PyModuleDef *module, int i)
599599
600600
601601static const char *
602- source_as_string (PyObject * cmd , const char * funcname , const char * what , PyCompilerFlags * cf , Py_buffer * view )
602+ source_as_string (PyObject * cmd , const char * funcname , const char * what , PyCompilerFlags * cf , PyObject * * cmd_copy )
603603{
604604 const char * str ;
605605 Py_ssize_t size ;
606+ Py_buffer view ;
606607
608+ * cmd_copy = NULL ;
607609 if (PyUnicode_Check (cmd )) {
608610 cf -> cf_flags |= PyCF_IGNORE_COOKIE ;
609611 str = PyUnicode_AsUTF8AndSize (cmd , & size );
610612 if (str == NULL )
611613 return NULL ;
612614 }
613- else if (PyObject_GetBuffer (cmd , view , PyBUF_SIMPLE ) == 0 ) {
614- str = (const char * )view -> buf ;
615- size = view -> len ;
615+ else if (PyBytes_Check (cmd )) {
616+ str = PyBytes_AS_STRING (cmd );
617+ size = PyBytes_GET_SIZE (cmd );
618+ }
619+ else if (PyByteArray_Check (cmd )) {
620+ str = PyByteArray_AS_STRING (cmd );
621+ size = PyByteArray_GET_SIZE (cmd );
622+ }
623+ else if (PyObject_GetBuffer (cmd , & view , PyBUF_SIMPLE ) == 0 ) {
624+ /* Copy to NUL-terminated buffer. */
625+ * cmd_copy = PyBytes_FromStringAndSize (
626+ (const char * )view .buf , view .len );
627+ PyBuffer_Release (& view );
628+ if (* cmd_copy == NULL ) {
629+ return NULL ;
630+ }
631+ str = PyBytes_AS_STRING (* cmd_copy );
632+ size = PyBytes_GET_SIZE (* cmd_copy );
616633 }
617634 else {
618635 PyErr_Format (PyExc_TypeError ,
@@ -624,7 +641,7 @@ source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompil
624641 if (strlen (str ) != (size_t )size ) {
625642 PyErr_SetString (PyExc_ValueError ,
626643 "source code string cannot contain null bytes" );
627- PyBuffer_Release ( view );
644+ Py_CLEAR ( * cmd_copy );
628645 return NULL ;
629646 }
630647 return str ;
@@ -660,7 +677,7 @@ builtin_compile_impl(PyModuleDef *module, PyObject *source,
660677 int dont_inherit , int optimize )
661678/*[clinic end generated code: output=31881762c1bb90c4 input=9d53e8cfb3c86414]*/
662679{
663- Py_buffer view = { NULL , NULL } ;
680+ PyObject * source_copy ;
664681 const char * str ;
665682 int compile_mode = -1 ;
666683 int is_ast ;
@@ -732,12 +749,12 @@ builtin_compile_impl(PyModuleDef *module, PyObject *source,
732749 goto finally ;
733750 }
734751
735- str = source_as_string (source , "compile" , "string, bytes or AST" , & cf , & view );
752+ str = source_as_string (source , "compile" , "string, bytes or AST" , & cf , & source_copy );
736753 if (str == NULL )
737754 goto error ;
738755
739756 result = Py_CompileStringObject (str , filename , start [compile_mode ], & cf , optimize );
740- PyBuffer_Release ( & view );
757+ Py_XDECREF ( source_copy );
741758 goto finally ;
742759
743760error :
@@ -812,8 +829,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
812829 PyObject * locals )
813830/*[clinic end generated code: output=7284501fb7b4d666 input=11ee718a8640e527]*/
814831{
815- PyObject * result , * tmp = NULL ;
816- Py_buffer view = {NULL , NULL };
832+ PyObject * result , * source_copy ;
817833 const char * str ;
818834 PyCompilerFlags cf ;
819835
@@ -861,7 +877,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
861877 }
862878
863879 cf .cf_flags = PyCF_SOURCE_IS_UTF8 ;
864- str = source_as_string (source , "eval" , "string, bytes or code" , & cf , & view );
880+ str = source_as_string (source , "eval" , "string, bytes or code" , & cf , & source_copy );
865881 if (str == NULL )
866882 return NULL ;
867883
@@ -870,8 +886,7 @@ builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
870886
871887 (void )PyEval_MergeCompilerFlags (& cf );
872888 result = PyRun_StringFlags (str , Py_eval_input , globals , locals , & cf );
873- PyBuffer_Release (& view );
874- Py_XDECREF (tmp );
889+ Py_XDECREF (source_copy );
875890 return result ;
876891}
877892
@@ -942,20 +957,21 @@ builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals,
942957 v = PyEval_EvalCode (source , globals , locals );
943958 }
944959 else {
945- Py_buffer view = { NULL , NULL } ;
960+ PyObject * source_copy ;
946961 const char * str ;
947962 PyCompilerFlags cf ;
948963 cf .cf_flags = PyCF_SOURCE_IS_UTF8 ;
949964 str = source_as_string (source , "exec" ,
950- "string, bytes or code" , & cf , & view );
965+ "string, bytes or code" , & cf ,
966+ & source_copy );
951967 if (str == NULL )
952968 return NULL ;
953969 if (PyEval_MergeCompilerFlags (& cf ))
954970 v = PyRun_StringFlags (str , Py_file_input , globals ,
955971 locals , & cf );
956972 else
957973 v = PyRun_String (str , Py_file_input , globals , locals );
958- PyBuffer_Release ( & view );
974+ Py_XDECREF ( source_copy );
959975 }
960976 if (v == NULL )
961977 return NULL ;
0 commit comments