sapling/remotefilelog/ctreemanifest/pythonutil.h
Tony Tung 33f0d41725 [ctree] modularize the code
Summary:
I like many small files.

There is one place where I'm making a functional change (convert.h) to satisfy angry compilers.

Test Plan: make local.

Reviewers: #fastmanifest, durham

Reviewed By: durham

Subscribers: mitrandir

Differential Revision: https://phabricator.intern.facebook.com/D3732584

Signature: t1:3732584:1471542758:d0b7804753ea4fd39a507090338ae3c5104dc7fa
2016-08-22 15:40:56 -07:00

67 lines
1.6 KiB
C++

// pythonutil.h - utilities to glue C++ code to python
//
// Copyright 2016 Facebook, Inc.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//
// no-check-code
#ifndef REMOTEFILELOG_PYTHONOBJ_H
#define REMOTEFILELOG_PYTHONOBJ_H
// The PY_SSIZE_T_CLEAN define must be defined before the Python.h include,
// as per the documentation.
#define PY_SSIZE_T_CLEAN
#include <exception>
#include <Python.h>
/**
* C++ exception that represents an issue at the python C api level.
* When this is thrown, it's assumed that the python error message has been set
* and that the catcher of the exception should just return an error code value
* to the python API.
*/
class pyexception : public std::exception {
public:
pyexception() {
}
};
/**
* Wrapper class for PyObject pointers.
* It is responsible for managing the Py_INCREF and Py_DECREF calls.
*/
class PythonObj {
private:
PyObject *obj;
public:
PythonObj();
PythonObj(PyObject *obj);
PythonObj(const PythonObj& other);
~PythonObj();
PythonObj& operator=(const PythonObj &other);
operator PyObject* () const;
/**
* Function used to obtain a return value that will persist beyond the life
* of the PythonObj. This is useful for returning objects to Python C apis
* and letting them manage the remaining lifetime of the object.
*/
PyObject *returnval();
/**
* Invokes getattr to retrieve the attribute from the python object.
*/
PythonObj getattr(const char *name);
};
#endif //REMOTEFILELOG_PYTHONOBJ_H