t230.py

00001 # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 #
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING up one directory from this.
00012 
00013 #
00014 # System Test 230
00015 # Fix classes during ipm
00016 # This test checks for a regression: __NATIVE__ __init__ method pushed None
00017 # on the stack causing trouble.
00018 #
00019 """__NATIVE__
00020 #include <stdio.h>
00021 """
00022 
00023 
00024 class File():
00025 
00026     def __init__(self, fn):
00027         """__NATIVE__
00028         pPmObj_t pself;
00029         pPmObj_t pfn;
00030         pPmObj_t pn;
00031         pPmObj_t pattrs;
00032         PmReturn_t retval = PM_RET_OK;
00033         FILE *pf;
00034 
00035         /* If wrong number of args, throw type exception */
00036         if (NATIVE_GET_NUM_ARGS() != 2)
00037         {
00038             PM_RAISE(retval, PM_RET_EX_TYPE);
00039             return retval;
00040         }
00041         pself = NATIVE_GET_LOCAL(0);
00042 
00043         /* Get the arg, throw type exception if needed */
00044         pfn = NATIVE_GET_LOCAL(1);
00045         if (OBJ_GET_TYPE(pfn) != OBJ_TYPE_STR)
00046         {
00047             PM_RAISE(retval, PM_RET_EX_TYPE);
00048             return retval;
00049         }
00050 
00051         /* Open the file in write/bin mode */
00052         pf = fopen((const char *)((pPmString_t)pfn)->val, "wb");
00053 
00054         /* Save the pointer to file as an inaccessible attribute */
00055         pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
00056         retval = int_new((uint32_t)pf, &pn);
00057         PM_RETURN_IF_ERROR(retval);
00058         retval = dict_setItem(pattrs, PM_NONE, pn);
00059         PM_RETURN_IF_ERROR(retval);
00060 
00061         NATIVE_SET_TOS(PM_NONE);
00062         return retval;
00063         """
00064         pass
00065 
00066 
00067     def close(self,):
00068         """__NATIVE__
00069         pPmObj_t pself;
00070         pPmObj_t pn;
00071         pPmObj_t pattrs;
00072         PmReturn_t retval = PM_RET_OK;
00073         FILE *pf;
00074 
00075         /* If wrong number of args, throw type exception */
00076         if (NATIVE_GET_NUM_ARGS() != 1)
00077         {
00078             PM_RAISE(retval, PM_RET_EX_TYPE);
00079             return retval;
00080         }
00081         pself = NATIVE_GET_LOCAL(0);
00082 
00083         /* Close the file */
00084         pattrs = (pPmObj_t)((pPmInstance_t)pself)->cli_attrs;
00085         retval = dict_getItem(pattrs, PM_NONE, &pn);
00086         PM_RETURN_IF_ERROR(retval);
00087         pf = (FILE *)((pPmInt_t)pn)->val;
00088         fclose(pf);
00089 
00090         NATIVE_SET_TOS(PM_NONE);
00091         return retval;
00092         """
00093         pass
00094 
00095 f1 = File("zem")
00096 print "Created instance: File('zem')"
00097 f1.close()
00098 print "File closed"

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9