Skip to content

Commit

Permalink
Base: remove some more deprecated Py2 code
Browse files Browse the repository at this point in the history
and remove deprecated void from function signature and replace 0 or NULL with nullptr
  • Loading branch information
wwmayer committed Apr 26, 2021
1 parent 142c2c2 commit af16ab0
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 452 deletions.
93 changes: 39 additions & 54 deletions src/Base/Interpreter.cpp
Expand Up @@ -46,10 +46,6 @@ unsigned int format2_len = 1024;

using namespace Base;

#if PY_VERSION_HEX <= 0x02050000
#error "Use Python2.5.x or higher"
#endif

PyException::PyException(const Py::Object &obj) {
_sErrMsg = obj.as_string();
// WARNING: we are assuming that python type object will never be
Expand All @@ -59,7 +55,7 @@ PyException::PyException(const Py::Object &obj) {
_errorType = obj.ptr()->ob_type->tp_name;
}

PyException::PyException(void)
PyException::PyException()
{
PP_Fetch_Error_Text(); /* fetch (and clear) exception */

Expand Down Expand Up @@ -104,7 +100,7 @@ PyException::~PyException() throw()
{
}

void PyException::ThrowException(void)
void PyException::ThrowException()
{
PyException myexcp;
myexcp.ReportException();
Expand All @@ -113,11 +109,10 @@ void PyException::ThrowException(void)

void PyException::raiseException() {
PyGILStateLocker locker;

if (PP_PyDict_Object!=NULL) {
if (PP_PyDict_Object!=nullptr) {
// delete the Python dict upon destruction of edict
Py::Dict edict(PP_PyDict_Object, true);
PP_PyDict_Object = 0;
PP_PyDict_Object = nullptr;

std::string exceptionname;
if (_exceptionType == Base::BaseExceptionFreeCADAbort)
Expand All @@ -137,7 +132,7 @@ void PyException::raiseException() {
throw *this;
}

void PyException::ReportException (void) const
void PyException::ReportException () const
{
if (!_isReported) {
_isReported = true;
Expand Down Expand Up @@ -178,7 +173,7 @@ SystemExitException::SystemExitException()

if (value) {
code = PyObject_GetAttrString(value, "code");
if (code != NULL && value != Py_None) {
if (code != nullptr && value != Py_None) {
Py_DECREF(value);
value = code;
}
Expand All @@ -203,7 +198,7 @@ SystemExitException::SystemExitException()
class PythonStdOutput : public Py::PythonExtension<PythonStdOutput>
{
public:
static void init_type(void)
static void init_type()
{
behaviors().name("PythonStdOutput");
behaviors().doc("Python standard output");
Expand Down Expand Up @@ -232,7 +227,7 @@ class PythonStdOutput : public Py::PythonExtension<PythonStdOutput>

InterpreterSingleton::InterpreterSingleton()
{
this->_global = 0;
this->_global = nullptr;
}

InterpreterSingleton::~InterpreterSingleton()
Expand All @@ -247,10 +242,10 @@ std::string InterpreterSingleton::runString(const char *sCmd)

PyGILStateLocker locker;
module = PP_Load_Module("__main__"); /* get module, init python */
if (module == NULL)
if (module == nullptr)
throw PyException(); /* not incref'd */
dict = PyModule_GetDict(module); /* get dict namespace */
if (dict == NULL)
if (dict == nullptr)
throw PyException(); /* not incref'd */


Expand Down Expand Up @@ -324,10 +319,10 @@ Py::Object InterpreterSingleton::runStringObject(const char *sCmd)

PyGILStateLocker locker;
module = PP_Load_Module("__main__"); /* get module, init python */
if (module == NULL)
if (module == nullptr)
throw PyException(); /* not incref'd */
dict = PyModule_GetDict(module); /* get dict namespace */
if (dict == NULL)
if (dict == nullptr)
throw PyException(); /* not incref'd */


Expand All @@ -342,15 +337,15 @@ Py::Object InterpreterSingleton::runStringObject(const char *sCmd)
return Py::asObject(presult);
}

void InterpreterSingleton::systemExit(void)
void InterpreterSingleton::systemExit()
{
/* This code is taken from the original Python code */
PyObject *exception, *value, *tb;
int exitcode = 0;

PyErr_Fetch(&exception, &value, &tb);
fflush(stdout);
if (value == NULL || value == Py_None)
if (value == nullptr || value == Py_None)
goto done;
if (PyExceptionInstance_Check(value)) {
/* The error code should be in the `code' attribute. */
Expand All @@ -364,8 +359,9 @@ void InterpreterSingleton::systemExit(void)
/* If we failed to dig out the 'code' attribute,
just let the else clause below print the error. */
}
if (PyLong_Check(value))
if (PyLong_Check(value)) {
exitcode = (int)PyLong_AsLong(value);
}
else {
PyObject_Print(value, stderr, Py_PRINT_RAW);
PySys_WriteStderr("\n");
Expand All @@ -389,10 +385,10 @@ void InterpreterSingleton::runInteractiveString(const char *sCmd)

PyGILStateLocker locker;
module = PP_Load_Module("__main__"); /* get module, init python */
if (module == NULL)
if (module == nullptr)
throw PyException(); /* not incref'd */
dict = PyModule_GetDict(module); /* get dict namespace */
if (dict == NULL)
if (dict == nullptr)
throw PyException(); /* not incref'd */

presult = PyRun_String(sCmd, Py_single_input, dict, dict); /* eval direct */
Expand Down Expand Up @@ -442,9 +438,9 @@ void InterpreterSingleton::runFile(const char*pxFileName, bool local)
Py_INCREF(dict); // avoid to further distinguish between local and global dict
}

if (PyDict_GetItemString(dict, "__file__") == NULL) {
if (PyDict_GetItemString(dict, "__file__") == nullptr) {
PyObject *f = PyUnicode_FromString(pxFileName);
if (f == NULL) {
if (f == nullptr) {
fclose(fp);
Py_DECREF(dict);
return;
Expand Down Expand Up @@ -514,14 +510,10 @@ void InterpreterSingleton::addPythonPath(const char* Path)
const char* InterpreterSingleton::init(int argc,char *argv[])
{
if (!Py_IsInitialized()) {
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION >= 5
Py_SetProgramName(Py_DecodeLocale(argv[0],NULL));
#else
Py_SetProgramName(_Py_char2wchar(argv[0],NULL));
#endif
#if PY_VERSION_HEX >= 0x03050000
Py_SetProgramName(Py_DecodeLocale(argv[0],nullptr));
#else
Py_SetProgramName(argv[0]);
Py_SetProgramName(_Py_char2wchar(argv[0],nullptr));
#endif
// There is a serious bug in VS from 2010 until 2013 where the file descriptor for stdin, stdout or stderr
// returns a valid value for GUI applications (i.e. subsystem = Windows) where it shouldn't.
Expand Down Expand Up @@ -551,22 +543,15 @@ const char* InterpreterSingleton::init(int argc,char *argv[])
size_t size = argc;
static std::vector<wchar_t *> _argv(size);
for (int i = 0; i < argc; i++) {
#if PY_MINOR_VERSION >= 5
_argv[i] = Py_DecodeLocale(argv[i],NULL);
#else
_argv[i] = _Py_char2wchar(argv[i],NULL);
#endif
_argv[i] = Py_DecodeLocale(argv[i],nullptr);
}
PySys_SetArgv(argc, _argv.data());
PythonStdOutput::init_type();
this->_global = PyEval_SaveThread();
}

PyGILStateLocker lock;
#if PY_MINOR_VERSION >= 5
return Py_EncodeLocale(Py_GetPath(),NULL);
#else
return _Py_wchar2char(Py_GetPath(),NULL);
#endif
return Py_EncodeLocale(Py_GetPath(),nullptr);
}

void InterpreterSingleton::replaceStdOutput()
Expand All @@ -577,7 +562,7 @@ void InterpreterSingleton::replaceStdOutput()
PySys_SetObject("stderr", out);
}

int InterpreterSingleton::cleanup(void (*func)(void))
int InterpreterSingleton::cleanup(void (*func)())
{
return Py_AtExit( func );
}
Expand Down Expand Up @@ -610,22 +595,22 @@ void InterpreterSingleton::runStringArg(const char * psCom,...)

// Singelton:

InterpreterSingleton * InterpreterSingleton::_pcSingelton = 0;
InterpreterSingleton * InterpreterSingleton::_pcSingelton = nullptr;

InterpreterSingleton & InterpreterSingleton::Instance(void)
InterpreterSingleton & InterpreterSingleton::Instance()
{
// not initialized!
if (!_pcSingelton)
_pcSingelton = new InterpreterSingleton();
return *_pcSingelton;
}

void InterpreterSingleton::Destruct(void)
void InterpreterSingleton::Destruct()
{
// not initialized or double destruct!
assert(_pcSingelton);
delete _pcSingelton;
_pcSingelton = 0;
_pcSingelton = nullptr;
}

int InterpreterSingleton::runCommandLine(const char *prompt)
Expand All @@ -643,8 +628,8 @@ void InterpreterSingleton::runMethodVoid(PyObject *pobject, const char *method)
PyGILStateLocker locker;
if (PP_Run_Method(pobject , // object
method, // run method
0, // no return type
0, // so no return object
nullptr, // no return type
nullptr, // so no return object
"()") // no arguments
!= 0)
throw PyException(/*"Error running InterpreterSingleton::RunMethodVoid()"*/);
Expand Down Expand Up @@ -677,15 +662,15 @@ void InterpreterSingleton::runMethod(PyObject *pobject, const char *method,

PyGILStateLocker locker;
pmeth = PyObject_GetAttrString(pobject, method);
if (pmeth == NULL) { /* get callable object */
if (pmeth == nullptr) { /* get callable object */
va_end(argslist);
throw AttributeError("Error running InterpreterSingleton::RunMethod() method not defined"); /* bound method? has self */
}

pargs = Py_VaBuildValue(argfmt, argslist); /* args: c->python */
va_end(argslist);

if (pargs == NULL) {
if (pargs == nullptr) {
Py_DECREF(pmeth);
throw TypeError("InterpreterSingleton::RunMethod() wrong arguments");
}
Expand All @@ -711,10 +696,10 @@ PyObject * InterpreterSingleton::getValue(const char * key, const char * result_

PyGILStateLocker locker;
module = PP_Load_Module("__main__"); /* get module, init python */
if (module == NULL)
if (module == nullptr)
throw PyException(); /* not incref'd */
dict = PyModule_GetDict(module); /* get dict namespace */
if (dict == NULL)
if (dict == nullptr)
throw PyException(); /* not incref'd */


Expand Down Expand Up @@ -745,7 +730,7 @@ void InterpreterSingleton::dbgUnsetBreakPoint(unsigned int /*uiLineNumber*/)

}

void InterpreterSingleton::dbgStep(void)
void InterpreterSingleton::dbgStep()
{

}
Expand Down Expand Up @@ -832,7 +817,7 @@ namespace Swig_python { extern int createSWIGPointerObj_T(const char* TypeName,
PyObject* InterpreterSingleton::createSWIGPointerObj(const char* Module, const char* TypeName, void* Pointer, int own)
{
int result = 0;
PyObject* proxy=0;
PyObject* proxy=nullptr;
PyGILStateLocker locker;
(void)Module;
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
Expand Down

0 comments on commit af16ab0

Please sign in to comment.