Changeset 5559

Show
Ignore:
Timestamp:
07/16/12 17:37:30 (7 years ago)
Author:
cmatthew
Message:

Updated struct module to have a P type to hold Null and regular pointers.

Location:
seattle/branches/nacl_repy/seattlelib
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • seattle/branches/nacl_repy/seattlelib/struct.repy

    r5084 r5559  
    340340    raise TypeError("Packed string must have type str, not '"+str(type(packedstring))+"'") 
    341341 
    342  
    343342  # let's check the size and see if this matches.   We also check the format 
    344343  # and endianness at the same time. 
    345344  expectedpackedstringlength = struct_calcsize(formatstring) 
    346  
     345   
    347346  if expectedpackedstringlength != len(packedstring): 
    348347    raise ValueError("Expected packed string of length "+str(expectedpackedstringlength)+", got "+str(len(packedstring))) 
     
    352351  currentpackedstring = packedstring 
    353352 
     353 
    354354  # this will be built as we go 
    355355  retlist = [] 
    356356 
    357   while currentformatstring and currentpackedstring: 
    358  
     357  # keep going while there are tokens to consume, or None's (which dont have packed space) 
     358  while (currentformatstring and currentpackedstring): 
    359359    #### Get the endianness! ### 
    360360 
     
    385385     
    386386 
     387  
     388 
    387389 
    388390    #### Handle the type! ### 
     
    518520        # and append it. 
    519521        retlist.append(thisnumber) 
    520          
    521  
     522 
     523        # Pointer 
     524      elif typetounpack == 'P': 
     525        # shouldn't happen because we checked above 
     526        assert(endianness == "none") 
     527        assert(len(currentpackedstring) >= 8) 
     528   
     529  
     530        thisnumberstring = currentpackedstring[:8] 
     531  
     532        currentpackedstring = currentpackedstring[8:] 
     533 
     534        # okay, so now we have the big-endian description... 
     535        thisnumber = _convert_big_endian_string_to_number(thisnumberstring) 
     536 
     537        # NULL -> None, and Any other number will be in hex 
     538        if thisnumber == 0: 
     539          retlist.append(None) 
     540        else: 
     541          retlist.append(hex(thisnumber)) 
     542  
    522543      else: 
    523544        raise TypeError("Unknown format type '"+typetounpack+"'") 
     
    533554  # all done, did everything get consumed? 
    534555  if currentformatstring: 
    535     raise ValueError("Not enough arguments for formatstring in struct.unpack") 
     556    raise ValueError("Not enough arguments for formatstring in struct.unpack,\ 
     557remains: %s"%(currentformatstring)) 
    536558 
    537559  if currentpackedstring: 
     
    657679      currentlength += 8 * repeatcount 
    658680 
     681    # Pointer!!! 
     682    elif typetounpack == 'P': 
     683 
     684      if specifiesendianness: 
     685        raise ValueError("An pointer must not have endianness specified! (position "+str(position)+" of '"+formatstring+"')") 
     686 
     687      # add the size of an integer + the number of integers 
     688      currentlength += 8 * repeatcount 
     689 
     690    elif typetounpack == 'N' or typetounpack == 'n': 
     691      # None does not take any extra space 
     692      pass 
     693     
    659694 
    660695    else: 
  • seattle/branches/nacl_repy/seattlelib/tests/ut_seattlelibtests_teststruct.mix

    r5146 r5559  
    33include struct.repy 
    44 
     5 
    56def test_struct(): 
    67 
    78  # let's try some valid pack / unpack of things first 
    8   a = struct_pack('0s!i!2I10s0s','',-1,2,123,'helloworld','') 
     9  a = struct_pack('0s!i!2I10s0s', '', -1, 2, 123, 'helloworld', '') 
    910 
    10   result = struct_unpack('0s!i!2I10s0s',a) 
     11  result = struct_unpack('0s!i!2I10s0s', a) 
    1112 
    12   if result != ['',-1,2,123,"helloworld",'']: 
    13     print result,'does not match expected value' 
     13  if result != ['', -1, 2, 123, "helloworld", '']: 
     14    print result, 'does not match expected value' 
    1415 
    1516  # integer boundary conditions 
    16   a = struct_pack('!I!I!i!i',0, 2**32-1, -2**31, -1) 
     17  a = struct_pack('!I!I!i!i', 0, 2**32-1, -2**31, -1) 
    1718 
    18   result = struct_unpack('!I!I!i!i',a) 
     19  result = struct_unpack('!I!I!i!i', a) 
    1920 
    2021  if result != [0,2**32-1, -2**31, -1L]: 
     
    2829  assert('\x01\x00\x00\x00' == struct_pack('<i',1)) 
    2930 
    30 ###### PACK ERROR TESTS !!!##### 
     31  # None tests 
     32  assert [None] == struct_unpack("P", '\x00\x00\x00\x00\x00\x00\x00\x00'), "Simple None unpack" 
     33  assert [1,None,1] == struct_unpack("<iP<i", '\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00'), "Complex None unpack" 
     34 
     35  val =  struct_unpack("P", '\x00\x00\x00\x00\x00\x00\x00\x01') 
     36  assert [hex(1)] == val,  "Simple pointer type: %s"%(str(val)) 
     37   
     38 
     39  ###### PACK ERROR TESTS !!!##### 
    3140 
    3241  try: 
    33     bad =  struct_pack('10s','helloworld!')  # too long 
     42    bad = struct_pack('10s','helloworld!')  # too long 
    3443  except Exception, e: 
    3544    pass 
     
    5160    print "bad",bad 
    5261 
    53  
    5462  try: 
    5563    bad =  struct_pack('!i',-2**31-1)  # too small 
     
    5866  else: 
    5967    print "bad",bad 
    60  
    6168 
    6269  try: 
     
    6774    print "bad",bad 
    6875 
    69  
    7076  try: 
    7177    bad =  struct_pack('!I',2**32)  # too big 
     
    7480  else: 
    7581    print "bad",bad 
    76  
    77  
    7882 
    7983  try: 
     
    8488    print "bad",bad 
    8589 
    86  
    87  
    88  
    8990  try: 
    9091    bad =  struct_pack('2s1s','hi')  # missing arg 
     
    9394  else: 
    9495    print "bad",bad 
    95  
    9696 
    9797  try: 
     
    102102    print "bad",bad 
    103103 
    104  
    105  
    106  
    107 ###### UNPACK ERROR TESTS!!!##### 
    108  
    109  
     104  ###### UNPACK ERROR TESTS!!!##### 
    110105 
    111106  try: 
     
    116111    print "bad",bad 
    117112 
    118  
    119  
    120  
    121113  try: 
    122114    bad =  struct_pack('2s1s','hi')  # not enough 
     
    125117  else: 
    126118    print "bad",bad 
    127  
    128  
    129119 
    130120  try: 
     
    135125    print "bad",bad 
    136126 
    137  
    138  
    139127  try: 
    140128    bad =  struct_pack('!i','\x00\x00\x00\x00\x00')  # too much 
     
    143131  else: 
    144132    print "bad",bad 
    145  
    146  
    147133 
    148134  try: 
     
    154140 
    155141 
    156  
    157  
    158142test_struct()