Skip to content

Commit

Permalink
Simplify Python generated code (#2443)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Jul 10, 2024
1 parent 4dad3ba commit e2afab0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 68 deletions.
59 changes: 6 additions & 53 deletions cpp/src/slice2py/PythonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,6 @@ lookupKwd(const string& name)
return found ? "_" + name : name;
}

static string
getDictLookup(const ContainedPtr& cont, const string& suffix = "", const string& prefix = "")
{
string scope = Slice::Python::scopedToName(cont->scope());
assert(!scope.empty());

string package = Slice::Python::getPackageMetadata(cont);
if (!package.empty())
{
scope = package + "." + scope;
}

return "'" + suffix + Slice::Python::fixIdent(cont->name() + prefix) + "' not in _M_" + scope + "__dict__";
}

//
// ModuleVisitor implementation.
//
Expand Down Expand Up @@ -377,10 +362,7 @@ Slice::Python::CodeVisitor::visitClassDecl(const ClassDeclPtr& p)
string scoped = p->scoped();
if (_classHistory.count(scoped) == 0)
{
_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.declareValue('" << scoped << "')";
_out.dec();
_classHistory.insert(scoped); // Avoid redundant declarations.
}
}
Expand All @@ -394,11 +376,8 @@ Slice::Python::CodeVisitor::visitInterfaceDecl(const InterfaceDeclPtr& p)
string scoped = p->scoped();
if (_classHistory.count(scoped) == 0)
{
_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_", "Disp") << " = IcePy.declareClass('" << scoped << "')";
_out << nl << "_M_" << getAbsolute(p, "_t_", "Prx") << " = IcePy.declareProxy('" << scoped << "')";
_out.dec();
_classHistory.insert(scoped); // Avoid redundant declarations.
}
}
Expand Down Expand Up @@ -471,9 +450,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
string valueName = fixIdent(p->name());
ClassDefPtr base = p->base();

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = Ice.createTempClass()";
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << valueName << '(';
if (!base)
{
Expand Down Expand Up @@ -621,8 +598,6 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)

registerName(valueName);

_out.dec();

if (_classHistory.count(scoped) == 0)
{
_classHistory.insert(scoped); // Avoid redundant declarations.
Expand Down Expand Up @@ -650,11 +625,8 @@ Slice::Python::CodeVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
writeMetaData(p->getMetaData());
_out << ", True, None, ())";

_out << sp << nl << "if " << getDictLookup(p, "", "Prx") << ':';
_out.inc();

// Define the proxy class
_out << nl << "_M_" << prxAbs << " = Ice.createTempClass()";
_out << nl << "_M_" << prxAbs << " = None";
_out << nl << "class " << prxName << '(';

{
Expand Down Expand Up @@ -802,7 +774,7 @@ Slice::Python::CodeVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
registerName(prxName);

// Define the servant class
_out << sp << nl << "_M_" << classAbs << " = Ice.createTempClass()";
_out << sp << nl << "_M_" << classAbs << " = None";
_out << nl << "class " << className << '(';
{
vector<string> baseClasses;
Expand Down Expand Up @@ -1025,7 +997,6 @@ Slice::Python::CodeVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
}

registerName(className);
_out.dec();

if (_classHistory.count(scoped) == 0)
{
Expand All @@ -1042,9 +1013,7 @@ Slice::Python::CodeVisitor::visitExceptionStart(const ExceptionPtr& p)
string abs = getAbsolute(p);
string name = fixIdent(p->name());

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = Ice.createTempClass()";
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << name << '(';
ExceptionPtr base = p->base();
string baseName;
Expand Down Expand Up @@ -1172,8 +1141,6 @@ Slice::Python::CodeVisitor::visitExceptionStart(const ExceptionPtr& p)

registerName(name);

_out.dec();

return false;
}

Expand All @@ -1196,9 +1163,7 @@ Slice::Python::CodeVisitor::visitStructStart(const StructPtr& p)
}
}

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = Ice.createTempClass()";
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << name << "(object):";
_out.inc();

Expand Down Expand Up @@ -1445,8 +1410,6 @@ Slice::Python::CodeVisitor::visitStructStart(const StructPtr& p)

registerName(name);

_out.dec();

return false;
}

Expand Down Expand Up @@ -1474,8 +1437,6 @@ Slice::Python::CodeVisitor::visitSequence(const SequencePtr& p)

// Emit the type information.
string scoped = p->scoped();
_out << sp << nl << "if " << getDictLookup(p, "_t_") << ':';
_out.inc();
if (isCustom)
{
string package = customType.substr(0, customType.find('.'));
Expand All @@ -1491,24 +1452,20 @@ Slice::Python::CodeVisitor::visitSequence(const SequencePtr& p)
writeType(p->type());
_out << ")";
}
_out.dec();
}

void
Slice::Python::CodeVisitor::visitDictionary(const DictionaryPtr& p)
{
// Emit the type information.
string scoped = p->scoped();
_out << sp << nl << "if " << getDictLookup(p, "_t_") << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineDictionary('" << scoped << "', ";
writeMetaData(p->getMetaData());
_out << ", ";
writeType(p->keyType());
_out << ", ";
writeType(p->valueType());
_out << ")";
_out.dec();
}

void
Expand All @@ -1520,9 +1477,7 @@ Slice::Python::CodeVisitor::visitEnum(const EnumPtr& p)
EnumeratorList enums = p->enumerators();
EnumeratorList::iterator q;

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = Ice.createTempClass()";
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << name << "(Ice.EnumBase):";
_out.inc();

Expand Down Expand Up @@ -1572,8 +1527,6 @@ Slice::Python::CodeVisitor::visitEnum(const EnumPtr& p)
_out << ", " << name << "._enumerators)";

registerName(name);

_out.dec();
}

void
Expand Down
23 changes: 15 additions & 8 deletions python/modules/IcePy/Slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@
#include "Util.h"
#include "slice2py/PythonUtil.h"

#include <set>
#include <string>

//
// Python headers needed for PyEval_EvalCode.
//
#include <ceval.h>
#include <compile.h>
// Use ceval.h instead of eval.h with Python 3.11 and greater
#if PY_VERSION_HEX >= 0x030B0000
# include <ceval.h>
#else
# include <eval.h>
#endif

using namespace std;
using namespace IcePy;
using namespace Slice;
using namespace Slice::Python;
using namespace IceInternal;

namespace
{
set<string> loadedSliceFiles;
}

extern "C" PyObject*
IcePy_loadSlice(PyObject* /*self*/, PyObject* args)
{
Expand Down Expand Up @@ -123,9 +126,13 @@ IcePy_loadSlice(PyObject* /*self*/, PyObject* args)

bool keepComments = true;

for (vector<string>::const_iterator p = files.begin(); p != files.end(); ++p)
for (const auto& file : files)
{
string file = *p;
if (!loadedSliceFiles.insert(Slice::fullPath(file)).second)
{
continue;
}

Slice::PreprocessorPtr icecpp = Slice::Preprocessor::create("icecpp", file, cppArgs);
FILE* cppHandle = icecpp->preprocess(keepComments, "-D__SLICE2PY__");

Expand Down
7 changes: 0 additions & 7 deletions python/python/Ice/ModuleUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,3 @@ def updateModules():
else:
sys.modules[name] = _pendingModules[name]
_pendingModules = {}


def createTempClass():
class __temp:
pass

return __temp

0 comments on commit e2afab0

Please sign in to comment.