Debian Codegraph @

pypy/module/cpyext/pystrhex.py

raw symbols SCIP
1 import sys
2 from pypy.module.cpyext.api import cts
3 from rpython.rlib.rstring import StringBuilder
4
5 @cts.decl("PyObject * _Py_strhex(const char* argbuf, const Py_ssize_t arglen)")
6 def _Py_strhex(space, argbuf, arglen):
7 s = strhex(argbuf, arglen)
8 return space.newutf8(s, len(s)) # contains ascii only
9
10 @cts.decl("PyObject * _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen)")
11 def _Py_strhex_bytes(space, argbuf, arglen):
12 s = strhex(argbuf, arglen)
13 return space.newbytes(s)
14
15 hexdigits = "0123456789abcdef"
16
17 def strhex(argbuf, arglen):
18 assert arglen >= 0
19 if arglen > sys.maxint / 2:
20 raise MemoryError
21 builder = StringBuilder(arglen * 2)
22 for i in range(arglen):
23 b = ord(argbuf[i])
24 builder.append(hexdigits[(b >> 4) & 0xf])
25 builder.append(hexdigits[b & 0xf])
26 return builder.build()