Skip to content

Commit

Permalink
Drop Python2 support in CPP files
Browse files Browse the repository at this point in the history
  • Loading branch information
scotty007 committed Jun 25, 2020
1 parent cace56a commit dbe0101
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 108 deletions.
4 changes: 0 additions & 4 deletions src/player/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ void PluginManager::registerPlugin(void* handle, const std::string& sPluginName)

//TODO: Discuss –– maybe we shouldn't add plugins to the __builtins__ but rather
//have users ```import PLUGIN```explicitly
#if PY_MAJOR_VERSION < 3
PyObject* pyBuiltin = PyImport_AddModule("__builtin__");
#else
PyObject* pyBuiltin = PyImport_AddModule("builtins");
#endif
int success = PyModule_AddObject(pyBuiltin, sPluginName.c_str(), plugin);
AVG_ASSERT(success == 0);

Expand Down
12 changes: 0 additions & 12 deletions src/player/SubscriberInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ SubscriberInfo::SubscriberInfo(int id, PyObject* pCallable)
AVG_ASSERT(m_pWeakSelf != Py_None);
m_pPyFunction = PyWeakref_NewRef(PyMethod_Function(pCallable), NULL);
AVG_ASSERT(m_pPyFunction != Py_None);
#if PY_MAJOR_VERSION < 3
m_pWeakClass = PyWeakref_NewRef(PyMethod_Class(pCallable), NULL);
#endif

} else {
m_pPyFunction = pCallable;
Py_INCREF(m_pPyFunction); // We need to keep a reference to the unbound
Expand Down Expand Up @@ -88,17 +84,9 @@ void SubscriberInfo::invoke(py::list args) const {
AVG_ASSERT(pFunction != Py_None);
PyObject* pSelf = PyWeakref_GetObject(m_pWeakSelf);
AVG_ASSERT(pSelf != Py_None);
#if PY_MAJOR_VERSION < 3
PyObject* pClass = PyWeakref_GetObject(m_pWeakClass);
AVG_ASSERT(pClass != Py_None);
PyObject* pCallable = PyMethod_New(
pFunction, pSelf,
pClass); // Bind function to self --> creating a bound method
#else
PyObject* pCallable = PyMethod_New(
pFunction,
pSelf); // Bind function to self --> creating a bound method
#endif
AVG_ASSERT(pCallable != Py_None);
py::tuple argsTuple(args);
PyObject* pyResult = PyObject_CallObject(pCallable, argsTuple.ptr());
Expand Down
4 changes: 0 additions & 4 deletions src/player/WrapPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ void avgDeprecationWarning(const string& sVersion, const string& sOldEntryPoint,

PyFrameObject* pFrame = PyEval_GetFrame();
int lineNo = PyCode_Addr2Line(pFrame->f_code, pFrame->f_lasti);
#if PY_MAJOR_VERSION < 3
string sFName = getFilenamePart(PyString_AS_STRING(pFrame->f_code->co_filename));
#else
string sFName = getFilenamePart(PyUnicode_AS_DATA(pFrame->f_code->co_filename));
#endif
string sMsg = sFName + ":" + toString(lineNo) + ": ";
sMsg += string(sOldEntryPoint) + " deprecated since version " +
string(sVersion)+".";
Expand Down
9 changes: 1 addition & 8 deletions src/samples/firebirds/plugin/CollisionDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,5 @@ BOOST_PYTHON_MODULE(collisiondetector)

AVG_PLUGIN_API PyObject* registerPlugin()
{

#if PY_MAJOR_VERSION < 3
initcollisiondetector();
PyObject* pyCollisionDetectorModule = PyImport_ImportModule("collisiondetector");
#else
PyObject* pyCollisionDetectorModule = PyInit_collisiondetector(); // created by BOOST_PYTHON_MODULE
#endif
return pyCollisionDetectorModule;
return PyInit_collisiondetector(); // created by BOOST_PYTHON_MODULE
}
10 changes: 1 addition & 9 deletions src/test/plugin/ColorNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ BOOST_PYTHON_MODULE(colorplugin)
AVG_PLUGIN_API PyObject* registerPlugin()
{
avg::ColorNode::registerType();

#if PY_MAJOR_VERSION < 3
initcolorplugin();
PyObject* pyColorModule = PyImport_ImportModule("colorplugin");
#else
PyObject* pyColorModule = PyInit_colorplugin();
#endif

return pyColorModule;
return PyInit_colorplugin();
}

34 changes: 0 additions & 34 deletions src/wrapper/WrapHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,37 +394,6 @@ struct UTF8String_from_unicode
}
};

#if PY_MAJOR_VERSION < 3

struct UTF8String_from_string
{
UTF8String_from_string()
{
boost::python::converter::registry::push_back(
&convertible,
&construct,
boost::python::type_id<UTF8String>());
}

static void* convertible(PyObject* obj_ptr)
{
if (!PyString_Check(obj_ptr)) return 0;
return obj_ptr;
}

static void construct(PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
const char * psz = PyString_AsString(obj_ptr);
void* storage = (
(boost::python::converter::rvalue_from_python_storage<UTF8String>*)data)
->storage.bytes;
new (storage) UTF8String(psz);
data->convertible = storage;
}
};
#endif

void exportMessages(object& nodeClass, const string& sClassName)
{
PublisherDefinitionPtr pPubDef = PublisherDefinitionRegistry::get()
Expand Down Expand Up @@ -464,9 +433,6 @@ void export_base()
// string
to_python_converter<UTF8String, UTF8String_to_unicode>();
UTF8String_from_unicode();
#if PY_MAJOR_VERSION < 3
UTF8String_from_string();
#endif

// Exceptions
PyObject* pExceptionTypeObj = createExceptionClass("Exception");
Expand Down
39 changes: 2 additions & 37 deletions src/wrapper/bitmap_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,16 @@ struct Pixel32_to_python_tuple {
static bp::object Bitmap_getPixels(Bitmap& bitmap, bool bCopyData = true) {
unsigned char* pBuffer = bitmap.getPixels();
int buffSize = bitmap.getMemNeeded();
// TODO: both paths return the same for Python3 - correct?
if (bCopyData) {
#if PY_MAJOR_VERSION < 3
bp::object pyBuffer(handle<>(PyBuffer_New(buffSize)));
void* pTargetBuffer;
Py_ssize_t pyBuffSize = buffSize;
PyObject_AsWriteBuffer(pyBuffer.ptr(), &pTargetBuffer, &pyBuffSize);
memcpy(pTargetBuffer, pBuffer, buffSize);
return pyBuffer;
#else
//[todo] - Check pBuffer ownership
// TODO: Check pBuffer ownership
PyObject* py_memView =
PyMemoryView_FromMemory((char*)pBuffer, buffSize, PyBUF_READ);
return bp::object(handle<>(py_memView));
#endif
} else {
#if PY_MAJOR_VERSION < 3
return bp::object(handle<>(PyBuffer_FromMemory(pBuffer, buffSize)));
#else
PyObject* py_memView =
PyMemoryView_FromMemory((char*)pBuffer, buffSize, PyBUF_READ);
return bp::object(handle<>(py_memView));
#endif
}
}

Expand All @@ -122,29 +110,6 @@ static void Bitmap_setPixels(Bitmap& bitmap, PyObject* exporter, int stride=0)
const unsigned char* pBuf = reinterpret_cast<unsigned char*>(bufferView.buf);
bitmap.setPixels(pBuf, stride);
PyBuffer_Release(&bufferView);
#if PY_MAJOR_VERSION < 3
}
else if (PyBuffer_Check(exporter)) {
PyTypeObject* pType = exporter->ob_type;
PyBufferProcs* pProcs = pType->tp_as_buffer;
AVG_ASSERT(pProcs);
Py_ssize_t numBytes;
Py_ssize_t numSegments = pProcs->bf_getsegcount(exporter, &numBytes);
if (numBytes != bitmap.getMemNeeded()) {
throw Exception(
AVG_ERR_INVALID_ARGS,
"Second parameter to Bitmap.setPixels must fit bitmap size.");
}
// TODO: check if bitmap size is correct
unsigned char* pDestPixels = bitmap.getPixels();
for (unsigned i = 0; i < numSegments; ++i) {
void* pSrcPixels;
long bytesInSegment =
pProcs->bf_getreadbuffer(exporter, i, &pSrcPixels);
memcpy(pDestPixels, pSrcPixels, bytesInSegment);
pDestPixels += bytesInSegment;
}
#endif
} else {
throw Exception(AVG_ERR_INVALID_ARGS,
"Second parameter to Bitmap.setPixels must support the "
Expand Down

0 comments on commit dbe0101

Please sign in to comment.