aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Molodowitch2012-10-24 18:27:52 (GMT)
committerLance Stout2012-10-24 20:06:36 (GMT)
commit52feabbe7641f19a17810438d2469b2395b2819f (patch)
treea54ad92b80775233aafc05acdf27adf3942973df
parenta22ca228cc3a121da8bad4268c39bff5190db969 (diff)
downloadslixmpp-52feabbe7641f19a17810438d2469b2395b2819f.tar.gz
slixmpp-52feabbe7641f19a17810438d2469b2395b2819f.tar.xz
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
-rw-r--r--docs/getting_started/echobot.rst4
-rwxr-xr-xexamples/adhoc_provider.py4
-rwxr-xr-xexamples/adhoc_user.py4
-rwxr-xr-xexamples/admin_commands.py4
-rwxr-xr-xexamples/custom_stanzas/custom_stanza_provider.py8
-rwxr-xr-xexamples/custom_stanzas/custom_stanza_user.py4
-rwxr-xr-xexamples/disco_browser.py4
-rw-r--r--examples/download_avatars.py4
-rwxr-xr-xexamples/echo_client.py4
-rwxr-xr-xexamples/echo_component.py4
-rwxr-xr-xexamples/gtalk_custom_domain.py4
-rwxr-xr-xexamples/ibb_transfer/ibb_receiver.py4
-rwxr-xr-xexamples/ibb_transfer/ibb_sender.py4
-rwxr-xr-xexamples/muc.py4
-rwxr-xr-xexamples/ping.py4
-rwxr-xr-xexamples/proxy_echo_client.py4
-rw-r--r--examples/pubsub_client.py4
-rw-r--r--examples/pubsub_events.py4
-rw-r--r--examples/register_account.py4
-rw-r--r--examples/roster_browser.py4
-rwxr-xr-xexamples/send_client.py4
-rw-r--r--examples/set_avatar.py4
-rw-r--r--examples/thirdparty_auth.py4
-rw-r--r--sleekxmpp/basexmpp.py4
-rw-r--r--sleekxmpp/util/misc_ops.py24
25 files changed, 74 insertions, 50 deletions
diff --git a/docs/getting_started/echobot.rst b/docs/getting_started/echobot.rst
index 053a76f..7d29ec5 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 a72158c..86a575c 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 bbd42d8..7df9f79 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 13b1b17..5d9bf84 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 b532c17..b0e0024 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 5b5042c..418e321 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 67e12d1..aeb4fb5 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 b621c94..64300cf 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 7399008..f2d3884 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 82f6eb9..9a24f2f 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 0226c14..c974fc5 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 b11acab..18d18f9 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 cd85637..7c380b6 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 7b93da1..5b5c764 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 fe4d23a..0e53b1d 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 25bfc89..98935b9 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 0a244f3..2fa419f 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 6fe7159..0dfb6c6 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 20377b2..bd9b116 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 b366d00..485ac94 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 5b34bbc..192469a 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 ec32a7c..cae93c9 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 727311a..f4d5c40 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 4df7f11..c3ff5ba 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 9bb1db4..3b24662 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