From 52feabbe7641f19a17810438d2469b2395b2819f Mon Sep 17 00:00:00 2001 From: Paul Molodowitch Date: Wed, 24 Oct 2012 11:27:52 -0700 Subject: added setdefaultencoding method so reload(sys) not needed reload(sys) could cause problem in user code - ie, sys.stdout, excepthook, and displayhook would be reset, etc --- docs/getting_started/echobot.rst | 4 ++-- examples/adhoc_provider.py | 4 ++-- examples/adhoc_user.py | 4 ++-- examples/admin_commands.py | 4 ++-- examples/custom_stanzas/custom_stanza_provider.py | 8 ++++---- examples/custom_stanzas/custom_stanza_user.py | 4 ++-- examples/disco_browser.py | 4 ++-- examples/download_avatars.py | 4 ++-- examples/echo_client.py | 4 ++-- examples/echo_component.py | 4 ++-- examples/gtalk_custom_domain.py | 4 ++-- examples/ibb_transfer/ibb_receiver.py | 4 ++-- examples/ibb_transfer/ibb_sender.py | 4 ++-- examples/muc.py | 4 ++-- examples/ping.py | 4 ++-- examples/proxy_echo_client.py | 4 ++-- examples/pubsub_client.py | 4 ++-- examples/pubsub_events.py | 4 ++-- examples/register_account.py | 4 ++-- examples/roster_browser.py | 4 ++-- examples/send_client.py | 4 ++-- examples/set_avatar.py | 4 ++-- examples/thirdparty_auth.py | 4 ++-- sleekxmpp/basexmpp.py | 4 ++-- sleekxmpp/util/misc_ops.py | 24 +++++++++++++++++++++++ 25 files changed, 74 insertions(+), 50 deletions(-) diff --git a/docs/getting_started/echobot.rst b/docs/getting_started/echobot.rst index 053a76f2..7d29ec58 100644 --- a/docs/getting_started/echobot.rst +++ b/docs/getting_started/echobot.rst @@ -69,8 +69,8 @@ use ASCII. We can get Python to use UTF-8 as the default encoding by including: .. code-block:: python if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') .. warning:: diff --git a/examples/adhoc_provider.py b/examples/adhoc_provider.py index a72158c3..86a575c9 100755 --- a/examples/adhoc_provider.py +++ b/examples/adhoc_provider.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/adhoc_user.py b/examples/adhoc_user.py index bbd42d81..7df9f793 100755 --- a/examples/adhoc_user.py +++ b/examples/adhoc_user.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/admin_commands.py b/examples/admin_commands.py index 13b1b179..5d9bf841 100755 --- a/examples/admin_commands.py +++ b/examples/admin_commands.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/custom_stanzas/custom_stanza_provider.py b/examples/custom_stanzas/custom_stanza_provider.py index b532c17c..b0e00247 100755 --- a/examples/custom_stanzas/custom_stanza_provider.py +++ b/examples/custom_stanzas/custom_stanza_provider.py @@ -28,8 +28,8 @@ from stanza import Action # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input @@ -56,8 +56,8 @@ class ActionBot(sleekxmpp.ClientXMPP): StanzaPath('iq@type=set/action'), self._handle_action)) - self.add_event_handler('custom_action', - self._handle_action_event, + self.add_event_handler('custom_action', + self._handle_action_event, threaded=True) register_stanza_plugin(Iq, Action) diff --git a/examples/custom_stanzas/custom_stanza_user.py b/examples/custom_stanzas/custom_stanza_user.py index 5b5042c7..418e3218 100755 --- a/examples/custom_stanzas/custom_stanza_user.py +++ b/examples/custom_stanzas/custom_stanza_user.py @@ -26,8 +26,8 @@ from stanza import Action # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/disco_browser.py b/examples/disco_browser.py index 67e12d19..aeb4fb5e 100755 --- a/examples/disco_browser.py +++ b/examples/disco_browser.py @@ -23,8 +23,8 @@ from sleekxmpp.exceptions import IqError, IqTimeout # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/download_avatars.py b/examples/download_avatars.py index b621c940..64300cff 100644 --- a/examples/download_avatars.py +++ b/examples/download_avatars.py @@ -24,8 +24,8 @@ from sleekxmpp.exceptions import XMPPError # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/echo_client.py b/examples/echo_client.py index 73990089..f2d38847 100755 --- a/examples/echo_client.py +++ b/examples/echo_client.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/echo_component.py b/examples/echo_component.py index 82f6eb9f..9a24f2fa 100755 --- a/examples/echo_component.py +++ b/examples/echo_component.py @@ -22,8 +22,8 @@ from sleekxmpp.componentxmpp import ComponentXMPP # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/gtalk_custom_domain.py b/examples/gtalk_custom_domain.py index 0226c146..c974fc55 100755 --- a/examples/gtalk_custom_domain.py +++ b/examples/gtalk_custom_domain.py @@ -25,8 +25,8 @@ from sleekxmpp.xmlstream import cert # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/ibb_transfer/ibb_receiver.py b/examples/ibb_transfer/ibb_receiver.py index b11acabf..18d18f92 100755 --- a/examples/ibb_transfer/ibb_receiver.py +++ b/examples/ibb_transfer/ibb_receiver.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/ibb_transfer/ibb_sender.py b/examples/ibb_transfer/ibb_sender.py index cd856378..7c380b68 100755 --- a/examples/ibb_transfer/ibb_sender.py +++ b/examples/ibb_transfer/ibb_sender.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/muc.py b/examples/muc.py index 7b93da16..5b5c764c 100755 --- a/examples/muc.py +++ b/examples/muc.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/ping.py b/examples/ping.py index fe4d23a4..0e53b1dd 100755 --- a/examples/ping.py +++ b/examples/ping.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/proxy_echo_client.py b/examples/proxy_echo_client.py index 25bfc891..98935b9c 100755 --- a/examples/proxy_echo_client.py +++ b/examples/proxy_echo_client.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/pubsub_client.py b/examples/pubsub_client.py index 0a244f3b..2fa419fb 100644 --- a/examples/pubsub_client.py +++ b/examples/pubsub_client.py @@ -12,8 +12,8 @@ from sleekxmpp.xmlstream import ET, tostring # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/pubsub_events.py b/examples/pubsub_events.py index 6fe7159b..0dfb6c65 100644 --- a/examples/pubsub_events.py +++ b/examples/pubsub_events.py @@ -14,8 +14,8 @@ from sleekxmpp.xmlstream.handler import Callback # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/register_account.py b/examples/register_account.py index 20377b26..bd9b1160 100644 --- a/examples/register_account.py +++ b/examples/register_account.py @@ -22,8 +22,8 @@ from sleekxmpp.exceptions import IqError, IqTimeout # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/roster_browser.py b/examples/roster_browser.py index b366d00f..485ac941 100644 --- a/examples/roster_browser.py +++ b/examples/roster_browser.py @@ -24,8 +24,8 @@ from sleekxmpp.exceptions import IqError, IqTimeout # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/send_client.py b/examples/send_client.py index 5b34bbc9..192469ae 100755 --- a/examples/send_client.py +++ b/examples/send_client.py @@ -21,8 +21,8 @@ import sleekxmpp # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/set_avatar.py b/examples/set_avatar.py index ec32a7c4..cae93c99 100644 --- a/examples/set_avatar.py +++ b/examples/set_avatar.py @@ -26,8 +26,8 @@ from sleekxmpp.exceptions import XMPPError # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/examples/thirdparty_auth.py b/examples/thirdparty_auth.py index 727311ae..f4d5c400 100644 --- a/examples/thirdparty_auth.py +++ b/examples/thirdparty_auth.py @@ -29,8 +29,8 @@ from sleekxmpp.xmlstream import JID # throughout SleekXMPP, we will set the default encoding # ourselves to UTF-8. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') else: raw_input = input diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py index 4df7f11a..c3ff5ba3 100644 --- a/sleekxmpp/basexmpp.py +++ b/sleekxmpp/basexmpp.py @@ -43,8 +43,8 @@ log = logging.getLogger(__name__) # In order to make sure that Unicode is handled properly # in Python 2.x, reset the default encoding. if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') + from sleekxmpp.util.misc_ops import setdefaultencoding + setdefaultencoding('utf8') class BaseXMPP(XMLStream): diff --git a/sleekxmpp/util/misc_ops.py b/sleekxmpp/util/misc_ops.py index 9bb1db4b..3b246625 100644 --- a/sleekxmpp/util/misc_ops.py +++ b/sleekxmpp/util/misc_ops.py @@ -125,3 +125,27 @@ def hashes(): t += ['MD2'] hashes = ['SHA-' + h[3:] for h in dir(hashlib) if h.startswith('sha')] return t + hashes + +def setdefaultencoding(encoding): + """ + Set the current default string encoding used by the Unicode implementation. + + Actually calls sys.setdefaultencoding under the hood - see the docs for that + for more details. This method exists only as a way to call find/call it + even after it has been 'deleted' when the site module is executed. + + :param string encoding: An encoding name, compatible with sys.setdefaultencoding + """ + func = getattr(sys, 'setdefaultencoding', None) + if func is None: + import gc + import types + for obj in gc.get_objects(): + if (isinstance(obj, types.BuiltinFunctionType) + and obj.__name__ == 'setdefaultencoding'): + func = obj + break + if func is None: + raise RuntimeError("Could not find setdefaultencoding") + sys.setdefaultencoding = func + return func(encoding) \ No newline at end of file -- cgit v1.2.3