PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rpython/module/support.py

https://github.com/woodrow/pyoac
Python | 94 lines | 89 code | 4 blank | 1 comment | 6 complexity | 0e31f3f8d37e107b53c7374d27486ea0 MD5 | raw file
  1. from pypy.rpython.lltypesystem import lltype
  2. from pypy.rpython.ootypesystem import ootype
  3. from pypy.rpython.lltypesystem.lltype import \
  4. GcStruct, Signed, Array, Char, Ptr, malloc, GcArray
  5. from pypy.rpython.rlist import ll_append
  6. from pypy.rpython.lltypesystem.rlist import ll_newlist, ListRepr,\
  7. ll_getitem_fast
  8. from pypy.rpython.lltypesystem.rstr import string_repr
  9. from pypy.rpython.lltypesystem.rdict import ll_newdict, DictRepr, dum_items,\
  10. ll_kvi, dum_keys, ll_dict_getitem, ll_dict_setitem
  11. from pypy.rpython.lltypesystem.rstr import StringRepr
  12. from pypy.rpython.lltypesystem.rtuple import TupleRepr
  13. from pypy.annotation.dictdef import DictKey, DictValue
  14. from pypy.annotation.model import SomeString
  15. import os
  16. # utility conversion functions
  17. class LLSupport:
  18. _mixin_ = True
  19. def to_rstr(s):
  20. from pypy.rpython.lltypesystem.rstr import STR, mallocstr
  21. if s is None:
  22. return lltype.nullptr(STR)
  23. p = mallocstr(len(s))
  24. for i in range(len(s)):
  25. p.chars[i] = s[i]
  26. return p
  27. to_rstr = staticmethod(to_rstr)
  28. def to_runicode(s):
  29. from pypy.rpython.lltypesystem.rstr import UNICODE, mallocunicode
  30. if s is None:
  31. return lltype.nullptr(UNICODE)
  32. p = mallocunicode(len(s))
  33. for i in range(len(s)):
  34. p.chars[i] = s[i]
  35. return p
  36. to_runicode = staticmethod(to_runicode)
  37. def from_rstr(rs):
  38. if not rs: # null pointer
  39. return None
  40. else:
  41. return ''.join([rs.chars[i] for i in range(len(rs.chars))])
  42. from_rstr = staticmethod(from_rstr)
  43. def from_rstr_nonnull(rs):
  44. assert rs
  45. return ''.join([rs.chars[i] for i in range(len(rs.chars))])
  46. from_rstr_nonnull = staticmethod(from_rstr_nonnull)
  47. class OOSupport:
  48. _mixin_ = True
  49. def to_rstr(s):
  50. if s is None:
  51. return ootype.null(ootype.String)
  52. return ootype.oostring(s, -1)
  53. to_rstr = staticmethod(to_rstr)
  54. def to_runicode(u):
  55. return ootype.oounicode(u, -1)
  56. to_runicode = staticmethod(to_runicode)
  57. def from_rstr(rs):
  58. if not rs: # null pointer
  59. return None
  60. else:
  61. return "".join([rs.ll_stritem_nonneg(i) for i in range(rs.ll_strlen())])
  62. from_rstr = staticmethod(from_rstr)
  63. def from_rstr_nonnull(rs):
  64. assert rs
  65. return "".join([rs.ll_stritem_nonneg(i) for i in range(rs.ll_strlen())])
  66. from_rstr_nonnull = staticmethod(from_rstr_nonnull)
  67. def ll_strcpy(dst_s, src_s, n):
  68. dstchars = dst_s.chars
  69. srcchars = src_s.chars
  70. i = 0
  71. while i < n:
  72. dstchars[i] = srcchars[i]
  73. i += 1
  74. def _ll_strfill(dst_s, srcchars, n):
  75. dstchars = dst_s.chars
  76. i = 0
  77. while i < n:
  78. dstchars[i] = srcchars[i]
  79. i += 1