Skip to content

Commit

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

Backport PR #13056 on branch 7.x (add expiry days option to pastebin magic and change http protocol to https)
  • Loading branch information
MrMino committed Jul 20, 2021
2 parents 4184b39 + b182c03 commit f34978f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions IPython/core/magics/code.py
Expand Up @@ -248,7 +248,7 @@ def pastebin(self, parameter_s=''):
"""Upload code to dpaste.com, returning the URL.
Usage:\\
%pastebin [-d "Custom description"] 1-7
%pastebin [-d "Custom description"][-e 24] 1-7
The argument can be an input history range, a filename, or the name of a
string or macro.
Expand All @@ -257,25 +257,38 @@ def pastebin(self, parameter_s=''):
-d: Pass a custom description. The default will say
"Pasted from IPython".
-e: Pass number of days for the link to be expired.
The default will be 7 days.
"""
opts, args = self.parse_options(parameter_s, 'd:')
opts, args = self.parse_options(parameter_s, "d:e:")

try:
code = self.shell.find_user_code(args)
except (ValueError, TypeError) as e:
print(e.args[0])
return

expiry_days = 7
try:
expiry_days = int(opts.get("e", 7))
except ValueError as e:
print(e.args[0].capitalize())
return
if expiry_days < 1 or expiry_days > 365:
print("Expiry days should be in range of 1 to 365")
return

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

request = Request(
"http://dpaste.com/api/v2/",
"https://dpaste.com/api/v2/",
headers={"User-Agent": "IPython v{}".format(version)},
)
response = urlopen(request, post_data)
Expand Down
7 changes: 7 additions & 0 deletions docs/source/whatsnew/pr/pastebin-expiry-days.rst
@@ -0,0 +1,7 @@
Pastebin magic expiry days option
=================================

The Pastebin magic now has ``-e`` option to determine
the number of days for paste expiration. For example
the paste that created with ``%pastebin -e 20 1`` magic will
be available for next 20 days.

0 comments on commit f34978f

Please sign in to comment.