-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPythonFileWrapper.h
More file actions
80 lines (63 loc) · 2.77 KB
/
Copy pathPythonFileWrapper.h
File metadata and controls
80 lines (63 loc) · 2.77 KB
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
//
// Created by Paul Ross on 08/07/2021.
//
#ifndef PYTHONEXTENSIONSBASIC_PYTHONFILEWRAPPER_H
#define PYTHONEXTENSIONSBASIC_PYTHONFILEWRAPPER_H
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#include <iostream>
#include <exception>
#include <string>
#include <utility>
//#include <utility>
class ExceptionPythonFileObjectWrapper : public std::exception {
public:
explicit ExceptionPythonFileObjectWrapper(std::string in_msg) : m_msg(std::move(in_msg)) {}
[[nodiscard]] const std::string &message() const { return m_msg; }
[[nodiscard]] const char *what() const
noexcept override{return m_msg.c_str();}
protected:
std::string m_msg{};
};
/// Class that is created with a PyObject* that looks like a Python File.
/// This can then read from that file object ans write to a user provided C++ stream or read from a user provided C++
/// stream and write to the give Python file like object.
class PythonFileObjectWrapper {
public:
explicit PythonFileObjectWrapper(PyObject *python_file_object);
/// Read from a Python file and write to the C++ stream.
/// Return zero on success, non-zero on failure.
int read_py_write_cpp(Py_ssize_t number_of_bytes, std::iostream &ios);
/// Read from a C++ stream and write to a Python file.
/// Return zero on success, non-zero on failure.
int read_cpp_write_py(std::iostream &ios, Py_ssize_t number_of_bytes);
/// Read a number of bytes from a Python file and load them into the result.
/// Return zero on success, non-zero on failure.
int read(Py_ssize_t number_of_bytes, std::vector<char> &result);
/// Write a number of bytes to a Python file.
/// Return zero on success, non-zero on failure.
int write(const char *buffer, Py_ssize_t number_of_bytes);
/// Move the file pointer to the given position.
/// whence is:
/// 0 – start of the stream (the default); offset should be zero or positive.
/// 1 – current stream position; offset may be negative.
/// 2 – end of the stream; offset is usually negative.
/// Returns the new absolute position.
long seek(Py_ssize_t pos, int whence = 0);
/// Returns the current absolute position.
long tell();
/// Returns a multi-line string that describes the class state.
std::string str_pointers() const;
/// Returns a Python multi-line bytes object that describes the class state.
PyObject *py_str_pointers() const;
/// Destructor, this decrements the held references.
virtual ~PythonFileObjectWrapper();
protected:
PyObject *m_python_file_object = NULL;
PyObject *m_python_read_method = NULL;
PyObject *m_python_write_method = NULL;
PyObject *m_python_seek_method = NULL;
PyObject *m_python_tell_method = NULL;
};
#endif //PYTHONEXTENSIONSBASIC_PYTHONFILEWRAPPER_H