sapling/remotefilelog/ctreemanifest/pythonutil.cpp
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

63 lines
1.4 KiB
C++

// pythonutil.cpp - 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
#include "pythonutil.h"
PythonObj::PythonObj() :
obj(NULL) {
}
PythonObj::PythonObj(PyObject *obj) {
if (!obj) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"attempted to construct null PythonObj");
}
throw pyexception();
}
this->obj = obj;
}
PythonObj::PythonObj(const PythonObj& other) {
this->obj = other.obj;
Py_XINCREF(this->obj);
}
PythonObj::~PythonObj() {
Py_XDECREF(this->obj);
}
PythonObj& PythonObj::operator=(const PythonObj &other) {
Py_XDECREF(this->obj);
this->obj = other.obj;
Py_XINCREF(this->obj);
return *this;
}
PythonObj::operator PyObject* () const {
return this->obj;
}
/**
* 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 *PythonObj::returnval() {
Py_XINCREF(this->obj);
return this->obj;
}
/**
* Invokes getattr to retrieve the attribute from the python object.
*/
PythonObj PythonObj::getattr(const char *name) {
return PyObject_GetAttrString(this->obj, name);
}