PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/_sha/interp_sha.py

https://bitbucket.org/pypy/pypy/
Python | 58 lines | 46 code | 9 blank | 3 comment | 0 complexity | 85a46f9f035971761e5e425d46e01d8a MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rlib import rsha
  2. from rpython.rlib.objectmodel import import_from_mixin
  3. from pypy.interpreter.baseobjspace import W_Root
  4. from pypy.interpreter.typedef import TypeDef
  5. from pypy.interpreter.gateway import interp2app, unwrap_spec
  6. class W_SHA(W_Root):
  7. """
  8. A subclass of RSHA that can be exposed to app-level.
  9. """
  10. import_from_mixin(rsha.RSHA)
  11. def __init__(self, space):
  12. self.space = space
  13. self._init()
  14. @unwrap_spec(string='bufferstr')
  15. def update_w(self, string):
  16. self.update(string)
  17. def digest_w(self):
  18. return self.space.wrap(self.digest())
  19. def hexdigest_w(self):
  20. return self.space.wrap(self.hexdigest())
  21. def copy_w(self):
  22. clone = W_SHA(self.space)
  23. clone._copyfrom(self)
  24. return self.space.wrap(clone)
  25. @unwrap_spec(initialdata='bufferstr')
  26. def W_SHA___new__(space, w_subtype, initialdata=''):
  27. """
  28. Create a new sha object and call its initializer.
  29. """
  30. w_sha = space.allocate_instance(W_SHA, w_subtype)
  31. sha = space.interp_w(W_SHA, w_sha)
  32. W_SHA.__init__(sha, space)
  33. sha.update(initialdata)
  34. return w_sha
  35. W_SHA.typedef = TypeDef(
  36. 'SHAType',
  37. __new__ = interp2app(W_SHA___new__),
  38. update = interp2app(W_SHA.update_w),
  39. digest = interp2app(W_SHA.digest_w),
  40. hexdigest = interp2app(W_SHA.hexdigest_w),
  41. copy = interp2app(W_SHA.copy_w),
  42. digest_size = 20,
  43. digestsize = 20,
  44. block_size = 64,
  45. __doc__ = """sha(arg) -> return new sha object.
  46. If arg is present, the method call update(arg) is made.""")