1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 PyDoc_STRVAR(memoryview_hex__doc__,
6 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
7 "--\n"
8 "\n"
9 "Return the data in the buffer as a str of hexadecimal numbers.\n"
10 "\n"
11 "  sep\n"
12 "    An optional single character or byte to separate hex bytes.\n"
13 "  bytes_per_sep\n"
14 "    How many bytes between separators.  Positive values count from the\n"
15 "    right, negative values count from the left.\n"
16 "\n"
17 "Example:\n"
18 ">>> value = memoryview(b\'\\xb9\\x01\\xef\')\n"
19 ">>> value.hex()\n"
20 "\'b901ef\'\n"
21 ">>> value.hex(\':\')\n"
22 "\'b9:01:ef\'\n"
23 ">>> value.hex(\':\', 2)\n"
24 "\'b9:01ef\'\n"
25 ">>> value.hex(\':\', -2)\n"
26 "\'b901:ef\'");
27 
28 #define MEMORYVIEW_HEX_METHODDEF    \
29     {"hex", (PyCFunction)(void(*)(void))memoryview_hex, METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
30 
31 static PyObject *
32 memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
33                     int bytes_per_sep);
34 
35 static PyObject *
memoryview_hex(PyMemoryViewObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)36 memoryview_hex(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
37 {
38     PyObject *return_value = NULL;
39     static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
40     static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
41     PyObject *argsbuf[2];
42     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
43     PyObject *sep = NULL;
44     int bytes_per_sep = 1;
45 
46     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
47     if (!args) {
48         goto exit;
49     }
50     if (!noptargs) {
51         goto skip_optional_pos;
52     }
53     if (args[0]) {
54         sep = args[0];
55         if (!--noptargs) {
56             goto skip_optional_pos;
57         }
58     }
59     if (PyFloat_Check(args[1])) {
60         PyErr_SetString(PyExc_TypeError,
61                         "integer argument expected, got float" );
62         goto exit;
63     }
64     bytes_per_sep = _PyLong_AsInt(args[1]);
65     if (bytes_per_sep == -1 && PyErr_Occurred()) {
66         goto exit;
67     }
68 skip_optional_pos:
69     return_value = memoryview_hex_impl(self, sep, bytes_per_sep);
70 
71 exit:
72     return return_value;
73 }
74 /*[clinic end generated code: output=ee265a73f68b0077 input=a9049054013a1b77]*/
75