t230.py
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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"