util.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. """
  2. Miscellaneous utilities, and serializers.
  3. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  4. """
  5. import sys
  6. import zlib
  7. import logging
  8. import linecache
  9. import traceback
  10. import inspect
  11. import Pyro4.errors
  12. import Pyro4.message
  13. try:
  14. import copyreg
  15. except ImportError:
  16. import copy_reg as copyreg
  17. log = logging.getLogger("Pyro4.util")
  18. def getPyroTraceback(ex_type=None, ex_value=None, ex_tb=None):
  19. """Returns a list of strings that form the traceback information of a
  20. Pyro exception. Any remote Pyro exception information is included.
  21. Traceback information is automatically obtained via ``sys.exc_info()`` if
  22. you do not supply the objects yourself."""
  23. def formatRemoteTraceback(remote_tb_lines):
  24. result = [" +--- This exception occured remotely (Pyro) - Remote traceback:"]
  25. for line in remote_tb_lines:
  26. if line.endswith("\n"):
  27. line = line[:-1]
  28. lines = line.split("\n")
  29. for line2 in lines:
  30. result.append("\n | ")
  31. result.append(line2)
  32. result.append("\n +--- End of remote traceback\n")
  33. return result
  34. try:
  35. if ex_type is not None and ex_value is None and ex_tb is None:
  36. # possible old (3.x) call syntax where caller is only providing exception object
  37. if type(ex_type) is not type:
  38. raise TypeError("invalid argument: ex_type should be an exception type, or just supply no arguments at all")
  39. if ex_type is None and ex_tb is None:
  40. ex_type, ex_value, ex_tb = sys.exc_info()
  41. remote_tb = getattr(ex_value, "_pyroTraceback", None)
  42. local_tb = formatTraceback(ex_type, ex_value, ex_tb, Pyro4.config.DETAILED_TRACEBACK)
  43. if remote_tb:
  44. remote_tb = formatRemoteTraceback(remote_tb)
  45. return local_tb + remote_tb
  46. else:
  47. # hmm. no remote tb info, return just the local tb.
  48. return local_tb
  49. finally:
  50. # clean up cycle to traceback, to allow proper GC
  51. del ex_type, ex_value, ex_tb
  52. def formatTraceback(ex_type=None, ex_value=None, ex_tb=None, detailed=False):
  53. """Formats an exception traceback. If you ask for detailed formatting,
  54. the result will contain info on the variables in each stack frame.
  55. You don't have to provide the exception info objects, if you omit them,
  56. this function will obtain them itself using ``sys.exc_info()``."""
  57. if ex_type is not None and ex_value is None and ex_tb is None:
  58. # possible old (3.x) call syntax where caller is only providing exception object
  59. if type(ex_type) is not type:
  60. raise TypeError("invalid argument: ex_type should be an exception type, or just supply no arguments at all")
  61. if ex_type is None and ex_tb is None:
  62. ex_type, ex_value, ex_tb = sys.exc_info()
  63. if detailed and sys.platform != "cli": # detailed tracebacks don't work in ironpython (most of the local vars are omitted)
  64. def makeStrValue(value):
  65. try:
  66. return repr(value)
  67. except:
  68. try:
  69. return str(value)
  70. except:
  71. return "<ERROR>"
  72. try:
  73. result = ["-" * 52 + "\n"]
  74. result.append(" EXCEPTION %s: %s\n" % (ex_type, ex_value))
  75. result.append(" Extended stacktrace follows (most recent call last)\n")
  76. skipLocals = True # don't print the locals of the very first stack frame
  77. while ex_tb:
  78. frame = ex_tb.tb_frame
  79. sourceFileName = frame.f_code.co_filename
  80. if "self" in frame.f_locals:
  81. location = "%s.%s" % (frame.f_locals["self"].__class__.__name__, frame.f_code.co_name)
  82. else:
  83. location = frame.f_code.co_name
  84. result.append("-" * 52 + "\n")
  85. result.append("File \"%s\", line %d, in %s\n" % (sourceFileName, ex_tb.tb_lineno, location))
  86. result.append("Source code:\n")
  87. result.append(" " + linecache.getline(sourceFileName, ex_tb.tb_lineno).strip() + "\n")
  88. if not skipLocals:
  89. names = set()
  90. names.update(getattr(frame.f_code, "co_varnames", ()))
  91. names.update(getattr(frame.f_code, "co_names", ()))
  92. names.update(getattr(frame.f_code, "co_cellvars", ()))
  93. names.update(getattr(frame.f_code, "co_freevars", ()))
  94. result.append("Local values:\n")
  95. for name2 in sorted(names):
  96. if name2 in frame.f_locals:
  97. value = frame.f_locals[name2]
  98. result.append(" %s = %s\n" % (name2, makeStrValue(value)))
  99. if name2 == "self":
  100. # print the local variables of the class instance
  101. for name3, value in vars(value).items():
  102. result.append(" self.%s = %s\n" % (name3, makeStrValue(value)))
  103. skipLocals = False
  104. ex_tb = ex_tb.tb_next
  105. result.append("-" * 52 + "\n")
  106. result.append(" EXCEPTION %s: %s\n" % (ex_type, ex_value))
  107. result.append("-" * 52 + "\n")
  108. return result
  109. except Exception:
  110. return ["-" * 52 + "\nError building extended traceback!!! :\n",
  111. "".join(traceback.format_exception(*sys.exc_info())) + '-' * 52 + '\n',
  112. "Original Exception follows:\n",
  113. "".join(traceback.format_exception(ex_type, ex_value, ex_tb))]
  114. else:
  115. # default traceback format.
  116. return traceback.format_exception(ex_type, ex_value, ex_tb)
  117. all_exceptions = {}
  118. if sys.version_info < (3, 0):
  119. import exceptions
  120. for name, t in vars(exceptions).items():
  121. if type(t) is type and issubclass(t, BaseException):
  122. all_exceptions[name] = t
  123. else:
  124. import builtins
  125. for name, t in vars(builtins).items():
  126. if type(t) is type and issubclass(t, BaseException):
  127. all_exceptions[name] = t
  128. for name, t in vars(Pyro4.errors).items():
  129. if type(t) is type and issubclass(t, Pyro4.errors.PyroError):
  130. all_exceptions[name] = t
  131. class SerializerBase(object):
  132. """Base class for (de)serializer implementations (which must be thread safe)"""
  133. __custom_class_to_dict_registry = {}
  134. __custom_dict_to_class_registry = {}
  135. def serializeData(self, data, compress=False):
  136. """Serialize the given data object, try to compress if told so.
  137. Returns a tuple of the serialized data (bytes) and a bool indicating if it is compressed or not."""
  138. data = self.dumps(data)
  139. return self.__compressdata(data, compress)
  140. def deserializeData(self, data, compressed=False):
  141. """Deserializes the given data (bytes). Set compressed to True to decompress the data first."""
  142. if compressed:
  143. data = zlib.decompress(data)
  144. return self.loads(data)
  145. def serializeCall(self, obj, method, vargs, kwargs, compress=False):
  146. """Serialize the given method call parameters, try to compress if told so.
  147. Returns a tuple of the serialized data and a bool indicating if it is compressed or not."""
  148. data = self.dumpsCall(obj, method, vargs, kwargs)
  149. return self.__compressdata(data, compress)
  150. def deserializeCall(self, data, compressed=False):
  151. """Deserializes the given call data back to (object, method, vargs, kwargs) tuple.
  152. Set compressed to True to decompress the data first."""
  153. if compressed:
  154. data = zlib.decompress(data)
  155. return self.loadsCall(data)
  156. def loads(self, data):
  157. raise NotImplementedError("implement in subclass")
  158. def loadsCall(self, data):
  159. raise NotImplementedError("implement in subclass")
  160. def dumps(self, data):
  161. raise NotImplementedError("implement in subclass")
  162. def dumpsCall(self, obj, method, vargs, kwargs):
  163. raise NotImplementedError("implement in subclass")
  164. def __compressdata(self, data, compress):
  165. if not compress or len(data) < 200:
  166. return data, False # don't waste time compressing small messages
  167. compressed = zlib.compress(data)
  168. if len(compressed) < len(data):
  169. return compressed, True
  170. return data, False
  171. @classmethod
  172. def decompress_if_needed(cls, message):
  173. if message.flags & Pyro4.message.FLAGS_COMPRESSED:
  174. message.data = zlib.decompress(message.data)
  175. message.flags &= ~Pyro4.message.FLAGS_COMPRESSED
  176. return message
  177. @classmethod
  178. def register_type_replacement(cls, object_type, replacement_function):
  179. raise NotImplementedError("implement in subclass")
  180. @classmethod
  181. def register_class_to_dict(cls, clazz, converter, serpent_too=True):
  182. """Registers a custom function that returns a dict representation of objects of the given class.
  183. The function is called with a single parameter; the object to be converted to a dict."""
  184. cls.__custom_class_to_dict_registry[clazz] = converter
  185. if serpent_too:
  186. try:
  187. get_serializer_by_id(SerpentSerializer.serializer_id)
  188. import serpent
  189. def serpent_converter(obj, serializer, stream, level):
  190. d = converter(obj)
  191. serializer.ser_builtins_dict(d, stream, level)
  192. serpent.register_class(clazz, serpent_converter)
  193. except Pyro4.errors.ProtocolError:
  194. pass
  195. @classmethod
  196. def unregister_class_to_dict(cls, clazz):
  197. """Removes the to-dict conversion function registered for the given class. Objects of the class
  198. will be serialized by the default mechanism again."""
  199. if clazz in cls.__custom_class_to_dict_registry:
  200. del cls.__custom_class_to_dict_registry[clazz]
  201. try:
  202. get_serializer_by_id(SerpentSerializer.serializer_id)
  203. import serpent
  204. serpent.unregister_class(clazz)
  205. except Pyro4.errors.ProtocolError:
  206. pass
  207. @classmethod
  208. def register_dict_to_class(cls, classname, converter):
  209. """
  210. Registers a custom converter function that creates objects from a dict with the given classname tag in it.
  211. The function is called with two parameters: the classname and the dictionary to convert to an instance of the class.
  212. This mechanism is not used for the pickle serializer.
  213. """
  214. cls.__custom_dict_to_class_registry[classname] = converter
  215. @classmethod
  216. def unregister_dict_to_class(cls, classname):
  217. """
  218. Removes the converter registered for the given classname. Dicts with that classname tag
  219. will be deserialized by the default mechanism again.
  220. This mechanism is not used for the pickle serializer.
  221. """
  222. if classname in cls.__custom_dict_to_class_registry:
  223. del cls.__custom_dict_to_class_registry[classname]
  224. @classmethod
  225. def class_to_dict(cls, obj):
  226. """
  227. Convert a non-serializable object to a dict. Partly borrowed from serpent.
  228. Not used for the pickle serializer.
  229. """
  230. for clazz in cls.__custom_class_to_dict_registry:
  231. if isinstance(obj, clazz):
  232. return cls.__custom_class_to_dict_registry[clazz](obj)
  233. if type(obj) in (set, dict, tuple, list):
  234. # we use a ValueError to mirror the exception type returned by serpent and other serializers
  235. raise ValueError("can't serialize type " + str(obj.__class__) + " into a dict")
  236. if hasattr(obj, "_pyroDaemon"):
  237. obj._pyroDaemon = None
  238. if isinstance(obj, BaseException):
  239. # special case for exceptions
  240. return {
  241. "__class__": obj.__class__.__module__ + "." + obj.__class__.__name__,
  242. "__exception__": True,
  243. "args": obj.args,
  244. "attributes": vars(obj) # add custom exception attributes
  245. }
  246. try:
  247. value = obj.__getstate__()
  248. except AttributeError:
  249. pass
  250. else:
  251. if isinstance(value, dict):
  252. return value
  253. try:
  254. value = dict(vars(obj)) # make sure we can serialize anything that resembles a dict
  255. value["__class__"] = obj.__class__.__module__ + "." + obj.__class__.__name__
  256. return value
  257. except TypeError:
  258. if hasattr(obj, "__slots__"):
  259. # use the __slots__ instead of the vars dict
  260. value = {}
  261. for slot in obj.__slots__:
  262. value[slot] = getattr(obj, slot)
  263. value["__class__"] = obj.__class__.__module__ + "." + obj.__class__.__name__
  264. return value
  265. else:
  266. raise Pyro4.errors.SerializeError("don't know how to serialize class " + str(obj.__class__) + " using serializer " + str(cls.__name__) + ". Give it vars() or an appropriate __getstate__")
  267. @classmethod
  268. def dict_to_class(cls, data):
  269. """
  270. Recreate an object out of a dict containing the class name and the attributes.
  271. Only a fixed set of classes are recognized.
  272. Not used for the pickle serializer.
  273. """
  274. classname = data.get("__class__", "<unknown>")
  275. if isinstance(classname, bytes):
  276. classname = classname.decode("utf-8")
  277. if classname in cls.__custom_dict_to_class_registry:
  278. converter = cls.__custom_dict_to_class_registry[classname]
  279. return converter(classname, data)
  280. if "__" in classname:
  281. raise Pyro4.errors.SecurityError("refused to deserialize types with double underscores in their name: " + classname)
  282. # because of efficiency reasons the constructors below are hardcoded here instead of added on a per-class basis to the dict-to-class registry
  283. if classname.startswith("Pyro4.core."):
  284. if classname == "Pyro4.core.URI":
  285. uri = Pyro4.core.URI.__new__(Pyro4.core.URI)
  286. uri.__setstate_from_dict__(data["state"])
  287. return uri
  288. elif classname == "Pyro4.core.Proxy":
  289. proxy = Pyro4.core.Proxy.__new__(Pyro4.core.Proxy)
  290. proxy.__setstate_from_dict__(data["state"])
  291. return proxy
  292. elif classname == "Pyro4.core.Daemon":
  293. daemon = Pyro4.core.Daemon.__new__(Pyro4.core.Daemon)
  294. daemon.__setstate_from_dict__(data["state"])
  295. return daemon
  296. elif classname.startswith("Pyro4.util."):
  297. if classname == "Pyro4.util.PickleSerializer":
  298. return PickleSerializer()
  299. elif classname == "Pyro4.util.DillSerializer":
  300. return DillSerializer()
  301. elif classname == "Pyro4.util.MarshalSerializer":
  302. return MarshalSerializer()
  303. elif classname == "Pyro4.util.JsonSerializer":
  304. return JsonSerializer()
  305. elif classname == "Pyro4.util.SerpentSerializer":
  306. return SerpentSerializer()
  307. elif classname.startswith("Pyro4.errors."):
  308. errortype = getattr(Pyro4.errors, classname.split('.', 2)[2])
  309. if issubclass(errortype, Pyro4.errors.PyroError):
  310. return SerializerBase.make_exception(errortype, data)
  311. elif classname == "Pyro4.futures._ExceptionWrapper":
  312. ex = SerializerBase.dict_to_class(data["exception"])
  313. return Pyro4.futures._ExceptionWrapper(ex)
  314. elif data.get("__exception__", False):
  315. if classname in all_exceptions:
  316. return SerializerBase.make_exception(all_exceptions[classname], data)
  317. # python 2.x: exceptions.ValueError
  318. # python 3.x: builtins.ValueError
  319. # translate to the appropriate namespace...
  320. namespace, short_classname = classname.split('.', 1)
  321. if namespace in ("builtins", "exceptions"):
  322. if sys.version_info < (3, 0):
  323. exceptiontype = getattr(exceptions, short_classname)
  324. if issubclass(exceptiontype, BaseException):
  325. return SerializerBase.make_exception(exceptiontype, data)
  326. else:
  327. exceptiontype = getattr(builtins, short_classname)
  328. if issubclass(exceptiontype, BaseException):
  329. return SerializerBase.make_exception(exceptiontype, data)
  330. elif namespace == "sqlite3" and short_classname.endswith("Error"):
  331. import sqlite3
  332. exceptiontype = getattr(sqlite3, short_classname)
  333. if issubclass(exceptiontype, BaseException):
  334. return SerializerBase.make_exception(exceptiontype, data)
  335. # try one of the serializer classes
  336. for serializer in _serializers.values():
  337. if classname == serializer.__class__.__name__:
  338. return serializer
  339. log.warning("unsupported serialized class: " + classname)
  340. raise Pyro4.errors.SerializeError("unsupported serialized class: " + classname)
  341. @staticmethod
  342. def make_exception(exceptiontype, data):
  343. ex = exceptiontype(*data["args"])
  344. if "attributes" in data:
  345. # restore custom attributes on the exception object
  346. for attr, value in data["attributes"].items():
  347. setattr(ex, attr, value)
  348. return ex
  349. def recreate_classes(self, literal):
  350. t = type(literal)
  351. if t is set:
  352. return {self.recreate_classes(x) for x in literal}
  353. if t is list:
  354. return [self.recreate_classes(x) for x in literal]
  355. if t is tuple:
  356. return tuple(self.recreate_classes(x) for x in literal)
  357. if t is dict:
  358. if "__class__" in literal:
  359. return self.dict_to_class(literal)
  360. result = {}
  361. for key, value in literal.items():
  362. result[key] = self.recreate_classes(value)
  363. return result
  364. return literal
  365. def __eq__(self, other):
  366. """this equality method is only to support the unit tests of this class"""
  367. return isinstance(other, SerializerBase) and vars(self) == vars(other)
  368. def __ne__(self, other):
  369. return not self.__eq__(other)
  370. __hash__ = object.__hash__
  371. class PickleSerializer(SerializerBase):
  372. """
  373. A (de)serializer that wraps the Pickle serialization protocol.
  374. It can optionally compress the serialized data, and is thread safe.
  375. """
  376. serializer_id = Pyro4.message.SERIALIZER_PICKLE
  377. def dumpsCall(self, obj, method, vargs, kwargs):
  378. return pickle.dumps((obj, method, vargs, kwargs), Pyro4.config.PICKLE_PROTOCOL_VERSION)
  379. def dumps(self, data):
  380. return pickle.dumps(data, Pyro4.config.PICKLE_PROTOCOL_VERSION)
  381. def loadsCall(self, data):
  382. return pickle.loads(data)
  383. def loads(self, data):
  384. return pickle.loads(data)
  385. @classmethod
  386. def register_type_replacement(cls, object_type, replacement_function):
  387. def copyreg_function(obj):
  388. return replacement_function(obj).__reduce__()
  389. try:
  390. copyreg.pickle(object_type, copyreg_function)
  391. except TypeError:
  392. pass
  393. class DillSerializer(SerializerBase):
  394. """
  395. A (de)serializer that wraps the Dill serialization protocol.
  396. It can optionally compress the serialized data, and is thread safe.
  397. """
  398. serializer_id = Pyro4.message.SERIALIZER_DILL
  399. def dumpsCall(self, obj, method, vargs, kwargs):
  400. return dill.dumps((obj, method, vargs, kwargs), Pyro4.config.DILL_PROTOCOL_VERSION)
  401. def dumps(self, data):
  402. return dill.dumps(data, Pyro4.config.DILL_PROTOCOL_VERSION)
  403. def loadsCall(self, data):
  404. return dill.loads(data)
  405. def loads(self, data):
  406. return dill.loads(data)
  407. @classmethod
  408. def register_type_replacement(cls, object_type, replacement_function):
  409. def copyreg_function(obj):
  410. return replacement_function(obj).__reduce__()
  411. try:
  412. copyreg.pickle(object_type, copyreg_function)
  413. except TypeError:
  414. pass
  415. class MarshalSerializer(SerializerBase):
  416. """(de)serializer that wraps the marshal serialization protocol."""
  417. serializer_id = Pyro4.message.SERIALIZER_MARSHAL
  418. def dumpsCall(self, obj, method, vargs, kwargs):
  419. return marshal.dumps((obj, method, vargs, kwargs))
  420. def dumps(self, data):
  421. try:
  422. return marshal.dumps(data)
  423. except (ValueError, TypeError):
  424. return marshal.dumps(self.class_to_dict(data))
  425. def loadsCall(self, data):
  426. obj, method, vargs, kwargs = marshal.loads(data)
  427. vargs = self.recreate_classes(vargs)
  428. kwargs = self.recreate_classes(kwargs)
  429. return obj, method, vargs, kwargs
  430. if sys.platform == "cli":
  431. def loads(self, data):
  432. if type(data) is not str:
  433. # Ironpython's marshal expects str...
  434. data = str(data)
  435. return self.recreate_classes(marshal.loads(data))
  436. else:
  437. def loads(self, data):
  438. return self.recreate_classes(marshal.loads(data))
  439. @classmethod
  440. def register_type_replacement(cls, object_type, replacement_function):
  441. pass # marshal serializer doesn't support per-type hooks
  442. class SerpentSerializer(SerializerBase):
  443. """(de)serializer that wraps the serpent serialization protocol."""
  444. serializer_id = Pyro4.message.SERIALIZER_SERPENT
  445. def dumpsCall(self, obj, method, vargs, kwargs):
  446. return serpent.dumps((obj, method, vargs, kwargs), module_in_classname=True)
  447. def dumps(self, data):
  448. return serpent.dumps(data, module_in_classname=True)
  449. def loadsCall(self, data):
  450. obj, method, vargs, kwargs = serpent.loads(data)
  451. vargs = self.recreate_classes(vargs)
  452. kwargs = self.recreate_classes(kwargs)
  453. return obj, method, vargs, kwargs
  454. def loads(self, data):
  455. return self.recreate_classes(serpent.loads(data))
  456. @classmethod
  457. def register_type_replacement(cls, object_type, replacement_function):
  458. def custom_serializer(object, serpent_serializer, outputstream, indentlevel):
  459. replaced = replacement_function(object)
  460. if replaced is object:
  461. serpent_serializer.ser_default_class(replaced, outputstream, indentlevel)
  462. else:
  463. serpent_serializer._serialize(replaced, outputstream, indentlevel)
  464. serpent.register_class(object_type, custom_serializer)
  465. class JsonSerializer(SerializerBase):
  466. """(de)serializer that wraps the json serialization protocol."""
  467. serializer_id = Pyro4.message.SERIALIZER_JSON
  468. __type_replacements = {}
  469. def dumpsCall(self, obj, method, vargs, kwargs):
  470. data = {"object": obj, "method": method, "params": vargs, "kwargs": kwargs}
  471. data = json.dumps(data, ensure_ascii=False, default=self.default)
  472. return data.encode("utf-8")
  473. def dumps(self, data):
  474. data = json.dumps(data, ensure_ascii=False, default=self.default)
  475. return data.encode("utf-8")
  476. def loadsCall(self, data):
  477. data = data.decode("utf-8")
  478. data = json.loads(data)
  479. vargs = self.recreate_classes(data["params"])
  480. kwargs = self.recreate_classes(data["kwargs"])
  481. return data["object"], data["method"], vargs, kwargs
  482. def loads(self, data):
  483. data = data.decode("utf-8")
  484. return self.recreate_classes(json.loads(data))
  485. def default(self, obj):
  486. replacer = self.__type_replacements.get(type(obj), None)
  487. if replacer:
  488. obj = replacer(obj)
  489. if isinstance(obj, set):
  490. return tuple(obj) # json module can't deal with sets so we make a tuple out of it
  491. return self.class_to_dict(obj)
  492. @classmethod
  493. def register_type_replacement(cls, object_type, replacement_function):
  494. cls.__type_replacements[object_type] = replacement_function
  495. """The various serializers that are supported"""
  496. _serializers = {}
  497. _serializers_by_id = {}
  498. def get_serializer(name):
  499. try:
  500. return _serializers[name]
  501. except KeyError:
  502. raise Pyro4.errors.SerializeError("serializer '%s' is unknown or not available" % name)
  503. def get_serializer_by_id(sid):
  504. try:
  505. return _serializers_by_id[sid]
  506. except KeyError:
  507. raise Pyro4.errors.SerializeError("no serializer available for id %d" % sid)
  508. # determine the serializers that are supported
  509. try:
  510. import cPickle as pickle
  511. except ImportError:
  512. import pickle
  513. assert Pyro4.config.PICKLE_PROTOCOL_VERSION >= 2, "pickle protocol needs to be 2 or higher"
  514. _ser = PickleSerializer()
  515. _serializers["pickle"] = _ser
  516. _serializers_by_id[_ser.serializer_id] = _ser
  517. import marshal
  518. _ser = MarshalSerializer()
  519. _serializers["marshal"] = _ser
  520. _serializers_by_id[_ser.serializer_id] = _ser
  521. try:
  522. import platform
  523. if platform.python_implementation() in ('PyPy', 'IronPython'):
  524. raise ImportError('Currently dill is not supported with PyPy and IronPython')
  525. import dill
  526. _ser = DillSerializer()
  527. _serializers["dill"] = _ser
  528. _serializers_by_id[_ser.serializer_id] = _ser
  529. except ImportError:
  530. pass
  531. try:
  532. try:
  533. import importlib
  534. json = importlib.import_module(Pyro4.config.JSON_MODULE)
  535. except ImportError:
  536. json = __import__(Pyro4.config.JSON_MODULE)
  537. _ser = JsonSerializer()
  538. _serializers["json"] = _ser
  539. _serializers_by_id[_ser.serializer_id] = _ser
  540. except ImportError:
  541. pass
  542. try:
  543. import serpent
  544. if '-' in serpent.__version__:
  545. ver = serpent.__version__.split('-', 1)[0]
  546. else:
  547. ver = serpent.__version__
  548. ver = tuple(map(int, ver.split(".")))
  549. if ver < (1, 15):
  550. raise RuntimeError("requires serpent 1.15 or better")
  551. _ser = SerpentSerializer()
  552. _serializers["serpent"] = _ser
  553. _serializers_by_id[_ser.serializer_id] = _ser
  554. except ImportError:
  555. log.warning("serpent serializer is not available")
  556. pass
  557. del _ser
  558. def getAttribute(obj, attr):
  559. """
  560. Resolves an attribute name to an object. Raises
  561. an AttributeError if any attribute in the chain starts with a '``_``'.
  562. Doesn't resolve a dotted name, because that is a security vulnerability.
  563. It treats it as a single attribute name (and the lookup will likely fail).
  564. """
  565. if is_private_attribute(attr):
  566. raise AttributeError("attempt to access private attribute '%s'" % attr)
  567. else:
  568. obj = getattr(obj, attr)
  569. if not Pyro4.config.REQUIRE_EXPOSE or getattr(obj, "_pyroExposed", False):
  570. return obj
  571. raise AttributeError("attempt to access unexposed attribute '%s'" % attr)
  572. def excepthook(ex_type, ex_value, ex_tb):
  573. """An exception hook you can use for ``sys.excepthook``, to automatically print remote Pyro tracebacks"""
  574. traceback = "".join(getPyroTraceback(ex_type, ex_value, ex_tb))
  575. sys.stderr.write(traceback)
  576. def fixIronPythonExceptionForPickle(exceptionObject, addAttributes):
  577. """
  578. Function to hack around a bug in IronPython where it doesn't pickle
  579. exception attributes. We piggyback them into the exception's args.
  580. Bug report is at http://ironpython.codeplex.com/workitem/30805
  581. migrated to github as https://github.com/IronLanguages/main/issues/943
  582. Bug is still present in Ironpython 2.7.5
  583. """
  584. if hasattr(exceptionObject, "args"):
  585. if addAttributes:
  586. # piggyback the attributes on the exception args instead.
  587. ironpythonArgs = vars(exceptionObject)
  588. ironpythonArgs["__ironpythonargs__"] = True
  589. exceptionObject.args += (ironpythonArgs,)
  590. else:
  591. # check if there is a piggybacked object in the args
  592. # if there is, extract the exception attributes from it.
  593. if len(exceptionObject.args) > 0:
  594. piggyback = exceptionObject.args[-1]
  595. if type(piggyback) is dict and piggyback.get("__ironpythonargs__"):
  596. del piggyback["__ironpythonargs__"]
  597. exceptionObject.args = exceptionObject.args[:-1]
  598. exceptionObject.__dict__.update(piggyback)
  599. __exposed_member_cache = {}
  600. def reset_exposed_members(obj, only_exposed=True, as_lists=False):
  601. """Delete any cached exposed members forcing recalculation on next request"""
  602. if not inspect.isclass(obj):
  603. obj = obj.__class__
  604. cache_key = (obj, only_exposed, as_lists)
  605. __exposed_member_cache.pop(cache_key, None)
  606. def get_exposed_members(obj, only_exposed=True, as_lists=False, use_cache=True):
  607. """
  608. Return public and exposed members of the given object's class.
  609. You can also provide a class directly.
  610. Private members are ignored no matter what (names starting with underscore).
  611. If only_exposed is True, only members tagged with the @expose decorator are
  612. returned. If it is False, all public members are returned.
  613. The return value consists of the exposed methods, exposed attributes, and methods
  614. tagged as @oneway.
  615. (All this is used as meta data that Pyro sends to the proxy if it asks for it)
  616. as_lists is meant for python 2 compatibility.
  617. """
  618. if not inspect.isclass(obj):
  619. obj = obj.__class__
  620. cache_key = (obj, only_exposed, as_lists)
  621. if use_cache and cache_key in __exposed_member_cache:
  622. return __exposed_member_cache[cache_key]
  623. methods = set() # all methods
  624. oneway = set() # oneway methods
  625. attrs = set() # attributes
  626. for m in dir(obj): # also lists names inherited from super classes
  627. if is_private_attribute(m):
  628. continue
  629. v = getattr(obj, m)
  630. if inspect.ismethod(v) or inspect.isfunction(v):
  631. if getattr(v, "_pyroExposed", not only_exposed):
  632. methods.add(m)
  633. # check if the method is marked with the @Pyro4.oneway decorator:
  634. if getattr(v, "_pyroOneway", False):
  635. oneway.add(m)
  636. elif inspect.isdatadescriptor(v):
  637. func = getattr(v, "fget", None) or getattr(v, "fset", None) or getattr(v, "fdel", None)
  638. if func is not None and getattr(func, "_pyroExposed", not only_exposed):
  639. attrs.add(m)
  640. # Note that we don't expose plain class attributes no matter what.
  641. # it is a syntax error to add a decorator on them, and it is not possible
  642. # to give them a _pyroExposed tag either.
  643. # The way to expose attributes is by using properties for them.
  644. # This automatically solves the protection/security issue: you have to
  645. # explicitly decide to make an attribute into a @property (and to @expose it
  646. # if REQUIRE_EXPOSED=True) before it is remotely accessible.
  647. if as_lists:
  648. methods = list(methods)
  649. oneway = list(oneway)
  650. attrs = list(attrs)
  651. result = {
  652. "methods": methods,
  653. "oneway": oneway,
  654. "attrs": attrs
  655. }
  656. __exposed_member_cache[cache_key] = result
  657. return result
  658. def get_exposed_property_value(obj, propname, only_exposed=True):
  659. """
  660. Return the value of an @exposed @property.
  661. If the requested property is not a @property or not exposed,
  662. an AttributeError is raised instead.
  663. """
  664. v = getattr(obj.__class__, propname)
  665. if inspect.isdatadescriptor(v):
  666. if v.fget and getattr(v.fget, "_pyroExposed", not only_exposed):
  667. return v.fget(obj)
  668. raise AttributeError("attempt to access unexposed or unknown remote attribute '%s'" % propname)
  669. def set_exposed_property_value(obj, propname, value, only_exposed=True):
  670. """
  671. Sets the value of an @exposed @property.
  672. If the requested property is not a @property or not exposed,
  673. an AttributeError is raised instead.
  674. """
  675. v = getattr(obj.__class__, propname)
  676. if inspect.isdatadescriptor(v):
  677. pfunc = v.fget or v.fset or v.fdel
  678. if v.fset and getattr(pfunc, "_pyroExposed", not only_exposed):
  679. return v.fset(obj, value)
  680. raise AttributeError("attempt to access unexposed or unknown remote attribute '%s'" % propname)
  681. _private_dunder_methods = frozenset([
  682. "__init__", "__call__", "__new__", "__del__", "__repr__", "__unicode__",
  683. "__str__", "__format__", "__nonzero__", "__bool__", "__coerce__",
  684. "__cmp__", "__eq__", "__ne__", "__hash__",
  685. "__dir__", "__enter__", "__exit__", "__copy__", "__deepcopy__", "__sizeof__",
  686. "__getattr__", "__setattr__", "__hasattr__", "__getattribute__", "__delattr__",
  687. "__instancecheck__", "__subclasscheck__", "__getinitargs__", "__getnewargs__",
  688. "__getstate__", "__setstate__", "__reduce__", "__reduce_ex__",
  689. "__getstate_for_dict__", "__setstate_from_dict__", "__subclasshook__"
  690. ])
  691. def is_private_attribute(attr_name):
  692. """returns if the attribute name is to be considered private or not."""
  693. if attr_name in _private_dunder_methods:
  694. return True
  695. if not attr_name.startswith('_'):
  696. return False
  697. if len(attr_name) > 4 and attr_name.startswith("__") and attr_name.endswith("__"):
  698. return False
  699. return True