Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 37 additions & 18 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ class Doc:
PYTHONDOCS = os.environ.get("PYTHONDOCS",
"https://docs.python.org/%d.%d/library"
% sys.version_info[:2])
STDLIB_DIR = sysconfig.get_path('stdlib')

def document(self, object, name=None, *args):
"""Generate documentation for an object."""
Expand All @@ -475,23 +476,12 @@ def fail(self, object, name=None, *args):

docmodule = docclass = docroutine = docother = docproperty = docdata = fail

def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
def getdocloc(self, object, basedir=None):
"""Return the location of module docs or None"""

try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'

basedir = self.STDLIB_DIR if basedir is None else basedir
docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS)

basedir = os.path.normcase(basedir)
if (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport') or
(file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages')))) and
if (self._is_stdlib_module(object, basedir) and
object.__name__ not in ('xml.etree', 'test.test_pydoc.pydoc_mod')):
if docloc.startswith(("http://", "https://")):
docloc = "{}/{}.html".format(docloc.rstrip("/"), object.__name__.lower())
Expand All @@ -501,6 +491,36 @@ def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):
docloc = None
return docloc

def _get_version(self, object):
if self._is_stdlib_module(object):
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
version = getattr(object, '__version__', None)
else:
version = getattr(object, '__version__', None)
return '' if version is None else str(version)

def _is_stdlib_module(self, object, basedir=None):
basedir = self.STDLIB_DIR if basedir is None else basedir

try:
file = inspect.getabsfile(object)
except TypeError:
file = '(built-in)'

if sysconfig.is_python_build():
srcdir = sysconfig.get_config_var('srcdir')
if srcdir:
basedir = os.path.join(srcdir, 'Lib')

basedir = os.path.normcase(basedir)
return (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport')
or (file.startswith(basedir) and
not file.startswith(os.path.join(basedir, 'site-packages')))))

# -------------------------------------------- HTML documentation generator

class HTMLRepr(Repr):
Expand Down Expand Up @@ -760,8 +780,8 @@ def docmodule(self, object, name=None, mod=None, *ignored):
except TypeError:
filelink = '(built-in)'
info = []
if hasattr(object, '__version__'):
version = str(object.__version__)

if version := self._get_version(object):
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
info.append('version %s' % self.escape(version))
Expand Down Expand Up @@ -1296,8 +1316,7 @@ def docmodule(self, object, name=None, mod=None, *ignored):
contents.append(self.docother(value, key, name, maxlen=70))
result = result + self.section('DATA', '\n'.join(contents))

if hasattr(object, '__version__'):
version = str(object.__version__)
if version := self._get_version(object):
if version[:11] == '$' + 'Revision: ' and version[-1:] == '$':
version = version[11:-1].strip()
result = result + self.section('VERSION', version)
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,24 @@ def testfunc(n):
self.assertNotIn("_GUARD_TOS_INT", uops)
self.assertNotIn("_GUARD_NOS_INT", uops)

def test_store_subscr_int(self):
def testfunc(n):
l = [0, 0, 0, 0]
for _ in range(n):
l[0] = 1
l[1] = 2
l[2] = 3
l[3] = 4
return sum(l)

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, 10)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_POP_TOP", uops)
self.assertNotIn("_POP_TOP_INT", uops)
self.assertIn("_POP_TOP_NOP", uops)

def test_attr_promotion_failure(self):
# We're not testing for any specific uops here, just
# testing it doesn't crash.
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,32 @@ def test_sys_path_adjustment_when_curdir_already_included(self):
trailing_argv0dir = trailing_curdir + [self.argv0dir]
self.assertIsNone(self._get_revised_path(trailing_argv0dir))

def test__get_version(self):
import json
import warnings

class MyModule:
__name__ = 'my_module'

@property
def __version__(self):
warnings._deprecated("__version__", remove=(3, 20))
return "1.2.3"

module = MyModule()
doc = pydoc.Doc()
with warnings.catch_warnings(record=True) as w: # TODO: remove in 3.20
warnings.simplefilter("always")
version = doc._get_version(json)
self.assertEqual(version, "2.0.9")
self.assertEqual(len(w), 0)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
version = doc._get_version(module)
self.assertEqual(version, "1.2.3")
self.assertEqual(len(w), 1)


def setUpModule():
thread_info = threading_helper.threading_setup()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`pydoc`: Fix :exc:`DeprecationWarning` being raised when generating doc for
:term:`stdlib` modules.
10 changes: 5 additions & 5 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,9 +1126,9 @@ dummy_func(
macro(STORE_SUBSCR) = _SPECIALIZE_STORE_SUBSCR + _STORE_SUBSCR;

macro(STORE_SUBSCR_LIST_INT) =
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT;
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;

op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- )) {
op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ls, ss)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *list = PyStackRef_AsPyObjectBorrow(list_st);

Expand All @@ -1151,9 +1151,9 @@ dummy_func(
PyStackRef_AsPyObjectSteal(value));
assert(old_value != NULL);
UNLOCK_OBJECT(list); // unlock before decrefs!
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
DEAD(sub_st);
PyStackRef_CLOSE(list_st);
INPUTS_DEAD();
ls = list_st;
ss = sub_st;
Py_DECREF(old_value);
}

Expand Down
20 changes: 14 additions & 6 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ dummy_func(void) {
GETLOCAL(oparg) = value;
}

op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ls, ss)) {
(void)value;
ls = list_st;
ss = sub_st;
}

op(_PUSH_NULL, (-- res)) {
res = sym_new_null(ctx);
}
Expand Down Expand Up @@ -529,6 +535,12 @@ dummy_func(void) {
}
}

op(_POP_TOP_INT, (value --)) {
if (PyJitRef_IsBorrowed(value)) {
REPLACE_OP(this_instr, _POP_TOP_NOP, 0, 0);
}
}

op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
assert(oparg > 0);
top = bottom;
Expand Down
22 changes: 20 additions & 2 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading