diff options
Diffstat (limited to 'cpp/src/Slice/PythonUtil.cpp')
-rw-r--r-- | cpp/src/Slice/PythonUtil.cpp | 358 |
1 files changed, 41 insertions, 317 deletions
diff --git a/cpp/src/Slice/PythonUtil.cpp b/cpp/src/Slice/PythonUtil.cpp index 262f104c2bb..6b6493666bb 100644 --- a/cpp/src/Slice/PythonUtil.cpp +++ b/cpp/src/Slice/PythonUtil.cpp @@ -12,8 +12,6 @@ #include <Slice/Util.h> #include <IceUtil/IceUtil.h> #include <IceUtil/StringUtil.h> -#include <IceUtil/InputUtil.h> -#include <IceUtil/Unicode.h> #include <climits> #include <iterator> @@ -183,7 +181,7 @@ private: }; bool parseOpComment(const string&, OpComment&); - enum DocstringMode { DocSync, DocAsyncBegin, DocAsyncEnd, DocDispatch, DocAsyncDispatch }; + enum DocstringMode { DocSync, DocAsync, DocAsyncBegin, DocAsyncEnd, DocDispatch, DocAsyncDispatch }; void writeDocstring(const OperationPtr&, DocstringMode, bool); @@ -196,44 +194,6 @@ private: } } -string -u32CodePoint(unsigned int value) -{ - ostringstream s; - s << "\\U"; - s << hex; - s.width(8); - s.fill('0'); - s << value; - return s.str(); -} - -void -writeU8Buffer(const vector<unsigned char>& u8buffer, ostringstream& out) -{ - vector<unsigned int> u32buffer; - IceUtilInternal::ConversionResult result = convertUTF8ToUTF32(u8buffer, u32buffer, IceUtil::lenientConversion); - switch(result) - { - case conversionOK: - break; - case sourceExhausted: - throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "string source exhausted"); - case sourceIllegal: - throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "string source illegal"); - default: - { - assert(0); - throw IceUtil::IllegalConversionException(__FILE__, __LINE__); - } - } - - for(vector<unsigned int>::const_iterator c = u32buffer.begin(); c != u32buffer.end(); ++c) - { - out << u32CodePoint(*c); - } -} - static string lookupKwd(const string& name) { @@ -603,9 +563,9 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) for(OperationList::iterator oli = ops.begin(); oli != ops.end(); ++oli) { string fixedOpName = fixIdent((*oli)->name()); - if(!p->isLocal() && (p->hasMetaData("amd") || (*oli)->hasMetaData("amd"))) + if(!p->isLocal()) { - _out << sp << nl << "def " << (*oli)->name() << "_async(self, _cb"; + _out << sp << nl << "def " << fixedOpName << "(self"; ParamDeclList params = (*oli)->parameters(); @@ -735,6 +695,23 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) // Async operations. // _out << sp; + writeDocstring(*oli, DocAsync, false); + _out << nl << "def " << (*oli)->name() << "Async(self"; + if(!inParams.empty()) + { + _out << ", " << inParams; + } + _out << ", _ctx=None):"; + _out.inc(); + _out << nl << "return _M_" << abs << "._op_" << (*oli)->name() << ".invokeAsync(self, ((" << inParams; + if(!inParams.empty() && inParams.find(',') == string::npos) + { + _out << ", "; + } + _out << "), _ctx))"; + _out.dec(); + + _out << sp; writeDocstring(*oli, DocAsyncBegin, false); _out << nl << "def begin_" << (*oli)->name() << "(self"; if(!inParams.empty()) @@ -772,7 +749,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) _out << nl << "uncheckedCast = staticmethod(uncheckedCast)"; - // + // // ice_staticId // _out << sp << nl << "def ice_staticId():"; @@ -781,7 +758,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) _out.dec(); _out << nl << "ice_staticId = staticmethod(ice_staticId)"; - _out.dec(); + _out.dec(); _out << sp << nl << "_M_" << prxType << " = IcePy.defineProxy('" << scoped << "', " << prxName << ")"; } @@ -1102,9 +1079,9 @@ Slice::Python::CodeVisitor::visitExceptionStart(const ExceptionPtr& p) _out << sp << nl << "__repr__ = __str__"; // - // _ice_name + // _ice_id // - _out << sp << nl << "_ice_name = '" << scoped.substr(2) << "'"; + _out << sp << nl << "_ice_id = '" << scoped << "'"; _out.dec(); @@ -1651,6 +1628,7 @@ Slice::Python::CodeVisitor::writeType(const TypePtr& p) break; } case Builtin::KindObject: + case Builtin::KindValue: { _out << "IcePy._t_Object"; break; @@ -1714,6 +1692,7 @@ Slice::Python::CodeVisitor::writeInitializer(const DataMemberPtr& m) _out << "''"; break; } + case Builtin::KindValue: case Builtin::KindObject: case Builtin::KindObjectProxy: case Builtin::KindLocalObject: @@ -1875,274 +1854,17 @@ Slice::Python::CodeVisitor::writeConstantValue(const TypePtr& type, const Syntax } case Slice::Builtin::KindString: { - ostringstream sv2; - ostringstream sv3; - - // - // Expand strings into the basic source character set. We can't use isalpha() and the like - // here because they are sensitive to the current locale. - // - static const string basicSourceChars = "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789" - "_{}[]#()<>%:;.?*+-/^&|~!=, '"; - static const set<char> charSet(basicSourceChars.begin(), basicSourceChars.end()); + string sv2 = toStringLiteral(value, "\a\b\f\n\r\t\v", "", Octal, 0); + string sv3 = toStringLiteral(value, "\a\b\f\n\r\t\v", "", UCN, 0); - for(size_t i = 0; i < value.size();) + _out << "\"" << sv2<< "\""; + if(sv2 != sv3) { - char c = value[i]; - switch(c) - { - case '"': - { - sv2 << "\\\""; - break; - } - case '\\': - { - string s = "\\"; - size_t j = i + 1; - for(; j < value.size(); ++j) - { - if(value[j] != '\\') - { - break; - } - s += "\\"; - } - - // - // An even number of slash \ will escape the backslash and - // the codepoint will be interpreted as its charaters - // - // \\u00000041 - ['\\', 'u', '0', '0', '0', '0', '0', '0', '4', '1'] - // \\\u00000041 - ['\\', 'A'] (41 is the codepoint for 'A') - // - if(s.size() % 2 != 0 && (value[j] == 'U' || value[j] == 'u')) - { - // - // Convert codepoint to UTF8 bytes and write the escaped bytes - // - sv2 << s.substr(0, s.size() - 1); - - size_t sz = value[j] == 'U' ? 8 : 4; - string codepoint = value.substr(j + 1, sz); - assert(codepoint.size() == sz); - - IceUtil::Int64 v = IceUtilInternal::strToInt64(codepoint.c_str(), 0, 16); - - vector<unsigned int> u32buffer; - u32buffer.push_back(static_cast<unsigned int>(v)); - - vector<unsigned char> u8buffer; - IceUtilInternal::ConversionResult result = convertUTF32ToUTF8(u32buffer, u8buffer, IceUtil::lenientConversion); - switch(result) - { - case conversionOK: - break; - case sourceExhausted: - throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "string source exhausted"); - case sourceIllegal: - throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "string source illegal"); - default: - { - assert(0); - throw IceUtil::IllegalConversionException(__FILE__, __LINE__); - } - } - - ostringstream s; - for(vector<unsigned char>::const_iterator q = u8buffer.begin(); q != u8buffer.end(); ++q) - { - s << "\\"; - s.fill('0'); - s.width(3); - s << oct; - s << static_cast<unsigned int>(*q); - } - sv2 << s.str(); - - i = j + 1 + sz; - } - else - { - sv2 << s; - i = j; - } - continue; - } - case '\r': - { - sv2 << "\\r"; - break; - } - case '\n': - { - sv2 << "\\n"; - break; - } - case '\t': - { - sv2 << "\\t"; - break; - } - case '\b': - { - sv2 << "\\b"; - break; - } - case '\f': - { - sv2 << "\\f"; - break; - } - default: - { - if(charSet.find(c) == charSet.end()) - { - unsigned char uc = c; // Char may be signed, so make it positive. - stringstream s; - s << "\\"; // Print as octal if not in basic source character set. - s.flags(ios_base::oct); - s.width(3); - s.fill('0'); - s << static_cast<unsigned>(uc); - sv2 << s.str(); - } - else - { - sv2 << c; // Print normally if in basic source character set. - } - break; - } - } - ++i; - } - - vector<unsigned char> u8buffer; // Buffer to convert multibyte characters - - for(size_t i = 0; i < value.size();) - { - if(charSet.find(value[i]) == charSet.end()) - { - char c = value[i]; - if(static_cast<unsigned char>(c) < 128) // Single byte character - { - // - // Print as unicode if not in basic source character set - // - switch(c) - { - // - // Don't encode this special characters as universal characters - // - case '\r': - { - sv3 << "\\r"; - break; - } - case '\n': - { - sv3 << "\\n"; - break; - } - case '\\': - { - sv3 << "\\"; - break; - } - default: - { - sv3 << u32CodePoint(c); - break; - } - } - } - else - { - u8buffer.push_back(value[i]); - } - } - else - { - // - // Write any pedding characters in the utf8 buffer - // - if(!u8buffer.empty()) - { - writeU8Buffer(u8buffer, sv3); - u8buffer.clear(); - } - switch(value[i]) - { - case '\\': - { - string s = "\\"; - size_t j = i + 1; - for(; j < value.size(); ++j) - { - if(value[j] != '\\') - { - break; - } - s += "\\"; - } - - // - // An even number of slash \ will escape the backslash and - // the codepoint will be interpreted as its charaters - // - // \\U00000041 - ['\\', 'U', '0', '0', '0', '0', '0', '0', '4', '1'] - // \\\U00000041 - ['\\', 'A'] (41 is the codepoint for 'A') - // - if(s.size() % 2 != 0 && (value[j] == 'U' || value[j] == 'u')) - { - size_t sz = value[j] == 'U' ? 8 : 4; - sv3 << s.substr(0, s.size() - 1); - i = j + 1; - - string codepoint = value.substr(j + 1, sz); - assert(codepoint.size() == sz); - - IceUtil::Int64 v = IceUtilInternal::strToInt64(codepoint.c_str(), 0, 16); - sv3 << u32CodePoint(static_cast<unsigned int>(v)); - i = j + 1 + sz; - } - else - { - sv3 << s; - i = j; - } - continue; - } - case '"': - { - sv3 << "\\"; - break; - } - } - sv3 << value[i]; // Print normally if in basic source character set - } - i++; - } - - // - // Write any pedding characters in the utf8 buffer - // - if(!u8buffer.empty()) - { - writeU8Buffer(u8buffer, sv3); - u8buffer.clear(); - } - - - _out << "\"" << sv2.str() << "\""; - if(sv2.str() != sv3.str()) - { - _out << " if _version_info_[0] < 3 else \"" << sv3.str() << "\""; + _out << " if _version_info_[0] < 3 else \"" << sv3 << "\""; } break; } + case Slice::Builtin::KindValue: case Slice::Builtin::KindObject: case Slice::Builtin::KindObjectProxy: case Slice::Builtin::KindLocalObject: @@ -2781,7 +2503,7 @@ Slice::Python::CodeVisitor::writeDocstring(const OperationPtr& op, DocstringMode { return; } - else if(mode == DocAsyncBegin && inParams.empty()) + else if((mode == DocAsync || mode == DocAsyncBegin) && inParams.empty()) { return; } @@ -2814,6 +2536,7 @@ Slice::Python::CodeVisitor::writeDocstring(const OperationPtr& op, DocstringMode switch(mode) { case DocSync: + case DocAsync: case DocAsyncBegin: case DocDispatch: needArgs = !local || !inParams.empty(); @@ -2827,10 +2550,6 @@ Slice::Python::CodeVisitor::writeDocstring(const OperationPtr& op, DocstringMode if(needArgs) { _out << nl << "Arguments:"; - if(mode == DocAsyncDispatch) - { - _out << nl << "_cb -- The asynchronous callback object."; - } for(vector<string>::iterator q = inParams.begin(); q != inParams.end(); ++q) { string fixed = fixIdent(*q); @@ -2851,7 +2570,7 @@ Slice::Python::CodeVisitor::writeDocstring(const OperationPtr& op, DocstringMode << nl << "_ex -- The asynchronous exception callback." << nl << "_sent -- The asynchronous sent callback."; } - if(!local && (mode == DocSync || mode == DocAsyncBegin)) + if(!local && (mode == DocSync || mode == DocAsync || mode == DocAsyncBegin)) { _out << nl << "_ctx -- The request context for the invocation."; } @@ -2869,6 +2588,10 @@ Slice::Python::CodeVisitor::writeDocstring(const OperationPtr& op, DocstringMode // // Emit return value(s). // + if(mode == DocAsync || mode == DocAsyncDispatch) + { + _out << nl << "Returns: A future object for the invocation."; + } if(mode == DocAsyncBegin) { _out << nl << "Returns: An asynchronous result object for the invocation."; @@ -2936,7 +2659,8 @@ Slice::Python::CodeVisitor::writeDocstring(const OperationPtr& op, DocstringMode } void -Slice::Python::generate(const UnitPtr& un, bool all, bool checksum, const vector<string>& includePaths, Output& out) +Slice::Python::generate(const UnitPtr& un, bool all, bool checksum, const vector<string>& includePaths, + Output& out) { Slice::Python::MetaDataVisitor visitor; un->visit(&visitor, false); |