@@ -340,11 +340,32 @@ builtin_compile(PyObject *self, PyObject *args)
340340 int dont_inherit = 0 ;
341341 int supplied_flags = 0 ;
342342 PyCompilerFlags cf ;
343+ PyObject * result , * cmd , * tmp = NULL ;
343344
344- if (!PyArg_ParseTuple (args , "sss |ii:compile" , & str , & filename ,
345+ if (!PyArg_ParseTuple (args , "Oss |ii:compile" , & cmd , & filename ,
345346 & startstr , & supplied_flags , & dont_inherit ))
346347 return NULL ;
347348
349+ cf .cf_flags = supplied_flags ;
350+
351+ #ifdef Py_USING_UNICODE
352+ if (PyUnicode_Check (cmd )) {
353+ tmp = PyUnicode_AsUTF8String (cmd );
354+ if (tmp == NULL )
355+ return NULL ;
356+ cmd = tmp ;
357+ cf .cf_flags |= PyCF_SOURCE_IS_UTF8 ;
358+ }
359+ #endif
360+ if (!PyString_Check (cmd )) {
361+ PyErr_SetString (PyExc_TypeError ,
362+ "compile() arg 1 must be a string" );
363+ return NULL ;
364+ }
365+
366+ if (PyString_AsStringAndSize (cmd , & str , NULL ))
367+ return NULL ;
368+
348369 if (strcmp (startstr , "exec" ) == 0 )
349370 start = Py_file_input ;
350371 else if (strcmp (startstr , "eval" ) == 0 )
@@ -364,11 +385,12 @@ builtin_compile(PyObject *self, PyObject *args)
364385 }
365386 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
366387
367- cf .cf_flags = supplied_flags ;
368388 if (!dont_inherit ) {
369389 PyEval_MergeCompilerFlags (& cf );
370390 }
371- return Py_CompileStringFlags (str , filename , start , & cf );
391+ result = Py_CompileStringFlags (str , filename , start , & cf );
392+ Py_XDECREF (tmp );
393+ return result ;
372394}
373395
374396PyDoc_STRVAR (compile_doc ,
@@ -428,7 +450,7 @@ Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
428450static PyObject *
429451builtin_eval (PyObject * self , PyObject * args )
430452{
431- PyObject * cmd ;
453+ PyObject * cmd , * result , * tmp = NULL ;
432454 PyObject * globals = Py_None , * locals = Py_None ;
433455 char * str ;
434456 PyCompilerFlags cf ;
@@ -467,14 +489,26 @@ builtin_eval(PyObject *self, PyObject *args)
467489 "eval() arg 1 must be a string or code object" );
468490 return NULL ;
469491 }
492+ cf .cf_flags = 0 ;
493+
494+ #ifdef Py_USING_UNICODE
495+ if (PyUnicode_Check (cmd )) {
496+ tmp = PyUnicode_AsUTF8String (cmd );
497+ if (tmp == NULL )
498+ return NULL ;
499+ cmd = tmp ;
500+ cf .cf_flags |= PyCF_SOURCE_IS_UTF8 ;
501+ }
502+ #endif
470503 if (PyString_AsStringAndSize (cmd , & str , NULL ))
471504 return NULL ;
472505 while (* str == ' ' || * str == '\t' )
473506 str ++ ;
474507
475- cf .cf_flags = 0 ;
476508 (void )PyEval_MergeCompilerFlags (& cf );
477- return PyRun_StringFlags (str , Py_eval_input , globals , locals , & cf );
509+ result = PyRun_StringFlags (str , Py_eval_input , globals , locals , & cf );
510+ Py_XDECREF (tmp );
511+ return result ;
478512}
479513
480514PyDoc_STRVAR (eval_doc ,
0 commit comments