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

/pypy/module/_md5/interp_md5.py

https://bitbucket.org/pypy/pypy/
Python | 58 lines | 46 code | 9 blank | 3 comment | 0 complexity | 0acf64c41dd2263a95294d80762640c6 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rlib import rmd5
  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_MD5(W_Root):
  7. """
  8. A subclass of RMD5 that can be exposed to app-level.
  9. """
  10. import_from_mixin(rmd5.RMD5)
  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.newbytes(self.digest())
  19. def hexdigest_w(self):
  20. return self.space.wrap(self.hexdigest())
  21. def copy_w(self):
  22. clone = W_MD5(self.space)
  23. clone._copyfrom(self)
  24. return self.space.wrap(clone)
  25. @unwrap_spec(initialdata='bufferstr')
  26. def W_MD5___new__(space, w_subtype, initialdata=''):
  27. """
  28. Create a new md5 object and call its initializer.
  29. """
  30. w_md5 = space.allocate_instance(W_MD5, w_subtype)
  31. md5 = space.interp_w(W_MD5, w_md5)
  32. W_MD5.__init__(md5, space)
  33. md5.update(initialdata)
  34. return w_md5
  35. W_MD5.typedef = TypeDef(
  36. 'MD5Type',
  37. __new__ = interp2app(W_MD5___new__),
  38. update = interp2app(W_MD5.update_w),
  39. digest = interp2app(W_MD5.digest_w),
  40. hexdigest = interp2app(W_MD5.hexdigest_w),
  41. copy = interp2app(W_MD5.copy_w),
  42. digest_size = 16,
  43. digestsize = 16,
  44. block_size = 64,
  45. __doc__ = """md5(arg) -> return new md5 object.
  46. If arg is present, the method call update(arg) is made.""")