1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2015 Red Hat Inc
*
* Author: John Spray <john.spray@redhat.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef PY_FORMATTER_H_
#define PY_FORMATTER_H_
// Python.h comes first because otherwise it clobbers ceph's assert
#include "PythonCompat.h"
#include <stack>
#include <memory>
#include <list>
#include "common/Formatter.h"
#include "include/ceph_assert.h"
class PyFormatter : public ceph::Formatter
{
public:
PyFormatter (const PyFormatter&) = delete;
PyFormatter& operator= (const PyFormatter&) = delete;
PyFormatter(bool pretty = false, bool array = false)
{
// It is forbidden to instantiate me outside of the GIL,
// because I construct python objects right away
// Initialise cursor to an empty dict
if (!array) {
root = cursor = PyDict_New();
} else {
root = cursor = PyList_New(0);
}
}
~PyFormatter() override
{
cursor = NULL;
Py_DECREF(root);
root = NULL;
}
// Obscure, don't care.
void open_array_section_in_ns(const char *name, const char *ns) override
{ceph_abort();}
void open_object_section_in_ns(const char *name, const char *ns) override
{ceph_abort();}
void reset() override
{
const bool array = PyList_Check(root);
Py_DECREF(root);
if (array) {
root = cursor = PyList_New(0);
} else {
root = cursor = PyDict_New();
}
}
void set_status(int status, const char* status_name) override {}
void output_header() override {};
void output_footer() override {};
void enable_line_break() override {};
void open_array_section(const char *name) override;
void open_object_section(const char *name) override;
void close_section() override
{
ceph_assert(cursor != root);
ceph_assert(!stack.empty());
cursor = stack.top();
stack.pop();
}
void dump_bool(const char *name, bool b) override;
void dump_unsigned(const char *name, uint64_t u) override;
void dump_int(const char *name, int64_t u) override;
void dump_float(const char *name, double d) override;
void dump_string(const char *name, std::string_view s) override;
std::ostream& dump_stream(const char *name) override;
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
void flush(std::ostream& os) override
{
// This class is not a serializer: this doesn't make sense
ceph_abort();
}
int get_len() const override
{
// This class is not a serializer: this doesn't make sense
ceph_abort();
return 0;
}
void write_raw_data(const char *data) override
{
// This class is not a serializer: this doesn't make sense
ceph_abort();
}
PyObject *get()
{
finish_pending_streams();
Py_INCREF(root);
return root;
}
void finish_pending_streams();
private:
PyObject *root;
PyObject *cursor;
std::stack<PyObject *> stack;
void dump_pyobject(const char *name, PyObject *p);
class PendingStream {
public:
PyObject *cursor;
std::string name;
std::stringstream stream;
};
std::list<std::shared_ptr<PendingStream> > pending_streams;
};
#endif
|