/usr/share/pyshared/omniORB/any.py is in python-omniorb 3.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | # -*- Mode: Python; -*-
# Package : omniORBpy
# any.py Created on: 2002/09/16
# Author : Duncan Grisby (dgrisby)
#
# Copyright (C) 2002-2008 Apasphere Ltd
#
# This file is part of the omniORBpy library
#
# The omniORBpy library is free software; you can redistribute it
# and/or modify it under the terms of the GNU Lesser General
# Public License as published by the Free Software Foundation;
# either version 2.1 of the License, or (at your option) any later
# version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA
#
#
# Description:
# Utility functions for working with Anys.
"""
omniORB.any module -- Any support functions.
to_any(data) -- try to coerce data to an Any.
from_any(any, keep_structs=0) -- return any's contents as plain Python
objects. If keep_structs is true,
CORBA structs are kept as Python class
instances; if false, they are expanded
to dictionaries.
"""
from types import *
import omniORB
import CORBA, tcInternal
import random
import threading
__all__ = ["to_any", "from_any"]
# Counter for generating repoIds. Ideally, should not clash with other
# IDs, but this will do for now...
random.seed()
_idbase = "%08x" % random.randrange(0, 0x7fffffff)
_idcount = 0
_idlock = threading.Lock()
# TypeCode kinds that must not be used in struct / sequence members
INVALID_MEMBER_KINDS = [ tcInternal.tv_null,
tcInternal.tv_void,
tcInternal.tv_except ]
# Fudge things for Pythons without unicode / bool
try:
UnicodeType
except NameError:
class UnicodeType:
pass
try:
BooleanType
except NameError:
class BooleanType:
pass
try:
bool(1)
except:
def bool(x): return x
# Fixed type
_f = CORBA.fixed(0)
FixedType = type(_f)
def to_any(data):
"""to_any(data) -- try to return data as a CORBA.Any"""
tc, val = _to_tc_value(data)
return CORBA.Any(tc, val)
def _to_tc_value(data):
"""_to_tc_value(data) -- return TypeCode and value for Any insertion"""
if data is None:
return CORBA.TC_null, None
elif isinstance(data, StringType):
return CORBA.TC_string, data
elif isinstance(data, UnicodeType):
return CORBA.TC_wstring, data
elif isinstance(data, BooleanType):
return CORBA.TC_boolean, data
elif isinstance(data, IntType) or isinstance(data, LongType):
if -2147483648L <= data <= 2147483647L:
return CORBA.TC_long, int(data)
elif 0 <= data <= 4294967295L:
return CORBA.TC_ulong, data
elif -9223372036854775808L <= data <= 9223372036854775807L:
return CORBA.TC_longlong, data
elif 0 <= data <= 18446744073709551615L:
return CORBA.TC_ulonglong, data
else:
raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_PythonValueOutOfRange,
CORBA.COMPLETED_NO)
elif isinstance(data, FloatType):
return CORBA.TC_double, data
elif isinstance(data, ListType):
if data == []:
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_any, 0))
return tc, data
d0 = data[0]
if isinstance(d0, StringType):
for d in data:
if not isinstance(d, StringType):
break
else:
# List of strings
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
CORBA.TC_string._d, 0))
return tc, data
elif isinstance(d0, UnicodeType):
for d in data:
if not isinstance(d, UnicodeType):
break
else:
# List of wstrings
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
CORBA.TC_wstring._d, 0))
return tc, data
elif isinstance(d0, BooleanType):
for d in data:
if not isinstance(d, BooleanType):
break
else:
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_boolean, 0))
return tc, data
elif isinstance(d0, IntType) or isinstance(d0, LongType):
# Numeric. Try to find a numeric type suitable for the whole list
min_v = max_v = 0
for d in data:
if (not (isinstance(d, IntType) or isinstance(d, LongType)) or
isinstance(d, BooleanType)):
break
if d < min_v: min_v = d
if d > max_v: max_v = d
else:
if min_v >= -2147483648L and max_v <= 2147483647L:
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_long, 0))
return tc, map(int, data)
elif min_v >= 0 and max_v <= 4294967295L:
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_ulong, 0))
return tc, map(long, data)
elif (min_v >= -9223372036854775808L and
max_v <= 9223372036854775807L):
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_longlong, 0))
return tc, map(long, data)
elif min_v >= 0 and max_v <= 18446744073709551615L:
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_ulonglong,0))
return tc, map(long, data)
else:
raise CORBA.BAD_PARAM(
omniORB.BAD_PARAM_PythonValueOutOfRange,
CORBA.COMPLETED_NO)
elif isinstance(d0, FloatType):
for d in data:
if not isinstance(d, FloatType):
break
else:
# List of doubles
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_double, 0))
return tc, data
elif isinstance(d0, CORBA.Any):
for d in data:
if not isinstance(d, CORBA.Any):
break
else:
# List of anys
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_any, 0))
return tc, data
# Generic list
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
tcInternal.tv_any, 0))
any_list = map(to_any, data)
atc = any_list[0]._t
if atc._k._v not in INVALID_MEMBER_KINDS:
for a in any_list:
if not a._t.equivalent(atc):
break
else:
tc = tcInternal.createTypeCode((tcInternal.tv_sequence,
atc._d, 0))
for i in range(len(any_list)):
any_list[i] = any_list[i]._v
return tc, any_list
elif isinstance(data, TupleType):
return _to_tc_value(list(data))
elif isinstance(data, DictType):
# Represent dictionaries as structs
global _idcount
_idlock.acquire()
try:
_idcount = _idcount + 1
id = "omni:%s:%08x" % (_idbase, _idcount)
finally:
_idlock.release()
dl = [tcInternal.tv_struct, None, id, ""]
ms = []
svals = []
items = data.items()
for (k,v) in items:
if not isinstance(k, StringType):
raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
CORBA.COMPLETED_NO)
t, v = _to_tc_value(v)
if t._k._v in INVALID_MEMBER_KINDS:
v = CORBA.Any(t,v)
t = CORBA.TC_any
ms.append(k)
dl.append(k)
dl.append(t._d)
svals.append(v)
cls = omniORB.createUnknownStruct(id, ms)
dl[1] = cls
tc = tcInternal.createTypeCode(tuple(dl))
value = apply(cls, svals)
return tc, value
elif isinstance(data, FixedType):
if data == CORBA.fixed(0):
tc = tcInternal.createTypeCode((tcInternal.tv_fixed, 1, 0))
else:
tc = tcInternal.createTypeCode((tcInternal.tv_fixed,
data.precision(), data.decimals()))
return tc, data
elif isinstance(data, CORBA.Any):
return data._t, data._v
elif isinstance(data, omniORB.EnumItem):
return omniORB.findTypeCode(data._parent_id), data
elif hasattr(data, "_NP_RepositoryId"):
return omniORB.findTypeCode(data._NP_RepositoryId), data
raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
CORBA.COMPLETED_NO)
def from_any(any, keep_structs=0):
"""from_any(any, keep_structs=0) -- extract the data from an Any.
If keep_structs is true, CORBA structs are left as Python
instances; if false, structs are turned into dictionaries with
string keys.
"""
if not isinstance(any, CORBA.Any):
raise CORBA.BAD_PARAM(omniORB.BAD_PARAM_WrongPythonType,
CORBA.COMPLETED_NO)
return _from_desc_value(any._t._d, any._v, keep_structs)
def _from_desc_value(desc, value, keep_structs=0):
"""_from_desc_value(desc,val,keep_structs) -- de-Any value"""
if type(desc) is IntType:
if desc == tcInternal.tv_boolean:
return bool(value)
elif desc != tcInternal.tv_any:
# Nothing to do
return value
else:
k = desc
else:
k = desc[0]
while k == tcInternal.tv_alias:
desc = desc[3]
if type(desc) is IntType:
if desc == tcInternal.tv_boolean:
return bool(value)
elif desc != tcInternal.tv_any:
return value
else:
k = desc
else:
k = desc[0]
if k == tcInternal.tv_any:
return _from_desc_value(value._t._d, value._v, keep_structs)
elif k == tcInternal.tv_struct:
if keep_structs:
return value
d = {}
for i in range(4, len(desc), 2):
sm = desc[i]
sd = desc[i+1]
d[sm] = _from_desc_value(sd, getattr(value, sm), keep_structs)
return d
elif k in [tcInternal.tv_sequence, tcInternal.tv_array]:
sd = desc[1]
if type(sd) is IntType and sd != tcInternal.tv_any:
return value
else:
rl = []
for i in value:
rl.append(_from_desc_value(sd, i, keep_structs))
return rl
return value
|