Commits

Dean Hall  committed 8a0c16a

#59: Mainlining directly.

  • Participants
  • Parent commits 05d07f9

Comments (0)

Files changed (8)

File src/tests/system/t010.py

 assert c == 2
 assert b == 5
 assert a == 6
+
+#
+# #59: Improve bytecode UNPACK_SEQUENCE
+#
+a,b,c = "256"
+assert a == "2"
+assert b == "5"
+assert c == "6"

File src/vm/fileid.txt

 0x11  sli.c
 0x12  string.c
 0x13  tuple.c
+0x14  seq.c       created 2006/11/29
 
 ---------------- IDs for Unit Test Files
 0x90  utest.c

File src/vm/interp.c

                 break;
 
             case UNPACK_SEQUENCE:
-                /* get expected size of the sequence */
-                t16 = GET_ARG();
                 /* get ptr to sequence */
                 pobj1 = PM_POP();
 
+                /*
+                 * Get the length of the sequence; this will
+                 * raise TypeError if obj is not a sequence.
+                 *
+                 * #59: Unpacking to a Dict shall not be supported
+                 */
+                retval = seq_getLength(pobj1, &t16);
+                if (retval != PM_RET_OK)
+                {
+                    GET_ARG();
+                    break;
+                }
+
+                /* Raise ValueError if seq length does not match num args */
+                if (t16 != GET_ARG())
+                {
+                    PM_RAISE(retval, PM_RET_EX_VAL);
+                    break;
+                }
+
                 /* Push sequence's objs onto stack */
-                if ((OBJ_GET_TYPE(*pobj1) == OBJ_TYPE_TUP)
-                    || (OBJ_GET_TYPE(*pobj1) == OBJ_TYPE_LST))
+                for (; --t16 >= 0; )
                 {
-                    for (; --t16 >= 0; )
-                    {
-                        retval = seq_getSubscript(pobj1, t16, &pobj2);
-                        PM_BREAK_IF_ERROR(retval);
-                        PM_PUSH(pobj2);
-                    }
-                    /* Test again outside the for loop */
+                    retval = seq_getSubscript(pobj1, t16, &pobj2);
                     PM_BREAK_IF_ERROR(retval);
+                    PM_PUSH(pobj2);
                 }
+
+                /* Test again outside the for loop */
+                PM_BREAK_IF_ERROR(retval);
                 continue;
 
             case STORE_ATTR:

File src/vm/obj.c

 }
 
 
-/*
- * Compares two sequence objects
- * Assumes both objects are of same type (guaranteed by obj_compare)
- */
-int8_t
-seq_compare(pPmObj_t pobj1, pPmObj_t pobj2)
-{
-    int16_t l1;
-    int16_t l2;
-    pPmObj_t pa;
-    pPmObj_t pb;
-    PmReturn_t retval;
-    int8_t retcompare;
-
-    /* Get the lengths of supported types or return differ */
-    if (OBJ_GET_TYPE(*pobj1) == OBJ_TYPE_TUP)
-    {
-        l1 = ((pPmTuple_t)pobj1)->length;
-        l2 = ((pPmTuple_t)pobj2)->length;
-    }
-    else if (OBJ_GET_TYPE(*pobj1) == OBJ_TYPE_LST)
-    {
-        l1 = ((pPmList_t)pobj2)->length;
-        l2 = ((pPmList_t)pobj2)->length;
-    }
-    else
-    {
-        return C_DIFFER;
-    }
-
-    /* Return if the lengths differ */
-    if (l1 != l2)
-    {
-        return C_DIFFER;
-    }
-
-    /* Compare all items in the sequences */
-    while (--l1 >= 0)
-    {
-        retval = seq_getSubscript(pobj1, l1, &pa);
-        if (retval != PM_RET_OK)
-        {
-            return C_DIFFER;
-        }
-        retval = seq_getSubscript(pobj2, l1, &pb);
-        if (retval != PM_RET_OK)
-        {
-            return C_DIFFER;
-        }
-        retcompare = obj_compare(pa, pb);
-        if (retcompare != C_SAME)
-        {
-            return retcompare;
-        }
-    }
-
-    return C_SAME;
-}
-
-
-/* Returns the object sequence[index] */
-PmReturn_t
-seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj)
-{
-    PmReturn_t retval;
-    uint8_t c;
-
-    switch (OBJ_GET_TYPE(*pobj))
-    {
-        case OBJ_TYPE_STR:
-            /* Adjust for negative index */
-            if (index < 0)
-            {
-                index += ((pPmString_t)pobj)->length;
-            }
-
-            /* Raise IndexError if index is out of bounds */
-            if ((index < 0) || (index > ((pPmString_t)pobj)->length))
-            {
-                PM_RAISE(retval, PM_RET_EX_INDX);
-                break;
-            }
-
-            /* Get the character from the string */
-            c = ((pPmString_t)pobj)->val[index];
-
-            /* Create a new string from the character */
-            retval = string_newFromChar(c, r_pobj);
-            break;
-
-        case OBJ_TYPE_TUP:
-            /* Get the tuple item */
-            retval = tuple_getItem(pobj, index, r_pobj);
-            break;
-
-        case OBJ_TYPE_LST:
-            /* Get the list item */
-            retval = list_getItem(pobj, index, r_pobj);
-            break;
-
-        default:
-            /* Raise TypeError, unsubscriptable object */
-            PM_RAISE(retval, PM_RET_EX_TYPE);
-            break;
-    }
-
-    return retval;
-}
-
-
 /***************************************************************
  * Test
  **************************************************************/

File src/vm/obj.h

  */
 int8_t obj_compare(pPmObj_t pobj1, pPmObj_t pobj2);
 
-/**
- * Compares two sequences for equality
- *
- * @param   pobj1 Ptr to first sequence.
- * @param   pobj2 Ptr to second sequence.
- * @return  C_SAME if the seuqences are equivalent, C_DIFFER otherwise.
- */
-int8_t seq_compare(pPmObj_t pobj1, pPmObj_t pobj2);
-
-/**
- * Returns the object from sequence[index]
- *
- * @param   pobj Ptr to sequence object to get object from
- * @param   index Int index into the sequence
- * @param   r_pobj Return arg, object from sequence
- * @return  Return status
- */
-PmReturn_t seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj);
-
 #endif /* __OBJ_H__ */
 #include "sli.h"
 #include "mem.h"
 #include "obj.h"
+#include "seq.h"
 #include "heap.h"
 #include "int.h"
 #include "string.h"

File src/vm/seq.c

+/*
+ * 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.
+ */
+
+#undef __FILE_ID__
+#define __FILE_ID__ 0x14
+/**
+ * Sequence
+ *
+ * Functions that operate on sequences
+ *
+ * Log
+ * ---
+ *
+ * 2006/11/29   #59: Improve bytecode UNPACK_SEQUENCE
+ */
+
+/***************************************************************
+ * Includes
+ **************************************************************/
+
+#include "pm.h"
+
+
+/*
+ * Compares two sequence objects
+ * Assumes both objects are of same type (guaranteed by obj_compare)
+ */
+int8_t
+seq_compare(pPmObj_t pobj1, pPmObj_t pobj2)
+{
+    int16_t l1;
+    int16_t l2;
+    pPmObj_t pa;
+    pPmObj_t pb;
+    PmReturn_t retval;
+    int8_t retcompare;
+
+    /* Get the lengths of supported types or return differ */
+    if (OBJ_GET_TYPE(*pobj1) == OBJ_TYPE_TUP)
+    {
+        l1 = ((pPmTuple_t)pobj1)->length;
+        l2 = ((pPmTuple_t)pobj2)->length;
+    }
+    else if (OBJ_GET_TYPE(*pobj1) == OBJ_TYPE_LST)
+    {
+        l1 = ((pPmList_t)pobj2)->length;
+        l2 = ((pPmList_t)pobj2)->length;
+    }
+    else
+    {
+        return C_DIFFER;
+    }
+
+    /* Return if the lengths differ */
+    if (l1 != l2)
+    {
+        return C_DIFFER;
+    }
+
+    /* Compare all items in the sequences */
+    while (--l1 >= 0)
+    {
+        retval = seq_getSubscript(pobj1, l1, &pa);
+        if (retval != PM_RET_OK)
+        {
+            return C_DIFFER;
+        }
+        retval = seq_getSubscript(pobj2, l1, &pb);
+        if (retval != PM_RET_OK)
+        {
+            return C_DIFFER;
+        }
+        retcompare = obj_compare(pa, pb);
+        if (retcompare != C_SAME)
+        {
+            return retcompare;
+        }
+    }
+
+    return C_SAME;
+}
+
+
+/* Returns the length of the sequence */
+PmReturn_t
+seq_getLength(pPmObj_t pobj, int16_t *r_index)
+{
+    PmReturn_t retval = PM_RET_OK;
+
+    switch (OBJ_GET_TYPE(*pobj))
+    {
+        case OBJ_TYPE_STR:
+            *r_index = ((pPmString_t)pobj)->length;
+            break;
+
+        case OBJ_TYPE_TUP:
+            *r_index = ((pPmTuple_t)pobj)->length;
+            break;
+
+        case OBJ_TYPE_LST:
+            *r_index = ((pPmList_t)pobj)->length;
+            break;
+
+        default:
+            /* Raise TypeError, non-sequence object */
+            PM_RAISE(retval, PM_RET_EX_TYPE);
+            break;
+    }
+
+    return retval;
+}
+
+
+/* Returns the object sequence[index] */
+PmReturn_t
+seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj)
+{
+    PmReturn_t retval;
+    uint8_t c;
+
+    switch (OBJ_GET_TYPE(*pobj))
+    {
+        case OBJ_TYPE_STR:
+            /* Adjust for negative index */
+            if (index < 0)
+            {
+                index += ((pPmString_t)pobj)->length;
+            }
+
+            /* Raise IndexError if index is out of bounds */
+            if ((index < 0) || (index > ((pPmString_t)pobj)->length))
+            {
+                PM_RAISE(retval, PM_RET_EX_INDX);
+                break;
+            }
+
+            /* Get the character from the string */
+            c = ((pPmString_t)pobj)->val[index];
+
+            /* Create a new string from the character */
+            retval = string_newFromChar(c, r_pobj);
+            break;
+
+        case OBJ_TYPE_TUP:
+            /* Get the tuple item */
+            retval = tuple_getItem(pobj, index, r_pobj);
+            break;
+
+        case OBJ_TYPE_LST:
+            /* Get the list item */
+            retval = list_getItem(pobj, index, r_pobj);
+            break;
+
+        default:
+            /* Raise TypeError, unsubscriptable object */
+            PM_RAISE(retval, PM_RET_EX_TYPE);
+            break;
+    }
+
+    return retval;
+}

File src/vm/seq.h

+/*
+ * 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.
+ */
+
+#ifndef __SEQ_H__
+#define __SEQ_H__
+/**
+ * Sequence Header
+ *
+ * Log
+ * ---
+ *
+ * 2006/11/29   #59: Improve bytecode UNPACK_SEQUENCE
+ */
+
+
+/**
+ * Compares two sequences for equality
+ *
+ * @param   pobj1 Ptr to first sequence.
+ * @param   pobj2 Ptr to second sequence.
+ * @return  C_SAME if the seuqences are equivalent, C_DIFFER otherwise.
+ */
+int8_t seq_compare(pPmObj_t pobj1, pPmObj_t pobj2);
+
+/**
+ * Returns the length of the sequence
+ *
+ * @param   pobj Ptr to  sequence.
+ * @param   r_index Return arg, length of sequence
+ * @return  Return status
+ */
+PmReturn_t seq_getLength(pPmObj_t pobj, int16_t *r_index);
+
+/**
+ * Returns the object from sequence[index]
+ *
+ * @param   pobj Ptr to sequence object to get object from
+ * @param   index Int index into the sequence
+ * @param   r_pobj Return arg, object from sequence
+ * @return  Return status
+ */
+PmReturn_t seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj);
+
+#endif /* __SEQ_H__ */