Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #12712 on branch 7.x (dpaste.com API correction) #12873

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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