Skip to content

Commit

Permalink
Merge branch 'main' into opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel authored Jul 16, 2023
2 parents df2c234 + 83bd568 commit 0a9faae
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 77 deletions.
35 changes: 0 additions & 35 deletions Doc/faq/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -669,41 +669,6 @@ and client-side web systems.
A summary of available frameworks is maintained by Paul Boddie at
https://wiki.python.org/moin/WebProgramming\ .
Cameron Laird maintains a useful set of pages about Python web technologies at
https://web.archive.org/web/20210224183619/http://phaseit.net/claird/comp.lang.python/web_python.
How can I mimic CGI form submission (METHOD=POST)?
--------------------------------------------------
I would like to retrieve web pages that are the result of POSTing a form. Is
there existing code that would let me do this easily?
Yes. Here's a simple example that uses :mod:`urllib.request`::
#!/usr/local/bin/python
import urllib.request
# build the query string
qs = "First=Josephine&MI=Q&Last=Public"
# connect and send the server a path
req = urllib.request.urlopen('http://www.some-server.out-there'
'/cgi-bin/some-cgi-script', data=qs)
with req:
msg, hdrs = req.read(), req.info()
Note that in general for percent-encoded POST operations, query strings must be
quoted using :func:`urllib.parse.urlencode`. For example, to send
``name=Guy Steele, Jr.``::
>>> import urllib.parse
>>> urllib.parse.urlencode({'name': 'Guy Steele, Jr.'})
'name=Guy+Steele%2C+Jr.'
.. seealso:: :ref:`urllib-howto` for extensive examples.
What module should I use to help with generating HTML?
------------------------------------------------------
Expand Down
29 changes: 18 additions & 11 deletions Doc/howto/clinic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Argument Clinic How-To
version of Argument Clinic that ships with the next version
of CPython *could* be totally incompatible and break all your code.

The Goals Of Argument Clinic

The goals of Argument Clinic
============================

Argument Clinic's primary goal
Expand Down Expand Up @@ -78,7 +79,7 @@ and it should be able to do many interesting and smart
things with all the information you give it.


Basic Concepts And Usage
Basic concepts and usage
========================

Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``.
Expand Down Expand Up @@ -141,7 +142,7 @@ For the sake of clarity, here's the terminology we'll use with Argument Clinic:
a block.)


Converting Your First Function
Converting your first function
==============================

The best way to get a sense of how Argument Clinic works is to
Expand Down Expand Up @@ -558,7 +559,8 @@ Let's dive in!

Congratulations, you've ported your first function to work with Argument Clinic!

Advanced Topics

Advanced topics
===============

Now that you've had some experience working with Argument Clinic, it's time
Expand Down Expand Up @@ -636,7 +638,8 @@ after the last argument).
Currently the generated code will use :c:func:`PyArg_ParseTuple`, but this
will change soon.

Optional Groups

Optional groups
---------------

Some legacy functions have a tricky approach to parsing their arguments:
Expand Down Expand Up @@ -899,6 +902,7 @@ available. For each converter it'll show you all the parameters
it accepts, along with the default value for each parameter.
Just run ``Tools/clinic/clinic.py --converters`` to see the full list.


Py_buffer
---------

Expand All @@ -908,7 +912,6 @@ you *must* not call :c:func:`PyBuffer_Release` on the provided buffer.
Argument Clinic generates code that does it for you (in the parsing function).



Advanced converters
-------------------

Expand Down Expand Up @@ -975,6 +978,7 @@ value called ``NULL`` for just this reason: from Python's perspective it
behaves like a default value of ``None``, but the C variable is initialized
with ``NULL``.


Expressions specified as default values
---------------------------------------

Expand Down Expand Up @@ -1032,7 +1036,6 @@ you're not permitted to use:
* Tuple/list/set/dict literals.



Using a return converter
------------------------

Expand Down Expand Up @@ -1146,6 +1149,7 @@ then modifying it. Cloning is an all-or nothing proposition.
Also, the function you are cloning from must have been previously defined
in the current file.


Calling Python code
-------------------

Expand Down Expand Up @@ -1380,6 +1384,7 @@ handle initialization and cleanup.
You can see more examples of custom converters in the CPython
source tree; grep the C files for the string ``CConverter``.


Writing a custom return converter
---------------------------------

Expand All @@ -1394,8 +1399,9 @@ write your own return converter, please read ``Tools/clinic/clinic.py``,
specifically the implementation of ``CReturnConverter`` and
all its subclasses.


METH_O and METH_NOARGS
----------------------------------------------
----------------------

To convert a function using ``METH_O``, make sure the function's
single argument is using the ``object`` converter, and mark the
Expand All @@ -1415,8 +1421,9 @@ any arguments.
You can still use a self converter, a return converter, and specify
a ``type`` argument to the object converter for ``METH_O``.


tp_new and tp_init functions
----------------------------------------------
----------------------------

You can convert ``tp_new`` and ``tp_init`` functions. Just name
them ``__new__`` or ``__init__`` as appropriate. Notes:
Expand All @@ -1437,6 +1444,7 @@ them ``__new__`` or ``__init__`` as appropriate. Notes:
(If your function doesn't support keywords, the parsing function
generated will throw an exception if it receives any.)


Changing and redirecting Clinic's output
----------------------------------------

Expand Down Expand Up @@ -1721,7 +1729,7 @@ the file was not modified by hand before it gets overwritten.


The #ifdef trick
----------------------------------------------
----------------

If you're converting a function that isn't available on all platforms,
there's a trick you can use to make life a little easier. The existing
Expand Down Expand Up @@ -1801,7 +1809,6 @@ Argument Clinic added to your file (it'll be at the very bottom), then
move it above the ``PyMethodDef`` structure where that macro is used.



Using Argument Clinic in Python files
-------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion Doc/library/devmode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ descriptor" error when finalizing the file object:

.. code-block:: shell-session
$ python script.py
$ python -X dev script.py
import os
script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
main()
Expand Down
74 changes: 44 additions & 30 deletions Doc/tools/templates/search.html
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
{% extends "!search.html" %}
{% block extrahead %}
{{ super() }}
<meta name="robots" content="noindex">
<script type="text/javascript">
var GLOSSARY_PAGE = 'glossary.html';
const GLOSSARY_PAGE = 'glossary.html';

jQuery(function() {
$.getJSON("_static/glossary.json", function(glossary) {
var RESULT_TEMPLATE = '<div style="display: none" class="admonition seealso" id="glossary-result">' +
document.addEventListener('DOMContentLoaded', function() {
fetch('_static/glossary.json')
.then(function(response) {
if (response.ok) {
return response.json();
} else {
throw new Error('Failed to fetch glossary.json');
}
})
.then(function(glossary) {
const RESULT_TEMPLATE = '<div style="display: none" class="admonition seealso" id="glossary-result">' +
' <p class="topic-title">' +
' <a class="glossary-title" href="#"></a>' +
' </p>' +
' <div class="glossary-body"></div>' +
'</div>';
$("#search-results").prepend(RESULT_TEMPLATE);
let searchResults = document.getElementById('search-results');
searchResults.insertAdjacentHTML('afterbegin', RESULT_TEMPLATE);

var params = $.getQueryParameters();
if (params.q) {
var search_param = params.q[0].toLowerCase();
var glossary_item = glossary[search_param];
if (glossary_item) {
var resultDiv = $("#glossary-result");
const params = new URLSearchParams(document.location.search).get("q");
if (params) {
const searchParam = params.toLowerCase();
const glossaryItem = glossary[searchParam];
if (glossaryItem) {
let resultDiv = document.getElementById('glossary-result');

// set up the title text with a link to the glossary page
resultDiv.find(".glossary-title").text('Glossary: ' + glossary_item.title);
var link_target = search_param.replace(/ /g, '-');
resultDiv.find(".glossary-title").attr(
'href', GLOSSARY_PAGE + '#term-' + link_target
);
// set up the title text with a link to the glossary page
let glossaryTitle = resultDiv.querySelector('.glossary-title');
glossaryTitle.textContent = 'Glossary: ' + glossaryItem.title;
const linkTarget = searchParam.replace(/ /g, '-');
glossaryTitle.href = GLOSSARY_PAGE + '#term-' + linkTarget;

// rewrite any anchor links (to other glossary terms)
// to have a full reference to the glossary page
var body = $(glossary_item.body).children();
body.find("a[href^='#']").each(function() {
var current_url = $(this).attr('href');
$(this).attr('href', GLOSSARY_PAGE + current_url);
});
resultDiv.find(".glossary-body").html(body);
// rewrite any anchor links (to other glossary terms)
// to have a full reference to the glossary page
let body = document.createElement('div');
body.innerHTML = glossaryItem.body;
const anchorLinks = body.querySelectorAll('a[href^="#"]');
anchorLinks.forEach(function(link) {
const currentUrl = link.getAttribute('href');
link.href = GLOSSARY_PAGE + currentUrl;
});
resultDiv.querySelector('.glossary-body').appendChild(body);

resultDiv.show();
} else {
$("#glossary-result").hide('');
}
resultDiv.style.display = '';
} else {
document.getElementById('glossary-result').style.display = 'none';
}
}
})
.catch(function(error) {
console.error(error);
});
});
</script>
{% endblock %}
{% endblock %}

0 comments on commit 0a9faae

Please sign in to comment.