Skip to content

Commit

Permalink
Merge pull request #12873 from meeseeksmachine/auto-backport-of-pr-12…
Browse files Browse the repository at this point in the history
…712-on-7.x

Backport PR #12712 on branch 7.x (dpaste.com API correction)
  • Loading branch information
Carreau committed Mar 27, 2021
2 parents f232d30 + be67713 commit 89905c0
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions IPython/core/magics/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import sys
import ast
from itertools import chain
from urllib.request import urlopen
from urllib.request import Request, urlopen
from urllib.parse import urlencode

# Our own packages
from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
from IPython.core.macro import Macro
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.oinspect import find_file, find_source_lines
from IPython.core.release import version
from IPython.testing.skipdoctest import skip_doctest
from IPython.utils.contexts import preserve_keys
from IPython.utils.path import get_py_filename
Expand Down Expand Up @@ -244,7 +245,7 @@ def save(self, parameter_s=''):

@line_magic
def pastebin(self, parameter_s=''):
"""Upload code to dpaste's paste bin, returning the URL.
"""Upload code to dpaste.com, returning the URL.
Usage:\\
%pastebin [-d "Custom description"] 1-7
Expand All @@ -254,7 +255,7 @@ def pastebin(self, parameter_s=''):
Options:
-d: Pass a custom description for the gist. The default will say
-d: Pass a custom description. The default will say
"Pasted from IPython".
"""
opts, args = self.parse_options(parameter_s, 'd:')
Expand All @@ -265,13 +266,19 @@ def pastebin(self, parameter_s=''):
print(e.args[0])
return

post_data = urlencode({
"title": opts.get('d', "Pasted from IPython"),
"syntax": "python3",
"content": code
}).encode('utf-8')

response = urlopen("http://dpaste.com/api/v2/", post_data)
post_data = urlencode(
{
"title": opts.get("d", "Pasted from IPython"),
"syntax": "python",
"content": code,
}
).encode("utf-8")

request = Request(
"http://dpaste.com/api/v2/",
headers={"User-Agent": "IPython v{}".format(version)},
)
response = urlopen(request, post_data)
return response.headers.get('Location')

@line_magic
Expand Down

0 comments on commit 89905c0

Please sign in to comment.