Commits

Anonymous committed b316574

#102: Mainlining directly

Comments (0)

Files changed (6)

src/tests/system/t102.c

+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+/**
+ * System Test 102
+ *
+ * Regression test for issue #102:
+ * Implement the remaining IMPORT_ bytecodes
+ *
+ * Log
+ * ---
+ *
+ * 2007/04/14   #102: First
+ */
+
+#include "pm.h"
+#include "stdio.h"
+
+
+extern unsigned char usrlib_img[];
+
+
+int main(void)
+{
+    PmReturn_t retval;
+
+    retval = pm_init(MEMSPACE_PROG, usrlib_img);
+    PM_RETURN_IF_ERROR(retval);
+
+    retval = pm_run((uint8_t *)"t102");
+    return (int)retval;
+}

src/tests/system/t102.py

+# PyMite - A flyweight Python interpreter for 8-bit microcontrollers and more.
+# Copyright 2002 Dean Hall
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+
+#
+# System Test 102
+#
+# Regression test for issue #102:
+# Implement the remaining IMPORT_ bytecodes
+#
+
+# Test IMPORT_FROM
+from sys import maxint
+assert maxint == 0x7fffffff
+
+# Test IMPORT_STAR
+from sys import *
+assert type(heap) == 0x08
+assert type(exit) == 0x08

src/tools/pmImgCreator.py

 #    "INPLACE_AND", "INPLACE_XOR", "INPLACE_OR",
 #    "BREAK_LOOP",
     "WITH_CLEANUP",
-#    "LOAD_LOCALS", "RETURN_VALUE",
-    "IMPORT_STAR", "EXEC_STMT", "YIELD_VALUE",
+#    "LOAD_LOCALS", "RETURN_VALUE", "IMPORT_STAR", 
+    "EXEC_STMT", "YIELD_VALUE",
 #    "POP_BLOCK",
     "END_FINALLY", "BUILD_CLASS",
 #    "STORE_NAME",
 #    "DUP_TOPX",
 #    "LOAD_CONST", "LOAD_NAME", "BUILD_TUPLE", "BUILD_LIST",
 #    "BUILD_MAP", "LOAD_ATTR", "COMPARE_OP", "IMPORT_NAME",
-    "IMPORT_FROM",
-#    "JUMP_FORWARD", "JUMP_IF_FALSE", "JUMP_IF_TRUE",
+#    "IMPORT_FROM", "JUMP_FORWARD", "JUMP_IF_FALSE", "JUMP_IF_TRUE",
 #    "JUMP_ABSOLUTE", "FOR_LOOP", "LOAD_GLOBAL",
 #    "CONTINUE_LOOP", "SETUP_LOOP",
     "SETUP_EXCEPT",
         PM_RAISE(retval, PM_RET_EX_TYPE);
         return retval;
     }
-    
+
     plat_putByte('{');
-    
+
     keys = ((pPmDict_t)pdict)->d_keys;
     vals = ((pPmDict_t)pdict)->d_vals;
 
 }
 #endif /* HAVE_PRINT */
 
+PmReturn_t
+dict_update(pPmObj_t pdestdict, pPmObj_t psourcedict)
+{
+    PmReturn_t retval = PM_RET_OK;
+    int16_t i;
+    pPmObj_t pkey;
+    pPmObj_t pval;
+
+    C_ASSERT(pdestdict != C_NULL);
+    C_ASSERT(psourcedict != C_NULL);
+
+    /* If it's not a dict, raise TypeError */
+    if (OBJ_GET_TYPE(*pdestdict) != OBJ_TYPE_DIC)
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    /* If it's not a dict, raise TypeError */
+    if (OBJ_GET_TYPE(*psourcedict) != OBJ_TYPE_DIC)
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    /* Iterate over the add-on dict */
+    for (i = 0; i < ((pPmDict_t)psourcedict)->length; i++)
+    {
+        /* Get the key,val from the add-on dict */
+        retval = seglist_getItem(((pPmDict_t)psourcedict)->d_keys, i, &pkey);
+        PM_RETURN_IF_ERROR(retval);
+        retval = seglist_getItem(((pPmDict_t)psourcedict)->d_vals, i, &pval);
+        PM_RETURN_IF_ERROR(retval);
+
+        /* Set the key,val to the destination dict */
+        retval = dict_setItem(pdestdict, pkey, pval);
+        PM_RETURN_IF_ERROR(retval);
+    }
+
+    return retval;
+}
+
+
 /***************************************************************
  * Test
  **************************************************************/
 PmReturn_t dict_print(pPmObj_t pdict);
 #endif /* HAVE_PRINT */
 
+/**
+ * Updates the destination dict with the key,value pairs from the source dict
+ *
+ * @param   pdestdict ptr to destination dict in which key,val pairs will go
+ * @param   psourcedict ptr to source dict which has all key,val pairs to copy
+ * @return  Return status
+ */
+PmReturn_t dict_update(pPmObj_t pdestdict, pPmObj_t psourcedict);
+
 #if 0
 /* TBD */
 pPmObj_t  dict_copy(pPmObj_t pdict);
 pPmObj_t  dict_keys(pPmObj_t pdict);
 pPmObj_t  dict_popItem(pPmObj_t pdict);
 void      dict_setDefault(pPmObj_t pdict, /*HERE*/);
-void      dict_update(pPmObj_t pdict, pPmObj_t pdictb);
 pPmObj_t  dict_values(pPmObj_t pdict);
 #endif
 
  * Log
  * ---
  *
+ * 2007/04/14   #102: Implement the remaining IMPORT_ bytecodes
  * 2007/01/29   #80: Fix DUP_TOPX bytecode
  * 2007/01/17   #76: Print will differentiate on strings and print tuples
  * 2007/01/09   #75: Changed IMPORT_NAME, printing (P.Adelt)
                 continue;
 
             case IMPORT_STAR:
+                /* #102: Implement the remaining IMPORT_ bytecodes */
+                /* Expect a module on the top of the stack */
+                C_ASSERT(OBJ_GET_TYPE(*TOS) == OBJ_TYPE_MOD);
+                pobj1 = PM_POP();
+
+                /* Update FP's attrs with those of the module on the stack */
+                retval = dict_update((pPmObj_t)FP->fo_attrs,
+                                     (pPmObj_t)((pPmFunc_t)pobj1)->f_attrs);
+                PM_BREAK_IF_ERROR(retval);
+                continue;
+
             case EXEC_STMT:
                 /* SystemError, unknown opcode */
                 PM_RAISE(retval, PM_RET_EX_SYS);
                 continue;
 
             case IMPORT_FROM:
-                /* SystemError, unknown opcode */
-                PM_RAISE(retval, PM_RET_EX_SYS);
-                break;
+                /* #102: Implement the remaining IMPORT_ bytecodes */
+                /* Expect the module on the top of the stack */
+                C_ASSERT(OBJ_GET_TYPE(*TOS) == OBJ_TYPE_MOD);
+                pobj1 = TOS;
+
+                /* Get the name of the object to import */
+                t16 = GET_ARG();
+                pobj2 = FP->fo_func->f_co->co_names->val[t16];
+
+                /* Get the object from the module's attributes */
+                retval = dict_getItem((pPmObj_t)((pPmFunc_t)pobj1)->f_attrs,
+                                      pobj2,
+                                      &pobj3);
+                PM_BREAK_IF_ERROR(retval);
+
+                /* Push the object onto the top of the stack */
+                PM_PUSH(pobj3);
+                continue;
 
             case JUMP_FORWARD:
                 t16 = GET_ARG();