From b37b68b66e2ca3fdf1f941fb6bf9b423afe23f37 2018-06-09 23:18:49 From: Anatol Ulrich Date: 2018-06-09 23:18:49 Subject: [PATCH] fix #11082 - use dpaste.com, also remove py2 compatibility imports --- diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index d16a569..8a718ae 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -20,6 +20,8 @@ import re import sys import ast from itertools import chain +from urllib.request import urlopen +from urllib.parse import urlencode # Our own packages from IPython.core.error import TryNext, StdinNotImplementedError, UsageError @@ -244,7 +246,7 @@ class CodeMagics(Magics): @line_magic def pastebin(self, parameter_s=''): - """Upload code to Github's Gist paste bin, returning the URL. + """Upload code to dpaste's paste bin, returning the URL. Usage:\\ %pastebin [-d "Custom description"] 1-7 @@ -265,25 +267,14 @@ class CodeMagics(Magics): print(e.args[0]) return - # Deferred import - try: - from urllib.request import urlopen # Py 3 - except ImportError: - from urllib2 import urlopen - import json - post_data = json.dumps({ - "description": opts.get('d', "Pasted from IPython"), - "public": True, - "files": { - "file1.py": { - "content": code - } - } + post_data = urlencode({ + "title": opts.get('d', "Pasted from IPython"), + "syntax": "python3", + "content": code }).encode('utf-8') - response = urlopen("https://api.github.com/gists", post_data) - response_data = json.loads(response.read().decode('utf-8')) - return response_data['html_url'] + response = urlopen("http://dpaste.com/api/v2/", post_data) + return response.headers.get('Location') @line_magic def loadpy(self, arg_s):