@@ -66,7 +66,8 @@ static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
6666static int assign_slice (PyObject * , PyObject * ,
6767 PyObject * , PyObject * );
6868static PyObject * cmp_outcome (int , PyObject * , PyObject * );
69- static int import_from (PyObject * , PyObject * , PyObject * );
69+ static PyObject * import_from (PyObject * , PyObject * );
70+ static int import_all_from (PyObject * , PyObject * );
7071static PyObject * build_class (PyObject * , PyObject * , PyObject * );
7172static int exec_statement (PyFrameObject * ,
7273 PyObject * , PyObject * , PyObject * );
@@ -1414,20 +1415,28 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
14141415 if (x != NULL ) continue ;
14151416 break ;
14161417
1417- case IMPORT_FROM :
1418- w = GETNAMEV (oparg );
1419- v = TOP ();
1418+ case IMPORT_STAR :
1419+ v = POP ();
14201420 PyFrame_FastToLocals (f );
14211421 if ((x = f -> f_locals ) == NULL ) {
14221422 PyErr_SetString (PyExc_SystemError ,
14231423 "no locals" );
14241424 break ;
14251425 }
1426- err = import_from (x , v , w );
1426+ err = import_all_from (x , v );
14271427 PyFrame_LocalsToFast (f , 0 );
1428+ Py_DECREF (v );
14281429 if (err == 0 ) continue ;
14291430 break ;
14301431
1432+ case IMPORT_FROM :
1433+ w = GETNAMEV (oparg );
1434+ v = TOP ();
1435+ x = import_from (v , w );
1436+ PUSH (x );
1437+ if (x != NULL ) continue ;
1438+ break ;
1439+
14311440 case JUMP_FORWARD :
14321441 JUMPBY (oparg );
14331442 continue ;
@@ -2647,43 +2656,51 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
26472656 return v ;
26482657}
26492658
2650- static int
2651- import_from (PyObject * locals , PyObject * v , PyObject * name )
2659+ static PyObject *
2660+ import_from (PyObject * v , PyObject * name )
26522661{
26532662 PyObject * w , * x ;
2663+ if (!PyModule_Check (v )) {
2664+ PyErr_SetString (PyExc_TypeError ,
2665+ "import-from requires module object" );
2666+ return NULL ;
2667+ }
2668+ w = PyModule_GetDict (v ); /* TDB: can this not fail ? */
2669+ x = PyDict_GetItem (w , name );
2670+ if (x == NULL ) {
2671+ PyErr_Format (PyExc_ImportError ,
2672+ "cannot import name %.230s" ,
2673+ PyString_AsString (name ));
2674+ } else
2675+ Py_INCREF (x );
2676+ return x ;
2677+ }
2678+
2679+ static int
2680+ import_all_from (PyObject * locals , PyObject * v )
2681+ {
2682+ int pos = 0 , err ;
2683+ PyObject * name , * value ;
2684+ PyObject * w ;
2685+
26542686 if (!PyModule_Check (v )) {
26552687 PyErr_SetString (PyExc_TypeError ,
26562688 "import-from requires module object" );
26572689 return -1 ;
26582690 }
2659- w = PyModule_GetDict (v );
2660- if (PyString_AsString (name )[0 ] == '*' ) {
2661- int pos , err ;
2662- PyObject * name , * value ;
2663- pos = 0 ;
2664- while (PyDict_Next (w , & pos , & name , & value )) {
2665- if (!PyString_Check (name ) ||
2666- PyString_AsString (name )[0 ] == '_' )
2691+ w = PyModule_GetDict (v ); /* TBD: can this not fail ? */
2692+
2693+ while (PyDict_Next (w , & pos , & name , & value )) {
2694+ if (!PyString_Check (name ) ||
2695+ PyString_AsString (name )[0 ] == '_' )
26672696 continue ;
2668- Py_INCREF (value );
2669- err = PyDict_SetItem (locals , name , value );
2670- Py_DECREF (value );
2671- if (err != 0 )
2672- return -1 ;
2673- }
2674- return 0 ;
2675- }
2676- else {
2677- x = PyDict_GetItem (w , name );
2678- if (x == NULL ) {
2679- PyErr_Format (PyExc_ImportError ,
2680- "cannot import name %.230s" ,
2681- PyString_AsString (name ));
2697+ Py_INCREF (value );
2698+ err = PyDict_SetItem (locals , name , value );
2699+ Py_DECREF (value );
2700+ if (err != 0 )
26822701 return -1 ;
2683- }
2684- else
2685- return PyDict_SetItem (locals , name , x );
26862702 }
2703+ return 0 ;
26872704}
26882705
26892706static PyObject *
@@ -2825,26 +2842,36 @@ find_from_args(PyFrameObject *f, int nexti)
28252842 next_instr += nexti ;
28262843
28272844 opcode = (* next_instr ++ );
2828- if (opcode != IMPORT_FROM ) {
2845+ if (opcode != IMPORT_FROM && opcode != IMPORT_STAR ) {
28292846 Py_INCREF (Py_None );
28302847 return Py_None ;
28312848 }
28322849
28332850 list = PyList_New (0 );
28342851 if (list == NULL )
28352852 return NULL ;
2836-
2837- do {
2838- oparg = (next_instr [1 ]<<8 ) + next_instr [0 ];
2839- next_instr += 2 ;
2840- name = Getnamev (f , oparg );
2841- if (PyList_Append (list , name ) < 0 ) {
2853+
2854+ if (opcode == IMPORT_STAR ) {
2855+ name = PyString_FromString ("*" );
2856+ if (!name )
28422857 Py_DECREF (list );
2843- break ;
2858+ else {
2859+ if (PyList_Append (list , name ) < 0 )
2860+ Py_DECREF (list );
2861+ Py_DECREF (name );
28442862 }
2845- opcode = (* next_instr ++ );
2846- } while (opcode == IMPORT_FROM );
2847-
2863+ } else {
2864+ do {
2865+ oparg = (next_instr [1 ]<<8 ) + next_instr [0 ];
2866+ next_instr += 2 ;
2867+ name = Getnamev (f , oparg );
2868+ if (PyList_Append (list , name ) < 0 ) {
2869+ Py_DECREF (list );
2870+ break ;
2871+ }
2872+ opcode = (* next_instr ++ );
2873+ } while (opcode == IMPORT_FROM );
2874+ }
28482875 return list ;
28492876}
28502877
0 commit comments